herb 0.11.1 → 0.11.2

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 (247) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +4 -2
  3. data/config.yml +6 -0
  4. data/ext/herb/extconf.rb +8 -31
  5. data/ext/herb/nodes.c +75 -75
  6. data/ext/herb/parser_options_helpers.c +26 -0
  7. data/lib/herb/analysis/partial_index.rb +1 -1
  8. data/lib/herb/analysis/partial_resolution.rb +27 -0
  9. data/lib/herb/ast/helpers.rb +1 -1
  10. data/lib/herb/configuration.rb +11 -1
  11. data/lib/herb/dev/classifier.rb +1 -1
  12. data/lib/herb/engine/compiler.rb +5 -8
  13. data/lib/herb/engine/slots/dynamics_compiler.rb +53 -14
  14. data/lib/herb/engine/slots/subtree_compiler.rb +1 -1
  15. data/lib/herb/engine/slots/visitor.rb +32 -4
  16. data/lib/herb/engine/visitors/instrumentation_visitor.rb +18 -6
  17. data/lib/herb/parser_options.rb +6 -3
  18. data/lib/herb/project.rb +19 -8
  19. data/lib/herb/version.rb +1 -1
  20. data/lib/herb.rb +1 -1
  21. data/sig/herb/analysis/partial_resolution.rbs +8 -0
  22. data/sig/herb/configuration.rbs +3 -0
  23. data/sig/herb/engine/slots/dynamics_compiler.rbs +24 -7
  24. data/sig/herb/engine/slots/visitor.rbs +14 -0
  25. data/sig/herb/engine/visitors/instrumentation_visitor.rbs +5 -1
  26. data/sig/herb/parser_options.rbs +6 -4
  27. data/sig/herb.rbs +2 -2
  28. data/src/analyze/action_view/attribute_extraction_helpers.c +40 -21
  29. data/src/analyze/action_view/generated_handlers.c +36 -36
  30. data/src/analyze/action_view/image_tag.c +7 -3
  31. data/src/analyze/action_view/javascript_include_tag.c +6 -3
  32. data/src/analyze/action_view/link_to.c +23 -23
  33. data/src/analyze/action_view/registry.c +7 -3
  34. data/src/analyze/action_view/tag_helper_node_builders.c +6 -13
  35. data/src/analyze/action_view/tag_helpers.c +139 -90
  36. data/src/analyze/action_view/turbo_frame_tag.c +5 -4
  37. data/src/analyze/analyze.c +35 -19
  38. data/src/analyze/analyze_helpers.c +161 -188
  39. data/src/analyze/analyzed_ruby.c +8 -7
  40. data/src/analyze/control_type.c +29 -22
  41. data/src/analyze/herb_directives.c +16 -20
  42. data/src/analyze/iteration_nodes.c +69 -24
  43. data/src/analyze/parse_errors.c +32 -22
  44. data/src/analyze/postfix_conditionals.c +23 -13
  45. data/src/analyze/prism_annotate.c +29 -28
  46. data/src/analyze/render_nodes.c +51 -41
  47. data/src/analyze/strict_locals.c +14 -15
  48. data/src/analyze/ternary_conditionals.c +14 -10
  49. data/src/herb.c +10 -8
  50. data/src/include/analyze/action_view/tag_helper_handler.h +1 -1
  51. data/src/include/analyze/action_view/tag_helper_node_builders.h +1 -0
  52. data/src/include/analyze/action_view/tag_helpers.h +1 -1
  53. data/src/include/analyze/analyze.h +3 -2
  54. data/src/include/analyze/analyzed_ruby.h +4 -2
  55. data/src/include/analyze/helpers.h +6 -6
  56. data/src/include/herb.h +3 -2
  57. data/src/include/parser/parser_options.h +5 -0
  58. data/src/include/prism/prism_context.h +12 -12
  59. data/src/include/prism/prism_helpers.h +30 -0
  60. data/src/include/prism_version.h +11 -0
  61. data/src/include/version.h +1 -1
  62. data/src/parser/parser_helpers.c +1 -2
  63. data/src/parser.c +6 -7
  64. data/src/parser_options_defaults.c +1 -0
  65. data/src/prism/prism_helpers.c +84 -50
  66. data/src/prism/ruby_parser.c +20 -20
  67. data/templates/ext/herb/nodes.c.erb +5 -5
  68. data/templates/ext/herb/parser_options_helpers.c.erb +33 -0
  69. data/templates/java/nodes.c.erb +6 -6
  70. data/templates/java/parser_options_helpers.c.erb +41 -2
  71. data/templates/java/parser_options_helpers.h.erb +2 -1
  72. data/templates/javascript/packages/node/extension/nodes.cpp.erb +5 -5
  73. data/templates/javascript/packages/node/extension/parser_options_helpers.cpp.erb +34 -2
  74. data/templates/javascript/packages/node/extension/parser_options_helpers.h.erb +1 -1
  75. data/templates/lib/herb/parser_options.rb.erb +1 -1
  76. data/templates/rust/src/prism/generated_deserialize.rs.erb +90 -44
  77. data/templates/rust/src/prism/generated_prettyprint.rs.erb +18 -27
  78. data/templates/src/analyze/action_view/generated_handlers.c.erb +23 -22
  79. data/templates/src/include/parser/parser_options.h.erb +10 -0
  80. data/templates/src/include/prism_version.h.erb +17 -0
  81. data/templates/src/parser_options_defaults.c.erb +1 -1
  82. data/templates/template.rb +17 -2
  83. data/templates/wasm/nodes.cpp.erb +5 -5
  84. data/templates/wasm/parser_options_helpers.cpp.erb +24 -0
  85. data/vendor/prism/Rakefile +76 -7
  86. data/vendor/prism/config.yml +174 -161
  87. data/vendor/prism/include/prism/arena.h +45 -0
  88. data/vendor/prism/include/prism/arena.hpp +42 -0
  89. data/vendor/prism/include/prism/ast.h +2738 -611
  90. data/vendor/prism/include/prism/ast.hpp +641 -0
  91. data/vendor/prism/include/prism/buffer.h +60 -0
  92. data/vendor/prism/include/prism/buffer.hpp +55 -0
  93. data/vendor/prism/include/prism/comments.h +72 -0
  94. data/vendor/prism/include/prism/compiler/accel.h +26 -0
  95. data/vendor/prism/include/prism/compiler/align.h +36 -0
  96. data/vendor/prism/include/prism/compiler/assume.h +25 -0
  97. data/vendor/prism/include/prism/compiler/branch.h +26 -0
  98. data/vendor/prism/include/prism/compiler/bswap.h +46 -0
  99. data/vendor/prism/include/prism/compiler/cplusplus.hpp +16 -0
  100. data/vendor/prism/include/prism/compiler/endianness.h +34 -0
  101. data/vendor/prism/include/prism/compiler/exceptions.hpp +17 -0
  102. data/vendor/prism/include/prism/compiler/exported.h +24 -0
  103. data/vendor/prism/include/prism/compiler/fallthrough.h +22 -0
  104. data/vendor/prism/include/prism/compiler/filesystem.h +32 -0
  105. data/vendor/prism/include/prism/compiler/flex_array.h +19 -0
  106. data/vendor/prism/include/prism/compiler/force_inline.h +21 -0
  107. data/vendor/prism/include/prism/compiler/format.h +27 -0
  108. data/vendor/prism/include/prism/compiler/inline.h +17 -0
  109. data/vendor/prism/include/prism/compiler/lifetimebound.hpp +25 -0
  110. data/vendor/prism/include/prism/compiler/nodiscard.h +22 -0
  111. data/vendor/prism/include/prism/compiler/nonnull.h +18 -0
  112. data/vendor/prism/include/prism/compiler/unused.h +18 -0
  113. data/vendor/prism/include/prism/constant_pool.h +106 -0
  114. data/vendor/prism/include/prism/diagnostic.h +55 -402
  115. data/vendor/prism/include/prism/errors_format.h +61 -0
  116. data/vendor/prism/include/prism/excludes.h +29 -0
  117. data/vendor/prism/include/prism/integer.h +41 -0
  118. data/vendor/prism/include/prism/internal/allocator.h +68 -0
  119. data/vendor/prism/include/prism/internal/allocator_debug.h +88 -0
  120. data/vendor/prism/include/prism/internal/arena.h +108 -0
  121. data/vendor/prism/include/prism/internal/bit.h +42 -0
  122. data/vendor/prism/include/prism/internal/buffer.h +94 -0
  123. data/vendor/prism/include/prism/internal/char.h +145 -0
  124. data/vendor/prism/include/prism/internal/comments.h +54 -0
  125. data/vendor/prism/include/prism/internal/constant_pool.h +152 -0
  126. data/vendor/prism/include/prism/internal/diagnostic.h +389 -0
  127. data/vendor/prism/include/prism/{encoding.h → internal/encoding.h} +27 -68
  128. data/vendor/prism/include/prism/internal/integer.h +68 -0
  129. data/vendor/prism/include/prism/internal/isinf.h +16 -0
  130. data/vendor/prism/include/prism/internal/line_offset_list.h +34 -0
  131. data/vendor/prism/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  132. data/vendor/prism/include/prism/internal/magic_comments.h +23 -0
  133. data/vendor/prism/include/prism/internal/memchr.h +15 -0
  134. data/vendor/prism/include/prism/internal/node.h +42 -0
  135. data/vendor/prism/include/prism/internal/options.h +232 -0
  136. data/vendor/prism/include/prism/internal/parser.h +1015 -0
  137. data/vendor/prism/include/prism/internal/regexp.h +26 -0
  138. data/vendor/prism/include/prism/internal/serialize.h +39 -0
  139. data/vendor/prism/include/prism/internal/source.h +72 -0
  140. data/vendor/prism/include/prism/internal/static_literals.h +60 -0
  141. data/vendor/prism/include/prism/internal/stringy.h +30 -0
  142. data/vendor/prism/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  143. data/vendor/prism/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  144. data/vendor/prism/include/prism/internal/tokens.h +11 -0
  145. data/vendor/prism/include/prism/internal/xxh3.h +16 -0
  146. data/vendor/prism/include/prism/json.h +40 -0
  147. data/vendor/prism/include/prism/line_offset_list.h +69 -0
  148. data/vendor/prism/include/prism/magic_comments.h +53 -0
  149. data/vendor/prism/include/prism/node.h +59 -49
  150. data/vendor/prism/include/prism/node.hpp +272 -0
  151. data/vendor/prism/include/prism/node_type.hpp +38 -0
  152. data/vendor/prism/include/prism/options.h +154 -279
  153. data/vendor/prism/include/prism/options.hpp +43 -0
  154. data/vendor/prism/include/prism/parser.h +367 -870
  155. data/vendor/prism/include/prism/parser.hpp +274 -0
  156. data/vendor/prism/include/prism/prettyprint.h +14 -9
  157. data/vendor/prism/include/prism/serialize.h +123 -0
  158. data/vendor/prism/include/prism/source.h +163 -0
  159. data/vendor/prism/include/prism/stream.h +36 -0
  160. data/vendor/prism/include/prism/string_query.h +82 -0
  161. data/vendor/prism/include/prism/stringy.h +61 -0
  162. data/vendor/prism/include/prism/stringy.hpp +31 -0
  163. data/vendor/prism/include/prism/version.h +20 -3
  164. data/vendor/prism/include/prism.h +35 -320
  165. data/vendor/prism/include/prism.hpp +24 -0
  166. data/vendor/prism/src/arena.c +122 -0
  167. data/vendor/prism/src/{util/pm_buffer.c → buffer.c} +53 -21
  168. data/vendor/prism/src/{util/pm_char.c → char.c} +28 -57
  169. data/vendor/prism/src/comments.c +455 -0
  170. data/vendor/prism/src/constant_pool.c +406 -0
  171. data/vendor/prism/src/diagnostic.c +120 -68
  172. data/vendor/prism/src/encoding.c +8 -3
  173. data/vendor/prism/src/errors_format.c +519 -0
  174. data/vendor/prism/src/{util/pm_integer.c → integer.c} +38 -22
  175. data/vendor/prism/src/json.c +5733 -0
  176. data/vendor/prism/src/line_offset_list.c +100 -0
  177. data/vendor/prism/src/list.c +24 -0
  178. data/vendor/prism/src/{util/pm_memchr.c → memchr.c} +8 -6
  179. data/vendor/prism/src/node.c +3465 -7787
  180. data/vendor/prism/src/options.c +161 -54
  181. data/vendor/prism/src/parser.c +371 -0
  182. data/vendor/prism/src/prettyprint.c +421 -354
  183. data/vendor/prism/src/prism.c +8522 -7280
  184. data/vendor/prism/src/regexp.c +960 -45
  185. data/vendor/prism/src/serialize.c +2434 -2048
  186. data/vendor/prism/src/source.c +524 -0
  187. data/vendor/prism/src/static_literals.c +303 -360
  188. data/vendor/prism/src/string_query.c +180 -0
  189. data/vendor/prism/src/stringy.c +61 -0
  190. data/vendor/prism/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  191. data/vendor/prism/src/strpbrk.c +447 -0
  192. data/vendor/prism/src/{token_type.c → tokens.c} +39 -18
  193. data/vendor/prism/src/xxh3.c +292 -0
  194. data/vendor/prism/templates/ext/prism/api_node.c.erb +58 -44
  195. data/vendor/prism/templates/include/prism/ast.h.erb +67 -17
  196. data/vendor/prism/templates/include/prism/ast.hpp.erb +31 -0
  197. data/vendor/prism/templates/include/prism/internal/diagnostic.h.erb +60 -0
  198. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/AbstractNodeVisitor.java.erb +1 -1
  199. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/Loader.java.erb +232 -133
  200. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/Nodes.java.erb +62 -23
  201. data/vendor/prism/templates/javascript/src/deserialize.js.erb +153 -146
  202. data/vendor/prism/templates/javascript/src/nodes.js.erb +4 -9
  203. data/vendor/prism/templates/lib/prism/compiler.rb.erb +14 -5
  204. data/vendor/prism/templates/lib/prism/dispatcher.rb.erb +20 -12
  205. data/vendor/prism/templates/lib/prism/dot_visitor.rb.erb +37 -11
  206. data/vendor/prism/templates/lib/prism/dsl.rb.erb +43 -4
  207. data/vendor/prism/templates/lib/prism/inspect_visitor.rb.erb +26 -10
  208. data/vendor/prism/templates/lib/prism/mutation_compiler.rb.erb +5 -2
  209. data/vendor/prism/templates/lib/prism/node.rb.erb +283 -105
  210. data/vendor/prism/templates/lib/prism/reflection.rb.erb +11 -2
  211. data/vendor/prism/templates/lib/prism/serialize.rb.erb +291 -134
  212. data/vendor/prism/templates/lib/prism/visitor.rb.erb +18 -0
  213. data/vendor/prism/templates/src/diagnostic.c.erb +114 -63
  214. data/vendor/prism/templates/src/json.c.erb +130 -0
  215. data/vendor/prism/templates/src/node.c.erb +79 -236
  216. data/vendor/prism/templates/src/prettyprint.c.erb +24 -13
  217. data/vendor/prism/templates/src/serialize.c.erb +369 -183
  218. data/vendor/prism/templates/src/{token_type.c.erb → tokens.c.erb} +22 -11
  219. data/vendor/prism/templates/template.rb +193 -91
  220. metadata +100 -42
  221. data/vendor/prism/include/prism/defines.h +0 -260
  222. data/vendor/prism/include/prism/pack.h +0 -163
  223. data/vendor/prism/include/prism/regexp.h +0 -43
  224. data/vendor/prism/include/prism/static_literals.h +0 -121
  225. data/vendor/prism/include/prism/util/pm_buffer.h +0 -236
  226. data/vendor/prism/include/prism/util/pm_char.h +0 -204
  227. data/vendor/prism/include/prism/util/pm_constant_pool.h +0 -218
  228. data/vendor/prism/include/prism/util/pm_integer.h +0 -130
  229. data/vendor/prism/include/prism/util/pm_memchr.h +0 -29
  230. data/vendor/prism/include/prism/util/pm_newline_list.h +0 -113
  231. data/vendor/prism/include/prism/util/pm_string.h +0 -200
  232. data/vendor/prism/src/pack.c +0 -509
  233. data/vendor/prism/src/util/pm_constant_pool.c +0 -342
  234. data/vendor/prism/src/util/pm_list.c +0 -49
  235. data/vendor/prism/src/util/pm_newline_list.c +0 -125
  236. data/vendor/prism/src/util/pm_string.c +0 -381
  237. data/vendor/prism/src/util/pm_strpbrk.c +0 -206
  238. data/vendor/prism/templates/include/prism/diagnostic.h.erb +0 -130
  239. data/vendor/prism/templates/rbi/prism/dsl.rbi.erb +0 -68
  240. data/vendor/prism/templates/rbi/prism/node.rbi.erb +0 -167
  241. data/vendor/prism/templates/rbi/prism/visitor.rbi.erb +0 -18
  242. data/vendor/prism/templates/sig/prism/_private/dot_visitor.rbs.erb +0 -45
  243. data/vendor/prism/templates/sig/prism/dsl.rbs.erb +0 -31
  244. data/vendor/prism/templates/sig/prism/mutation_compiler.rbs.erb +0 -7
  245. data/vendor/prism/templates/sig/prism/node.rbs.erb +0 -136
  246. data/vendor/prism/templates/sig/prism/visitor.rbs.erb +0 -17
  247. data/vendor/prism/templates/sig/prism.rbs.erb +0 -92
