herb 0.11.1 → 0.11.3

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 (275) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +2 -0
  3. data/Rakefile +4 -2
  4. data/config.yml +13 -8
  5. data/ext/herb/error_helpers.c +3 -3
  6. data/ext/herb/extconf.rb +8 -31
  7. data/ext/herb/nodes.c +75 -75
  8. data/ext/herb/parser_options_helpers.c +26 -0
  9. data/lib/herb/analysis/partial_index.rb +1 -1
  10. data/lib/herb/analysis/partial_resolution.rb +27 -0
  11. data/lib/herb/ast/erb_render_node.rb +6 -2
  12. data/lib/herb/ast/helpers.rb +1 -1
  13. data/lib/herb/cli.rb +7 -3
  14. data/lib/herb/configuration.rb +11 -1
  15. data/lib/herb/dev/classifier.rb +1 -1
  16. data/lib/herb/dev/runner.rb +5 -2
  17. data/lib/herb/engine/compiler.rb +44 -12
  18. data/lib/herb/engine/slots/dynamics_compiler.rb +54 -15
  19. data/lib/herb/engine/slots/state_compiler.rb +9 -1
  20. data/lib/herb/engine/slots/state_overrides.rb +17 -3
  21. data/lib/herb/engine/slots/subtree_compiler.rb +1 -1
  22. data/lib/herb/engine/slots/visitor.rb +69 -11
  23. data/lib/herb/engine/visitors/instrumentation_visitor.rb +18 -6
  24. data/lib/herb/engine.rb +11 -1
  25. data/lib/herb/errors.rb +7 -7
  26. data/lib/herb/parser_options.rb +6 -3
  27. data/lib/herb/project.rb +19 -8
  28. data/lib/herb/version.rb +1 -1
  29. data/lib/herb.rb +1 -1
  30. data/sig/herb/analysis/partial_resolution.rbs +8 -0
  31. data/sig/herb/ast/erb_render_node.rbs +2 -0
  32. data/sig/herb/configuration.rbs +3 -0
  33. data/sig/herb/dev/runner.rbs +2 -2
  34. data/sig/herb/engine/compiler.rbs +9 -0
  35. data/sig/herb/engine/slots/dynamics_compiler.rbs +24 -7
  36. data/sig/herb/engine/slots/state_compiler.rbs +3 -0
  37. data/sig/herb/engine/slots/state_overrides.rbs +11 -2
  38. data/sig/herb/engine/slots/visitor.rbs +21 -0
  39. data/sig/herb/engine/visitors/instrumentation_visitor.rbs +5 -1
  40. data/sig/herb/engine.rbs +2 -0
  41. data/sig/herb/errors.rbs +2 -2
  42. data/sig/herb/parser_options.rbs +6 -4
  43. data/sig/herb.rbs +2 -2
  44. data/sig/serialized_ast_errors.rbs +1 -1
  45. data/src/analyze/action_view/attribute_extraction_helpers.c +40 -21
  46. data/src/analyze/action_view/generated_handlers.c +36 -36
  47. data/src/analyze/action_view/image_tag.c +7 -3
  48. data/src/analyze/action_view/javascript_include_tag.c +6 -3
  49. data/src/analyze/action_view/link_to.c +23 -23
  50. data/src/analyze/action_view/registry.c +7 -3
  51. data/src/analyze/action_view/tag_helper_node_builders.c +6 -13
  52. data/src/analyze/action_view/tag_helpers.c +160 -97
  53. data/src/analyze/action_view/turbo_frame_tag.c +5 -4
  54. data/src/analyze/analyze.c +52 -29
  55. data/src/analyze/analyze_helpers.c +161 -188
  56. data/src/analyze/analyzed_ruby.c +8 -7
  57. data/src/analyze/control_type.c +29 -22
  58. data/src/analyze/herb_directives.c +16 -20
  59. data/src/analyze/iteration_nodes.c +69 -24
  60. data/src/analyze/parse_errors.c +32 -22
  61. data/src/analyze/postfix_conditionals.c +23 -13
  62. data/src/analyze/prism_annotate.c +29 -28
  63. data/src/analyze/render_nodes.c +239 -78
  64. data/src/analyze/strict_locals.c +14 -15
  65. data/src/analyze/ternary_conditionals.c +14 -10
  66. data/src/diff/herb_diff_children.c +1 -1
  67. data/src/diff/herb_diff_nodes.c +1 -9
  68. data/src/diff/herb_hash_tree.c +1 -1
  69. data/src/errors.c +17 -22
  70. data/src/extract.c +47 -7
  71. data/src/herb.c +10 -8
  72. data/src/include/analyze/action_view/tag_helper_handler.h +1 -1
  73. data/src/include/analyze/action_view/tag_helper_node_builders.h +1 -0
  74. data/src/include/analyze/action_view/tag_helpers.h +1 -1
  75. data/src/include/analyze/analyze.h +3 -2
  76. data/src/include/analyze/analyzed_ruby.h +4 -2
  77. data/src/include/analyze/helpers.h +6 -6
  78. data/src/include/errors.h +3 -3
  79. data/src/include/herb.h +3 -2
  80. data/src/include/parser/parser_options.h +5 -0
  81. data/src/include/prism/prism_context.h +12 -12
  82. data/src/include/prism/prism_helpers.h +30 -0
  83. data/src/include/prism_version.h +11 -0
  84. data/src/include/version.h +1 -1
  85. data/src/lexer/token.c +2 -2
  86. data/src/parser/dot_notation.c +15 -5
  87. data/src/parser/parser_helpers.c +1 -2
  88. data/src/parser.c +9 -8
  89. data/src/parser_options_defaults.c +1 -0
  90. data/src/prism/prism_helpers.c +84 -50
  91. data/src/prism/ruby_parser.c +20 -20
  92. data/templates/ext/herb/nodes.c.erb +5 -5
  93. data/templates/ext/herb/parser_options_helpers.c.erb +33 -0
  94. data/templates/java/nodes.c.erb +6 -6
  95. data/templates/java/parser_options_helpers.c.erb +41 -2
  96. data/templates/java/parser_options_helpers.h.erb +2 -1
  97. data/templates/javascript/packages/node/extension/nodes.cpp.erb +5 -5
  98. data/templates/javascript/packages/node/extension/parser_options_helpers.cpp.erb +34 -2
  99. data/templates/javascript/packages/node/extension/parser_options_helpers.h.erb +1 -1
  100. data/templates/lib/herb/parser_options.rb.erb +1 -1
  101. data/templates/rust/src/prism/generated_deserialize.rs.erb +90 -44
  102. data/templates/rust/src/prism/generated_prettyprint.rs.erb +18 -27
  103. data/templates/src/analyze/action_view/generated_handlers.c.erb +23 -22
  104. data/templates/src/diff/herb_diff_nodes.c.erb +1 -9
  105. data/templates/src/diff/herb_hash_tree.c.erb +1 -1
  106. data/templates/src/errors.c.erb +2 -2
  107. data/templates/src/include/parser/parser_options.h.erb +10 -0
  108. data/templates/src/include/prism_version.h.erb +17 -0
  109. data/templates/src/parser_options_defaults.c.erb +1 -1
  110. data/templates/template.rb +17 -2
  111. data/templates/wasm/nodes.cpp.erb +5 -5
  112. data/templates/wasm/parser_options_helpers.cpp.erb +24 -0
  113. data/vendor/prism/Rakefile +76 -7
  114. data/vendor/prism/config.yml +174 -161
  115. data/vendor/prism/include/prism/arena.h +45 -0
  116. data/vendor/prism/include/prism/arena.hpp +42 -0
  117. data/vendor/prism/include/prism/ast.h +2738 -611
  118. data/vendor/prism/include/prism/ast.hpp +641 -0
  119. data/vendor/prism/include/prism/buffer.h +60 -0
  120. data/vendor/prism/include/prism/buffer.hpp +55 -0
  121. data/vendor/prism/include/prism/comments.h +72 -0
  122. data/vendor/prism/include/prism/compiler/accel.h +26 -0
  123. data/vendor/prism/include/prism/compiler/align.h +36 -0
  124. data/vendor/prism/include/prism/compiler/assume.h +25 -0
  125. data/vendor/prism/include/prism/compiler/branch.h +26 -0
  126. data/vendor/prism/include/prism/compiler/bswap.h +46 -0
  127. data/vendor/prism/include/prism/compiler/cplusplus.hpp +16 -0
  128. data/vendor/prism/include/prism/compiler/endianness.h +34 -0
  129. data/vendor/prism/include/prism/compiler/exceptions.hpp +17 -0
  130. data/vendor/prism/include/prism/compiler/exported.h +24 -0
  131. data/vendor/prism/include/prism/compiler/fallthrough.h +22 -0
  132. data/vendor/prism/include/prism/compiler/filesystem.h +32 -0
  133. data/vendor/prism/include/prism/compiler/flex_array.h +19 -0
  134. data/vendor/prism/include/prism/compiler/force_inline.h +21 -0
  135. data/vendor/prism/include/prism/compiler/format.h +27 -0
  136. data/vendor/prism/include/prism/compiler/inline.h +17 -0
  137. data/vendor/prism/include/prism/compiler/lifetimebound.hpp +25 -0
  138. data/vendor/prism/include/prism/compiler/nodiscard.h +22 -0
  139. data/vendor/prism/include/prism/compiler/nonnull.h +18 -0
  140. data/vendor/prism/include/prism/compiler/unused.h +18 -0
  141. data/vendor/prism/include/prism/constant_pool.h +106 -0
  142. data/vendor/prism/include/prism/diagnostic.h +55 -402
  143. data/vendor/prism/include/prism/errors_format.h +61 -0
  144. data/vendor/prism/include/prism/excludes.h +29 -0
  145. data/vendor/prism/include/prism/integer.h +41 -0
  146. data/vendor/prism/include/prism/internal/allocator.h +68 -0
  147. data/vendor/prism/include/prism/internal/allocator_debug.h +88 -0
  148. data/vendor/prism/include/prism/internal/arena.h +108 -0
  149. data/vendor/prism/include/prism/internal/bit.h +42 -0
  150. data/vendor/prism/include/prism/internal/buffer.h +94 -0
  151. data/vendor/prism/include/prism/internal/char.h +145 -0
  152. data/vendor/prism/include/prism/internal/comments.h +54 -0
  153. data/vendor/prism/include/prism/internal/constant_pool.h +152 -0
  154. data/vendor/prism/include/prism/internal/diagnostic.h +389 -0
  155. data/vendor/prism/include/prism/{encoding.h → internal/encoding.h} +27 -68
  156. data/vendor/prism/include/prism/internal/integer.h +68 -0
  157. data/vendor/prism/include/prism/internal/isinf.h +16 -0
  158. data/vendor/prism/include/prism/internal/line_offset_list.h +34 -0
  159. data/vendor/prism/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  160. data/vendor/prism/include/prism/internal/magic_comments.h +23 -0
  161. data/vendor/prism/include/prism/internal/memchr.h +15 -0
  162. data/vendor/prism/include/prism/internal/node.h +42 -0
  163. data/vendor/prism/include/prism/internal/options.h +232 -0
  164. data/vendor/prism/include/prism/internal/parser.h +1015 -0
  165. data/vendor/prism/include/prism/internal/regexp.h +26 -0
  166. data/vendor/prism/include/prism/internal/serialize.h +39 -0
  167. data/vendor/prism/include/prism/internal/source.h +72 -0
  168. data/vendor/prism/include/prism/internal/static_literals.h +60 -0
  169. data/vendor/prism/include/prism/internal/stringy.h +30 -0
  170. data/vendor/prism/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  171. data/vendor/prism/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  172. data/vendor/prism/include/prism/internal/tokens.h +11 -0
  173. data/vendor/prism/include/prism/internal/xxh3.h +16 -0
  174. data/vendor/prism/include/prism/json.h +40 -0
  175. data/vendor/prism/include/prism/line_offset_list.h +69 -0
  176. data/vendor/prism/include/prism/magic_comments.h +53 -0
  177. data/vendor/prism/include/prism/node.h +59 -49
  178. data/vendor/prism/include/prism/node.hpp +272 -0
  179. data/vendor/prism/include/prism/node_type.hpp +38 -0
  180. data/vendor/prism/include/prism/options.h +154 -279
  181. data/vendor/prism/include/prism/options.hpp +43 -0
  182. data/vendor/prism/include/prism/parser.h +367 -870
  183. data/vendor/prism/include/prism/parser.hpp +274 -0
  184. data/vendor/prism/include/prism/prettyprint.h +14 -9
  185. data/vendor/prism/include/prism/serialize.h +123 -0
  186. data/vendor/prism/include/prism/source.h +163 -0
  187. data/vendor/prism/include/prism/stream.h +36 -0
  188. data/vendor/prism/include/prism/string_query.h +82 -0
  189. data/vendor/prism/include/prism/stringy.h +61 -0
  190. data/vendor/prism/include/prism/stringy.hpp +31 -0
  191. data/vendor/prism/include/prism/version.h +20 -3
  192. data/vendor/prism/include/prism.h +35 -320
  193. data/vendor/prism/include/prism.hpp +24 -0
  194. data/vendor/prism/src/arena.c +122 -0
  195. data/vendor/prism/src/{util/pm_buffer.c → buffer.c} +53 -21
  196. data/vendor/prism/src/{util/pm_char.c → char.c} +28 -57
  197. data/vendor/prism/src/comments.c +455 -0
  198. data/vendor/prism/src/constant_pool.c +406 -0
  199. data/vendor/prism/src/diagnostic.c +120 -68
  200. data/vendor/prism/src/encoding.c +8 -3
  201. data/vendor/prism/src/errors_format.c +519 -0
  202. data/vendor/prism/src/{util/pm_integer.c → integer.c} +38 -22
  203. data/vendor/prism/src/json.c +5733 -0
  204. data/vendor/prism/src/line_offset_list.c +100 -0
  205. data/vendor/prism/src/list.c +24 -0
  206. data/vendor/prism/src/{util/pm_memchr.c → memchr.c} +8 -6
  207. data/vendor/prism/src/node.c +3465 -7787
  208. data/vendor/prism/src/options.c +161 -54
  209. data/vendor/prism/src/parser.c +371 -0
  210. data/vendor/prism/src/prettyprint.c +421 -354
  211. data/vendor/prism/src/prism.c +8522 -7280
  212. data/vendor/prism/src/regexp.c +960 -45
  213. data/vendor/prism/src/serialize.c +2434 -2048
  214. data/vendor/prism/src/source.c +524 -0
  215. data/vendor/prism/src/static_literals.c +303 -360
  216. data/vendor/prism/src/string_query.c +180 -0
  217. data/vendor/prism/src/stringy.c +61 -0
  218. data/vendor/prism/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  219. data/vendor/prism/src/strpbrk.c +447 -0
  220. data/vendor/prism/src/{token_type.c → tokens.c} +39 -18
  221. data/vendor/prism/src/xxh3.c +292 -0
  222. data/vendor/prism/templates/ext/prism/api_node.c.erb +58 -44
  223. data/vendor/prism/templates/include/prism/ast.h.erb +67 -17
  224. data/vendor/prism/templates/include/prism/ast.hpp.erb +31 -0
  225. data/vendor/prism/templates/include/prism/internal/diagnostic.h.erb +60 -0
  226. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/AbstractNodeVisitor.java.erb +1 -1
  227. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/Loader.java.erb +232 -133
  228. data/vendor/prism/templates/java/{org → api/src/main/java-templates/org/ruby_lang}/prism/Nodes.java.erb +62 -23
  229. data/vendor/prism/templates/javascript/src/deserialize.js.erb +153 -146
  230. data/vendor/prism/templates/javascript/src/nodes.js.erb +4 -9
  231. data/vendor/prism/templates/lib/prism/compiler.rb.erb +14 -5
  232. data/vendor/prism/templates/lib/prism/dispatcher.rb.erb +20 -12
  233. data/vendor/prism/templates/lib/prism/dot_visitor.rb.erb +37 -11
  234. data/vendor/prism/templates/lib/prism/dsl.rb.erb +43 -4
  235. data/vendor/prism/templates/lib/prism/inspect_visitor.rb.erb +26 -10
  236. data/vendor/prism/templates/lib/prism/mutation_compiler.rb.erb +5 -2
  237. data/vendor/prism/templates/lib/prism/node.rb.erb +283 -105
  238. data/vendor/prism/templates/lib/prism/reflection.rb.erb +11 -2
  239. data/vendor/prism/templates/lib/prism/serialize.rb.erb +291 -134
  240. data/vendor/prism/templates/lib/prism/visitor.rb.erb +18 -0
  241. data/vendor/prism/templates/src/diagnostic.c.erb +114 -63
  242. data/vendor/prism/templates/src/json.c.erb +130 -0
  243. data/vendor/prism/templates/src/node.c.erb +79 -236
  244. data/vendor/prism/templates/src/prettyprint.c.erb +24 -13
  245. data/vendor/prism/templates/src/serialize.c.erb +369 -183
  246. data/vendor/prism/templates/src/{token_type.c.erb → tokens.c.erb} +22 -11
  247. data/vendor/prism/templates/template.rb +193 -91
  248. metadata +100 -42
  249. data/vendor/prism/include/prism/defines.h +0 -260
  250. data/vendor/prism/include/prism/pack.h +0 -163
  251. data/vendor/prism/include/prism/regexp.h +0 -43
  252. data/vendor/prism/include/prism/static_literals.h +0 -121
  253. data/vendor/prism/include/prism/util/pm_buffer.h +0 -236
  254. data/vendor/prism/include/prism/util/pm_char.h +0 -204
  255. data/vendor/prism/include/prism/util/pm_constant_pool.h +0 -218
  256. data/vendor/prism/include/prism/util/pm_integer.h +0 -130
  257. data/vendor/prism/include/prism/util/pm_memchr.h +0 -29
  258. data/vendor/prism/include/prism/util/pm_newline_list.h +0 -113
  259. data/vendor/prism/include/prism/util/pm_string.h +0 -200
  260. data/vendor/prism/src/pack.c +0 -509
  261. data/vendor/prism/src/util/pm_constant_pool.c +0 -342
  262. data/vendor/prism/src/util/pm_list.c +0 -49
  263. data/vendor/prism/src/util/pm_newline_list.c +0 -125
  264. data/vendor/prism/src/util/pm_string.c +0 -381
  265. data/vendor/prism/src/util/pm_strpbrk.c +0 -206
  266. data/vendor/prism/templates/include/prism/diagnostic.h.erb +0 -130
  267. data/vendor/prism/templates/rbi/prism/dsl.rbi.erb +0 -68
  268. data/vendor/prism/templates/rbi/prism/node.rbi.erb +0 -167
  269. data/vendor/prism/templates/rbi/prism/visitor.rbi.erb +0 -18
  270. data/vendor/prism/templates/sig/prism/_private/dot_visitor.rbs.erb +0 -45
  271. data/vendor/prism/templates/sig/prism/dsl.rbs.erb +0 -31
  272. data/vendor/prism/templates/sig/prism/mutation_compiler.rbs.erb +0 -7
  273. data/vendor/prism/templates/sig/prism/node.rbs.erb +0 -136
  274. data/vendor/prism/templates/sig/prism/visitor.rbs.erb +0 -17
  275. data/vendor/prism/templates/sig/prism.rbs.erb +0 -92
