herb 0.7.4-aarch64-linux-gnu → 0.8.0-aarch64-linux-gnu

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 (176) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +8 -5
  3. data/config.yml +40 -20
  4. data/ext/herb/error_helpers.c +57 -3
  5. data/ext/herb/error_helpers.h +1 -1
  6. data/ext/herb/extconf.rb +1 -0
  7. data/ext/herb/extension.c +10 -24
  8. data/ext/herb/extension_helpers.c +12 -18
  9. data/ext/herb/extension_helpers.h +4 -4
  10. data/ext/herb/nodes.c +72 -37
  11. data/herb.gemspec +0 -2
  12. data/lib/herb/3.0/herb.so +0 -0
  13. data/lib/herb/3.1/herb.so +0 -0
  14. data/lib/herb/3.2/herb.so +0 -0
  15. data/lib/herb/3.3/herb.so +0 -0
  16. data/lib/herb/3.4/herb.so +0 -0
  17. data/lib/herb/ast/helpers.rb +11 -0
  18. data/lib/herb/ast/node.rb +15 -6
  19. data/lib/herb/ast/nodes.rb +609 -392
  20. data/lib/herb/cli.rb +31 -0
  21. data/lib/herb/colors.rb +82 -0
  22. data/lib/herb/engine/compiler.rb +140 -14
  23. data/lib/herb/engine/debug_visitor.rb +1 -5
  24. data/lib/herb/engine/parser_error_overlay.rb +1 -1
  25. data/lib/herb/engine.rb +18 -20
  26. data/lib/herb/errors.rb +166 -56
  27. data/lib/herb/location.rb +2 -2
  28. data/lib/herb/project.rb +86 -21
  29. data/lib/herb/token.rb +14 -2
  30. data/lib/herb/version.rb +1 -1
  31. data/lib/herb.rb +1 -0
  32. data/sig/herb/ast/helpers.rbs +3 -0
  33. data/sig/herb/ast/node.rbs +12 -5
  34. data/sig/herb/ast/nodes.rbs +124 -62
  35. data/sig/herb/colors.rbs +35 -0
  36. data/sig/herb/engine/compiler.rbs +23 -1
  37. data/sig/herb/errors.rbs +74 -20
  38. data/sig/herb/token.rbs +8 -0
  39. data/sig/herb_c_extension.rbs +1 -1
  40. data/sig/serialized_ast_errors.rbs +8 -0
  41. data/src/analyze.c +461 -249
  42. data/src/analyze_helpers.c +5 -0
  43. data/src/analyze_missing_end.c +147 -0
  44. data/src/analyze_transform.c +196 -0
  45. data/src/analyzed_ruby.c +23 -2
  46. data/src/ast_node.c +14 -17
  47. data/src/ast_nodes.c +179 -181
  48. data/src/ast_pretty_print.c +232 -232
  49. data/src/element_source.c +7 -6
  50. data/src/errors.c +272 -152
  51. data/src/extract.c +92 -34
  52. data/src/herb.c +37 -49
  53. data/src/html_util.c +34 -96
  54. data/src/include/analyze.h +10 -2
  55. data/src/include/analyze_helpers.h +3 -0
  56. data/src/include/analyzed_ruby.h +4 -2
  57. data/src/include/ast_node.h +4 -4
  58. data/src/include/ast_nodes.h +68 -67
  59. data/src/include/ast_pretty_print.h +2 -2
  60. data/src/include/element_source.h +3 -1
  61. data/src/include/errors.h +42 -26
  62. data/src/include/extract.h +4 -4
  63. data/src/include/herb.h +6 -7
  64. data/src/include/html_util.h +4 -5
  65. data/src/include/lexer.h +1 -3
  66. data/src/include/lexer_peek_helpers.h +21 -19
  67. data/src/include/lexer_struct.h +12 -10
  68. data/src/include/location.h +10 -13
  69. data/src/include/macros.h +4 -0
  70. data/src/include/parser.h +12 -6
  71. data/src/include/parser_helpers.h +26 -16
  72. data/src/include/position.h +3 -14
  73. data/src/include/pretty_print.h +38 -28
  74. data/src/include/prism_helpers.h +1 -1
  75. data/src/include/range.h +4 -13
  76. data/src/include/token.h +5 -11
  77. data/src/include/token_struct.h +2 -2
  78. data/src/include/utf8.h +3 -2
  79. data/src/include/util/hb_arena.h +31 -0
  80. data/src/include/util/hb_arena_debug.h +8 -0
  81. data/src/include/util/hb_array.h +33 -0
  82. data/src/include/util/hb_buffer.h +34 -0
  83. data/src/include/util/hb_string.h +29 -0
  84. data/src/include/util/hb_system.h +9 -0
  85. data/src/include/util.h +3 -14
  86. data/src/include/version.h +1 -1
  87. data/src/include/visitor.h +1 -1
  88. data/src/io.c +7 -4
  89. data/src/lexer.c +62 -88
  90. data/src/lexer_peek_helpers.c +42 -38
  91. data/src/location.c +9 -37
  92. data/src/main.c +19 -23
  93. data/src/parser.c +373 -313
  94. data/src/parser_helpers.c +60 -54
  95. data/src/parser_match_tags.c +316 -0
  96. data/src/pretty_print.c +88 -117
  97. data/src/prism_helpers.c +7 -7
  98. data/src/range.c +2 -35
  99. data/src/token.c +36 -87
  100. data/src/utf8.c +4 -4
  101. data/src/util/hb_arena.c +179 -0
  102. data/src/util/hb_arena_debug.c +237 -0
  103. data/src/{array.c → util/hb_array.c} +26 -27
  104. data/src/util/hb_buffer.c +203 -0
  105. data/src/util/hb_string.c +85 -0
  106. data/src/util/hb_system.c +30 -0
  107. data/src/util.c +29 -99
  108. data/src/visitor.c +54 -54
  109. data/templates/ext/herb/error_helpers.c.erb +3 -3
  110. data/templates/ext/herb/error_helpers.h.erb +1 -1
  111. data/templates/ext/herb/nodes.c.erb +11 -6
  112. data/templates/java/error_helpers.c.erb +75 -0
  113. data/templates/java/error_helpers.h.erb +20 -0
  114. data/templates/java/nodes.c.erb +97 -0
  115. data/templates/java/nodes.h.erb +23 -0
  116. data/templates/java/org/herb/ast/Errors.java.erb +121 -0
  117. data/templates/java/org/herb/ast/NodeVisitor.java.erb +14 -0
  118. data/templates/java/org/herb/ast/Nodes.java.erb +220 -0
  119. data/templates/java/org/herb/ast/Visitor.java.erb +56 -0
  120. data/templates/javascript/packages/core/src/visitor.ts.erb +29 -1
  121. data/templates/javascript/packages/node/extension/error_helpers.cpp.erb +8 -8
  122. data/templates/javascript/packages/node/extension/error_helpers.h.erb +1 -1
  123. data/templates/javascript/packages/node/extension/nodes.cpp.erb +9 -9
  124. data/templates/javascript/packages/node/extension/nodes.h.erb +1 -1
  125. data/templates/lib/herb/ast/nodes.rb.erb +28 -16
  126. data/templates/lib/herb/errors.rb.erb +17 -12
  127. data/templates/rust/src/ast/nodes.rs.erb +220 -0
  128. data/templates/rust/src/errors.rs.erb +216 -0
  129. data/templates/rust/src/nodes.rs.erb +374 -0
  130. data/templates/src/analyze_missing_end.c.erb +36 -0
  131. data/templates/src/analyze_transform.c.erb +24 -0
  132. data/templates/src/ast_nodes.c.erb +14 -16
  133. data/templates/src/ast_pretty_print.c.erb +36 -36
  134. data/templates/src/errors.c.erb +36 -38
  135. data/templates/src/include/ast_nodes.h.erb +11 -10
  136. data/templates/src/include/ast_pretty_print.h.erb +2 -2
  137. data/templates/src/include/errors.h.erb +9 -9
  138. data/templates/src/parser_match_tags.c.erb +38 -0
  139. data/templates/src/visitor.c.erb +4 -4
  140. data/templates/template.rb +22 -3
  141. data/templates/wasm/error_helpers.cpp.erb +9 -9
  142. data/templates/wasm/error_helpers.h.erb +1 -1
  143. data/templates/wasm/nodes.cpp.erb +9 -9
  144. data/templates/wasm/nodes.h.erb +1 -1
  145. data/vendor/prism/Rakefile +4 -1
  146. data/vendor/prism/config.yml +2 -1
  147. data/vendor/prism/include/prism/ast.h +31 -1
  148. data/vendor/prism/include/prism/diagnostic.h +1 -0
  149. data/vendor/prism/include/prism/version.h +3 -3
  150. data/vendor/prism/src/diagnostic.c +3 -1
  151. data/vendor/prism/src/prism.c +130 -71
  152. data/vendor/prism/src/util/pm_string.c +6 -8
  153. data/vendor/prism/templates/include/prism/ast.h.erb +2 -0
  154. data/vendor/prism/templates/java/org/prism/Loader.java.erb +2 -2
  155. data/vendor/prism/templates/javascript/src/deserialize.js.erb +2 -2
  156. data/vendor/prism/templates/lib/prism/serialize.rb.erb +2 -2
  157. data/vendor/prism/templates/sig/prism.rbs.erb +4 -0
  158. data/vendor/prism/templates/src/diagnostic.c.erb +1 -0
  159. metadata +34 -21
  160. data/lib/herb/libherb/array.rb +0 -51
  161. data/lib/herb/libherb/ast_node.rb +0 -50
  162. data/lib/herb/libherb/buffer.rb +0 -56
  163. data/lib/herb/libherb/extract_result.rb +0 -20
  164. data/lib/herb/libherb/lex_result.rb +0 -32
  165. data/lib/herb/libherb/libherb.rb +0 -52
  166. data/lib/herb/libherb/parse_result.rb +0 -20
  167. data/lib/herb/libherb/token.rb +0 -46
  168. data/lib/herb/libherb.rb +0 -35
  169. data/src/buffer.c +0 -232
  170. data/src/include/array.h +0 -33
  171. data/src/include/buffer.h +0 -39
  172. data/src/include/json.h +0 -28
  173. data/src/include/memory.h +0 -12
  174. data/src/json.c +0 -205
  175. data/src/memory.c +0 -53
  176. data/src/position.c +0 -33