@@ -6,931 +6,428 @@
6
6
  #ifndef PRISM_PARSER_H
7
7
  #define PRISM_PARSER_H
8
8
 
9
- #include "prism/defines.h"
9
+ #include "prism/compiler/nodiscard.h"
10
+ #include "prism/compiler/nonnull.h"
11
+
10
12
  #include "prism/ast.h"
11
- #include "prism/encoding.h"
13
+ #include "prism/comments.h"
14
+ #include "prism/diagnostic.h"
15
+ #include "prism/line_offset_list.h"
16
+ #include "prism/magic_comments.h"
12
17
  #include "prism/options.h"
13
- #include "prism/static_literals.h"
14
- #include "prism/util/pm_constant_pool.h"
15
- #include "prism/util/pm_list.h"
16
- #include "prism/util/pm_newline_list.h"
17
- #include "prism/util/pm_string.h"
18
-
19
- #include <stdbool.h>
20
-
21
- /**
22
- * This enum provides various bits that represent different kinds of states that
23
- * the lexer can track. This is used to determine which kind of token to return
24
- * based on the context of the parser.
25
- */
26
- typedef enum {
27
- PM_LEX_STATE_BIT_BEG,
28
- PM_LEX_STATE_BIT_END,
29
- PM_LEX_STATE_BIT_ENDARG,
30
- PM_LEX_STATE_BIT_ENDFN,
31
- PM_LEX_STATE_BIT_ARG,
32
- PM_LEX_STATE_BIT_CMDARG,
33
- PM_LEX_STATE_BIT_MID,
34
- PM_LEX_STATE_BIT_FNAME,
35
- PM_LEX_STATE_BIT_DOT,
36
- PM_LEX_STATE_BIT_CLASS,
37
- PM_LEX_STATE_BIT_LABEL,
38
- PM_LEX_STATE_BIT_LABELED,
39
- PM_LEX_STATE_BIT_FITEM
40
- } pm_lex_state_bit_t;
41
-
42
- /**
43
- * This enum combines the various bits from the above enum into individual
44
- * values that represent the various states of the lexer.
45
- */
46
- typedef enum {
47
- PM_LEX_STATE_NONE = 0,
48
- PM_LEX_STATE_BEG = (1 << PM_LEX_STATE_BIT_BEG),
49
- PM_LEX_STATE_END = (1 << PM_LEX_STATE_BIT_END),
50
- PM_LEX_STATE_ENDARG = (1 << PM_LEX_STATE_BIT_ENDARG),
51
- PM_LEX_STATE_ENDFN = (1 << PM_LEX_STATE_BIT_ENDFN),
52
- PM_LEX_STATE_ARG = (1 << PM_LEX_STATE_BIT_ARG),
53
- PM_LEX_STATE_CMDARG = (1 << PM_LEX_STATE_BIT_CMDARG),
54
- PM_LEX_STATE_MID = (1 << PM_LEX_STATE_BIT_MID),
55
- PM_LEX_STATE_FNAME = (1 << PM_LEX_STATE_BIT_FNAME),
56
- PM_LEX_STATE_DOT = (1 << PM_LEX_STATE_BIT_DOT),
57
- PM_LEX_STATE_CLASS = (1 << PM_LEX_STATE_BIT_CLASS),
58
- PM_LEX_STATE_LABEL = (1 << PM_LEX_STATE_BIT_LABEL),
59
- PM_LEX_STATE_LABELED = (1 << PM_LEX_STATE_BIT_LABELED),
60
- PM_LEX_STATE_FITEM = (1 << PM_LEX_STATE_BIT_FITEM),
61
- PM_LEX_STATE_BEG_ANY = PM_LEX_STATE_BEG | PM_LEX_STATE_MID | PM_LEX_STATE_CLASS,
62
- PM_LEX_STATE_ARG_ANY = PM_LEX_STATE_ARG | PM_LEX_STATE_CMDARG,
63
- PM_LEX_STATE_END_ANY = PM_LEX_STATE_END | PM_LEX_STATE_ENDARG | PM_LEX_STATE_ENDFN
64
- } pm_lex_state_t;
65
-
66
- /**
67
- * The type of quote that a heredoc uses.
68
- */
69
- typedef enum {
70
- PM_HEREDOC_QUOTE_NONE,
71
- PM_HEREDOC_QUOTE_SINGLE = '\'',
72
- PM_HEREDOC_QUOTE_DOUBLE = '"',
73
- PM_HEREDOC_QUOTE_BACKTICK = '`',
74
- } pm_heredoc_quote_t;
75
-
76
- /**
77
- * The type of indentation that a heredoc uses.
78
- */
79
- typedef enum {
80
- PM_HEREDOC_INDENT_NONE,
81
- PM_HEREDOC_INDENT_DASH,
82
- PM_HEREDOC_INDENT_TILDE,
83
- } pm_heredoc_indent_t;
84
-
85
- /**
86
- * All of the information necessary to store to lexing a heredoc.
87
- */
88
- typedef struct {
89
- /** A pointer to the start of the heredoc identifier. */
90
- const uint8_t *ident_start;
91
-
92
- /** The length of the heredoc identifier. */
93
- size_t ident_length;
94
-
95
- /** The type of quote that the heredoc uses. */
96
- pm_heredoc_quote_t quote;
97
-
98
- /** The type of indentation that the heredoc uses. */
99
- pm_heredoc_indent_t indent;
100
- } pm_heredoc_lex_mode_t;
101
-
102
- /**
103
- * When lexing Ruby source, the lexer has a small amount of state to tell which
104
- * kind of token it is currently lexing. For example, when we find the start of
105
- * a string, the first token that we return is a TOKEN_STRING_BEGIN token. After
106
- * that the lexer is now in the PM_LEX_STRING mode, and will return tokens that
107
- * are found as part of a string.
108
- */
109
- typedef struct pm_lex_mode {
110
- /** The type of this lex mode. */
111
- enum {
112
- /** This state is used when any given token is being lexed. */
113
- PM_LEX_DEFAULT,
114
-
115
- /**
116
- * This state is used when we're lexing as normal but inside an embedded
117
- * expression of a string.
118
- */
119
- PM_LEX_EMBEXPR,
120
-
121
- /**
122
- * This state is used when we're lexing a variable that is embedded
123
- * directly inside of a string with the # shorthand.
124
- */
125
- PM_LEX_EMBVAR,
126
-
127
- /** This state is used when you are inside the content of a heredoc. */
128
- PM_LEX_HEREDOC,
129
-
130
- /**
131
- * This state is used when we are lexing a list of tokens, as in a %w
132
- * word list literal or a %i symbol list literal.
133
- */
134
- PM_LEX_LIST,
135
-
136
- /**
137
- * This state is used when a regular expression has been begun and we
138
- * are looking for the terminator.
139
- */
140
- PM_LEX_REGEXP,
141
-
142
- /**
143
- * This state is used when we are lexing a string or a string-like
144
- * token, as in string content with either quote or an xstring.
145
- */
146
- PM_LEX_STRING
147
- } mode;
148
-
149
- /** The data associated with this type of lex mode. */
150
- union {
151
- struct {
152
- /** This keeps track of the nesting level of the list. */
153
- size_t nesting;
154
-
155
- /** Whether or not interpolation is allowed in this list. */
156
- bool interpolation;
157
-
158
- /**
159
- * When lexing a list, it takes into account balancing the
160
- * terminator if the terminator is one of (), [], {}, or <>.
161
- */
162
- uint8_t incrementor;
163
-
164
- /** This is the terminator of the list literal. */
165
- uint8_t terminator;
166
-
167
- /**
168
- * This is the character set that should be used to delimit the
169
- * tokens within the list.
170
- */
171
- uint8_t breakpoints[11];
172
- } list;
173
-
174
- struct {
175
- /**
176
- * This keeps track of the nesting level of the regular expression.
177
- */
178
- size_t nesting;
179
-
180
- /**
181
- * When lexing a regular expression, it takes into account balancing
182
- * the terminator if the terminator is one of (), [], {}, or <>.
183
- */
184
- uint8_t incrementor;
185
-
186
- /** This is the terminator of the regular expression. */
187
- uint8_t terminator;
188
-
189
- /**
190
- * This is the character set that should be used to delimit the
191
- * tokens within the regular expression.
192
- */
193
- uint8_t breakpoints[7];
194
- } regexp;
195
-
196
- struct {
197
- /** This keeps track of the nesting level of the string. */
198
- size_t nesting;
199
-
200
- /** Whether or not interpolation is allowed in this string. */
201
- bool interpolation;
202
-
203
- /**
204
- * Whether or not at the end of the string we should allow a :,
205
- * which would indicate this was a dynamic symbol instead of a
206
- * string.
207
- */
208
- bool label_allowed;
209
-
210
- /**
211
- * When lexing a string, it takes into account balancing the
212
- * terminator if the terminator is one of (), [], {}, or <>.
213
- */
214
- uint8_t incrementor;
215
-
216
- /**
217
- * This is the terminator of the string. It is typically either a
218
- * single or double quote.
219
- */
220
- uint8_t terminator;
221
-
222
- /**
223
- * This is the character set that should be used to delimit the
224
- * tokens within the string.
225
- */
226
- uint8_t breakpoints[7];
227
- } string;
228
-
229
- struct {
230
- /**
231
- * All of the data necessary to lex a heredoc.
232
- */
233
- pm_heredoc_lex_mode_t base;
234
-
235
- /**
236
- * This is the pointer to the character where lexing should resume
237
- * once the heredoc has been completely processed.
238
- */
239
- const uint8_t *next_start;
240
-
241
- /**
242
- * This is used to track the amount of common whitespace on each
243
- * line so that we know how much to dedent each line in the case of
244
- * a tilde heredoc.
245
- */
246
- size_t *common_whitespace;
247
-
248
- /** True if the previous token ended with a line continuation. */
249
- bool line_continuation;
250
- } heredoc;
251
- } as;
252
-
253
- /** The previous lex state so that it knows how to pop. */
254
- struct pm_lex_mode *prev;
255
- } pm_lex_mode_t;
256
-
257
- /**
258
- * We pre-allocate a certain number of lex states in order to avoid having to
259
- * call malloc too many times while parsing. You really shouldn't need more than
260
- * this because you only really nest deeply when doing string interpolation.
261
- */
262
- #define PM_LEX_STACK_SIZE 4
18
+
19
+ #ifdef __cplusplus
20
+ extern "C" {
21
+ #endif
263
22
 
264
23
  /**
265
24
  * The parser used to parse Ruby source.
266
25
  */
267
- typedef struct pm_parser pm_parser_t;
26
+ typedef struct pm_parser_t pm_parser_t;
268
27
 
269
28
  /**
270
- * While parsing, we keep track of a stack of contexts. This is helpful for
271
- * error recovery so that we can pop back to a previous context when we hit a
272
- * token that is understood by a parent context but not by the current context.
29
+ * Allocate and initialize a parser with the given start and end pointers.
30
+ *
31
+ * @param arena The arena to use for all AST-lifetime allocations. It is caller-
32
+ * owned and must outlive the parser.
33
+ * @param source The source to parse.
34
+ * @param size The size of the source.
35
+ * @param options The optional options to use when parsing. These options must
36
+ * live for the whole lifetime of this parser.
37
+ * @returns The initialized parser. It is the responsibility of the caller to
38
+ * free the parser with `pm_parser_free()`.
273
39
  */
274
- typedef enum {
275
- /** a null context, used for returning a value from a function */
276
- PM_CONTEXT_NONE = 0,
277
-
278
- /** a begin statement */
279
- PM_CONTEXT_BEGIN,
280
-
281
- /** an ensure statement with an explicit begin */
282
- PM_CONTEXT_BEGIN_ENSURE,
283
-
284
- /** a rescue else statement with an explicit begin */
285
- PM_CONTEXT_BEGIN_ELSE,
286
-
287
- /** a rescue statement with an explicit begin */
288
- PM_CONTEXT_BEGIN_RESCUE,
289
-
290
- /** expressions in block arguments using braces */
291
- PM_CONTEXT_BLOCK_BRACES,
292
-
293
- /** expressions in block arguments using do..end */
294
- PM_CONTEXT_BLOCK_KEYWORDS,
295
-
296
- /** an ensure statement within a do..end block */
297
- PM_CONTEXT_BLOCK_ENSURE,
298
-
299
- /** a rescue else statement within a do..end block */
300
- PM_CONTEXT_BLOCK_ELSE,
301
-
302
- /** expressions in block parameters `foo do |...| end ` */
303
- PM_CONTEXT_BLOCK_PARAMETERS,
304
-
305
- /** a rescue statement within a do..end block */
306
- PM_CONTEXT_BLOCK_RESCUE,
307
-
308
- /** a case when statements */
309
- PM_CONTEXT_CASE_WHEN,
310
-
311
- /** a case in statements */
312
- PM_CONTEXT_CASE_IN,
313
-
314
- /** a class declaration */
315
- PM_CONTEXT_CLASS,
316
-
317
- /** an ensure statement within a class statement */
318
- PM_CONTEXT_CLASS_ENSURE,
319
-
320
- /** a rescue else statement within a class statement */
321
- PM_CONTEXT_CLASS_ELSE,
40
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_parser_t * pm_parser_new(pm_arena_t *arena, const uint8_t *source, size_t size, const pm_options_t *options) PRISM_NONNULL(1);
322
41
 
323
- /** a rescue statement within a class statement */
324
- PM_CONTEXT_CLASS_RESCUE,
325
-
326
- /** a method definition */
327
- PM_CONTEXT_DEF,
328
-
329
- /** an ensure statement within a method definition */
330
- PM_CONTEXT_DEF_ENSURE,
331
-
332
- /** a rescue else statement within a method definition */
333
- PM_CONTEXT_DEF_ELSE,
334
-
335
- /** a rescue statement within a method definition */
336
- PM_CONTEXT_DEF_RESCUE,
337
-
338
- /** a method definition's parameters */
339
- PM_CONTEXT_DEF_PARAMS,
340
-
341
- /** a defined? expression */
342
- PM_CONTEXT_DEFINED,
343
-
344
- /** a method definition's default parameter */
345
- PM_CONTEXT_DEFAULT_PARAMS,
346
-
347
- /** an else clause */
348
- PM_CONTEXT_ELSE,
349
-
350
- /** an elsif clause */
351
- PM_CONTEXT_ELSIF,
352
-
353
- /** an interpolated expression */
354
- PM_CONTEXT_EMBEXPR,
355
-
356
- /** a for loop */
357
- PM_CONTEXT_FOR,
358
-
359
- /** a for loop's index */
360
- PM_CONTEXT_FOR_INDEX,
361
-
362
- /** an if statement */
363
- PM_CONTEXT_IF,
364
-
365
- /** a lambda expression with braces */
366
- PM_CONTEXT_LAMBDA_BRACES,
42
+ /**
43
+ * Free both the memory held by the given parser and the parser itself.
44
+ *
45
+ * @param parser The parser to free.
46
+ */
47
+ PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser) PRISM_NONNULL(1);
367
48
 
368
- /** a lambda expression with do..end */
369
- PM_CONTEXT_LAMBDA_DO_END,
49
+ /**
50
+ * When the encoding that is being used to parse the source is changed by prism,
51
+ * we provide the ability here to call out to a user-defined function.
52
+ */
53
+ typedef void (*pm_encoding_changed_callback_t)(pm_parser_t *parser);
370
54
 
371
- /** an ensure statement within a lambda expression */
372
- PM_CONTEXT_LAMBDA_ENSURE,
55
+ /**
56
+ * This is the callback that is called when a token is lexed. It is passed
57
+ * the opaque data pointer, the parser, and the token that was lexed.
58
+ */
59
+ typedef void (*pm_lex_callback_t)(pm_parser_t *parser, pm_token_t *token, void *data);
373
60
 
374
- /** a rescue else statement within a lambda expression */
375
- PM_CONTEXT_LAMBDA_ELSE,
61
+ /**
62
+ * Register a callback that will be called whenever prism changes the encoding
63
+ * it is using to parse based on the magic comment.
64
+ *
65
+ * @param parser The parser to register the callback with.
66
+ * @param callback The callback to register.
67
+ */
68
+ PRISM_EXPORTED_FUNCTION void pm_parser_encoding_changed_callback_set(pm_parser_t *parser, pm_encoding_changed_callback_t callback) PRISM_NONNULL(1);
376
69
 
377
- /** a rescue statement within a lambda expression */
378
- PM_CONTEXT_LAMBDA_RESCUE,
70
+ /**
71
+ * Register a callback that will be called whenever a token is lexed.
72
+ *
73
+ * @param parser The parser to register the callback with.
74
+ * @param data The opaque data to pass to the callback when it is called.
75
+ * @param callback The callback to register.
76
+ */
77
+ PRISM_EXPORTED_FUNCTION void pm_parser_lex_callback_set(pm_parser_t *parser, pm_lex_callback_t callback, void *data) PRISM_NONNULL(1);
379
78
 
380
- /** the predicate clause of a loop statement */
381
- PM_CONTEXT_LOOP_PREDICATE,
79
+ /**
80
+ * Returns the opaque data that is passed to the lex callback when it is called.
81
+ *
82
+ * @param parser The parser whose lex callback data we want to get.
83
+ * @returns The opaque data that is passed to the lex callback when it is called.
84
+ */
85
+ PRISM_EXPORTED_FUNCTION void * pm_parser_lex_callback_data(const pm_parser_t *parser) PRISM_NONNULL(1);
382
86
 
383
- /** the top level context */
384
- PM_CONTEXT_MAIN,
87
+ /**
88
+ * Returns the raw pointer to the start of the source that is being parsed.
89
+ *
90
+ * @param parser the parser whose start pointer we want to get
91
+ * @returns the raw pointer to the start of the source that is being parsed
92
+ */
93
+ PRISM_EXPORTED_FUNCTION const uint8_t * pm_parser_start(const pm_parser_t *parser) PRISM_NONNULL(1);
385
94
 
386
- /** a module declaration */
387
- PM_CONTEXT_MODULE,
95
+ /**
96
+ * Returns the raw pointer to the end of the source that is being parsed.
97
+ *
98
+ * @param parser the parser whose end pointer we want to get
99
+ * @returns the raw pointer to the end of the source that is being parsed
100
+ */
101
+ PRISM_EXPORTED_FUNCTION const uint8_t * pm_parser_end(const pm_parser_t *parser) PRISM_NONNULL(1);
388
102
 
389
- /** an ensure statement within a module statement */
390
- PM_CONTEXT_MODULE_ENSURE,
103
+ /**
104
+ * Returns the line that the parser was considered to have started on.
105
+ *
106
+ * @param parser the parser whose start line we want to get
107
+ * @returns the line that the parser was considered to have started on
108
+ */
109
+ PRISM_EXPORTED_FUNCTION int32_t pm_parser_start_line(const pm_parser_t *parser) PRISM_NONNULL(1);
391
110
 
392
- /** a rescue else statement within a module statement */
393
- PM_CONTEXT_MODULE_ELSE,
111
+ /**
112
+ * Returns the name of the encoding that is being used to parse the source.
113
+ *
114
+ * @param parser the parser whose encoding name we want to get
115
+ * @returns the name of the encoding that is being used to parse the source
116
+ */
117
+ PRISM_EXPORTED_FUNCTION const char * pm_parser_encoding_name(const pm_parser_t *parser) PRISM_NONNULL(1);
394
118
 
395
- /** a rescue statement within a module statement */
396
- PM_CONTEXT_MODULE_RESCUE,
119
+ /**
120
+ * Returns the width of the character at the given pointer in the encoding that
121
+ * is being used to parse the source.
122
+ *
123
+ * @param parser the parser whose encoding we want to use
124
+ * @param start a pointer to the start of the character
125
+ * @param remaining the number of bytes remaining in the source
126
+ * @returns the width of the character in bytes
127
+ */
128
+ PRISM_EXPORTED_FUNCTION size_t pm_parser_encoding_char_width(const pm_parser_t *parser, const uint8_t *start, ptrdiff_t remaining) PRISM_NONNULL(1, 2);
397
129
 
398
- /** a multiple target expression */
399
- PM_CONTEXT_MULTI_TARGET,
130
+ /**
131
+ * Returns whether or not the parser is using the US-ASCII encoding.
132
+ *
133
+ * @param parser the parser to check
134
+ * @returns true if the parser is using US-ASCII encoding, false otherwise
135
+ */
136
+ PRISM_EXPORTED_FUNCTION bool pm_parser_encoding_us_ascii(const pm_parser_t *parser) PRISM_NONNULL(1);
400
137
 
401
- /** a parenthesized expression */
402
- PM_CONTEXT_PARENS,
138
+ /**
139
+ * Returns the filepath that is being used to parse the source.
140
+ *
141
+ * @param parser the parser whose filepath we want to get
142
+ * @returns a pointer to the filepath string
143
+ */
144
+ PRISM_EXPORTED_FUNCTION const pm_string_t * pm_parser_filepath(const pm_parser_t *parser) PRISM_NONNULL(1);
403
145
 
404
- /** an END block */
405
- PM_CONTEXT_POSTEXE,
146
+ /**
147
+ * Find a constant in the parser's constant pool. Returns the id of the
148
+ * constant, or 0 if the constant is not found.
149
+ *
150
+ * @param parser the parser whose constant pool we want to search
151
+ * @param start a pointer to the start of the string to search for
152
+ * @param length the length of the string to search for
153
+ * @returns the id of the constant, or 0 if the constant is not found
154
+ */
155
+ PRISM_EXPORTED_FUNCTION pm_constant_id_t pm_parser_constant_find(const pm_parser_t *parser, const uint8_t *start, size_t length) PRISM_NONNULL(1, 2);
406
156
 
407
- /** a predicate inside an if/elsif/unless statement */
408
- PM_CONTEXT_PREDICATE,
157
+ /**
158
+ * Add a constant to the parser's constant pool, for naming a node that was not
159
+ * parsed from the source. If the constant is already in the pool, its existing
160
+ * id is returned. Otherwise the parser copies the bytes, so they only need to
161
+ * remain valid for the duration of the call.
162
+ *
163
+ * Adding a constant makes the pool larger, so a cache with one entry per
164
+ * constant built beforehand from pm_parser_constants_size and
165
+ * pm_parser_constant_at does not cover it. Build such caches after adding any
166
+ * constants.
167
+ *
168
+ * @param parser the parser whose constant pool we want to add to
169
+ * @param start a pointer to the start of the string to add
170
+ * @param length the length of the string to add
171
+ * @returns the id of the constant
172
+ */
173
+ PRISM_EXPORTED_FUNCTION pm_constant_id_t pm_parser_constant_insert(pm_parser_t *parser, const uint8_t *start, size_t length) PRISM_NONNULL(1, 2);
409
174
 
410
- /** a BEGIN block */
411
- PM_CONTEXT_PREEXE,
175
+ /**
176
+ * Returns the frozen string literal value of the parser, as determined by the
177
+ * frozen_string_literal magic comment or the option set on the parser.
178
+ *
179
+ * @param parser the parser whose frozen string literal value we want to get
180
+ * @returns -1 if disabled, 0 if unset, 1 if enabled
181
+ */
182
+ PRISM_EXPORTED_FUNCTION int8_t pm_parser_frozen_string_literal(const pm_parser_t *parser) PRISM_NONNULL(1);
412
183
 
413
- /** a modifier rescue clause */
414
- PM_CONTEXT_RESCUE_MODIFIER,
184
+ /**
185
+ * Returns the line offsets that are associated with the given parser.
186
+ *
187
+ * @param parser the parser whose line offsets we want to get
188
+ * @returns the line offsets that are associated with the given parser
189
+ */
190
+ PRISM_EXPORTED_FUNCTION const pm_line_offset_list_t * pm_parser_line_offsets(const pm_parser_t *parser) PRISM_NONNULL(1);
415
191
 
416
- /** a singleton class definition */
417
- PM_CONTEXT_SCLASS,
192
+ /**
193
+ * Returns the location of the __DATA__ section that is associated with the
194
+ * given parser.
195
+ *
196
+ * @param parser the parser whose data location we want to get
197
+ * @returns the location of the __DATA__ section that is associated with the
198
+ * given parser. If it is unset, then the length will be set to 0.
199
+ */
200
+ PRISM_EXPORTED_FUNCTION const pm_location_t * pm_parser_data_loc(const pm_parser_t *parser) PRISM_NONNULL(1);
418
201
 
419
- /** an ensure statement with a singleton class */
420
- PM_CONTEXT_SCLASS_ENSURE,
202
+ /**
203
+ * Returns the checksum of the source code that was parsed. The data section
204
+ * after an __END__ marker is not code, so the hash covers the source only up to
205
+ * and including the newline that ends the __END__ line. The __END__ marker is
206
+ * only found by parsing, so this must be called after pm_parse.
207
+ *
208
+ * The hash is computed from the source on each call rather than during
209
+ * parsing, so that parses that never ask for it do not pay for it. This
210
+ * requires the source to still be alive, and callers that need the value more
211
+ * than once should keep it.
212
+ *
213
+ * The hash is currently the 64-bit variant of XXH3 with a seed of 0. It is not
214
+ * cryptographic, and the algorithm may change between versions of prism, so
215
+ * hashes should only be compared against other hashes computed by the same
216
+ * version of prism.
217
+ *
218
+ * @param parser the parser whose checksum we want to get
219
+ * @returns the checksum of the source code that was parsed
220
+ */
221
+ PRISM_EXPORTED_FUNCTION uint64_t pm_parser_checksum(const pm_parser_t *parser) PRISM_NONNULL(1);
421
222
 
422
- /** a rescue else statement with a singleton class */
423
- PM_CONTEXT_SCLASS_ELSE,
223
+ /**
224
+ * Returns whether the given parser is continuable, meaning that it could become
225
+ * valid if more input were appended, as opposed to being definitively invalid.
226
+ *
227
+ * @param parser the parser whose continuable status we want to get
228
+ * @returns whether the given parser is continuable
229
+ */
230
+ PRISM_EXPORTED_FUNCTION bool pm_parser_continuable(const pm_parser_t *parser) PRISM_NONNULL(1);
424
231
 
425
- /** a rescue statement with a singleton class */
426
- PM_CONTEXT_SCLASS_RESCUE,
232
+ /**
233
+ * Returns the lex state of the parser. Note that this is an internal detail,
234
+ * and we are purposefully not returning an instance of the internal enum that
235
+ * we use to track this. This is only exposed because we need it for some very
236
+ * niche use cases. Most consumers should avoid this function.
237
+ *
238
+ * @param parser the parser whose lex state we want to get
239
+ * @returns the lex state of the parser
240
+ */
241
+ PRISM_EXPORTED_FUNCTION int pm_parser_lex_state(const pm_parser_t *parser) PRISM_NONNULL(1);
427
242
 
428
- /** a ternary expression */
429
- PM_CONTEXT_TERNARY,
243
+ /**
244
+ * Returns the number of comments associated with the given parser.
245
+ *
246
+ * @param parser the parser whose comments we want to get the size of
247
+ * @returns the number of comments associated with the given parser
248
+ */
249
+ PRISM_EXPORTED_FUNCTION size_t pm_parser_comments_size(const pm_parser_t *parser) PRISM_NONNULL(1);
430
250
 
431
- /** an unless statement */
432
- PM_CONTEXT_UNLESS,
251
+ /**
252
+ * Returns the first comment associated with the given parser. Iterate over the
253
+ * rest with pm_comment_next, as in:
254
+ *
255
+ * ```c
256
+ * for (const pm_comment_t *comment = pm_parser_comments_first(parser); comment != NULL; comment = pm_comment_next(comment)) {
257
+ * pm_location_t location = pm_comment_location(comment);
258
+ * }
259
+ * ```
260
+ *
261
+ * @param parser the parser whose comments we want to iterate over
262
+ * @returns the first comment, or NULL if there are no comments
263
+ */
264
+ PRISM_EXPORTED_FUNCTION const pm_comment_t * pm_parser_comments_first(const pm_parser_t *parser) PRISM_NONNULL(1);
433
265
 
434
- /** an until statement */
435
- PM_CONTEXT_UNTIL,
266
+ /**
267
+ * Returns the number of comments owned by the given node. This is 0 if the
268
+ * node owns no comments or if the attach_comments option was not set.
269
+ *
270
+ * @param parser the parser that produced the node
271
+ * @param node the node whose comments we want to count
272
+ * @returns the number of comments owned by the given node
273
+ */
274
+ PRISM_EXPORTED_FUNCTION uint32_t pm_node_comments_size(const pm_parser_t *parser, const pm_node_t *node) PRISM_NONNULL(1, 2);
436
275
 
437
- /** a while statement */
438
- PM_CONTEXT_WHILE,
439
- } pm_context_t;
276
+ /**
277
+ * Returns the comment at the given index among the comments owned by the given
278
+ * node, which are ordered by start offset.
279
+ *
280
+ * @param parser the parser that produced the node
281
+ * @param node the node whose comment we want to get
282
+ * @param index the index of the comment among those owned by the node
283
+ * @returns the comment at the given index, or NULL if the index is out of range
284
+ */
285
+ PRISM_EXPORTED_FUNCTION const pm_comment_t * pm_node_comment(const pm_parser_t *parser, const pm_node_t *node, uint32_t index) PRISM_NONNULL(1, 2);
440
286
 