@@ -0,0 +1,180 @@
1
+ #include "prism/string_query.h"
2
+
3
+ #include "prism/internal/char.h"
4
+ #include "prism/internal/encoding.h"
5
+ #include "prism/internal/parser.h"
6
+
7
+ #include <assert.h>
8
+ #include <string.h>
9
+
10
+ /** The category of slice returned from pm_slice_type. */
11
+ typedef enum {
12
+ /** Returned when the given encoding name is invalid. */
13
+ PM_SLICE_TYPE_ERROR = -1,
14
+
15
+ /** Returned when no other types apply to the slice. */
16
+ PM_SLICE_TYPE_NONE,
17
+
18
+ /** Returned when the slice is a valid local variable name. */
19
+ PM_SLICE_TYPE_LOCAL,
20
+
21
+ /** Returned when the slice is a valid constant name. */
22
+ PM_SLICE_TYPE_CONSTANT,
23
+
24
+ /** Returned when the slice is a valid method name. */
25
+ PM_SLICE_TYPE_METHOD_NAME
26
+ } pm_slice_type_t;
27
+
28
+ /**
29
+ * Check that the slice is a valid local variable name or constant.
30
+ */
31
+ static pm_slice_type_t
32
+ pm_slice_type(const uint8_t *source, size_t length, const char *encoding_name) {
33
+ // first, get the right encoding object
34
+ const pm_encoding_t *encoding = pm_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name)));
35
+ if (encoding == NULL) return PM_SLICE_TYPE_ERROR;
36
+
37
+ // check that there is at least one character
38
+ if (length == 0) return PM_SLICE_TYPE_NONE;
39
+
40
+ size_t width;
41
+ if ((width = encoding->alpha_char(source, (ptrdiff_t) length)) != 0) {
42
+ // valid because alphabetical
43
+ } else if (*source == '_') {
44
+ // valid because underscore
45
+ width = 1;
46
+ } else if ((*source >= 0x80) && ((width = encoding->char_width(source, (ptrdiff_t) length)) > 0)) {
47
+ // valid because multibyte
48
+ } else {
49
+ // invalid because no match
50
+ return PM_SLICE_TYPE_NONE;
51
+ }
52
+
53
+ // determine the type of the slice based on the first character
54
+ const uint8_t *end = source + length;
55
+ pm_slice_type_t result = encoding->isupper_char(source, end - source) ? PM_SLICE_TYPE_CONSTANT : PM_SLICE_TYPE_LOCAL;
56
+
57
+ // next, iterate through all of the bytes of the string to ensure that they
58
+ // are all valid identifier characters
59
+ source += width;
60
+
61
+ while (source < end) {
62
+ if ((width = encoding->alnum_char(source, end - source)) != 0) {
63
+ // valid because alphanumeric
64
+ source += width;
65
+ } else if (*source == '_') {
66
+ // valid because underscore
67
+ source++;
68
+ } else if ((*source >= 0x80) && ((width = encoding->char_width(source, end - source)) > 0)) {
69
+ // valid because multibyte
70
+ source += width;
71
+ } else {
72
+ // invalid because no match
73
+ break;
74
+ }
75
+ }
76
+
77
+ // accept a ! or ? at the end of the slice as a method name
78
+ if (*source == '!' || *source == '?' || *source == '=') {
79
+ source++;
80
+ result = PM_SLICE_TYPE_METHOD_NAME;
81
+ }
82
+
83
+ // valid if we are at the end of the slice
84
+ return source == end ? result : PM_SLICE_TYPE_NONE;
85
+ }
86
+
87
+ /**
88
+ * Check that the slice is a valid local variable name.
89
+ */
90
+ pm_string_query_t
91
+ pm_string_query_local(const uint8_t *source, size_t length, const char *encoding_name) {
92
+ switch (pm_slice_type(source, length, encoding_name)) {
93
+ case PM_SLICE_TYPE_ERROR:
94
+ return PM_STRING_QUERY_ERROR;
95
+ case PM_SLICE_TYPE_NONE:
96
+ case PM_SLICE_TYPE_CONSTANT:
97
+ case PM_SLICE_TYPE_METHOD_NAME:
98
+ return PM_STRING_QUERY_FALSE;
99
+ case PM_SLICE_TYPE_LOCAL:
100
+ return PM_STRING_QUERY_TRUE;
101
+ }
102
+
103
+ assert(false && "unreachable");
104
+ return PM_STRING_QUERY_FALSE;
105
+ }
106
+
107
+ /**
108
+ * Check that the slice is a valid constant name.
109
+ */
110
+ pm_string_query_t
111
+ pm_string_query_constant(const uint8_t *source, size_t length, const char *encoding_name) {
112
+ switch (pm_slice_type(source, length, encoding_name)) {
113
+ case PM_SLICE_TYPE_ERROR:
114
+ return PM_STRING_QUERY_ERROR;
115
+ case PM_SLICE_TYPE_NONE:
116
+ case PM_SLICE_TYPE_LOCAL:
117
+ case PM_SLICE_TYPE_METHOD_NAME:
118
+ return PM_STRING_QUERY_FALSE;
119
+ case PM_SLICE_TYPE_CONSTANT:
120
+ return PM_STRING_QUERY_TRUE;
121
+ }
122
+
123
+ assert(false && "unreachable");
124
+ return PM_STRING_QUERY_FALSE;
125
+ }
126
+
127
+ /**
128
+ * Check that the slice is a valid method name.
129
+ */
130
+ pm_string_query_t
131
+ pm_string_query_method_name(const uint8_t *source, size_t length, const char *encoding_name) {
132
+ #define B(p) ((p) ? PM_STRING_QUERY_TRUE : PM_STRING_QUERY_FALSE)
133
+ #define C1(c) (*source == c)
134
+ #define C2(s) (memcmp(source, s, 2) == 0)
135
+ #define C3(s) (memcmp(source, s, 3) == 0)
136
+
137
+ switch (pm_slice_type(source, length, encoding_name)) {
138
+ case PM_SLICE_TYPE_ERROR:
139
+ return PM_STRING_QUERY_ERROR;
140
+ case PM_SLICE_TYPE_NONE:
141
+ break;
142
+ case PM_SLICE_TYPE_LOCAL:
143
+ // numbered parameters are not valid method names
144
+ return B((length != 2) || (source[0] != '_') || (source[1] == '0') || !pm_char_is_decimal_digit(source[1]));
145
+ case PM_SLICE_TYPE_CONSTANT:
146
+ // all constants are valid method names
147
+ case PM_SLICE_TYPE_METHOD_NAME:
148
+ // all method names are valid method names
149
+ return PM_STRING_QUERY_TRUE;
150
+ }
151
+
152
+ switch (length) {
153
+ case 1:
154
+ return B(C1('&') || C1('`') || C1('!') || C1('^') || C1('>') || C1('<') || C1('-') || C1('%') || C1('|') || C1('+') || C1('/') || C1('*') || C1('~'));
155
+ case 2:
156
+ return B(C2("!=") || C2("!~") || C2("[]") || C2("==") || C2("=~") || C2(">=") || C2(">>") || C2("<=") || C2("<<") || C2("**"));
157
+ case 3:
158
+ return B(C3("===") || C3("<=>") || C3("[]="));
159
+ default:
160
+ return PM_STRING_QUERY_FALSE;
161
+ }
162
+
163
+ #undef B
164
+ #undef C1
165
+ #undef C2
166
+ #undef C3
167
+ }
168
+
169
+ /**
170
+ * Check that the slice is a keyword. Keywords are spelled in ASCII, so any
171
+ * ASCII-compatible encoding spells them with the same bytes.
172
+ */
173
+ pm_string_query_t
174
+ pm_string_query_keyword(const uint8_t *source, size_t length, const char *encoding_name) {
175
+ if (pm_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))) == NULL) {
176
+ return PM_STRING_QUERY_ERROR;
177
+ }
178
+
179
+ return pm_keyword_p(source, source + length) ? PM_STRING_QUERY_TRUE : PM_STRING_QUERY_FALSE;
180
+ }
@@ -0,0 +1,61 @@
1
+ #include "prism/internal/stringy.h"
2
+
3
+ #include <assert.h>
4
+ #include <string.h>
5
+
6
+ /**
7
+ * Initialize a shared string that is based on initial input.
8
+ */
9
+ void
10
+ pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end) {
11
+ assert(start <= end);
12
+
13
+ *string = (pm_string_t) {
14
+ .source = start,
15
+ .length = (size_t) (end - start)
16
+ };
17
+ }
18
+
19
+ /**
20
+ * Initialize a string that views the given bytes.
21
+ */
22
+ void
23
+ pm_string_init(pm_string_t *string, const uint8_t *source, size_t length) {
24
+ *string = (pm_string_t) { .source = source, .length = length };
25
+ }
26
+
27
+ /**
28
+ * Compare the underlying lengths and bytes of two strings. Returns 0 if the
29
+ * strings are equal, a negative number if the left string is less than the
30
+ * right string, and a positive number if the left string is greater than the
31
+ * right string.
32
+ */
33
+ int
34
+ pm_string_compare(const pm_string_t *left, const pm_string_t *right) {
35
+ size_t left_length = pm_string_length(left);
36
+ size_t right_length = pm_string_length(right);
37
+
38
+ if (left_length < right_length) {
39
+ return -1;
40
+ } else if (left_length > right_length) {
41
+ return 1;
42
+ }
43
+
44
+ return memcmp(pm_string_source(left), pm_string_source(right), left_length);
45
+ }
46
+
47
+ /**
48
+ * Returns the length associated with the string.
49
+ */
50
+ size_t
51
+ pm_string_length(const pm_string_t *string) {
52
+ return string->length;
53
+ }
54
+
55
+ /**
56
+ * Returns the start pointer associated with the string.
57
+ */
58
+ const uint8_t *
59
+ pm_string_source(const pm_string_t *string) {
60
+ return string->source;
61
+ }
@@ -1,11 +1,12 @@
1
- #include "prism/util/pm_strncasecmp.h"
1
+ #include "prism/internal/strncasecmp.h"
2
+
3
+ #include "prism/compiler/inline.h"
2
4
 
3
5
  /**
4
6
  * A locale-insensitive version of `tolower(3)`
5
7
  */
6
- static inline int
7
- pm_tolower(int c)
8
- {
8
+ static PRISM_INLINE int
9
+ pm_tolower(int c) {
9
10
  if ('A' <= c && c <= 'Z') {
10
11
  return c | 0x20;
11
12
  }