@herb-tools/node 0.8.10 → 0.9.0

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 (169) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/binding.gyp +26 -8
  3. package/dist/herb-node.cjs +41 -12
  4. package/dist/herb-node.cjs.map +1 -1
  5. package/dist/herb-node.esm.js +8 -1
  6. package/dist/herb-node.esm.js.map +1 -1
  7. package/dist/types/node-backend.d.ts +3 -1
  8. package/extension/error_helpers.cpp +395 -73
  9. package/extension/error_helpers.h +13 -3
  10. package/extension/extension_helpers.cpp +38 -35
  11. package/extension/extension_helpers.h +2 -2
  12. package/extension/herb.cpp +183 -64
  13. package/extension/libherb/analyze/action_view/attribute_extraction_helpers.c +290 -0
  14. package/extension/libherb/analyze/action_view/attribute_extraction_helpers.h +36 -0
  15. package/extension/libherb/analyze/action_view/content_tag.c +70 -0
  16. package/extension/libherb/analyze/action_view/link_to.c +143 -0
  17. package/extension/libherb/analyze/action_view/registry.c +60 -0
  18. package/extension/libherb/analyze/action_view/tag.c +64 -0
  19. package/extension/libherb/analyze/action_view/tag_helper_handler.h +41 -0
  20. package/extension/libherb/analyze/action_view/tag_helper_node_builders.c +305 -0
  21. package/extension/libherb/analyze/action_view/tag_helper_node_builders.h +70 -0
  22. package/extension/libherb/analyze/action_view/tag_helpers.c +748 -0
  23. package/extension/libherb/analyze/action_view/tag_helpers.h +38 -0
  24. package/extension/libherb/analyze/action_view/turbo_frame_tag.c +88 -0
  25. package/extension/libherb/analyze/analyze.c +882 -0
  26. package/extension/libherb/{include → analyze}/analyze.h +14 -4
  27. package/extension/libherb/{analyzed_ruby.c → analyze/analyzed_ruby.c} +13 -11
  28. package/extension/libherb/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
  29. package/extension/libherb/analyze/builders.c +343 -0
  30. package/extension/libherb/analyze/builders.h +27 -0
  31. package/extension/libherb/analyze/conditional_elements.c +594 -0
  32. package/extension/libherb/analyze/conditional_elements.h +9 -0
  33. package/extension/libherb/analyze/conditional_open_tags.c +640 -0
  34. package/extension/libherb/analyze/conditional_open_tags.h +9 -0
  35. package/extension/libherb/analyze/control_type.c +250 -0
  36. package/extension/libherb/analyze/control_type.h +14 -0
  37. package/extension/libherb/{analyze_helpers.c → analyze/helpers.c} +48 -23
  38. package/extension/libherb/{analyze_helpers.h → analyze/helpers.h} +4 -2
  39. package/extension/libherb/analyze/invalid_structures.c +193 -0
  40. package/extension/libherb/analyze/invalid_structures.h +11 -0
  41. package/extension/libherb/{analyze_missing_end.c → analyze/missing_end.c} +33 -22
  42. package/extension/libherb/analyze/parse_errors.c +84 -0
  43. package/extension/libherb/analyze/prism_annotate.c +397 -0
  44. package/extension/libherb/analyze/prism_annotate.h +16 -0
  45. package/extension/libherb/{analyze_transform.c → analyze/transform.c} +17 -3
  46. package/extension/libherb/ast_node.c +17 -7
  47. package/extension/libherb/ast_node.h +11 -5
  48. package/extension/libherb/ast_nodes.c +663 -388
  49. package/extension/libherb/ast_nodes.h +118 -39
  50. package/extension/libherb/ast_pretty_print.c +191 -7
  51. package/extension/libherb/ast_pretty_print.h +6 -1
  52. package/extension/libherb/element_source.h +3 -8
  53. package/extension/libherb/errors.c +1077 -521
  54. package/extension/libherb/errors.h +149 -56
  55. package/extension/libherb/extract.c +145 -49
  56. package/extension/libherb/extract.h +21 -5
  57. package/extension/libherb/herb.c +52 -34
  58. package/extension/libherb/herb.h +18 -6
  59. package/extension/libherb/herb_prism_node.h +13 -0
  60. package/extension/libherb/html_util.c +241 -12
  61. package/extension/libherb/html_util.h +7 -2
  62. package/extension/libherb/include/analyze/action_view/attribute_extraction_helpers.h +36 -0
  63. package/extension/libherb/include/analyze/action_view/tag_helper_handler.h +41 -0
  64. package/extension/libherb/include/analyze/action_view/tag_helper_node_builders.h +70 -0
  65. package/extension/libherb/include/analyze/action_view/tag_helpers.h +38 -0
  66. package/extension/libherb/{analyze.h → include/analyze/analyze.h} +14 -4
  67. package/extension/libherb/include/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
  68. package/extension/libherb/include/analyze/builders.h +27 -0
  69. package/extension/libherb/include/analyze/conditional_elements.h +9 -0
  70. package/extension/libherb/include/analyze/conditional_open_tags.h +9 -0
  71. package/extension/libherb/include/analyze/control_type.h +14 -0
  72. package/extension/libherb/include/{analyze_helpers.h → analyze/helpers.h} +4 -2
  73. package/extension/libherb/include/analyze/invalid_structures.h +11 -0
  74. package/extension/libherb/include/analyze/prism_annotate.h +16 -0
  75. package/extension/libherb/include/ast_node.h +11 -5
  76. package/extension/libherb/include/ast_nodes.h +118 -39
  77. package/extension/libherb/include/ast_pretty_print.h +6 -1
  78. package/extension/libherb/include/element_source.h +3 -8
  79. package/extension/libherb/include/errors.h +149 -56
  80. package/extension/libherb/include/extract.h +21 -5
  81. package/extension/libherb/include/herb.h +18 -6
  82. package/extension/libherb/include/herb_prism_node.h +13 -0
  83. package/extension/libherb/include/html_util.h +7 -2
  84. package/extension/libherb/include/io.h +3 -1
  85. package/extension/libherb/include/lex_helpers.h +29 -0
  86. package/extension/libherb/include/lexer.h +1 -1
  87. package/extension/libherb/include/lexer_peek_helpers.h +87 -13
  88. package/extension/libherb/include/lexer_struct.h +2 -0
  89. package/extension/libherb/include/location.h +2 -1
  90. package/extension/libherb/include/parser.h +27 -2
  91. package/extension/libherb/include/parser_helpers.h +19 -3
  92. package/extension/libherb/include/pretty_print.h +10 -5
  93. package/extension/libherb/include/prism_context.h +45 -0
  94. package/extension/libherb/include/prism_helpers.h +10 -7
  95. package/extension/libherb/include/prism_serialized.h +12 -0
  96. package/extension/libherb/include/token.h +16 -4
  97. package/extension/libherb/include/token_struct.h +10 -3
  98. package/extension/libherb/include/utf8.h +2 -1
  99. package/extension/libherb/include/util/hb_allocator.h +78 -0
  100. package/extension/libherb/include/util/hb_arena.h +6 -1
  101. package/extension/libherb/include/util/hb_arena_debug.h +12 -1
  102. package/extension/libherb/include/util/hb_array.h +7 -3
  103. package/extension/libherb/include/util/hb_buffer.h +6 -4
  104. package/extension/libherb/include/util/hb_foreach.h +79 -0
  105. package/extension/libherb/include/util/hb_narray.h +8 -4
  106. package/extension/libherb/include/util/hb_string.h +56 -9
  107. package/extension/libherb/include/util.h +6 -3
  108. package/extension/libherb/include/version.h +1 -1
  109. package/extension/libherb/io.c +3 -2
  110. package/extension/libherb/io.h +3 -1
  111. package/extension/libherb/lex_helpers.h +29 -0
  112. package/extension/libherb/lexer.c +42 -30
  113. package/extension/libherb/lexer.h +1 -1
  114. package/extension/libherb/lexer_peek_helpers.c +12 -74
  115. package/extension/libherb/lexer_peek_helpers.h +87 -13
  116. package/extension/libherb/lexer_struct.h +2 -0
  117. package/extension/libherb/location.c +2 -2
  118. package/extension/libherb/location.h +2 -1
  119. package/extension/libherb/main.c +53 -28
  120. package/extension/libherb/parser.c +783 -247
  121. package/extension/libherb/parser.h +27 -2
  122. package/extension/libherb/parser_helpers.c +110 -23
  123. package/extension/libherb/parser_helpers.h +19 -3
  124. package/extension/libherb/parser_match_tags.c +110 -49
  125. package/extension/libherb/pretty_print.c +29 -24
  126. package/extension/libherb/pretty_print.h +10 -5
  127. package/extension/libherb/prism_context.h +45 -0
  128. package/extension/libherb/prism_helpers.c +30 -27
  129. package/extension/libherb/prism_helpers.h +10 -7
  130. package/extension/libherb/prism_serialized.h +12 -0
  131. package/extension/libherb/ruby_parser.c +2 -0
  132. package/extension/libherb/token.c +151 -66
  133. package/extension/libherb/token.h +16 -4
  134. package/extension/libherb/token_matchers.c +0 -1
  135. package/extension/libherb/token_struct.h +10 -3
  136. package/extension/libherb/utf8.c +7 -6
  137. package/extension/libherb/utf8.h +2 -1
  138. package/extension/libherb/util/hb_allocator.c +341 -0
  139. package/extension/libherb/util/hb_allocator.h +78 -0
  140. package/extension/libherb/util/hb_arena.c +81 -56
  141. package/extension/libherb/util/hb_arena.h +6 -1
  142. package/extension/libherb/util/hb_arena_debug.c +32 -17
  143. package/extension/libherb/util/hb_arena_debug.h +12 -1
  144. package/extension/libherb/util/hb_array.c +30 -15
  145. package/extension/libherb/util/hb_array.h +7 -3
  146. package/extension/libherb/util/hb_buffer.c +17 -21
  147. package/extension/libherb/util/hb_buffer.h +6 -4
  148. package/extension/libherb/util/hb_foreach.h +79 -0
  149. package/extension/libherb/util/hb_narray.c +22 -7
  150. package/extension/libherb/util/hb_narray.h +8 -4
  151. package/extension/libherb/util/hb_string.c +49 -35
  152. package/extension/libherb/util/hb_string.h +56 -9
  153. package/extension/libherb/util.c +21 -11
  154. package/extension/libherb/util.h +6 -3
  155. package/extension/libherb/version.h +1 -1
  156. package/extension/libherb/visitor.c +48 -1
  157. package/extension/nodes.cpp +451 -6
  158. package/extension/nodes.h +8 -1
  159. package/package.json +12 -8
  160. package/src/node-backend.ts +11 -1
  161. package/dist/types/index-cjs.d.cts +0 -1
  162. package/extension/libherb/analyze.c +0 -1608
  163. package/extension/libherb/element_source.c +0 -12
  164. package/extension/libherb/include/util/hb_system.h +0 -9
  165. package/extension/libherb/util/hb_system.c +0 -30
  166. package/extension/libherb/util/hb_system.h +0 -9
  167. package/src/index-cjs.cts +0 -22
  168. /package/dist/types/{index-esm.d.mts → index.d.ts} +0 -0
  169. /package/src/{index-esm.mts → index.ts} +0 -0