441
- /** This is a node in a linked list of contexts. */
442
- typedef struct pm_context_node {
443
- /** The context that this node represents. */
444
- pm_context_t context;
287
+ /**
288
+ * Returns the index, among the comments associated with the given parser, of
289
+ * the comment at the given index among the comment attachments. The attachments
290
+ * are every comment, grouped by owning node in order of node id and ordered by
291
+ * start offset within each node, so there are as many attachments as there are
292
+ * comments when the attach_comments option is set.
293
+ *
294
+ * @param parser the parser whose comment attachments we want to read
295
+ * @param index the index of the attachment
296
+ * @returns the index of the comment, or UINT32_MAX if the index is out of range
297
+ * or the attach_comments option was not set
298
+ */
299
+ PRISM_EXPORTED_FUNCTION uint32_t pm_parser_comment_attachment(const pm_parser_t *parser, uint32_t index) PRISM_NONNULL(1);
445
300
 
446
- /** A pointer to the previous context in the linked list. */
447
- struct pm_context_node *prev;
448
- } pm_context_node_t;
301
+ /**
302
+ * Returns the number of node ids that have a comment range, which is one more
303
+ * than the largest id of a node that owns a comment, or 0 if the
304
+ * attach_comments option was not set or there are no comments.
305
+ *
306
+ * @param parser the parser whose comment ranges we want to count
307
+ * @returns the number of comment ranges
308
+ */
309
+ PRISM_EXPORTED_FUNCTION uint32_t pm_parser_comment_ranges_size(const pm_parser_t *parser) PRISM_NONNULL(1);
449
310
 