data/src/io.c CHANGED
@@ -1,5 +1,5 @@
1
1
  #include "include/io.h"
2
- #include "include/buffer.h"
2
+ #include "include/util/hb_buffer.h"
3
3
 
4
4
  #include <errno.h>
5
5
  #include <stdio.h>
@@ -8,6 +8,8 @@
8
8
  #define FILE_READ_CHUNK 4096
9
9
 
10
10
  char* herb_read_file(const char* filename) {
11
+ if (!filename) { return NULL; }
12
+
11
13
  FILE* fp = fopen(filename, "rb");
12
14
 
13
15
  if (fp == NULL) {
@@ -15,16 +17,17 @@ char* herb_read_file(const char* filename) {
15
17
  exit(1);
16
18
  }
17
19
 
18
- buffer_T buffer = buffer_new();
20
+ hb_buffer_T buffer;
21
+ hb_buffer_init(&buffer, 4096);
19
22
 
20
23
  char chunk[FILE_READ_CHUNK];
21
24
  size_t bytes_read;
22
25
 
23
26
  while ((bytes_read = fread(chunk, 1, FILE_READ_CHUNK, fp)) > 0) {
24
- buffer_append_with_length(&buffer, chunk, bytes_read);
27
+ hb_buffer_append_with_length(&buffer, chunk, bytes_read);
25
28
  }
26
29
 
27
30
  fclose(fp);
28
31
 
29
- return buffer_value(&buffer);
32
+ return hb_buffer_value(&buffer);
30
33
  }
data/src/lexer.c CHANGED
@@ -1,24 +1,21 @@
1
- #include "include/buffer.h"
2
1
  #include "include/lexer_peek_helpers.h"
3
2
  #include "include/token.h"
4
3
  #include "include/utf8.h"
5
4
  #include "include/util.h"
5
+ #include "include/util/hb_buffer.h"
6
+ #include "include/util/hb_string.h"
6
7
 
7
8
  #include <ctype.h>
8
9
  #include <string.h>
9
10
 
10
11
  #define LEXER_STALL_LIMIT 5
11
12
 
12
- static size_t lexer_sizeof(void) {
13
- return sizeof(struct LEXER_STRUCT);
14
- }
15
-
16
13
  static bool lexer_eof(const lexer_T* lexer) {
17
14
  return lexer->current_character == '\0' || lexer->stalled;
18
15
  }
19
16
 
20
17
  static bool lexer_has_more_characters(const lexer_T* lexer) {
21
- return lexer->current_position < lexer->source_length;
18
+ return lexer->current_position < lexer->source.length;
22
19
  }
23
20
 
24
21
  static bool lexer_stalled(lexer_T* lexer) {
@@ -34,17 +31,16 @@ static bool lexer_stalled(lexer_T* lexer) {
34
31
  return lexer->stalled;
35
32
  }
36
33
 
37
- lexer_T* lexer_init(const char* source) {
38
- if (source == NULL) { source = ""; }
39
-
40
- lexer_T* lexer = calloc(1, lexer_sizeof());
34
+ void lexer_init(lexer_T* lexer, const char* source) {
35
+ if (source != NULL) {
36
+ lexer->source = hb_string(source);
37
+ } else {
38
+ lexer->source = hb_string("");
39
+ }
41
40
 
41
+ lexer->current_character = lexer->source.data[0];
42
42
  lexer->state = STATE_DATA;
43
43
 
44
- lexer->source = source;
45
- lexer->source_length = strlen(source);
46
- lexer->current_character = source[0];
47
-
48
44
  lexer->current_line = 1;
49
45
  lexer->current_column = 0;
50
46
  lexer->current_position = 0;
@@ -56,8 +52,6 @@ lexer_T* lexer_init(const char* source) {
56
52
  lexer->stall_counter = 0;
57
53
  lexer->last_position = 0;
58
54
  lexer->stalled = false;
59
-
60
- return lexer;
61
55
  }
62
56
 
63
57
  token_T* lexer_error(lexer_T* lexer, const char* message) {
@@ -66,14 +60,14 @@ token_T* lexer_error(lexer_T* lexer, const char* message) {
66
60
  snprintf(
67
61
  error_message,
68
62
  sizeof(error_message),
69
- "[Lexer] Error: %s (character '%c', line %zu, col %zu)\n",
63
+ "[Lexer] Error: %s (character '%c', line %u, col %u)\n",
70
64
  message,
71
65
  lexer->current_character,
72
66
  lexer->current_line,
73
67
  lexer->current_column
74
68
  );
75
69
 
76
- return token_init(error_message, TOKEN_ERROR, lexer);
70
+ return token_init(hb_string(error_message), TOKEN_ERROR, lexer);
77
71
  }
78
72
 
79
73
  static void lexer_advance(lexer_T* lexer) {
@@ -81,7 +75,7 @@ static void lexer_advance(lexer_T* lexer) {
81
75
  if (!is_newline(lexer->current_character)) { lexer->current_column++; }
82
76
 
83
77
  lexer->current_position++;
84
- lexer->current_character = lexer->source[lexer->current_position];
78
+ lexer->current_character = lexer->source.data[lexer->current_position];
85
79
  }
86
80
  }
87
81
 
@@ -93,11 +87,11 @@ static void lexer_advance_utf8_bytes(lexer_T* lexer, int byte_count) {
93
87
 
94
88
  lexer->current_position += byte_count;
95
89
 
96
- if (lexer->current_position >= lexer->source_length) {
97
- lexer->current_position = lexer->source_length;
90
+ if (lexer->current_position >= lexer->source.length) {
91
+ lexer->current_position = lexer->source.length;
98
92
  lexer->current_character = '\0';
99
93
  } else {
100
- lexer->current_character = lexer->source[lexer->current_position];
94
+ lexer->current_character = lexer->source.data[lexer->current_position];
101
95
  }
102
96
  }
103
97
  }
@@ -108,65 +102,50 @@ static void lexer_advance_by(lexer_T* lexer, const size_t count) {
108
102
  }
109
103
  }
110
104
 
111
- static token_T* lexer_advance_with(lexer_T* lexer, const char* value, const token_type_T type) {
112
- lexer_advance_by(lexer, strlen(value));
105
+ static token_T* lexer_advance_with(lexer_T* lexer, hb_string_T value, const token_type_T type) {
106
+ lexer_advance_by(lexer, value.length);
113
107
  return token_init(value, type, lexer);
114
108
  }
115
109
 
116
110
  static token_T* lexer_advance_with_next(lexer_T* lexer, size_t count, token_type_T type) {
117
- char* collected = malloc(count + 1);
118
- if (!collected) { return NULL; }
111
+ uint32_t start_position = lexer->current_position;
119
112
 
120
113
  for (size_t i = 0; i < count; i++) {
121
- collected[i] = lexer->current_character;
122
114
  lexer_advance(lexer);
123
115
  }
124
116
 
125
- collected[count] = '\0';
126
-
127
- token_T* token = token_init(collected, type, lexer);
128
- free(collected);
117
+ token_T* token = token_init(hb_string_range(lexer->source, start_position, lexer->current_position), type, lexer);
129
118
 
130
119
  return token;
131
120
  }
132
121
 
133
122
  static token_T* lexer_advance_current(lexer_T* lexer, const token_type_T type) {
134
- return lexer_advance_with(lexer, (char[]) { lexer->current_character, '\0' }, type);
123
+ char buffer[2];
124
+ buffer[0] = lexer->current_character;
125
+ buffer[1] = '\0';
126
+
127
+ return lexer_advance_with(lexer, hb_string(buffer), type);
135
128
  }
136
129
 
137
130
  static token_T* lexer_advance_utf8_character(lexer_T* lexer, const token_type_T type) {
138
- int char_byte_length = utf8_sequence_length(lexer->source, lexer->current_position, lexer->source_length);
139
-
131
+ int char_byte_length = utf8_sequence_length(lexer->source.data, lexer->current_position, lexer->source.length);
140
132
  if (char_byte_length <= 1) { return lexer_advance_current(lexer, type); }
141
-
142
- char* utf8_char = malloc(char_byte_length + 1);
143
-
144
- if (!utf8_char) { return lexer_advance_current(lexer, type); }
133
+ uint32_t start_position = lexer->current_position;
145
134
 
146
135
  for (int i = 0; i < char_byte_length; i++) {
147
- if (lexer->current_position + i >= lexer->source_length) {
148
- free(utf8_char);
149
- return lexer_advance_current(lexer, type);
150
- }
151
-
152
- utf8_char[i] = lexer->source[lexer->current_position + i];
136
+ if (lexer->current_position + i >= lexer->source.length) { return lexer_advance_current(lexer, type); }
153
137
  }
154
138
 
155
- utf8_char[char_byte_length] = '\0';
156
-
157
139
  lexer_advance_utf8_bytes(lexer, char_byte_length);
158
140
 
159
- token_T* token = token_init(utf8_char, type, lexer);
160
-
161
- free(utf8_char);
141
+ token_T* token = token_init(hb_string_range(lexer->source, start_position, lexer->current_position), type, lexer);
162
142
 
163
143
  return token;
164
144
  }
165
145
 
166
- static token_T* lexer_match_and_advance(lexer_T* lexer, const char* value, const token_type_T type) {
167
- if (strncmp(lexer->source + lexer->current_position, value, strlen(value)) == 0) {
168
- return lexer_advance_with(lexer, value, type);
169
- }
146
+ static token_T* lexer_match_and_advance(lexer_T* lexer, hb_string_T value, const token_type_T type) {
147
+ hb_string_T remaining_source = hb_string_slice(lexer->source, lexer->current_position);
148
+ if (hb_string_starts_with(remaining_source, value)) { return lexer_advance_with(lexer, value, type); }
170
149
 
171
150
  return NULL;
172
151
  }
@@ -174,35 +153,31 @@ static token_T* lexer_match_and_advance(lexer_T* lexer, const char* value, const
174
153
  // ===== Specialized Parsers
175
154
 
176
155
  static token_T* lexer_parse_whitespace(lexer_T* lexer) {
177
- buffer_T buffer = buffer_new();
156
+ uint32_t start_position = lexer->current_position;
178
157
 
179
158
  while (isspace(lexer->current_character) && lexer->current_character != '\n' && lexer->current_character != '\r'
180
159
  && !lexer_eof(lexer)) {
181
- buffer_append_char(&buffer, lexer->current_character);
182
160
  lexer_advance(lexer);
183
161
  }
184
162
 
185
- token_T* token = token_init(buffer.value, TOKEN_WHITESPACE, lexer);
186
-
187
- buffer_free(&buffer);
163
+ token_T* token =
164
+ token_init(hb_string_range(lexer->source, start_position, lexer->current_position), TOKEN_WHITESPACE, lexer);
188
165
 
189
166
  return token;
190
167
  }
191
168
 
192
169
  static token_T* lexer_parse_identifier(lexer_T* lexer) {
193
- buffer_T buffer = buffer_new();
170
+ uint32_t start_position = lexer->current_position;
194
171
 
195
172
  while ((isalnum(lexer->current_character) || lexer->current_character == '-' || lexer->current_character == '_'
196
173
  || lexer->current_character == ':')
197
174
  && !lexer_peek_for_html_comment_end(lexer, 0) && !lexer_eof(lexer)) {
198
175
 
199
- buffer_append_char(&buffer, lexer->current_character);
200
176
  lexer_advance(lexer);
201
177
  }
202
178
 
203
- token_T* token = token_init(buffer.value, TOKEN_IDENTIFIER, lexer);
204
-
205
- buffer_free(&buffer);
179
+ token_T* token =
180
+ token_init(hb_string_range(lexer->source, start_position, lexer->current_position), TOKEN_IDENTIFIER, lexer);
206
181
 
207
182
  return token;
208
183
  }
@@ -210,7 +185,8 @@ static token_T* lexer_parse_identifier(lexer_T* lexer) {
210
185
  // ===== ERB Parsing
211
186
 
212
187
  static token_T* lexer_parse_erb_open(lexer_T* lexer) {
213
- const char* erb_patterns[] = { "<%==", "<%%=", "<%=", "<%#", "<%-", "<%%", "<%" };
188
+ hb_string_T erb_patterns[] = { hb_string("<%=="), hb_string("<%%="), hb_string("<%="), hb_string("<%#"),
189
+ hb_string("<%-"), hb_string("<%%"), hb_string("<%") };
214
190
 
215
191
  lexer->state = STATE_ERB_CONTENT;
216
192
 
@@ -223,14 +199,18 @@ static token_T* lexer_parse_erb_open(lexer_T* lexer) {
223
199
  }
224
200
 
225
201
  static token_T* lexer_parse_erb_content(lexer_T* lexer) {
226
- buffer_T buffer = buffer_new();
202
+ uint32_t start_position = lexer->current_position;
227
203
 
228
204
  while (!lexer_peek_erb_end(lexer, 0)) {
229
205
  if (lexer_eof(lexer)) {
230
- return token_init(buffer.value, TOKEN_ERROR, lexer); // Handle unexpected EOF
231
- }
206
+ token_T* token = token_init(
207
+ hb_string_range(lexer->source, start_position, lexer->current_position),
208
+ TOKEN_ERROR,
209
+ lexer
210
+ ); // Handle unexpected EOF
232
211
 
233
- buffer_append_char(&buffer, lexer->current_character);
212
+ return token;
213
+ }
234
214
 
235
215
  if (is_newline(lexer->current_character)) {
236
216
  lexer->current_line++;
@@ -240,14 +220,13 @@ static token_T* lexer_parse_erb_content(lexer_T* lexer) {
240
220
  }
241
221
 
242
222
  lexer->current_position++;
243
- lexer->current_character = lexer->source[lexer->current_position];
223
+ lexer->current_character = lexer->source.data[lexer->current_position];
244
224
  }
245
225
 
246
226
  lexer->state = STATE_ERB_CLOSE;
247
227
 
248
- token_T* token = token_init(buffer.value, TOKEN_ERB_CONTENT, lexer);
249
-
250
- buffer_free(&buffer);
228
+ token_T* token =
229
+ token_init(hb_string_range(lexer->source, start_position, lexer->current_position), TOKEN_ERB_CONTENT, lexer);
251
230
 
252
231
  return token;
253
232
  }
@@ -255,16 +234,17 @@ static token_T* lexer_parse_erb_content(lexer_T* lexer) {
255
234
  static token_T* lexer_parse_erb_close(lexer_T* lexer) {
256
235
  lexer->state = STATE_DATA;
257
236
 
258
- if (lexer_peek_erb_percent_close_tag(lexer, 0)) { return lexer_advance_with(lexer, "%%>", TOKEN_ERB_END); }
259
- if (lexer_peek_erb_dash_close_tag(lexer, 0)) { return lexer_advance_with(lexer, "-%>", TOKEN_ERB_END); }
237
+ if (lexer_peek_erb_percent_close_tag(lexer, 0)) { return lexer_advance_with(lexer, hb_string("%%>"), TOKEN_ERB_END); }
238
+ if (lexer_peek_erb_equals_close_tag(lexer, 0)) { return lexer_advance_with(lexer, hb_string("=%>"), TOKEN_ERB_END); }
239
+ if (lexer_peek_erb_dash_close_tag(lexer, 0)) { return lexer_advance_with(lexer, hb_string("-%>"), TOKEN_ERB_END); }
260
240
 
261
- return lexer_advance_with(lexer, "%>", TOKEN_ERB_END);
241
+ return lexer_advance_with(lexer, hb_string("%>"), TOKEN_ERB_END);
262
242
  }
263
243
 
264
244
  // ===== Tokenizing Function
265
245
 
266
246
  token_T* lexer_next_token(lexer_T* lexer) {
267
- if (lexer_eof(lexer)) { return token_init("", TOKEN_EOF, lexer); }
247
+ if (lexer_eof(lexer)) { return token_init(hb_string(""), TOKEN_EOF, lexer); }
268
248
  if (lexer_stalled(lexer)) { return lexer_error(lexer, "Lexer stalled after 5 iterations"); }
269
249
 
270
250
  if (lexer->state == STATE_ERB_CONTENT) { return lexer_parse_erb_content(lexer); }
@@ -301,33 +281,33 @@ token_T* lexer_next_token(lexer_T* lexer) {
301
281
  if (isalnum(lexer_peek(lexer, 1))) { return lexer_advance_current(lexer, TOKEN_HTML_TAG_START); }
302
282
 
303
283
  if (lexer_peek_for_html_comment_start(lexer, 0)) {
304
- return lexer_advance_with(lexer, "<!--", TOKEN_HTML_COMMENT_START);
284
+ return lexer_advance_with(lexer, hb_string("<!--"), TOKEN_HTML_COMMENT_START);
305
285
  }
306
286
 
307
287
  if (lexer_peek_for_close_tag_start(lexer, 0)) {
308
- return lexer_advance_with(lexer, "</", TOKEN_HTML_TAG_START_CLOSE);
288
+ return lexer_advance_with(lexer, hb_string("</"), TOKEN_HTML_TAG_START_CLOSE);
309
289
  }
310
290
 
311
291
  return lexer_advance_current(lexer, TOKEN_LT);
312
292
  }
313
293
 
314
294
  case '/': {
315
- token_T* token = lexer_match_and_advance(lexer, "/>", TOKEN_HTML_TAG_SELF_CLOSE);
295
+ token_T* token = lexer_match_and_advance(lexer, hb_string("/>"), TOKEN_HTML_TAG_SELF_CLOSE);
316
296
  return token ? token : lexer_advance_current(lexer, TOKEN_SLASH);
317
297
  }
318
298
 
319
299
  case '?': {
320
- token_T* token = lexer_match_and_advance(lexer, "?>", TOKEN_XML_DECLARATION_END);
300
+ token_T* token = lexer_match_and_advance(lexer, hb_string("?>"), TOKEN_XML_DECLARATION_END);
321
301
  return token ? token : lexer_advance_current(lexer, TOKEN_CHARACTER);
322
302
  }
323
303
 
324
304
  case '-': {
325
- token_T* token = lexer_match_and_advance(lexer, "-->", TOKEN_HTML_COMMENT_END);
305
+ token_T* token = lexer_match_and_advance(lexer, hb_string("-->"), TOKEN_HTML_COMMENT_END);
326
306
  return token ? token : lexer_advance_current(lexer, TOKEN_DASH);
327
307
  }
328
308
 
329
309
  case ']': {
330
- token_T* token = lexer_match_and_advance(lexer, "]]>", TOKEN_CDATA_END);
310
+ token_T* token = lexer_match_and_advance(lexer, hb_string("]]>"), TOKEN_CDATA_END);
331
311
  return token ? token : lexer_advance_current(lexer, TOKEN_CHARACTER);
332
312
  }
333
313
 
@@ -353,9 +333,3 @@ token_T* lexer_next_token(lexer_T* lexer) {
353
333
  }
354
334
  }
355
335
  }
356
-
357
- void lexer_free(lexer_T* lexer) {
358
- if (lexer == NULL) { return; }
359
-
360
- free(lexer);
361
- }
@@ -3,80 +3,83 @@
3
3
  #include "include/lexer_struct.h"
4
4
  #include "include/macros.h"
5
5
  #include "include/token.h"
6
+ #include "include/util/hb_string.h"
6
7
 
7
8
  #include <ctype.h>
8
9
  #include <stdbool.h>
9
10
 
10
- char lexer_backtrack(const lexer_T* lexer, const int offset) {
11
- return lexer->source[MAX(lexer->current_position - offset, 0)];
11
+ char lexer_backtrack(const lexer_T* lexer, uint32_t offset) {
12
+ return lexer->source.data[MAX(lexer->current_position - offset, 0)];
12
13
  }
13
14
 
14
- char lexer_peek(const lexer_T* lexer, const int offset) {
15
- return lexer->source[MIN(lexer->current_position + offset, lexer->source_length)];
15
+ char lexer_peek(const lexer_T* lexer, uint32_t offset) {
16
+ return lexer->source.data[MIN(lexer->current_position + offset, lexer->source.length)];
16
17
  }
17
18
 
18
- bool lexer_peek_for(const lexer_T* lexer, const int offset, const char* pattern, const bool case_insensitive) {
19
- for (int index = 0; pattern[index]; index++) {
20
- const char character = lexer_peek(lexer, offset + index);
19
+ bool lexer_peek_for(const lexer_T* lexer, uint32_t offset, hb_string_T pattern, const bool case_insensitive) {
20
+ hb_string_T remaining_source = hb_string_slice(lexer->source, lexer->current_position + offset);
21
+ remaining_source.length = MIN(pattern.length, remaining_source.length);
21
22
 
22
- if (case_insensitive) {
23
- if (tolower(character) != tolower(pattern[index])) { return false; }
24
- } else {
25
- if (character != pattern[index]) { return false; }
26
- }
23
+ if (case_insensitive) {
24
+ return hb_string_equals_case_insensitive(remaining_source, pattern);
25
+ } else {
26
+ return hb_string_equals(remaining_source, pattern);
27
27
  }
28
+ }
28
29
 
29
- return true;
30
+ bool lexer_peek_for_doctype(const lexer_T* lexer, uint32_t offset) {
31
+ return lexer_peek_for(lexer, offset, hb_string("<!DOCTYPE"), true);
30
32
  }
31
33
 
32
- bool lexer_peek_for_doctype(const lexer_T* lexer, const int offset) {
33
- return lexer_peek_for(lexer, offset, "<!DOCTYPE", true);
34
+ bool lexer_peek_for_xml_declaration(const lexer_T* lexer, uint32_t offset) {
35
+ return lexer_peek_for(lexer, offset, hb_string("<?xml"), true);
34
36
  }
35
37
 
36
- bool lexer_peek_for_xml_declaration(const lexer_T* lexer, const int offset) {
37
- return lexer_peek_for(lexer, offset, "<?xml", true);
38
+ bool lexer_peek_for_cdata_start(const lexer_T* lexer, uint32_t offset) {
39
+ return lexer_peek_for(lexer, offset, hb_string("<![CDATA["), false);
38
40
  }
39
41
 
40
- bool lexer_peek_for_cdata_start(const lexer_T* lexer, const int offset) {
41
- return lexer_peek_for(lexer, offset, "<![CDATA[", false);
42
+ bool lexer_peek_for_cdata_end(const lexer_T* lexer, uint32_t offset) {
43
+ return lexer_peek_for(lexer, offset, hb_string("]]>"), false);
42
44
  }
43
45
 
44
- bool lexer_peek_for_cdata_end(const lexer_T* lexer, const int offset) {
45
- return lexer_peek_for(lexer, offset, "]]>", false);
46
+ bool lexer_peek_for_html_comment_start(const lexer_T* lexer, uint32_t offset) {
47
+ return lexer_peek_for(lexer, offset, hb_string("<!--"), false);
46
48
  }
47
49
 
48
- bool lexer_peek_for_html_comment_start(const lexer_T* lexer, const int offset) {
49
- return lexer_peek_for(lexer, offset, "<!--", false);
50
+ bool lexer_peek_for_html_comment_end(const lexer_T* lexer, uint32_t offset) {
51
+ return lexer_peek_for(lexer, offset, hb_string("-->"), false);
50
52
  }
51
53
 
52
- bool lexer_peek_for_html_comment_end(const lexer_T* lexer, const int offset) {
53
- return lexer_peek_for(lexer, offset, "-->", false);
54
+ bool lexer_peek_erb_close_tag(const lexer_T* lexer, uint32_t offset) {
55
+ return lexer_peek_for(lexer, offset, hb_string("%>"), false);
54
56
  }
55
57
 
56
- bool lexer_peek_erb_close_tag(const lexer_T* lexer, const int offset) {
57
- return lexer_peek_for(lexer, offset, "%>", false);
58
+ bool lexer_peek_erb_dash_close_tag(const lexer_T* lexer, uint32_t offset) {
59
+ return lexer_peek_for(lexer, offset, hb_string("-%>"), false);
58
60
  }
59
61
 
60
- bool lexer_peek_erb_dash_close_tag(const lexer_T* lexer, const int offset) {
61
- return lexer_peek_for(lexer, offset, "-%>", false);
62
+ bool lexer_peek_erb_percent_close_tag(const lexer_T* lexer, uint32_t offset) {
63
+ return lexer_peek_for(lexer, offset, hb_string("%%>"), false);
62
64
  }
63
65
 
64
- bool lexer_peek_erb_percent_close_tag(const lexer_T* lexer, const int offset) {
65
- return lexer_peek_for(lexer, offset, "%%>", false);
66
+ bool lexer_peek_erb_equals_close_tag(const lexer_T* lexer, uint32_t offset) {
67
+ return lexer_peek_for(lexer, offset, hb_string("=%>"), false);
66
68
  }
67
69
 
68
- bool lexer_peek_erb_end(const lexer_T* lexer, const int offset) {
70
+ bool lexer_peek_erb_end(const lexer_T* lexer, uint32_t offset) {
69
71
  return (
70
72
  lexer_peek_erb_close_tag(lexer, offset) || lexer_peek_erb_dash_close_tag(lexer, offset)
71
- || lexer_peek_erb_percent_close_tag(lexer, offset)
73
+ || lexer_peek_erb_percent_close_tag(lexer, offset) || lexer_peek_erb_equals_close_tag(lexer, offset)
72
74
  );
73
75
  }
74
76
 
75
77
  bool lexer_peek_for_token_type_after_whitespace(lexer_T* lexer, token_type_T token_type) {
76
- size_t saved_position = lexer->current_position;
77
- size_t saved_line = lexer->current_line;
78
- size_t saved_column = lexer->current_column;
78
+ uint32_t saved_position = lexer->current_position;
79
+ uint32_t saved_line = lexer->current_line;
80
+ uint32_t saved_column = lexer->current_column;
79
81
  char saved_character = lexer->current_character;
82
+ lexer_state_T saved_state = lexer->state;
80
83
 
81
84
  token_T* token = lexer_next_token(lexer);
82
85
 
@@ -93,14 +96,15 @@ bool lexer_peek_for_token_type_after_whitespace(lexer_T* lexer, token_type_T tok
93
96
  lexer->current_line = saved_line;
94
97
  lexer->current_column = saved_column;
95
98
  lexer->current_character = saved_character;
99
+ lexer->state = saved_state;
96
100
 
97
101
  return result;
98
102
  }
99
103
 
100
- bool lexer_peek_for_close_tag_start(const lexer_T* lexer, const int offset) {
104
+ bool lexer_peek_for_close_tag_start(const lexer_T* lexer, uint32_t offset) {
101
105
  if (lexer_peek(lexer, offset) != '<' || lexer_peek(lexer, offset + 1) != '/') { return false; }
102
106
 
103
- int pos = offset + 2;
107
+ uint32_t pos = offset + 2;
104
108
 
105
109
  while (lexer_peek(lexer, pos) == ' ' || lexer_peek(lexer, pos) == '\t' || lexer_peek(lexer, pos) == '\n'
106
110
  || lexer_peek(lexer, pos) == '\r') {
data/src/location.c CHANGED
@@ -1,41 +1,13 @@
1
1
  #include "include/location.h"
2
- #include "include/memory.h"
3
2
  #include "include/position.h"
4
3
 
5
- size_t location_sizeof(void) {
6
- return sizeof(location_T);
7
- }
8
-
9
- location_T* location_init(position_T* start, position_T* end) {
10
- location_T* location = safe_malloc(location_sizeof());
11
-
12
- location->start = start;
13
- location->end = end;
14
-
15
- return location;
16
- }
17
-
18
- location_T* location_from(size_t start_line, size_t start_column, size_t end_line, size_t end_column) {
19
- return location_init(position_init(start_line, start_column), position_init(end_line, end_column));
20
- }
21
-
22
- position_T* location_start(location_T* location) {
23
- return location->start;
24
- }
25
-
26
- position_T* location_end(location_T* location) {
27
- return location->end;
28
- }
29
-
30
- location_T* location_copy(location_T* location) {
31
- if (location == NULL) { return NULL; }
32
-
33
- return location_init(position_copy(location->start), position_copy(location->end));
34
- }
35
-
36
- void location_free(location_T* location) {
37
- if (location->start != NULL) { position_free(location->start); }
38
- if (location->end != NULL) { position_free(location->end); }
39
-
40
- free(location);
4
+ void location_from(
5
+ location_T* location,
6
+ uint32_t start_line,
7
+ uint32_t start_column,
8
+ uint32_t end_line,
9
+ uint32_t end_column
10
+ ) {
11
+ location->start = (position_T) { .line = start_line, .column = start_column };
12
+ location->end = (position_T) { .line = end_line, .column = end_column };
41
13
  }
data/src/main.c CHANGED
@@ -4,11 +4,11 @@
4
4
  #include "include/ast_node.h"
5
5
  #include "include/ast_nodes.h"
6
6
  #include "include/ast_pretty_print.h"
7
- #include "include/buffer.h"
8
7
  #include "include/extract.h"
9
8
  #include "include/herb.h"
10
9
  #include "include/io.h"
11
10
  #include "include/ruby_parser.h"
11
+ #include "include/util/hb_buffer.h"
12
12
 
13
13
  #include <stdio.h>
14
14
  #include <string.h>
@@ -39,7 +39,6 @@ int main(const int argc, char* argv[]) {
39
39
  printf("Herb 🌿 Powerful and seamless HTML-aware ERB parsing and tooling.\n\n");
40
40
 
41
41
  printf("./herb lex [file] - Lex a file\n");
42
- printf("./herb lex_json [file] - Lex a file and return the result as json.\n");
43
42
  printf("./herb parse [file] - Parse a file\n");
44
43
  printf("./herb ruby [file] - Extract Ruby from a file\n");
45
44
  printf("./herb html [file] - Extract HTML from a file\n");
@@ -53,9 +52,9 @@ int main(const int argc, char* argv[]) {
53
52
  return 1;
54
53
  }
55
54
 
56
- buffer_T output;
55
+ hb_buffer_T output;
57
56
 
58
- if (!buffer_init(&output)) { return 1; }
57
+ if (!hb_buffer_init(&output, 4096)) { return 1; }
59
58
 
60
59
  char* source = herb_read_file(argv[2]);
61
60
 
@@ -74,7 +73,7 @@ int main(const int argc, char* argv[]) {
74
73
  print_time_diff(start, end, "visiting");
75
74
 
76
75
  ast_node_free((AST_NODE_T*) root);
77
- buffer_free(&output);
76
+ free(output.value);
78
77
  free(source);
79
78
 
80
79
  return 0;
@@ -87,18 +86,7 @@ int main(const int argc, char* argv[]) {
87
86
  printf("%s\n", output.value);
88
87
  print_time_diff(start, end, "lexing");
89
88
 
90
- buffer_free(&output);
91
- free(source);
92
-
93
- return 0;
94
- }
95
-
96
- if (strcmp(argv[1], "lex_json") == 0) {
97
- herb_lex_json_to_buffer(source, &output);
98
-
99
- printf("%s\n", output.value);
100
-
101
- buffer_free(&output);
89
+ free(output.value);
102
90
  free(source);
103
91
 
104
92
  return 0;
@@ -106,15 +94,23 @@ int main(const int argc, char* argv[]) {
106
94
 
107
95
  if (strcmp(argv[1], "parse") == 0) {
108
96
  AST_DOCUMENT_NODE_T* root = herb_parse(source, NULL);
97
+
98
+ herb_analyze_parse_tree(root, source);
99
+
109
100
  clock_gettime(CLOCK_MONOTONIC, &end);
110
101
 
111
- ast_pretty_print_node((AST_NODE_T*) root, 0, 0, &output);
112
- printf("%s\n", output.value);
102
+ int silent = 0;
103
+ if (argc > 3 && strcmp(argv[3], "--silent") == 0) { silent = 1; }
104
+
105
+ if (!silent) {
106
+ ast_pretty_print_node((AST_NODE_T*) root, 0, 0, &output);
107
+ printf("%s\n", output.value);
113
108
 
114
- print_time_diff(start, end, "parsing");
109
+ print_time_diff(start, end, "parsing");
110
+ }
115
111
 
116
112
  ast_node_free((AST_NODE_T*) root);
117
- buffer_free(&output);
113
+ free(output.value);
118
114
  free(source);
119
115
 
120
116
  return 0;
@@ -127,7 +123,7 @@ int main(const int argc, char* argv[]) {
127
123
  printf("%s\n", output.value);
128
124
  print_time_diff(start, end, "extracting Ruby");
129
125
 
130
- buffer_free(&output);
126
+ free(output.value);
131
127
  free(source);
132
128
 
133
129
  return 0;
@@ -140,7 +136,7 @@ int main(const int argc, char* argv[]) {
140
136
  printf("%s\n", output.value);
141
137
  print_time_diff(start, end, "extracting HTML");
142
138
 
143
- buffer_free(&output);
139
+ free(output.value);
144
140
  free(source);
145
141
 
146
142
  return 0;