@@ -1,13 +1,21 @@
1
1
  #define _POSIX_C_SOURCE 199309L // Enables `clock_gettime()`
2
2
 
3
- #include "include/analyze.h"
4
3
  #include "include/ast_node.h"
5
4
  #include "include/ast_nodes.h"
6
- #include "include/ast_pretty_print.h"
5
+
6
+ #ifndef HERB_EXCLUDE_PRETTYPRINT
7
+ # include "include/ast_pretty_print.h"
8
+ #endif
9
+
7
10
  #include "include/extract.h"
8
11
  #include "include/herb.h"
9
12
  #include "include/io.h"
13
+ #include "include/lex_helpers.h"
14
+ #include "include/macros.h"
10
15
  #include "include/ruby_parser.h"
16
+ #include "include/util/hb_allocator.h"
17
+ #include "include/util/hb_arena.h"
18
+ #include "include/util/hb_arena_debug.h"
11
19
  #include "include/util/hb_buffer.h"
12
20
  #include "include/util/string.h"
13
21
 
@@ -38,7 +46,7 @@ int main(const int argc, char* argv[]) {
38
46
  if (argc < 2) {
39
47
  puts("./herb [command] [options]\n");
40
48
 
41
- puts("Herb 🌿 Powerful and seamless HTML-aware ERB parsing and tooling.\n");
49
+ puts("Herb 🌿 Powerful and seamless HTML-aware ERB toolchain.\n");
42
50
 
43
51
  puts("./herb lex [file] - Lex a file");
44
52
  puts("./herb parse [file] - Parse a file");
@@ -54,74 +62,90 @@ int main(const int argc, char* argv[]) {
54
62
  return EXIT_FAILURE;
55
63
  }
56
64
 
57
- hb_buffer_T output;
65
+ hb_allocator_T malloc_allocator = hb_allocator_with_malloc();
66
+ char* source = herb_read_file(argv[2], &malloc_allocator);
58
67
 
59
- if (!hb_buffer_init(&output, 4096)) { return 1; }
68
+ hb_allocator_T allocator;
69
+ if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
70
+ fprintf(stderr, "Failed to initialize allocator\n");
71
+ hb_allocator_dealloc(&malloc_allocator, source);
72
+ return EXIT_FAILURE;
73
+ }
60
74
 
61
- char* source = herb_read_file(argv[2]);
75
+ hb_buffer_T output;
76
+ if (!hb_buffer_init(&output, 4096, &allocator)) { return 1; }
62
77
 
63
78
  struct timespec start, end;
64
79
  clock_gettime(CLOCK_MONOTONIC, &start);
65
80
 
81
+ int silent = 0;
82
+ if (argc > 3 && string_equals(argv[3], "--silent")) { silent = 1; }
83
+
66
84
  if (string_equals(argv[1], "lex")) {
67
- herb_lex_to_buffer(source, &output);
85
+ herb_lex_to_buffer(source, &output, &allocator);
68
86
  clock_gettime(CLOCK_MONOTONIC, &end);
69
87
 
88
+ if (!silent) { hb_arena_print_stats((hb_arena_T*) allocator.context); }
89
+
70
90
  puts(output.value);
71
91
  print_time_diff(start, end, "lexing");
72
92
 
73
- free(output.value);
74
- free(source);
93
+ hb_buffer_free(&output);
94
+ hb_allocator_destroy(&allocator);
95
+ hb_allocator_dealloc(&malloc_allocator, source);
75
96
 
76
97
  return EXIT_SUCCESS;
77
98
  }
78
99
 
79
100
  if (string_equals(argv[1], "parse")) {
80
- AST_DOCUMENT_NODE_T* root = herb_parse(source, NULL);
81
-
82
- herb_analyze_parse_tree(root, source);
101
+ AST_DOCUMENT_NODE_T* root = herb_parse(source, NULL, &allocator);
83
102
 
84
103
  clock_gettime(CLOCK_MONOTONIC, &end);
85
104
 
86
- int silent = 0;
87
- if (argc > 3 && string_equals(argv[3], "--silent")) { silent = 1; }
88
-
89
105
  if (!silent) {
106
+ hb_arena_print_stats((hb_arena_T*) allocator.context);
107
+
108
+ #ifndef HERB_EXCLUDE_PRETTYPRINT
90
109
  ast_pretty_print_node((AST_NODE_T*) root, 0, 0, &output);
91
110
  puts(output.value);
111
+ #endif
92
112
 
93
113
  print_time_diff(start, end, "parsing");
94
114
  }
95
115
 
96
- ast_node_free((AST_NODE_T*) root);
97
- free(output.value);
98
- free(source);
116
+ ast_node_free((AST_NODE_T*) root, &allocator);
117
+
118
+ hb_buffer_free(&output);
119
+ hb_allocator_destroy(&allocator);
120
+ hb_allocator_dealloc(&malloc_allocator, source);
99
121
 
100
122
  return EXIT_SUCCESS;
101
123
  }
102
124
 
103
125
  if (string_equals(argv[1], "ruby")) {
104
- herb_extract_ruby_to_buffer(source, &output);
126
+ herb_extract_ruby_to_buffer(source, &output, &allocator);
105
127
  clock_gettime(CLOCK_MONOTONIC, &end);
106
128
 
107
129
  puts(output.value);
108
130
  print_time_diff(start, end, "extracting Ruby");
109
131
 
110
- free(output.value);
111
- free(source);
132
+ hb_buffer_free(&output);
133
+ hb_allocator_destroy(&allocator);
134
+ hb_allocator_dealloc(&malloc_allocator, source);
112
135
 
113
136
  return EXIT_SUCCESS;
114
137
  }
115
138
 
116
139
  if (string_equals(argv[1], "html")) {
117
- herb_extract_html_to_buffer(source, &output);
140
+ herb_extract_html_to_buffer(source, &output, &allocator);
118
141
  clock_gettime(CLOCK_MONOTONIC, &end);
119
142
 
120
143
  puts(output.value);
121
144
  print_time_diff(start, end, "extracting HTML");
122
145
 
123
- free(output.value);
124
- free(source);
146
+ hb_buffer_free(&output);
147
+ hb_allocator_destroy(&allocator);
148
+ hb_allocator_dealloc(&malloc_allocator, source);
125
149
 
126
150
  return EXIT_SUCCESS;
127
151
  }
@@ -129,14 +153,15 @@ int main(const int argc, char* argv[]) {
129
153
  if (string_equals(argv[1], "prism")) {
130
154
  printf("HTML+ERB File: \n%s\n", source);
131
155
 
132
- char* ruby_source = herb_extract(source, HERB_EXTRACT_LANGUAGE_RUBY);
156
+ char* ruby_source = herb_extract(source, HERB_EXTRACT_LANGUAGE_RUBY, &allocator);
133
157
  printf("Extracted Ruby: \n%s\n", ruby_source);
134
158
 
135
159
  herb_parse_ruby_to_stdout(ruby_source);
136
160
 
137
- free(ruby_source);
138
- free(output.value);
139
- free(source);
161
+ hb_allocator_dealloc(&allocator, ruby_source);
162
+ hb_buffer_free(&output);
163
+ hb_allocator_destroy(&allocator);
164
+ hb_allocator_dealloc(&malloc_allocator, source);
140
165
 
141
166
  return EXIT_SUCCESS;
142
167
  }