450
- /** This is the type of a comment that we've found while parsing. */
451
- typedef enum {
452
- PM_COMMENT_INLINE,
453
- PM_COMMENT_EMBDOC
454
- } pm_comment_type_t;
311
+ /**
312
+ * Returns the range of comment attachments owned by the node with the given id.
313
+ *
314
+ * @param parser the parser that produced the node
315
+ * @param node_id the id of the node
316
+ * @returns the range of attachments owned by the node, whose length is 0 if the
317
+ * node owns no comments, the node id is out of range, or the
318
+ * attach_comments option was not set
319
+ */
320
+ PRISM_EXPORTED_FUNCTION pm_comment_range_t pm_parser_comment_range(const pm_parser_t *parser, uint32_t node_id) PRISM_NONNULL(1);
455
321
 
456
322
  /**
457
- * This is a node in the linked list of comments that we've found while parsing.
323
+ * Returns the number of magic comments associated with the given parser.
458
324
  *
459
- * @extends pm_list_node_t
325
+ * @param parser the parser whose magic comments we want to get the size of
326
+ * @returns the number of magic comments associated with the given parser
460
327
  */
461
- typedef struct pm_comment {
462
- /** The embedded base node. */
463
- pm_list_node_t node;
328
+ PRISM_EXPORTED_FUNCTION size_t pm_parser_magic_comments_size(const pm_parser_t *parser) PRISM_NONNULL(1);
464
329
 
465
- /** The location of the comment in the source. */
466
- pm_location_t location;
330
+ /**
331
+ * Returns the first magic comment associated with the given parser. Iterate
332
+ * over the rest with pm_magic_comment_next.
333
+ *
334
+ * @param parser the parser whose magic comments we want to iterate over
335
+ * @returns the first magic comment, or NULL if there are no magic comments
336
+ */
337
+ PRISM_EXPORTED_FUNCTION const pm_magic_comment_t * pm_parser_magic_comments_first(const pm_parser_t *parser) PRISM_NONNULL(1);
467
338
 
468
- /** The type of comment that we've found. */
469
- pm_comment_type_t type;
470
- } pm_comment_t;
339
+ /**
340
+ * Returns the number of errors associated with the given parser.
341
+ *
342
+ * @param parser the parser whose errors we want to get the size of
343
+ * @returns the number of errors associated with the given parser
344
+ */
345
+ PRISM_EXPORTED_FUNCTION size_t pm_parser_errors_size(const pm_parser_t *parser) PRISM_NONNULL(1);
471
346
 
