jarbin-c-coding-style 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
  2. jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
  3. jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
  4. jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
  5. jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
  6. sources/__init__.py +0 -0
  7. sources/__main__.py +6 -0
  8. sources/analysis/__init__.py +0 -0
  9. sources/analysis/directives.py +258 -0
  10. sources/analysis/lexer.py +288 -0
  11. sources/analysis/parser.py +1754 -0
  12. sources/analysis/project.py +286 -0
  13. sources/analysis/source.py +326 -0
  14. sources/analysis/syntax.py +238 -0
  15. sources/analysis/tokens.py +116 -0
  16. sources/analysis/unit.py +700 -0
  17. sources/application.py +118 -0
  18. sources/build_info.json +6 -0
  19. sources/cli/__init__.py +0 -0
  20. sources/cli/arguments.py +507 -0
  21. sources/cli/completion.py +610 -0
  22. sources/cli/context.py +258 -0
  23. sources/cli/converters.py +204 -0
  24. sources/cli/help.py +162 -0
  25. sources/cli/parser.py +659 -0
  26. sources/configs/__init__.py +0 -0
  27. sources/configs/editor.py +207 -0
  28. sources/configs/ini.py +327 -0
  29. sources/configs/project.py +735 -0
  30. sources/configs/schema/__init__.py +0 -0
  31. sources/configs/schema/project.py +68 -0
  32. sources/constants.py +801 -0
  33. sources/diagnostics/__init__.py +0 -0
  34. sources/diagnostics/fingerprint.py +82 -0
  35. sources/diagnostics/model.py +146 -0
  36. sources/engine/__init__.py +0 -0
  37. sources/engine/api.py +355 -0
  38. sources/engine/examples.py +157 -0
  39. sources/engine/outcome.py +114 -0
  40. sources/engine/plan.py +440 -0
  41. sources/engine/runner.py +345 -0
  42. sources/engine/scanner.py +179 -0
  43. sources/enums.py +838 -0
  44. sources/errors/__init__.py +0 -0
  45. sources/errors/exceptions.py +727 -0
  46. sources/fixers/__init__.py +0 -0
  47. sources/fixers/base.py +209 -0
  48. sources/fixers/conflicts.py +129 -0
  49. sources/fixers/diff.py +73 -0
  50. sources/fixers/engine.py +434 -0
  51. sources/fixers/journal.py +290 -0
  52. sources/fixers/kinds.py +30 -0
  53. sources/fixers/layout.py +411 -0
  54. sources/fixers/model.py +151 -0
  55. sources/fixers/registry.py +54 -0
  56. sources/fixers/text.py +353 -0
  57. sources/fixers/verify.py +123 -0
  58. sources/fixers/writer.py +88 -0
  59. sources/integrations/__init__.py +0 -0
  60. sources/integrations/ci/__init__.py +0 -0
  61. sources/integrations/ci/github.py +183 -0
  62. sources/integrations/editors/__init__.py +0 -0
  63. sources/integrations/git/__init__.py +0 -0
  64. sources/integrations/git/repository.py +176 -0
  65. sources/integrations/update/__init__.py +0 -0
  66. sources/integrations/update/installers.py +529 -0
  67. sources/integrations/update/releases.py +397 -0
  68. sources/integrations/update/state.py +143 -0
  69. sources/outputs/__init__.py +0 -0
  70. sources/outputs/github/__init__.py +0 -0
  71. sources/outputs/github/renderer.py +135 -0
  72. sources/outputs/html/__init__.py +0 -0
  73. sources/outputs/html/renderer.py +694 -0
  74. sources/outputs/json/__init__.py +0 -0
  75. sources/outputs/json/renderer.py +154 -0
  76. sources/outputs/json/report.schema.json +121 -0
  77. sources/outputs/junit/__init__.py +0 -0
  78. sources/outputs/junit/renderer.py +151 -0
  79. sources/outputs/registry.py +135 -0
  80. sources/outputs/report.py +224 -0
  81. sources/outputs/sarif/__init__.py +0 -0
  82. sources/outputs/sarif/renderer.py +182 -0
  83. sources/outputs/terminal/__init__.py +0 -0
  84. sources/outputs/terminal/renderer.py +184 -0
  85. sources/outputs/terminal/style.py +159 -0
  86. sources/rules/__init__.py +0 -0
  87. sources/rules/arguments.py +362 -0
  88. sources/rules/catalog.py +227 -0
  89. sources/rules/categories/advanced/constant_pointers.ini +46 -0
  90. sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
  91. sources/rules/categories/advanced/static.ini +65 -0
  92. sources/rules/categories/advanced/typing.ini +47 -0
  93. sources/rules/categories/control_structures/conditional_branching.ini +89 -0
  94. sources/rules/categories/control_structures/goto.ini +45 -0
  95. sources/rules/categories/control_structures/ternary_operators.ini +57 -0
  96. sources/rules/categories/extensions/language_extensions.ini +71 -0
  97. sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
  98. sources/rules/categories/files_organization/file_coherence.ini +166 -0
  99. sources/rules/categories/files_organization/file_extension.ini +68 -0
  100. sources/rules/categories/files_organization/fixtures/program +0 -0
  101. sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
  102. sources/rules/categories/functions/coherence_of_functions.ini +45 -0
  103. sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
  104. sources/rules/categories/functions/functions_without_parameters.ini +39 -0
  105. sources/rules/categories/functions/naming_functions.ini +79 -0
  106. sources/rules/categories/functions/number_of_columns.ini +61 -0
  107. sources/rules/categories/functions/number_of_lines.ini +70 -0
  108. sources/rules/categories/functions/number_of_parameters.ini +64 -0
  109. sources/rules/categories/functions/structures_as_parameters.ini +44 -0
  110. sources/rules/categories/global_scope/constant_values.ini +49 -0
  111. sources/rules/categories/global_scope/file_header.ini +92 -0
  112. sources/rules/categories/global_scope/global_variables.ini +53 -0
  113. sources/rules/categories/global_scope/include.ini +39 -0
  114. sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
  115. sources/rules/categories/global_scope/inline_assembly.ini +43 -0
  116. sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
  117. sources/rules/categories/global_scope/line_endings.ini +53 -0
  118. sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
  119. sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
  120. sources/rules/categories/header_files/content.ini +104 -0
  121. sources/rules/categories/header_files/include_guard.ini +47 -0
  122. sources/rules/categories/header_files/macros.ini +57 -0
  123. sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
  124. sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
  125. sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
  126. sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
  127. sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
  128. sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
  129. sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
  130. sources/rules/categories/variables_and_types/pointers.ini +67 -0
  131. sources/rules/categories/variables_and_types/structures.ini +47 -0
  132. sources/rules/data/verbs.txt +182 -0
  133. sources/rules/definition.py +430 -0
  134. sources/rules/loader.py +925 -0
  135. sources/rules/paths.py +6 -0
  136. sources/rules/primitives/__init__.py +0 -0
  137. sources/rules/primitives/base.py +297 -0
  138. sources/rules/primitives/braces.py +211 -0
  139. sources/rules/primitives/directives.py +203 -0
  140. sources/rules/primitives/indentation.py +376 -0
  141. sources/rules/primitives/layout.py +199 -0
  142. sources/rules/primitives/naming.py +277 -0
  143. sources/rules/primitives/project.py +62 -0
  144. sources/rules/primitives/registry.py +49 -0
  145. sources/rules/primitives/repository.py +129 -0
  146. sources/rules/primitives/review.py +87 -0
  147. sources/rules/primitives/semantic.py +319 -0
  148. sources/rules/primitives/spacing.py +210 -0
  149. sources/rules/primitives/structure.py +586 -0
  150. sources/rules/primitives/syntax.py +200 -0
  151. sources/rules/primitives/text.py +408 -0
  152. sources/rules/primitives/tokens.py +84 -0
  153. sources/rules/template.py +138 -0
  154. sources/selftest/__init__.py +0 -0
  155. sources/selftest/cases.py +292 -0
  156. sources/services.py +90 -0
  157. sources/subcommands/__init__.py +0 -0
  158. sources/subcommands/base.py +45 -0
  159. sources/subcommands/check/__init__.py +0 -0
  160. sources/subcommands/check/command.py +45 -0
  161. sources/subcommands/check/fixing.py +224 -0
  162. sources/subcommands/check/handler.py +757 -0
  163. sources/subcommands/info/__init__.py +0 -0
  164. sources/subcommands/info/command.py +45 -0
  165. sources/subcommands/info/handler.py +527 -0
  166. sources/subcommands/registry.py +37 -0
  167. sources/subcommands/rule/__init__.py +0 -0
  168. sources/subcommands/rule/command.py +45 -0
  169. sources/subcommands/rule/handler.py +1067 -0
  170. sources/subcommands/rule/render.py +273 -0
  171. sources/subcommands/runner.py +124 -0
  172. sources/subcommands/test/__init__.py +0 -0
  173. sources/subcommands/test/command.py +45 -0
  174. sources/subcommands/test/handler.py +166 -0
  175. sources/subcommands/test/render.py +219 -0
  176. sources/subcommands/test/runner.py +430 -0
  177. sources/subcommands/update/__init__.py +0 -0
  178. sources/subcommands/update/command.py +45 -0
  179. sources/subcommands/update/handler.py +408 -0
  180. sources/utils/__init__.py +0 -0
  181. sources/utils/build_info.py +188 -0
  182. sources/utils/git.py +73 -0
  183. sources/utils/globs.py +98 -0
  184. sources/utils/logging.py +154 -0
  185. sources/utils/schema.py +77 -0
  186. sources/utils/versions.py +87 -0
