@herb-tools/node 0.7.5 → 0.8.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 (117) hide show
  1. package/binding.gyp +8 -4
  2. package/dist/herb-node.esm.js +6 -6
  3. package/dist/herb-node.esm.js.map +1 -1
  4. package/extension/error_helpers.cpp +67 -9
  5. package/extension/error_helpers.h +4 -2
  6. package/extension/extension_helpers.cpp +12 -5
  7. package/extension/extension_helpers.h +4 -2
  8. package/extension/herb.cpp +10 -42
  9. package/extension/libherb/analyze.c +420 -171
  10. package/extension/libherb/analyze.h +10 -2
  11. package/extension/libherb/analyze_helpers.c +5 -0
  12. package/extension/libherb/analyze_helpers.h +3 -0
  13. package/extension/libherb/analyze_missing_end.c +147 -0
  14. package/extension/libherb/analyze_transform.c +196 -0
  15. package/extension/libherb/analyzed_ruby.c +23 -2
  16. package/extension/libherb/analyzed_ruby.h +4 -2
  17. package/extension/libherb/ast_node.c +5 -5
  18. package/extension/libherb/ast_node.h +2 -2
  19. package/extension/libherb/ast_nodes.c +180 -180
  20. package/extension/libherb/ast_nodes.h +68 -67
  21. package/extension/libherb/ast_pretty_print.c +233 -233
  22. package/extension/libherb/ast_pretty_print.h +3 -3
  23. package/extension/libherb/element_source.c +7 -6
  24. package/extension/libherb/element_source.h +3 -1
  25. package/extension/libherb/errors.c +247 -127
  26. package/extension/libherb/errors.h +31 -15
  27. package/extension/libherb/extract.c +92 -34
  28. package/extension/libherb/extract.h +4 -4
  29. package/extension/libherb/herb.c +37 -49
  30. package/extension/libherb/herb.h +6 -7
  31. package/extension/libherb/html_util.c +34 -96
  32. package/extension/libherb/html_util.h +4 -5
  33. package/extension/libherb/include/analyze.h +10 -2
  34. package/extension/libherb/include/analyze_helpers.h +3 -0
  35. package/extension/libherb/include/analyzed_ruby.h +4 -2
  36. package/extension/libherb/include/ast_node.h +2 -2
  37. package/extension/libherb/include/ast_nodes.h +68 -67
  38. package/extension/libherb/include/ast_pretty_print.h +3 -3
  39. package/extension/libherb/include/element_source.h +3 -1
  40. package/extension/libherb/include/errors.h +31 -15
  41. package/extension/libherb/include/extract.h +4 -4
  42. package/extension/libherb/include/herb.h +6 -7
  43. package/extension/libherb/include/html_util.h +4 -5
  44. package/extension/libherb/include/lexer.h +1 -3
  45. package/extension/libherb/include/lexer_peek_helpers.h +14 -14
  46. package/extension/libherb/include/lexer_struct.h +3 -2
  47. package/extension/libherb/include/macros.h +4 -0
  48. package/extension/libherb/include/parser.h +12 -6
  49. package/extension/libherb/include/parser_helpers.h +25 -15
  50. package/extension/libherb/include/pretty_print.h +38 -28
  51. package/extension/libherb/include/token.h +5 -8
  52. package/extension/libherb/include/utf8.h +3 -2
  53. package/extension/libherb/include/util/hb_arena.h +31 -0
  54. package/extension/libherb/include/util/hb_arena_debug.h +8 -0
  55. package/extension/libherb/include/util/hb_array.h +33 -0
  56. package/extension/libherb/include/util/hb_buffer.h +34 -0
  57. package/extension/libherb/include/util/hb_string.h +29 -0
  58. package/extension/libherb/include/util/hb_system.h +9 -0
  59. package/extension/libherb/include/util.h +3 -14
  60. package/extension/libherb/include/version.h +1 -1
  61. package/extension/libherb/include/visitor.h +1 -1
  62. package/extension/libherb/io.c +7 -4
  63. package/extension/libherb/lexer.c +61 -88
  64. package/extension/libherb/lexer.h +1 -3
  65. package/extension/libherb/lexer_peek_helpers.c +35 -37
  66. package/extension/libherb/lexer_peek_helpers.h +14 -14
  67. package/extension/libherb/lexer_struct.h +3 -2
  68. package/extension/libherb/macros.h +4 -0
  69. package/extension/libherb/main.c +19 -23
  70. package/extension/libherb/parser.c +282 -201
  71. package/extension/libherb/parser.h +12 -6
  72. package/extension/libherb/parser_helpers.c +46 -40
  73. package/extension/libherb/parser_helpers.h +25 -15
  74. package/extension/libherb/parser_match_tags.c +316 -0
  75. package/extension/libherb/pretty_print.c +82 -106
  76. package/extension/libherb/pretty_print.h +38 -28
  77. package/extension/libherb/token.c +18 -65
  78. package/extension/libherb/token.h +5 -8
  79. package/extension/libherb/utf8.c +4 -4
  80. package/extension/libherb/utf8.h +3 -2
  81. package/extension/libherb/util/hb_arena.c +179 -0
  82. package/extension/libherb/util/hb_arena.h +31 -0
  83. package/extension/libherb/util/hb_arena_debug.c +237 -0
  84. package/extension/libherb/util/hb_arena_debug.h +8 -0
  85. package/extension/libherb/{array.c → util/hb_array.c} +26 -27
  86. package/extension/libherb/util/hb_array.h +33 -0
  87. package/extension/libherb/util/hb_buffer.c +203 -0
  88. package/extension/libherb/util/hb_buffer.h +34 -0
  89. package/extension/libherb/util/hb_string.c +85 -0
  90. package/extension/libherb/util/hb_string.h +29 -0
  91. package/extension/libherb/util/hb_system.c +30 -0
  92. package/extension/libherb/util/hb_system.h +9 -0
  93. package/extension/libherb/util.c +29 -99
  94. package/extension/libherb/util.h +3 -14
  95. package/extension/libherb/version.h +1 -1
  96. package/extension/libherb/visitor.c +55 -55
  97. package/extension/libherb/visitor.h +1 -1
  98. package/extension/nodes.cpp +40 -40
  99. package/extension/nodes.h +2 -2
  100. package/extension/prism/include/prism/ast.h +31 -1
  101. package/extension/prism/include/prism/diagnostic.h +1 -0
  102. package/extension/prism/include/prism/version.h +3 -3
  103. package/extension/prism/src/diagnostic.c +3 -1
  104. package/extension/prism/src/prism.c +130 -71
  105. package/extension/prism/src/util/pm_string.c +6 -8
  106. package/package.json +3 -3
  107. package/extension/libherb/array.h +0 -33
  108. package/extension/libherb/buffer.c +0 -241
  109. package/extension/libherb/buffer.h +0 -39
  110. package/extension/libherb/include/array.h +0 -33
  111. package/extension/libherb/include/buffer.h +0 -39
  112. package/extension/libherb/include/json.h +0 -28
  113. package/extension/libherb/include/memory.h +0 -12
  114. package/extension/libherb/json.c +0 -205
  115. package/extension/libherb/json.h +0 -28
  116. package/extension/libherb/memory.c +0 -53
  117. package/extension/libherb/memory.h +0 -12