472
347
  /**
473
- * This is a node in the linked list of magic comments that we've found while
474
- * parsing.
348
+ * Returns the number of warnings associated with the given parser.
475
349
  *
476
- * @extends pm_list_node_t
350
+ * @param parser the parser whose warnings we want to get the size of
351
+ * @returns the number of warnings associated with the given parser
477
352
  */
478
- typedef struct {
479
- /** The embedded base node. */
480
- pm_list_node_t node;
353
+ PRISM_EXPORTED_FUNCTION size_t pm_parser_warnings_size(const pm_parser_t *parser) PRISM_NONNULL(1);
481
354
 
482
- /** A pointer to the start of the key in the source. */
483
- const uint8_t *key_start;
355
+ /**
356
+ * Returns the first error associated with the given parser. Iterate over the
357
+ * rest with pm_diagnostic_next.
358
+ *
359
+ * @param parser the parser whose errors we want to iterate over
360
+ * @returns the first error, or NULL if there are no errors
361
+ */
362
+ PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_parser_errors_first(const pm_parser_t *parser) PRISM_NONNULL(1);
484
363
 
485
- /** A pointer to the start of the value in the source. */
486
- const uint8_t *value_start;
364
+ /**
365
+ * Returns the first warning associated with the given parser. Iterate over the
366
+ * rest with pm_diagnostic_next.
367
+ *
368
+ * @param parser the parser whose warnings we want to iterate over
369
+ * @returns the first warning, or NULL if there are no warnings
370
+ */
371
+ PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_parser_warnings_first(const pm_parser_t *parser) PRISM_NONNULL(1);
487
372
 
488
- /** The length of the key in the source. */
489
- uint32_t key_length;
373
+ /**
374
+ * Returns the number of constants in the constant pool associated with the
375
+ * given parser.
376
+ *
377
+ * @param parser the parser whose constant pool constants we want to get the
378
+ * size of
379
+ * @returns the number of constants in the constant pool associated with the
380
+ * given parser
381
+ */
382
+ PRISM_EXPORTED_FUNCTION size_t pm_parser_constants_size(const pm_parser_t *parser) PRISM_NONNULL(1);
490
383
 
491
- /** The length of the value in the source. */
492
- uint32_t value_length;
493
- } pm_magic_comment_t;
384
+ /**
385
+ * Returns the constant at the given index in the constant pool associated with
386
+ * the given parser. Together with pm_constant_id_index, this lets a caller
387
+ * build a cache with one entry per constant and then find the entry for any
388
+ * constant id, as in:
389
+ *
390
+ * ```c
391
+ * size_t size = pm_parser_constants_size(parser);
392
+ * for (size_t index = 0; index < size; index++) {
393
+ * cache[index] = convert(pm_parser_constant_at(parser, index));
394
+ * }
395
+ *
396
+ * value = cache[pm_constant_id_index(constant_id)];
397
+ * ```
398
+ *
399
+ * For any constant id, pm_parser_constant_at(parser,
400
+ * pm_constant_id_index(constant_id)) is pm_parser_constant(parser,
401
+ * constant_id).
402
+ *
403
+ * @param parser the parser whose constant pool we want to look up from
404
+ * @param index the index of the constant, which must be less than
405
+ * pm_parser_constants_size(parser)
406
+ * @returns a pointer to the constant at the given index
407
+ */
408
+ PRISM_EXPORTED_FUNCTION const pm_constant_t * pm_parser_constant_at(const pm_parser_t *parser, size_t index) PRISM_NONNULL(1);
494
409
 