@@ -0,0 +1,210 @@
1
+ from collections.abc import Iterator
2
+
3
+ from sources.analysis.syntax import Node
4
+ from sources.analysis.tokens import Token
5
+ from sources.analysis.unit import CUnit
6
+ from sources.enums import ArgumentType, Layer, NodeKind, TokenKind
7
+ from sources.rules.arguments import ArgumentSpec
8
+ from sources.rules.primitives.base import CheckRun, Finding, Primitive
9
+ from sources.rules.primitives.layout import BEFORE_COMMENT, LINE_BREAK_KINDS, LINE_END, LINE_START, gap_after, gap_before, next_code
10
+
11
+
12
+ # Token classes checked by `spacing` (plan 03, section 6.4).
13
+ SPACING_CLASSES: tuple[str, ...] = (
14
+ "comma", "semicolon", "for_semicolon", "keyword", "else", "call_paren", "unary_operator",
15
+ "binary_operator", "ternary_operator", "separator", "tab_separator", "pointer_star",
16
+ )
17
+
18
+ # Keywords that take an argument and must be followed by a space when they have one.
19
+ ARGUMENT_KEYWORDS: frozenset[str] = frozenset({"if", "while", "for", "switch", "case", "return", "goto"})
20
+
21
+ # Unary operators whose operand must be attached (the pointer `*` belongs to the pointer_star class).
22
+ ATTACHED_UNARY_OPERATORS: frozenset[str] = frozenset({"!", "~", "-", "+", "&"})
23
+
24
+ # Accepted whitespace on each side of a binary or ternary operator.
25
+ OPERATOR_BEFORE: frozenset[str] = frozenset({" ", LINE_START})
26
+
27
+ # Accepted whitespace after a binary or ternary operator, or a comma.
28
+ OPERATOR_AFTER: frozenset[str] = frozenset({" ", LINE_END, BEFORE_COMMENT})
29
+
30
+
31
+ def _finding(unit: CUnit, token: Token, kind: str) -> Finding:
32
+ """
33
+ Finding located on a token, for one spacing class.
34
+
35
+ Parameters
36
+ ----------
37
+ unit : CUnit
38
+ File of the token.
39
+ token : Token
40
+ The token.
41
+ kind : str
42
+ Spacing class that is broken.
43
+
44
+ Returns
45
+ ----------
46
+ Finding
47
+ The finding.
48
+ """
49
+ return Finding(unit.path, token.line, token.column, token.end_line, token.end_column, name=token.text, value=kind, node_key=(token.index, kind))
50
+
51
+
52
+ class Spacing(Primitive):
53
+ """
54
+ Whitespace around tokens, by token class: commas, semicolons (and the `for` header rule), keywords with
55
+ arguments, `else`, function call/declaration parentheses, unary, binary and ternary operators,
56
+ separators (one space, never a tab), and pointer asterisks (attached to their right, preceded by a space
57
+ unless after `(` or `*`).
58
+ (ABC -> Primitive)
59
+
60
+ Attributes
61
+ ----------
62
+ kind : str
63
+ "spacing".
64
+ """
65
+
66
+ kind = "spacing"
67
+ layer = Layer.SYNTAX
68
+ parameters = (
69
+ ArgumentSpec("classes", ArgumentType.LIST, required=True, item_type=ArgumentType.ENUM, choices=SPACING_CLASSES, description="Token classes checked."),
70
+ )
71
+ default_message = "{code}: bad spacing around '{name}' ({value})"
72
+
73
+ def _token_classes(self, unit: CUnit, classes: frozenset[str]) -> Iterator[Finding]:
74
+ """
75
+ Checks that only need tokens: commas, semicolons, keywords, `else` and separators.
76
+
77
+ Parameters
78
+ ----------
79
+ unit : CUnit
80
+ The file.
81
+ classes : frozenset[str]
82
+ Enabled classes.
83
+
84
+ Returns
85
+ ----------
86
+ Iterator[Finding]
87
+ The problems found.
88
+ """
89
+ tokens: tuple[Token, ...] = unit.tokens
90
+ for token in tokens:
91
+ if token.kind is TokenKind.WHITESPACE:
92
+ index: int = token.index
93
+ leading: bool = index == 0 or tokens[index - 1].kind in LINE_BREAK_KINDS
94
+ trailing: bool = index + 1 >= len(tokens) or tokens[index + 1].kind in LINE_BREAK_KINDS
95
+ if leading or trailing:
96
+ continue
97
+ if "\t" in token.text:
98
+ if "tab_separator" in classes:
99
+ yield _finding(unit, token, "tab_separator")
100
+ elif len(token.text) > 1 and "separator" in classes and not token.in_directive and tokens[index + 1].kind is not TokenKind.COMMENT:
101
+ yield _finding(unit, token, "separator")
102
+ continue
103
+ if token.in_directive or token.is_trivia:
104
+ continue
105
+ if token.kind is TokenKind.PUNCTUATOR and token.text == "," and "comma" in classes:
106
+ if gap_before(unit, token) not in ("", LINE_START) or gap_after(unit, token) not in OPERATOR_AFTER:
107
+ yield _finding(unit, token, "comma")
108
+ elif token.kind is TokenKind.PUNCTUATOR and token.text == ";" and "semicolon" in classes:
109
+ if gap_before(unit, token) not in ("", LINE_START):
110
+ yield _finding(unit, token, "semicolon")
111
+ elif token.kind is TokenKind.KEYWORD and token.text in ARGUMENT_KEYWORDS and "keyword" in classes:
112
+ following: Token | None = next_code(unit, token)
113
+ if gap_after(unit, token) == "" and following is not None and following.text not in (";", ":"):
114
+ yield _finding(unit, token, "keyword")
115
+ elif token.kind is TokenKind.KEYWORD and token.text == "else" and "else" in classes:
116
+ if gap_before(unit, token) not in OPERATOR_BEFORE or gap_after(unit, token) not in OPERATOR_AFTER:
117
+ yield _finding(unit, token, "else")
118
+
119
+ def _syntax_classes(self, unit: CUnit, classes: frozenset[str]) -> Iterator[Finding]:
120
+ """
121
+ Checks that need the syntax tree: `for` semicolons, call/declaration parentheses, operators and
122
+ pointer asterisks.
123
+
124
+ Parameters
125
+ ----------
126
+ unit : CUnit
127
+ The file.
128
+ classes : frozenset[str]
129
+ Enabled classes.
130
+
131
+ Returns
132
+ ----------
133
+ Iterator[Finding]
134
+ The problems found.
135
+ """
136
+ if "for_semicolon" in classes:
137
+ for loop in unit.nodes(NodeKind.FOR_STATEMENT):
138
+ opening: Token = loop.fields["open_paren"]
139
+ closing: Token = loop.fields["close_paren"]
140
+ for token in unit.tokens[opening.index + 1:closing.index]:
141
+ if token.text == ";" and token.kind is TokenKind.PUNCTUATOR:
142
+ following: Token = unit.tokens[token.index + 1]
143
+ if following.text != ";" and gap_after(unit, token) != " ":
144
+ yield _finding(unit, token, "for_semicolon")
145
+ if "call_paren" in classes:
146
+ for call in unit.nodes(NodeKind.CALL_EXPRESSION):
147
+ opening = call.fields.get("open_paren")
148
+ if opening is not None and not call.fields.get("opaque") and gap_before(unit, opening) != "":
149
+ yield _finding(unit, opening, "call_paren")
150
+ for declarator in unit.nodes(NodeKind.FUNCTION_DECLARATOR):
151
+ parameters: Node | None = declarator.fields.get("own_parameters")
152
+ name: Token | None = declarator.fields.get("name_token")
153
+ if parameters is None or name is None or declarator.fields.get("nested") is not None:
154
+ continue
155
+ if gap_before(unit, parameters.first) != "": # type: ignore[arg-type]
156
+ yield _finding(unit, parameters.first, "call_paren") # type: ignore[arg-type]
157
+ if "unary_operator" in classes:
158
+ for node in unit.nodes(NodeKind.UNARY_EXPRESSION, NodeKind.UPDATE_EXPRESSION, NodeKind.SIZEOF_EXPRESSION):
159
+ token = node.fields.get("operator_token") or node.first
160
+ if node.kind is NodeKind.UNARY_EXPRESSION and token.text not in ATTACHED_UNARY_OPERATORS:
161
+ continue
162
+ if node.kind is NodeKind.UPDATE_EXPRESSION and not node.fields["prefix"]:
163
+ if gap_before(unit, token) != "":
164
+ yield _finding(unit, token, "unary_operator")
165
+ continue
166
+ if node.kind is NodeKind.SIZEOF_EXPRESSION:
167
+ following = next_code(unit, token)
168
+ if following is not None and following.text == "(" and gap_after(unit, token) != "":
169
+ yield _finding(unit, token, "unary_operator")
170
+ continue
171
+ if gap_after(unit, token) != "":
172
+ yield _finding(unit, token, "unary_operator")
173
+ if "binary_operator" in classes:
174
+ for node in unit.nodes(NodeKind.BINARY_EXPRESSION, NodeKind.LOGICAL_EXPRESSION, NodeKind.ASSIGNMENT_EXPRESSION):
175
+ token = node.fields["operator_token"]
176
+ if gap_before(unit, token) not in OPERATOR_BEFORE or gap_after(unit, token) not in OPERATOR_AFTER:
177
+ yield _finding(unit, token, "binary_operator")
178
+ if "ternary_operator" in classes:
179
+ for node in unit.nodes(NodeKind.CONDITIONAL_EXPRESSION):
180
+ for token in (node.fields["question_token"], node.fields["colon_token"]):
181
+ if gap_before(unit, token) not in OPERATOR_BEFORE or gap_after(unit, token) not in OPERATOR_AFTER:
182
+ yield _finding(unit, token, "ternary_operator")
183
+ if "pointer_star" in classes:
184
+ stars: list[Token] = [star for node in unit.nodes(NodeKind.DECLARATOR, NodeKind.FUNCTION_DECLARATOR) for star in node.fields.get("star_tokens", ())]
185
+ stars.extend(node.fields["operator_token"] for node in unit.nodes(NodeKind.UNARY_EXPRESSION) if node.fields["operator"] == "*")
186
+ for star in sorted(stars, key=lambda item: item.index):
187
+ previous: Token | None = unit.tokens[star.index - 1] if star.index > 0 else None
188
+ before_ok: bool = (previous is not None and previous.text in ("(", "*")) or gap_before(unit, star) in OPERATOR_BEFORE
189
+ if gap_after(unit, star) != "" or not before_ok:
190
+ yield _finding(unit, star, "pointer_star")
191
+
192
+ def run(self, run: CheckRun) -> Iterator[Finding]:
193
+ """
194
+ Run the check (see class description).
195
+
196
+ Parameters
197
+ ----------
198
+ run : CheckRun
199
+ Project context, files and parameters.
200
+
201
+ Returns
202
+ ----------
203
+ Iterator[Finding]
204
+ The problems found.
205
+ """
206
+ classes: frozenset[str] = frozenset(run.parameters["classes"])
207
+ for unit in run.units():
208
+ found: list[Finding] = list(self._token_classes(unit, classes))
209
+ found.extend(self._syntax_classes(unit, classes))
210
+ yield from sorted(found, key=lambda finding: (finding.line, finding.column))