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,238 @@
1
+ from collections.abc import Iterable, Iterator
2
+ from typing import Any
3
+
4
+ from sources.analysis.tokens import Token
5
+ from sources.enums import NodeKind
6
+
7
+
8
+ class Node:
9
+ """
10
+ Node of the syntax tree of a C or H file. Structural parts are reachable both as ordered `children`
11
+ and by name in `fields` (e.g. `fields["body"]`); scalar facts (names, operators, storage) live in
12
+ `fields` too.
13
+
14
+ Attributes
15
+ ----------
16
+ kind : NodeKind
17
+ Kind of the node.
18
+ first : Token | None
19
+ First token of the node (None only for the root of an empty file).
20
+ last : Token | None
21
+ Last token of the node (None only for the root of an empty file).
22
+ children : list[Node]
23
+ Child nodes, in source order.
24
+ parent : Node | None
25
+ Parent node, or None for the root.
26
+ fields : dict[str, Any]
27
+ Named parts and facts of the node.
28
+ """
29
+
30
+ __slots__ = ("kind", "first", "last", "children", "parent", "fields")
31
+
32
+ def __init__(self, kind: NodeKind, first: Token | None, last: Token | None, children: Iterable["Node | None"] = (), **fields: Any) -> None:
33
+ """
34
+ Create a node and attach its children.
35
+
36
+ Parameters
37
+ ----------
38
+ kind : NodeKind
39
+ Kind of the node.
40
+ first : Token | None
41
+ First token of the node.
42
+ last : Token | None
43
+ Last token of the node.
44
+ children : Iterable[Node | None]
45
+ Child nodes in source order (None entries are dropped).
46
+ **fields : Any
47
+ Named parts and facts of the node.
48
+
49
+ Returns
50
+ ----------
51
+ None
52
+ Nothing.
53
+ """
54
+ self.kind: NodeKind = kind
55
+ self.first: Token | None = first
56
+ self.last: Token | None = last
57
+ self.children: list[Node] = [child for child in children if child is not None]
58
+ self.parent: Node | None = None
59
+ self.fields: dict[str, Any] = fields
60
+ for child in self.children:
61
+ child.parent = self
62
+
63
+ def __repr__(self) -> str:
64
+ """
65
+ Short debug representation, e.g. `<if_statement 3:5-5:6>`.
66
+
67
+ Returns
68
+ ----------
69
+ str
70
+ The representation.
71
+ """
72
+ return f"<{self.kind.value} {self.line}:{self.column}-{self.end_line}:{self.end_column}>"
73
+
74
+ @property
75
+ def name(self) -> str | None:
76
+ """
77
+ Name carried by the node (function, variable, tag, called function...), if any.
78
+
79
+ Returns
80
+ ----------
81
+ str | None
82
+ The name, or None.
83
+ """
84
+ return self.fields.get("name")
85
+
86
+ @property
87
+ def line(self) -> int:
88
+ """
89
+ 1-based line of the first token.
90
+
91
+ Returns
92
+ ----------
93
+ int
94
+ The line.
95
+ """
96
+ return self.first.line if self.first is not None else 1
97
+
98
+ @property
99
+ def column(self) -> int:
100
+ """
101
+ 1-based column of the first token.
102
+
103
+ Returns
104
+ ----------
105
+ int
106
+ The column.
107
+ """
108
+ return self.first.column if self.first is not None else 1
109
+
110
+ @property
111
+ def end_line(self) -> int:
112
+ """
113
+ 1-based line of the last token.
114
+
115
+ Returns
116
+ ----------
117
+ int
118
+ The line.
119
+ """
120
+ return self.last.end_line if self.last is not None else 1
121
+
122
+ @property
123
+ def end_column(self) -> int:
124
+ """
125
+ 1-based column just after the last token.
126
+
127
+ Returns
128
+ ----------
129
+ int
130
+ The column.
131
+ """
132
+ return self.last.end_column if self.last is not None else 1
133
+
134
+ @property
135
+ def start_offset(self) -> int:
136
+ """
137
+ 0-based character offset of the first token.
138
+
139
+ Returns
140
+ ----------
141
+ int
142
+ The offset.
143
+ """
144
+ return self.first.offset if self.first is not None else 0
145
+
146
+ @property
147
+ def end_offset(self) -> int:
148
+ """
149
+ 0-based character offset just after the last token.
150
+
151
+ Returns
152
+ ----------
153
+ int
154
+ The offset.
155
+ """
156
+ return self.last.end_offset if self.last is not None else 0
157
+
158
+ def contains_offset(self, offset: int) -> bool:
159
+ """
160
+ Whether a character offset lies inside the node.
161
+
162
+ Parameters
163
+ ----------
164
+ offset : int
165
+ 0-based character offset.
166
+
167
+ Returns
168
+ ----------
169
+ bool
170
+ True when the offset is between the first and the last token (inclusive).
171
+ """
172
+ return self.start_offset <= offset < self.end_offset
173
+
174
+ def walk(self) -> Iterator["Node"]:
175
+ """
176
+ Iterate over the node and all its descendants, in pre-order.
177
+
178
+ Returns
179
+ ----------
180
+ Iterator[Node]
181
+ The nodes.
182
+ """
183
+ stack: list[Node] = [self]
184
+ while stack:
185
+ node: Node = stack.pop()
186
+ yield node
187
+ stack.extend(reversed(node.children))
188
+
189
+ def ancestors(self) -> Iterator["Node"]:
190
+ """
191
+ Iterate over the parents of the node, nearest first.
192
+
193
+ Returns
194
+ ----------
195
+ Iterator[Node]
196
+ The ancestors.
197
+ """
198
+ node: Node | None = self.parent
199
+ while node is not None:
200
+ yield node
201
+ node = node.parent
202
+
203
+ def nearest(self, *kinds: NodeKind) -> "Node | None":
204
+ """
205
+ Nearest ancestor of one of the given kinds.
206
+
207
+ Parameters
208
+ ----------
209
+ *kinds : NodeKind
210
+ Accepted kinds.
211
+
212
+ Returns
213
+ ----------
214
+ Node | None
215
+ The ancestor, or None.
216
+ """
217
+ for ancestor in self.ancestors():
218
+ if ancestor.kind in kinds:
219
+ return ancestor
220
+ return None
221
+
222
+ def descendants(self, *kinds: NodeKind) -> Iterator["Node"]:
223
+ """
224
+ Iterate over the descendants (the node excluded) of the given kinds, in pre-order.
225
+
226
+ Parameters
227
+ ----------
228
+ *kinds : NodeKind
229
+ Accepted kinds (all kinds when none is given).
230
+
231
+ Returns
232
+ ----------
233
+ Iterator[Node]
234
+ The matching descendants.
235
+ """
236
+ for node in self.walk():
237
+ if node is not self and (not kinds or node.kind in kinds):
238
+ yield node
@@ -0,0 +1,116 @@
1
+ from dataclasses import dataclass
2
+
3
+ from sources.enums import TokenKind
4
+
5
+
6
+ # Token kinds that carry no syntax (layout and comments).
7
+ TRIVIA_KINDS: frozenset[TokenKind] = frozenset({
8
+ TokenKind.WHITESPACE,
9
+ TokenKind.NEWLINE,
10
+ TokenKind.COMMENT,
11
+ TokenKind.LINE_SPLICE,
12
+ })
13
+
14
+ # C keywords (C11, plus the C23 spellings of the most common ones).
15
+ KEYWORDS: frozenset[str] = frozenset({
16
+ "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum",
17
+ "extern", "float", "for", "goto", "if", "inline", "int", "long", "register", "restrict", "return",
18
+ "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void",
19
+ "volatile", "while", "_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic",
20
+ "_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local", "bool", "true", "false",
21
+ "alignas", "alignof", "static_assert", "thread_local", "nullptr", "typeof", "typeof_unqual",
22
+ })
23
+
24
+
25
+ @dataclass(frozen=True, slots=True)
26
+ class Token:
27
+ """
28
+ One token of a C or H file. Concatenating the text of every token of a file gives the file back
29
+ exactly (lossless lexer).
30
+ (dataclass)
31
+
32
+ Attributes
33
+ ----------
34
+ kind : TokenKind
35
+ Kind of the token.
36
+ text : str
37
+ Exact text of the token.
38
+ index : int
39
+ Position of the token in the full token stream.
40
+ offset : int
41
+ 0-based character offset of the first character.
42
+ line : int
43
+ 1-based line of the first character.
44
+ column : int
45
+ 1-based column (code points) of the first character.
46
+ end_line : int
47
+ 1-based line of the last character.
48
+ end_column : int
49
+ 1-based column just after the last character (exclusive).
50
+ in_directive : bool
51
+ Whether the token belongs to a preprocessor directive line (continuation lines included).
52
+ in_macro_body : bool
53
+ Whether the token belongs to the replacement list of a `#define`.
54
+ terminated : bool
55
+ False for an unterminated comment, string or character literal.
56
+ """
57
+
58
+ kind: TokenKind
59
+ text: str
60
+ index: int
61
+ offset: int
62
+ line: int
63
+ column: int
64
+ end_line: int
65
+ end_column: int
66
+ in_directive: bool = False
67
+ in_macro_body: bool = False
68
+ terminated: bool = True
69
+
70
+ @property
71
+ def end_offset(self) -> int:
72
+ """
73
+ 0-based character offset just after the token.
74
+
75
+ Returns
76
+ ----------
77
+ int
78
+ The end offset.
79
+ """
80
+ return self.offset + len(self.text)
81
+
82
+ @property
83
+ def is_trivia(self) -> bool:
84
+ """
85
+ Whether the token carries no syntax (whitespace, newline, comment, line splice).
86
+
87
+ Returns
88
+ ----------
89
+ bool
90
+ True for trivia.
91
+ """
92
+ return self.kind in TRIVIA_KINDS
93
+
94
+ @property
95
+ def is_significant(self) -> bool:
96
+ """
97
+ Whether the parser reads this token (not trivia, not part of a preprocessor directive).
98
+
99
+ Returns
100
+ ----------
101
+ bool
102
+ True for tokens of the C syntax.
103
+ """
104
+ return not self.is_trivia and not self.in_directive and self.kind is not TokenKind.DIRECTIVE
105
+
106
+ @property
107
+ def is_block_comment(self) -> bool:
108
+ """
109
+ Whether the token is a `/* ... */` comment.
110
+
111
+ Returns
112
+ ----------
113
+ bool
114
+ True for a block comment.
115
+ """
116
+ return self.kind is TokenKind.COMMENT and self.text.startswith("/*")