495
410
  /**
496
- * When the encoding that is being used to parse the source is changed by prism,
497
- * we provide the ability here to call out to a user-defined function.
411
+ * Returns a pointer to the constant at the given id in the constant pool
412
+ * associated with the given parser.
413
+ *
414
+ * @param parser the parser whose constant pool we want to look up from
415
+ * @param constant_id the id of the constant to look up. Optional constant
416
+ * fields on nodes hold 0 when they are absent, which is not a valid id.
417
+ * @returns a pointer to the constant at the given id
498
418
  */
499
- typedef void (*pm_encoding_changed_callback_t)(pm_parser_t *parser);
419
+ PRISM_EXPORTED_FUNCTION const pm_constant_t * pm_parser_constant(const pm_parser_t *parser, pm_constant_id_t constant_id) PRISM_NONNULL(1);
500
420
 
501
421
  /**
502
- * When you are lexing through a file, the lexer needs all of the information
503
- * that the parser additionally provides (for example, the local table). So if
504
- * you want to properly lex Ruby, you need to actually lex it in the context of
505
- * the parser. In order to provide this functionality, we optionally allow a
506
- * struct to be attached to the parser that calls back out to a user-provided
507
- * callback when each token is lexed.
508
- */
509
- typedef struct {
510
- /**
511
- * This opaque pointer is used to provide whatever information the user
512
- * deemed necessary to the callback. In our case we use it to pass the array
513
- * that the tokens get appended into.
514
- */
515
- void *data;
516
-
517
- /**
518
- * This is the callback that is called when a token is lexed. It is passed
519
- * the opaque data pointer, the parser, and the token that was lexed.
520
- */
521
- void (*callback)(void *data, pm_parser_t *parser, pm_token_t *token);
522
- } pm_lex_callback_t;
523
-
524
- /** The type of shareable constant value that can be set. */
525
- typedef uint8_t pm_shareable_constant_value_t;
526
- static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_NONE = 0x0;
527
- static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_LITERAL = PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL;
528
- static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_EVERYTHING = PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING;
529
- static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_COPY = PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY;
530
-
531
- /**
532
- * This tracks an individual local variable in a certain lexical context, as
533
- * well as the number of times is it read.
534
- */
535
- typedef struct {
536
- /** The name of the local variable. */
537
- pm_constant_id_t name;
538
-
539
- /** The location of the local variable in the source. */
540
- pm_location_t location;
541
-
542
- /** The index of the local variable in the local table. */
543
- uint32_t index;
544
-
545
- /** The number of times the local variable is read. */
546
- uint32_t reads;
547
-
548
- /** The hash of the local variable. */
549
- uint32_t hash;
550
- } pm_local_t;
551
-
552
- /**
553
- * This is a set of local variables in a certain lexical context (method, class,
554
- * module, etc.). We need to track how many times these variables are read in
555
- * order to warn if they only get written.
556
- */
557
- typedef struct pm_locals {
558
- /** The number of local variables in the set. */
559
- uint32_t size;
560
-
561
- /** The capacity of the local variables set. */
562
- uint32_t capacity;
563
-
564
- /** The nullable allocated memory for the local variables in the set. */
565
- pm_local_t *locals;
566
- } pm_locals_t;
567
-
568
- /** The flags about scope parameters that can be set. */
569
- typedef uint8_t pm_scope_parameters_t;
570
- static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NONE = 0x0;
571
- static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_POSITIONALS = 0x1;
572
- static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_KEYWORDS = 0x2;
573
- static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_BLOCK = 0x4;
574
- static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_ALL = 0x8;
575
- static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_IMPLICIT_DISALLOWED = 0x10;
576
- static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NUMBERED_INNER = 0x20;
577
- static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NUMBERED_FOUND = 0x40;
578
-
579
- /**
580
- * This struct represents a node in a linked list of scopes. Some scopes can see
581
- * into their parent scopes, while others cannot.
582
- */
583
- typedef struct pm_scope {
584
- /** A pointer to the previous scope in the linked list. */
585
- struct pm_scope *previous;
586
-
587
- /** The IDs of the locals in the given scope. */
588
- pm_locals_t locals;
589
-
590
- /**
591
- * This is a list of the implicit parameters contained within the block.
592
- * These will be processed after the block is parsed to determine the kind
593
- * of parameters node that should be used and to check if any errors need to
594
- * be added.
595
- */
596
- pm_node_list_t implicit_parameters;
597
-
598
- /**
599
- * This is a bitfield that indicates the parameters that are being used in
600
- * this scope. It is a combination of the PM_SCOPE_PARAMETERS_* constants.
601
- * There are three different kinds of parameters that can be used in a
602
- * scope:
603
- *
604
- * - Ordinary parameters (e.g., def foo(bar); end)
605
- * - Numbered parameters (e.g., def foo; _1; end)
606
- * - The it parameter (e.g., def foo; it; end)
607
- *
608
- * If ordinary parameters are being used, then certain parameters can be
609
- * forwarded to another method/structure. Those are indicated by four
610
- * additional bits in the params field. For example, some combinations of:
611
- *
612
- * - def foo(*); end
613
- * - def foo(**); end
614
- * - def foo(&); end
615
- * - def foo(...); end
616
- */
617
- pm_scope_parameters_t parameters;
618
-
619
- /**
620
- * The current state of constant shareability for this scope. This is
621
- * changed by magic shareable_constant_value comments.
622
- */
623
- pm_shareable_constant_value_t shareable_constant;
624
-
625
- /**
626
- * A boolean indicating whether or not this scope can see into its parent.
627
- * If closed is true, then the scope cannot see into its parent.
628
- */
629
- bool closed;
630
- } pm_scope_t;
631
-
632
- /**
633
- * A struct that represents a stack of boolean values.
634
- */
635
- typedef uint32_t pm_state_stack_t;
636
-
637
- /**
638
- * This struct represents the overall parser. It contains a reference to the
639
- * source file, as well as pointers that indicate where in the source it's
640
- * currently parsing. It also contains the most recent and current token that
641
- * it's considering.
642
- */
643
- struct pm_parser {
644
- /**
645
- * The next node identifier that will be assigned. This is a unique
646
- * identifier used to track nodes such that the syntax tree can be dropped
647
- * but the node can be found through another parse.
648
- */
649
- uint32_t node_id;
650
-
651
- /** The current state of the lexer. */
652
- pm_lex_state_t lex_state;
653
-
654
- /** Tracks the current nesting of (), [], and {}. */
655
- int enclosure_nesting;
656
-
657
- /**
658
- * Used to temporarily track the nesting of enclosures to determine if a {
659
- * is the beginning of a lambda following the parameters of a lambda.
660
- */
661
- int lambda_enclosure_nesting;
662
-
663
- /**
664
- * Used to track the nesting of braces to ensure we get the correct value
665
- * when we are interpolating blocks with braces.
666
- */
667
- int brace_nesting;
668
-
669
- /**
670
- * The stack used to determine if a do keyword belongs to the predicate of a
671
- * while, until, or for loop.
672
- */
673
- pm_state_stack_t do_loop_stack;
674
-
675
- /**
676
- * The stack used to determine if a do keyword belongs to the beginning of a
677
- * block.
678
- */
679
- pm_state_stack_t accepts_block_stack;
680
-
681
- /** A stack of lex modes. */
682
- struct {
683
- /** The current mode of the lexer. */
684
- pm_lex_mode_t *current;
685
-
686
- /** The stack of lexer modes. */
687
- pm_lex_mode_t stack[PM_LEX_STACK_SIZE];
688
-
689
- /** The current index into the lexer mode stack. */
690
- size_t index;
691
- } lex_modes;
692
-
693
- /** The pointer to the start of the source. */
694
- const uint8_t *start;
695
-
696
- /** The pointer to the end of the source. */
697
- const uint8_t *end;
698
-
699
- /** The previous token we were considering. */
700
- pm_token_t previous;
701
-
702
- /** The current token we're considering. */
703
- pm_token_t current;
704
-
705
- /**
706
- * This is a special field set on the parser when we need the parser to jump
707
- * to a specific location when lexing the next token, as opposed to just
708
- * using the end of the previous token. Normally this is NULL.
709
- */
710
- const uint8_t *next_start;
711
-
712
- /**
713
- * This field indicates the end of a heredoc whose identifier was found on
714
- * the current line. If another heredoc is found on the same line, then this
715
- * will be moved forward to the end of that heredoc. If no heredocs are
716
- * found on a line then this is NULL.
717
- */
718
- const uint8_t *heredoc_end;
719
-
720
- /** The list of comments that have been found while parsing. */
721
- pm_list_t comment_list;
722
-
723
- /** The list of magic comments that have been found while parsing. */
724
- pm_list_t magic_comment_list;
725
-
726
- /**
727
- * An optional location that represents the location of the __END__ marker
728
- * and the rest of the content of the file. This content is loaded into the
729
- * DATA constant when the file being parsed is the main file being executed.
730
- */
731
- pm_location_t data_loc;
732
-
733
- /** The list of warnings that have been found while parsing. */
734
- pm_list_t warning_list;
735
-
736
- /** The list of errors that have been found while parsing. */
737
- pm_list_t error_list;
738
-
739
- /** The current local scope. */
740
- pm_scope_t *current_scope;
741
-
742
- /** The current parsing context. */
743
- pm_context_node_t *current_context;
744
-
745
- /**
746
- * The hash keys for the hash that is currently being parsed. This is not
747
- * usually necessary because it can pass it down the various call chains,
748
- * but in the event that you're parsing a hash that is being directly
749
- * pushed into another hash with **, we need to share the hash keys so that
750
- * we can warn for the nested hash as well.
751
- */
752
- pm_static_literals_t *current_hash_keys;
753
-
754
- /**
755
- * The encoding functions for the current file is attached to the parser as
756
- * it's parsing so that it can change with a magic comment.
757
- */
758
- const pm_encoding_t *encoding;
759
-
760
- /**
761
- * When the encoding that is being used to parse the source is changed by
762
- * prism, we provide the ability here to call out to a user-defined
763
- * function.
764
- */
765
- pm_encoding_changed_callback_t encoding_changed_callback;
766
-
767
- /**
768
- * This pointer indicates where a comment must start if it is to be
769
- * considered an encoding comment.
770
- */
771
- const uint8_t *encoding_comment_start;
772
-
773
- /**
774
- * This is an optional callback that can be attached to the parser that will
775
- * be called whenever a new token is lexed by the parser.
776
- */
777
- pm_lex_callback_t *lex_callback;
778
-
779
- /**
780
- * This is the path of the file being parsed. We use the filepath when
781
- * constructing SourceFileNodes.
782
- */
783
- pm_string_t filepath;
784
-
785
- /**
786
- * This constant pool keeps all of the constants defined throughout the file
787
- * so that we can reference them later.
788
- */
789
- pm_constant_pool_t constant_pool;
790
-
791
- /** This is the list of newline offsets in the source file. */
792
- pm_newline_list_t newline_list;
793
-
794
- /**
795
- * We want to add a flag to integer nodes that indicates their base. We only
796
- * want to parse these once, but we don't have space on the token itself to
797
- * communicate this information. So we store it here and pass it through
798
- * when we find tokens that we need it for.
799
- */
800
- pm_node_flags_t integer_base;
801
-
802
- /**
803
- * This string is used to pass information from the lexer to the parser. It
804
- * is particularly necessary because of escape sequences.
805
- */
806
- pm_string_t current_string;
807
-
808
- /**
809
- * The line number at the start of the parse. This will be used to offset
810
- * the line numbers of all of the locations.
811
- */
812
- int32_t start_line;
813
-
814
- /**
815
- * When a string-like expression is being lexed, any byte or escape sequence
816
- * that resolves to a value whose top bit is set (i.e., >= 0x80) will
817
- * explicitly set the encoding to the same encoding as the source.
818
- * Alternatively, if a unicode escape sequence is used (e.g., \\u{80}) that
819
- * resolves to a value whose top bit is set, then the encoding will be
820
- * explicitly set to UTF-8.
821
- *
822
- * The _next_ time this happens, if the encoding that is about to become the
823
- * explicitly set encoding does not match the previously set explicit
824
- * encoding, a mixed encoding error will be emitted.
825
- *
826
- * When the expression is finished being lexed, the explicit encoding
827
- * controls the encoding of the expression. For the most part this means
828
- * that the expression will either be encoded in the source encoding or
829
- * UTF-8. This holds for all encodings except US-ASCII. If the source is
830
- * US-ASCII and an explicit encoding was set that was _not_ UTF-8, then the
831
- * expression will be encoded as ASCII-8BIT.
832
- *
833
- * Note that if the expression is a list, different elements within the same
834
- * list can have different encodings, so this will get reset between each
835
- * element. Furthermore all of this only applies to lists that support
836
- * interpolation, because otherwise escapes that could change the encoding
837
- * are ignored.
838
- *
839
- * At first glance, it may make more sense for this to live on the lexer
840
- * mode, but we need it here to communicate back to the parser for character
841
- * literals that do not push a new lexer mode.
842
- */
843
- const pm_encoding_t *explicit_encoding;
844
-
845
- /**
846
- * When parsing block exits (e.g., break, next, redo), we need to validate
847
- * that they are in correct contexts. For the most part we can do this by
848
- * looking at our parent contexts. However, modifier while and until
849
- * expressions can change that context to make block exits valid. In these
850
- * cases, we need to keep track of the block exits and then validate them
851
- * after the expression has been parsed.
852
- *
853
- * We use a pointer here because we don't want to keep a whole list attached
854
- * since this will only be used in the context of begin/end expressions.
855
- */
856
- pm_node_list_t *current_block_exits;
857
-
858
- /** The version of prism that we should use to parse. */
859
- pm_options_version_t version;
860
-
861
- /** The command line flags given from the options. */
862
- uint8_t command_line;
863
-
864
- /**
865
- * Whether or not we have found a frozen_string_literal magic comment with
866
- * a true or false value.
867
- * May be:
868
- * - PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED
869
- * - PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED
870
- * - PM_OPTIONS_FROZEN_STRING_LITERAL_UNSET
871
- */
872
- int8_t frozen_string_literal;
873
-
874
- /**
875
- * Whether or not we are parsing an eval string. This impacts whether or not
876
- * we should evaluate if block exits/yields are valid.
877
- */
878
- bool parsing_eval;
879
-
880
- /**
881
- * Whether or not we are parsing a "partial" script, which is a script that
882
- * will be evaluated in the context of another script, so we should not
883
- * check jumps (next/break/etc.) for validity.
884
- */
885
- bool partial_script;
886
-
887
- /** Whether or not we're at the beginning of a command. */
888
- bool command_start;
889
-
890
- /** Whether or not we're currently recovering from a syntax error. */
891
- bool recovering;
892
-
893
- /**
894
- * This is very specialized behavior for when you want to parse in a context
895
- * that does not respect encoding comments. Its main use case is translating
896
- * into the whitequark/parser AST which re-encodes source files in UTF-8
897
- * before they are parsed and ignores encoding comments.
898
- */
899
- bool encoding_locked;
900
-
901
- /**
902
- * Whether or not the encoding has been changed by a magic comment. We use
903
- * this to provide a fast path for the lexer instead of going through the
904
- * function pointer.
905
- */
906
- bool encoding_changed;
907
-
908
- /**
909
- * This flag indicates that we are currently parsing a pattern matching
910
- * expression and impacts that calculation of newlines.
911
- */
912
- bool pattern_matching_newlines;
913
-
914
- /** This flag indicates that we are currently parsing a keyword argument. */
915
- bool in_keyword_arg;
916
-
917
- /**
918
- * Whether or not the parser has seen a token that has semantic meaning
919
- * (i.e., a token that is not a comment or whitespace).
920
- */
921
- bool semantic_token_seen;
922
-
923
- /**
924
- * True if the current regular expression being lexed contains only ASCII
925
- * characters.
926
- */
927
- bool current_regular_expression_ascii_only;
928
-
929
- /**
930
- * By default, Ruby always warns about mismatched indentation. This can be
931
- * toggled with a magic comment.
932
- */
933
- bool warn_mismatched_indentation;
934
- };
422
+ * Initiate the parser with the given parser.
423
+ *
424
+ * @param parser The parser to use.
425
+ * @returns The AST representing the source.
426
+ */
427
+ PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser) PRISM_NONNULL(1);
428
+
429
+ #ifdef __cplusplus
430
+ }
431
+ #endif
935
432
 
936
433
  #endif