@@ -1,15 +1,15 @@
1
1
  // NOTE: This file is generated by the templates/template.rb script and should not
2
- // be modified manually. See /Users/marcoroth/Development/herb-release-0.7.5/templates/src/include/errors.h.erb
2
+ // be modified manually. See /Users/marcoroth/Development/herb-release-0.8.0/templates/src/include/errors.h.erb
3
3
 
4
4
  #ifndef HERB_ERRORS_H
5
5
  #define HERB_ERRORS_H
6
6
 
7
- #include "array.h"
8
- #include "buffer.h"
9
7
  #include "errors.h"
10
8
  #include "location.h"
11
9
  #include "position.h"
12
10
  #include "token.h"
11
+ #include "util/hb_array.h"
12
+ #include "util/hb_buffer.h"
13
13
 
14
14
  typedef enum {
15
15
  UNEXPECTED_ERROR,
@@ -21,6 +21,8 @@ typedef enum {
21
21
  VOID_ELEMENT_CLOSING_TAG_ERROR,
22
22
  UNCLOSED_ELEMENT_ERROR,
23
23
  RUBY_PARSE_ERROR,
24
+ ERB_CONTROL_FLOW_SCOPE_ERROR,
25
+ MISSINGERB_END_TAG_ERROR,
24
26
  } error_type_T;
25
27
 
26
28
  typedef struct ERROR_STRUCT {
@@ -84,24 +86,38 @@ typedef struct {
84
86
  const char* level;
85
87
  } RUBY_PARSE_ERROR_T;
86
88
 
89
+ typedef struct {
90
+ ERROR_T base;
91
+ const char* keyword;
92
+ } ERB_CONTROL_FLOW_SCOPE_ERROR_T;
93
+
94
+ typedef struct {
95
+ ERROR_T base;
96
+ const char* keyword;
97
+ } MISSINGERB_END_TAG_ERROR_T;
98
+
87
99
  UNEXPECTED_ERROR_T* unexpected_error_init(const char* description, const char* expected, const char* found, position_T start, position_T end);
88
- void append_unexpected_error(const char* description, const char* expected, const char* found, position_T start, position_T end, array_T* errors);
100
+ void append_unexpected_error(const char* description, const char* expected, const char* found, position_T start, position_T end, hb_array_T* errors);
89
101
  UNEXPECTED_TOKEN_ERROR_T* unexpected_token_error_init(token_type_T expected_type, token_T* found, position_T start, position_T end);
90
- void append_unexpected_token_error(token_type_T expected_type, token_T* found, position_T start, position_T end, array_T* errors);
102
+ void append_unexpected_token_error(token_type_T expected_type, token_T* found, position_T start, position_T end, hb_array_T* errors);
91
103
  MISSING_OPENING_TAG_ERROR_T* missing_opening_tag_error_init(token_T* closing_tag, position_T start, position_T end);
92
- void append_missing_opening_tag_error(token_T* closing_tag, position_T start, position_T end, array_T* errors);
104
+ void append_missing_opening_tag_error(token_T* closing_tag, position_T start, position_T end, hb_array_T* errors);
93
105
  MISSING_CLOSING_TAG_ERROR_T* missing_closing_tag_error_init(token_T* opening_tag, position_T start, position_T end);
94
- void append_missing_closing_tag_error(token_T* opening_tag, position_T start, position_T end, array_T* errors);
106
+ void append_missing_closing_tag_error(token_T* opening_tag, position_T start, position_T end, hb_array_T* errors);
95
107
  TAG_NAMES_MISMATCH_ERROR_T* tag_names_mismatch_error_init(token_T* opening_tag, token_T* closing_tag, position_T start, position_T end);
96
- void append_tag_names_mismatch_error(token_T* opening_tag, token_T* closing_tag, position_T start, position_T end, array_T* errors);
108
+ void append_tag_names_mismatch_error(token_T* opening_tag, token_T* closing_tag, position_T start, position_T end, hb_array_T* errors);
97
109
  QUOTES_MISMATCH_ERROR_T* quotes_mismatch_error_init(token_T* opening_quote, token_T* closing_quote, position_T start, position_T end);
98
- void append_quotes_mismatch_error(token_T* opening_quote, token_T* closing_quote, position_T start, position_T end, array_T* errors);
110
+ void append_quotes_mismatch_error(token_T* opening_quote, token_T* closing_quote, position_T start, position_T end, hb_array_T* errors);
99
111
  VOID_ELEMENT_CLOSING_TAG_ERROR_T* void_element_closing_tag_error_init(token_T* tag_name, const char* expected, const char* found, position_T start, position_T end);
100
- void append_void_element_closing_tag_error(token_T* tag_name, const char* expected, const char* found, position_T start, position_T end, array_T* errors);
112
+ void append_void_element_closing_tag_error(token_T* tag_name, const char* expected, const char* found, position_T start, position_T end, hb_array_T* errors);
101
113
  UNCLOSED_ELEMENT_ERROR_T* unclosed_element_error_init(token_T* opening_tag, position_T start, position_T end);
102
- void append_unclosed_element_error(token_T* opening_tag, position_T start, position_T end, array_T* errors);
114
+ void append_unclosed_element_error(token_T* opening_tag, position_T start, position_T end, hb_array_T* errors);
103
115
  RUBY_PARSE_ERROR_T* ruby_parse_error_init(const char* error_message, const char* diagnostic_id, const char* level, position_T start, position_T end);
104
- void append_ruby_parse_error(const char* error_message, const char* diagnostic_id, const char* level, position_T start, position_T end, array_T* errors);
116
+ void append_ruby_parse_error(const char* error_message, const char* diagnostic_id, const char* level, position_T start, position_T end, hb_array_T* errors);
117
+ ERB_CONTROL_FLOW_SCOPE_ERROR_T* erb_control_flow_scope_error_init(const char* keyword, position_T start, position_T end);
118
+ void append_erb_control_flow_scope_error(const char* keyword, position_T start, position_T end, hb_array_T* errors);
119
+ MISSINGERB_END_TAG_ERROR_T* missingerb_end_tag_error_init(const char* keyword, position_T start, position_T end);
120
+ void append_missingerb_end_tag_error(const char* keyword, position_T start, position_T end, hb_array_T* errors);
105
121
 
106
122
  void error_init(ERROR_T* error, error_type_T type, position_T start, position_T end);
107
123
 
@@ -115,11 +131,11 @@ const char* error_human_type(ERROR_T* error);
115
131
 
116
132
  void error_free(ERROR_T* error);
117
133
 
118
- void error_pretty_print(ERROR_T* error, size_t indent, size_t relative_indent, buffer_T* buffer);
134
+ void error_pretty_print(ERROR_T* error, size_t indent, size_t relative_indent, hb_buffer_T* buffer);
119
135
 
120
136
  void error_pretty_print_array(
121
- const char* name, array_T* array, size_t indent, size_t relative_indent, bool last_property,
122
- buffer_T* buffer
137
+ const char* name, hb_array_T* array, size_t indent, size_t relative_indent, bool last_property,
138
+ hb_buffer_T* buffer
123
139
  );
124
140
 
125
141
  #endif
@@ -1,53 +1,107 @@
1
- #include "include/array.h"
2
- #include "include/buffer.h"
3
1
  #include "include/herb.h"
4
2
  #include "include/io.h"
5
3
  #include "include/lexer.h"
4
+ #include "include/util/hb_array.h"
5
+ #include "include/util/hb_buffer.h"
6
6
 
7
7
  #include <stdlib.h>
8
+ #include <string.h>
8
9
 
9
- void herb_extract_ruby_to_buffer_with_semicolons(const char* source, buffer_T* output) {
10
- array_T* tokens = herb_lex(source);
10
+ void herb_extract_ruby_to_buffer_with_semicolons(const char* source, hb_buffer_T* output) {
11
+ hb_array_T* tokens = herb_lex(source);
11
12
  bool skip_erb_content = false;
13
+ bool is_comment_tag = false;
12
14
 
13
- for (size_t i = 0; i < array_size(tokens); i++) {
14
- const token_T* token = array_get(tokens, i);
15
+ for (size_t i = 0; i < hb_array_size(tokens); i++) {
16
+ const token_T* token = hb_array_get(tokens, i);
15
17
 
16
18
  switch (token->type) {
17
19
  case TOKEN_NEWLINE: {
18
- buffer_append(output, token->value);
20
+ hb_buffer_append(output, token->value);
19
21
  break;
20
22
  }
21
23
 
22
24
  case TOKEN_ERB_START: {
23
- if (strcmp(token->value, "<%#") == 0 || strcmp(token->value, "<%%") == 0 || strcmp(token->value, "<%%=") == 0) {
25
+ if (strcmp(token->value, "<%#") == 0) {
26
+ skip_erb_content = true;
27
+ is_comment_tag = true;
28
+ } else if (strcmp(token->value, "<%%") == 0 || strcmp(token->value, "<%%=") == 0) {
24
29
  skip_erb_content = true;
30
+ is_comment_tag = false;
31
+ } else {
32
+ skip_erb_content = false;
33
+ is_comment_tag = false;
25
34
  }
26
35
 
27
- buffer_append_whitespace(output, range_length(token->range));
36
+ hb_buffer_append_whitespace(output, range_length(token->range));
28
37
  break;
29
38
  }
30
39
 
31
40
  case TOKEN_ERB_CONTENT: {
32
41
  if (skip_erb_content == false) {
33
- buffer_append(output, token->value);
42
+ bool is_inline_comment = false;
43
+
44
+ if (!is_comment_tag && token->value != NULL) {
45
+ const char* content = token->value;
46
+
47
+ while (*content == ' ' || *content == '\t') {
48
+ content++;
49
+ }
50
+
51
+ if (*content == '#' && token->location.start.line == token->location.end.line) {
52
+ is_comment_tag = true;
53
+ is_inline_comment = true;
54
+ }
55
+ }
56
+
57
+ if (is_inline_comment) {
58
+ hb_buffer_append_whitespace(output, range_length(token->range));
59
+ } else {
60
+ hb_buffer_append(output, token->value);
61
+ }
34
62
  } else {
35
- buffer_append_whitespace(output, range_length(token->range));
63
+ hb_buffer_append_whitespace(output, range_length(token->range));
36
64
  }
37
65
 
38
66
  break;
39
67
  }
40
68
 
41
69
  case TOKEN_ERB_END: {
70
+ bool was_comment = is_comment_tag;
42
71
  skip_erb_content = false;
72
+ is_comment_tag = false;
73
+
74
+ if (was_comment) {
75
+ hb_buffer_append_whitespace(output, range_length(token->range));
76
+ break;
77
+ }
78
+
79
+ bool needs_semicolon = false;
80
+ uint32_t current_line = token->location.end.line;
81
+
82
+ for (size_t j = i + 1; j < hb_array_size(tokens); j++) {
83
+ const token_T* next_token = hb_array_get(tokens, j);
43
84
 
44
- buffer_append_char(output, ';');
45
- buffer_append_whitespace(output, range_length(token->range) - 1);
85
+ if (next_token->type == TOKEN_NEWLINE) { break; }
86
+
87
+ if (next_token->type == TOKEN_ERB_START && next_token->location.start.line == current_line) {
88
+ needs_semicolon = true;
89
+ break;
90
+ }
91
+ }
92
+
93
+ if (needs_semicolon) {
94
+ hb_buffer_append_char(output, ' ');
95
+ hb_buffer_append_char(output, ';');
96
+ hb_buffer_append_whitespace(output, range_length(token->range) - 2);
97
+ } else {
98
+ hb_buffer_append_whitespace(output, range_length(token->range));
99
+ }
46
100
  break;
47
101
  }
48
102
 
49
103
  default: {
50
- buffer_append_whitespace(output, range_length(token->range));
104
+ hb_buffer_append_whitespace(output, range_length(token->range));
51
105
  }
52
106
  }
53
107
  }
@@ -55,16 +109,16 @@ void herb_extract_ruby_to_buffer_with_semicolons(const char* source, buffer_T* o
55
109
  herb_free_tokens(&tokens);
56
110
  }
57
111
 
58
- void herb_extract_ruby_to_buffer(const char* source, buffer_T* output) {
59
- array_T* tokens = herb_lex(source);
112
+ void herb_extract_ruby_to_buffer(const char* source, hb_buffer_T* output) {
113
+ hb_array_T* tokens = herb_lex(source);
60
114
  bool skip_erb_content = false;
61
115
 
62
- for (size_t i = 0; i < array_size(tokens); i++) {
63
- const token_T* token = array_get(tokens, i);
116
+ for (size_t i = 0; i < hb_array_size(tokens); i++) {
117
+ const token_T* token = hb_array_get(tokens, i);
64
118
 
65
119
  switch (token->type) {
66
120
  case TOKEN_NEWLINE: {
67
- buffer_append(output, token->value);
121
+ hb_buffer_append(output, token->value);
68
122
  break;
69
123
  }
70
124
 
@@ -73,15 +127,15 @@ void herb_extract_ruby_to_buffer(const char* source, buffer_T* output) {
73
127
  skip_erb_content = true;
74
128
  }
75
129
 
76
- buffer_append_whitespace(output, range_length(token->range));
130
+ hb_buffer_append_whitespace(output, range_length(token->range));
77
131
  break;
78
132
  }
79
133
 
80
134
  case TOKEN_ERB_CONTENT: {
81
135
  if (skip_erb_content == false) {
82
- buffer_append(output, token->value);
136
+ hb_buffer_append(output, token->value);
83
137
  } else {
84
- buffer_append_whitespace(output, range_length(token->range));
138
+ hb_buffer_append_whitespace(output, range_length(token->range));
85
139
  }
86
140
 
87
141
  break;
@@ -90,12 +144,12 @@ void herb_extract_ruby_to_buffer(const char* source, buffer_T* output) {
90
144
  case TOKEN_ERB_END: {
91
145
  skip_erb_content = false;
92
146
 
93
- buffer_append_whitespace(output, range_length(token->range));
147
+ hb_buffer_append_whitespace(output, range_length(token->range));
94
148
  break;
95
149
  }
96
150
 
97
151
  default: {
98
- buffer_append_whitespace(output, range_length(token->range));
152
+ hb_buffer_append_whitespace(output, range_length(token->range));
99
153
  }
100
154
  }
101
155
  }
@@ -103,17 +157,17 @@ void herb_extract_ruby_to_buffer(const char* source, buffer_T* output) {
103
157
  herb_free_tokens(&tokens);
104
158
  }
105
159
 
106
- void herb_extract_html_to_buffer(const char* source, buffer_T* output) {
107
- array_T* tokens = herb_lex(source);
160
+ void herb_extract_html_to_buffer(const char* source, hb_buffer_T* output) {
161
+ hb_array_T* tokens = herb_lex(source);
108
162
 
109
- for (size_t i = 0; i < array_size(tokens); i++) {
110
- const token_T* token = array_get(tokens, i);
163
+ for (size_t i = 0; i < hb_array_size(tokens); i++) {
164
+ const token_T* token = hb_array_get(tokens, i);
111
165
 
112
166
  switch (token->type) {
113
167
  case TOKEN_ERB_START:
114
168
  case TOKEN_ERB_CONTENT:
115
- case TOKEN_ERB_END: buffer_append_whitespace(output, range_length(token->range)); break;
116
- default: buffer_append(output, token->value);
169
+ case TOKEN_ERB_END: hb_buffer_append_whitespace(output, range_length(token->range)); break;
170
+ default: hb_buffer_append(output, token->value);
117
171
  }
118
172
  }
119
173
 
@@ -121,8 +175,10 @@ void herb_extract_html_to_buffer(const char* source, buffer_T* output) {
121
175
  }
122
176
 
123
177
  char* herb_extract_ruby_with_semicolons(const char* source) {
124
- buffer_T output;
125
- buffer_init(&output);
178
+ if (!source) { return NULL; }
179
+
180
+ hb_buffer_T output;
181
+ hb_buffer_init(&output, strlen(source));
126
182
 
127
183
  herb_extract_ruby_to_buffer_with_semicolons(source, &output);
128
184
 
@@ -130,8 +186,10 @@ char* herb_extract_ruby_with_semicolons(const char* source) {
130
186
  }
131
187
 
132
188
  char* herb_extract(const char* source, const herb_extract_language_T language) {
133
- buffer_T output;
134
- buffer_init(&output);
189
+ if (!source) { return NULL; }
190
+
191
+ hb_buffer_T output;
192
+ hb_buffer_init(&output, strlen(source));
135
193
 
136
194
  switch (language) {
137
195
  case HERB_EXTRACT_LANGUAGE_RUBY: herb_extract_ruby_to_buffer(source, &output); break;
@@ -1,18 +1,18 @@
1
1
  #ifndef HERB_EXTRACT_H
2
2
  #define HERB_EXTRACT_H
3
3
 
4
- #include "buffer.h"
4
+ #include "util/hb_buffer.h"
5
5
 
6
6
  typedef enum {
7
7
  HERB_EXTRACT_LANGUAGE_RUBY,
8
8
  HERB_EXTRACT_LANGUAGE_HTML,
9
9
  } herb_extract_language_T;
10
10
 
11
- void herb_extract_ruby_to_buffer(const char* source, buffer_T* output);
12
- void herb_extract_html_to_buffer(const char* source, buffer_T* output);
11
+ void herb_extract_ruby_to_buffer(const char* source, hb_buffer_T* output);
12
+ void herb_extract_html_to_buffer(const char* source, hb_buffer_T* output);
13
13
 
14
14
  char* herb_extract_ruby_with_semicolons(const char* source);
15
- void herb_extract_ruby_to_buffer_with_semicolons(const char* source, buffer_T* output);
15
+ void herb_extract_ruby_to_buffer_with_semicolons(const char* source, hb_buffer_T* output);
16
16
 
17
17
  char* herb_extract(const char* source, herb_extract_language_T language);
18
18
  char* herb_extract_from_file(const char* path, herb_extract_language_T language);
@@ -1,97 +1,85 @@
1
1
  #include "include/herb.h"
2
- #include "include/array.h"
3
- #include "include/buffer.h"
4
2
  #include "include/io.h"
5
- #include "include/json.h"
6
3
  #include "include/lexer.h"
7
4
  #include "include/parser.h"
8
5
  #include "include/token.h"
6
+ #include "include/util/hb_array.h"
7
+ #include "include/util/hb_buffer.h"
9
8
  #include "include/version.h"
10
9
 
11
10
  #include <prism.h>
12
11
  #include <stdlib.h>
13
12
 
14
- array_T* herb_lex(const char* source) {
15
- lexer_T* lexer = lexer_init(source);
13
+ hb_array_T* herb_lex(const char* source) {
14
+ lexer_T lexer = { 0 };
15
+ lexer_init(&lexer, source);
16
+
16
17
  token_T* token = NULL;
17
- array_T* tokens = array_init(128);
18
+ hb_array_T* tokens = hb_array_init(128);
18
19
 
19
- while ((token = lexer_next_token(lexer))->type != TOKEN_EOF) {
20
- array_append(tokens, token);
20
+ while ((token = lexer_next_token(&lexer))->type != TOKEN_EOF) {
21
+ hb_array_append(tokens, token);
21
22
  }
22
23
 
23
- array_append(tokens, token);
24
-
25
- lexer_free(lexer);
24
+ hb_array_append(tokens, token);
26
25
 
27
26
  return tokens;
28
27
  }
29
28
 
30
29
  AST_DOCUMENT_NODE_T* herb_parse(const char* source, parser_options_T* options) {
31
- lexer_T* lexer = lexer_init(source);
32
- parser_T* parser = herb_parser_init(lexer, options);
30
+ if (!source) { source = ""; }
31
+
32
+ lexer_T lexer = { 0 };
33
+ lexer_init(&lexer, source);
34
+ parser_T parser = { 0 };
35
+
36
+ parser_options_T parser_options = HERB_DEFAULT_PARSER_OPTIONS;
33
37
 
34
- AST_DOCUMENT_NODE_T* document = herb_parser_parse(parser);
38
+ if (options != NULL) { parser_options = *options; }
35
39
 
36
- parser_free(parser);
40
+ herb_parser_init(&parser, &lexer, parser_options);
41
+
42
+ AST_DOCUMENT_NODE_T* document = herb_parser_parse(&parser);
43
+
44
+ herb_parser_deinit(&parser);
37
45
 
38
46
  return document;
39
47
  }
40
48
 
41
- array_T* herb_lex_file(const char* path) {
49
+ hb_array_T* herb_lex_file(const char* path) {
42
50
  char* source = herb_read_file(path);
43
- array_T* tokens = herb_lex(source);
51
+ hb_array_T* tokens = herb_lex(source);
44
52
 
45
53
  free(source);
46
54
 
47
55
  return tokens;
48
56
  }
49
57
 
50
- void herb_lex_to_buffer(const char* source, buffer_T* output) {
51
- array_T* tokens = herb_lex(source);
52
-
53
- for (size_t i = 0; i < array_size(tokens); i++) {
54
- token_T* token = array_get(tokens, i);
58
+ void herb_lex_to_buffer(const char* source, hb_buffer_T* output) {
59
+ hb_array_T* tokens = herb_lex(source);
55
60
 
56
- char* type = token_to_string(token);
57
- buffer_append(output, type);
58
- free(type);
61
+ for (size_t i = 0; i < hb_array_size(tokens); i++) {
62
+ token_T* token = hb_array_get(tokens, i);
59
63
 
60
- buffer_append(output, "\n");
61
- }
62
-
63
- herb_free_tokens(&tokens);
64
- }
64
+ hb_string_T type = token_to_string(token);
65
+ hb_buffer_append_string(output, type);
66
+ free(type.data);
65
67
 
66
- void herb_lex_json_to_buffer(const char* source, buffer_T* output) {
67
- array_T* tokens = herb_lex(source);
68
-
69
- buffer_T json = buffer_new();
70
- json_start_root_array(&json);
71
-
72
- for (size_t i = 0; i < array_size(tokens); i++) {
73
- token_T* token = array_get(tokens, i);
74
- char* token_json = token_to_json(token);
75
- json_add_raw_string(&json, token_json);
76
- free(token_json);
68
+ hb_buffer_append(output, "\n");
77
69
  }
78
70
 
79
- json_end_array(&json);
80
- buffer_concat(output, &json);
81
-
82
- buffer_free(&json);
83
71
  herb_free_tokens(&tokens);
84
72
  }
85
73
 
86
- void herb_free_tokens(array_T** tokens) {
74
+ void herb_free_tokens(hb_array_T** tokens) {
87
75
  if (!tokens || !*tokens) { return; }
88
76
 
89
- for (size_t i = 0; i < array_size(*tokens); i++) {
90
- token_T* token = array_get(*tokens, i);
77
+ for (size_t i = 0; i < hb_array_size(*tokens); i++) {
78
+ token_T* token = hb_array_get(*tokens, i);
91
79
  if (token) { token_free(token); }
92
80
  }
93
81
 
94
- array_free(tokens);
82
+ hb_array_free(tokens);
95
83
  }
96
84
 
97
85
  const char* herb_version(void) {
@@ -1,11 +1,11 @@
1
1
  #ifndef HERB_H
2
2
  #define HERB_H
3
3
 
4
- #include "array.h"
5
4
  #include "ast_node.h"
6
- #include "buffer.h"
7
5
  #include "extract.h"
8
6
  #include "parser.h"
7
+ #include "util/hb_array.h"
8
+ #include "util/hb_buffer.h"
9
9
 
10
10
  #include <stdint.h>
11
11
 
@@ -13,18 +13,17 @@
13
13
  extern "C" {
14
14
  #endif
15
15
 
16
- void herb_lex_to_buffer(const char* source, buffer_T* output);
17
- void herb_lex_json_to_buffer(const char* source, buffer_T* output);
16
+ void herb_lex_to_buffer(const char* source, hb_buffer_T* output);
18
17
 
19
- array_T* herb_lex(const char* source);
20
- array_T* herb_lex_file(const char* path);
18
+ hb_array_T* herb_lex(const char* source);
19
+ hb_array_T* herb_lex_file(const char* path);
21
20
 
22
21
  AST_DOCUMENT_NODE_T* herb_parse(const char* source, parser_options_T* options);
23
22
 
24
23
  const char* herb_version(void);
25
24
  const char* herb_prism_version(void);
26
25
 
27
- void herb_free_tokens(array_T** tokens);
26
+ void herb_free_tokens(hb_array_T** tokens);
28
27
 
29
28
  #ifdef __cplusplus
30
29
  }