@herb-tools/node 0.8.9 → 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.
- package/CHANGELOG.md +19 -0
- package/binding.gyp +26 -8
- package/dist/herb-node.cjs +41 -12
- package/dist/herb-node.cjs.map +1 -1
- package/dist/herb-node.esm.js +8 -1
- package/dist/herb-node.esm.js.map +1 -1
- package/dist/types/node-backend.d.ts +3 -1
- package/extension/error_helpers.cpp +419 -71
- package/extension/error_helpers.h +14 -3
- package/extension/extension_helpers.cpp +38 -35
- package/extension/extension_helpers.h +2 -2
- package/extension/herb.cpp +183 -64
- package/extension/libherb/analyze/action_view/attribute_extraction_helpers.c +290 -0
- package/extension/libherb/analyze/action_view/attribute_extraction_helpers.h +36 -0
- package/extension/libherb/analyze/action_view/content_tag.c +70 -0
- package/extension/libherb/analyze/action_view/link_to.c +143 -0
- package/extension/libherb/analyze/action_view/registry.c +60 -0
- package/extension/libherb/analyze/action_view/tag.c +64 -0
- package/extension/libherb/analyze/action_view/tag_helper_handler.h +41 -0
- package/extension/libherb/analyze/action_view/tag_helper_node_builders.c +305 -0
- package/extension/libherb/analyze/action_view/tag_helper_node_builders.h +70 -0
- package/extension/libherb/analyze/action_view/tag_helpers.c +748 -0
- package/extension/libherb/analyze/action_view/tag_helpers.h +38 -0
- package/extension/libherb/analyze/action_view/turbo_frame_tag.c +88 -0
- package/extension/libherb/analyze/analyze.c +882 -0
- package/extension/libherb/{include → analyze}/analyze.h +14 -4
- package/extension/libherb/{analyzed_ruby.c → analyze/analyzed_ruby.c} +13 -11
- package/extension/libherb/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
- package/extension/libherb/analyze/builders.c +343 -0
- package/extension/libherb/analyze/builders.h +27 -0
- package/extension/libherb/analyze/conditional_elements.c +594 -0
- package/extension/libherb/analyze/conditional_elements.h +9 -0
- package/extension/libherb/analyze/conditional_open_tags.c +640 -0
- package/extension/libherb/analyze/conditional_open_tags.h +9 -0
- package/extension/libherb/analyze/control_type.c +250 -0
- package/extension/libherb/analyze/control_type.h +14 -0
- package/extension/libherb/{analyze_helpers.c → analyze/helpers.c} +79 -31
- package/extension/libherb/{analyze_helpers.h → analyze/helpers.h} +22 -17
- package/extension/libherb/analyze/invalid_structures.c +193 -0
- package/extension/libherb/analyze/invalid_structures.h +11 -0
- package/extension/libherb/{analyze_missing_end.c → analyze/missing_end.c} +33 -22
- package/extension/libherb/analyze/parse_errors.c +84 -0
- package/extension/libherb/analyze/prism_annotate.c +397 -0
- package/extension/libherb/analyze/prism_annotate.h +16 -0
- package/extension/libherb/{analyze_transform.c → analyze/transform.c} +17 -3
- package/extension/libherb/ast_node.c +17 -7
- package/extension/libherb/ast_node.h +11 -5
- package/extension/libherb/ast_nodes.c +663 -388
- package/extension/libherb/ast_nodes.h +118 -39
- package/extension/libherb/ast_pretty_print.c +191 -7
- package/extension/libherb/ast_pretty_print.h +6 -1
- package/extension/libherb/element_source.h +3 -8
- package/extension/libherb/errors.c +1100 -507
- package/extension/libherb/errors.h +155 -54
- package/extension/libherb/extract.c +148 -49
- package/extension/libherb/extract.h +21 -5
- package/extension/libherb/herb.c +52 -34
- package/extension/libherb/herb.h +18 -6
- package/extension/libherb/herb_prism_node.h +13 -0
- package/extension/libherb/html_util.c +241 -12
- package/extension/libherb/html_util.h +7 -2
- package/extension/libherb/include/analyze/action_view/attribute_extraction_helpers.h +36 -0
- package/extension/libherb/include/analyze/action_view/tag_helper_handler.h +41 -0
- package/extension/libherb/include/analyze/action_view/tag_helper_node_builders.h +70 -0
- package/extension/libherb/include/analyze/action_view/tag_helpers.h +38 -0
- package/extension/libherb/{analyze.h → include/analyze/analyze.h} +14 -4
- package/extension/libherb/include/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
- package/extension/libherb/include/analyze/builders.h +27 -0
- package/extension/libherb/include/analyze/conditional_elements.h +9 -0
- package/extension/libherb/include/analyze/conditional_open_tags.h +9 -0
- package/extension/libherb/include/analyze/control_type.h +14 -0
- package/extension/libherb/include/{analyze_helpers.h → analyze/helpers.h} +22 -17
- package/extension/libherb/include/analyze/invalid_structures.h +11 -0
- package/extension/libherb/include/analyze/prism_annotate.h +16 -0
- package/extension/libherb/include/ast_node.h +11 -5
- package/extension/libherb/include/ast_nodes.h +118 -39
- package/extension/libherb/include/ast_pretty_print.h +6 -1
- package/extension/libherb/include/element_source.h +3 -8
- package/extension/libherb/include/errors.h +155 -54
- package/extension/libherb/include/extract.h +21 -5
- package/extension/libherb/include/herb.h +18 -6
- package/extension/libherb/include/herb_prism_node.h +13 -0
- package/extension/libherb/include/html_util.h +7 -2
- package/extension/libherb/include/io.h +3 -1
- package/extension/libherb/include/lex_helpers.h +29 -0
- package/extension/libherb/include/lexer.h +1 -1
- package/extension/libherb/include/lexer_peek_helpers.h +87 -13
- package/extension/libherb/include/lexer_struct.h +2 -0
- package/extension/libherb/include/location.h +2 -1
- package/extension/libherb/include/parser.h +27 -2
- package/extension/libherb/include/parser_helpers.h +19 -3
- package/extension/libherb/include/pretty_print.h +10 -5
- package/extension/libherb/include/prism_context.h +45 -0
- package/extension/libherb/include/prism_helpers.h +10 -7
- package/extension/libherb/include/prism_serialized.h +12 -0
- package/extension/libherb/include/token.h +16 -4
- package/extension/libherb/include/token_struct.h +10 -3
- package/extension/libherb/include/utf8.h +2 -1
- package/extension/libherb/include/util/hb_allocator.h +78 -0
- package/extension/libherb/include/util/hb_arena.h +6 -1
- package/extension/libherb/include/util/hb_arena_debug.h +12 -1
- package/extension/libherb/include/util/hb_array.h +7 -3
- package/extension/libherb/include/util/hb_buffer.h +6 -4
- package/extension/libherb/include/util/hb_foreach.h +79 -0
- package/extension/libherb/include/util/hb_narray.h +8 -4
- package/extension/libherb/include/util/hb_string.h +56 -9
- package/extension/libherb/include/util/string.h +11 -0
- package/extension/libherb/include/util.h +6 -3
- package/extension/libherb/include/version.h +1 -1
- package/extension/libherb/io.c +3 -2
- package/extension/libherb/io.h +3 -1
- package/extension/libherb/lex_helpers.h +29 -0
- package/extension/libherb/lexer.c +42 -30
- package/extension/libherb/lexer.h +1 -1
- package/extension/libherb/lexer_peek_helpers.c +12 -74
- package/extension/libherb/lexer_peek_helpers.h +87 -13
- package/extension/libherb/lexer_struct.h +2 -0
- package/extension/libherb/location.c +2 -2
- package/extension/libherb/location.h +2 -1
- package/extension/libherb/main.c +79 -66
- package/extension/libherb/parser.c +784 -247
- package/extension/libherb/parser.h +27 -2
- package/extension/libherb/parser_helpers.c +110 -23
- package/extension/libherb/parser_helpers.h +19 -3
- package/extension/libherb/parser_match_tags.c +110 -49
- package/extension/libherb/pretty_print.c +29 -24
- package/extension/libherb/pretty_print.h +10 -5
- package/extension/libherb/prism_context.h +45 -0
- package/extension/libherb/prism_helpers.c +30 -27
- package/extension/libherb/prism_helpers.h +10 -7
- package/extension/libherb/prism_serialized.h +12 -0
- package/extension/libherb/ruby_parser.c +2 -0
- package/extension/libherb/token.c +151 -66
- package/extension/libherb/token.h +16 -4
- package/extension/libherb/token_matchers.c +0 -1
- package/extension/libherb/token_struct.h +10 -3
- package/extension/libherb/utf8.c +7 -6
- package/extension/libherb/utf8.h +2 -1
- package/extension/libherb/util/hb_allocator.c +341 -0
- package/extension/libherb/util/hb_allocator.h +78 -0
- package/extension/libherb/util/hb_arena.c +81 -56
- package/extension/libherb/util/hb_arena.h +6 -1
- package/extension/libherb/util/hb_arena_debug.c +32 -17
- package/extension/libherb/util/hb_arena_debug.h +12 -1
- package/extension/libherb/util/hb_array.c +30 -15
- package/extension/libherb/util/hb_array.h +7 -3
- package/extension/libherb/util/hb_buffer.c +17 -21
- package/extension/libherb/util/hb_buffer.h +6 -4
- package/extension/libherb/util/hb_foreach.h +79 -0
- package/extension/libherb/util/hb_narray.c +22 -7
- package/extension/libherb/util/hb_narray.h +8 -4
- package/extension/libherb/util/hb_string.c +49 -35
- package/extension/libherb/util/hb_string.h +56 -9
- package/extension/libherb/util/string.h +11 -0
- package/extension/libherb/util.c +21 -11
- package/extension/libherb/util.h +6 -3
- package/extension/libherb/version.h +1 -1
- package/extension/libherb/visitor.c +48 -1
- package/extension/nodes.cpp +451 -6
- package/extension/nodes.h +8 -1
- package/extension/prism/include/prism/ast.h +4 -4
- package/extension/prism/include/prism/version.h +2 -2
- package/extension/prism/src/prism.c +1 -1
- package/package.json +12 -8
- package/src/node-backend.ts +11 -1
- package/dist/types/index-cjs.d.cts +0 -1
- package/extension/libherb/analyze.c +0 -1594
- package/extension/libherb/element_source.c +0 -12
- package/extension/libherb/include/util/hb_system.h +0 -9
- package/extension/libherb/util/hb_system.c +0 -30
- package/extension/libherb/util/hb_system.h +0 -9
- package/src/index-cjs.cts +0 -22
- /package/dist/types/{index-esm.d.mts → index.d.ts} +0 -0
- /package/src/{index-esm.mts → index.ts} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
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.
|
|
2
|
+
// be modified manually. See /Users/marcoroth/Development/herb-release-0.9.0/templates/javascript/packages/node/extension/error_helpers.h.erb
|
|
3
3
|
|
|
4
4
|
#ifndef HERB_EXTENSION_ERRORS_H
|
|
5
5
|
#define HERB_EXTENSION_ERRORS_H
|
|
@@ -18,12 +18,23 @@ napi_value UnexpectedTokenErrorFromCStruct(napi_env env, UNEXPECTED_TOKEN_ERROR_
|
|
|
18
18
|
napi_value MissingOpeningTagErrorFromCStruct(napi_env env, MISSING_OPENING_TAG_ERROR_T* missing_opening_tag_error);
|
|
19
19
|
napi_value MissingClosingTagErrorFromCStruct(napi_env env, MISSING_CLOSING_TAG_ERROR_T* missing_closing_tag_error);
|
|
20
20
|
napi_value TagNamesMismatchErrorFromCStruct(napi_env env, TAG_NAMES_MISMATCH_ERROR_T* tag_names_mismatch_error);
|
|
21
|
-
napi_value QuotesMismatchErrorFromCStruct(napi_env env, QUOTES_MISMATCH_ERROR_T* quotes_mismatch_error);
|
|
22
21
|
napi_value VoidElementClosingTagErrorFromCStruct(napi_env env, VOID_ELEMENT_CLOSING_TAG_ERROR_T* void_element_closing_tag_error);
|
|
23
22
|
napi_value UnclosedElementErrorFromCStruct(napi_env env, UNCLOSED_ELEMENT_ERROR_T* unclosed_element_error);
|
|
24
23
|
napi_value RubyParseErrorFromCStruct(napi_env env, RUBY_PARSE_ERROR_T* ruby_parse_error);
|
|
25
24
|
napi_value ERBControlFlowScopeErrorFromCStruct(napi_env env, ERB_CONTROL_FLOW_SCOPE_ERROR_T* erb_control_flow_scope_error);
|
|
26
|
-
napi_value MissingERBEndTagErrorFromCStruct(napi_env env,
|
|
25
|
+
napi_value MissingERBEndTagErrorFromCStruct(napi_env env, MISSING_ERB_END_TAG_ERROR_T* missing_erb_end_tag_error);
|
|
27
26
|
napi_value ERBMultipleBlocksInTagErrorFromCStruct(napi_env env, ERB_MULTIPLE_BLOCKS_IN_TAG_ERROR_T* erb_multiple_blocks_in_tag_error);
|
|
27
|
+
napi_value ERBCaseWithConditionsErrorFromCStruct(napi_env env, ERB_CASE_WITH_CONDITIONS_ERROR_T* erb_case_with_conditions_error);
|
|
28
|
+
napi_value ConditionalElementMultipleTagsErrorFromCStruct(napi_env env, CONDITIONAL_ELEMENT_MULTIPLE_TAGS_ERROR_T* conditional_element_multiple_tags_error);
|
|
29
|
+
napi_value ConditionalElementConditionMismatchErrorFromCStruct(napi_env env, CONDITIONAL_ELEMENT_CONDITION_MISMATCH_ERROR_T* conditional_element_condition_mismatch_error);
|
|
30
|
+
napi_value InvalidCommentClosingTagErrorFromCStruct(napi_env env, INVALID_COMMENT_CLOSING_TAG_ERROR_T* invalid_comment_closing_tag_error);
|
|
31
|
+
napi_value OmittedClosingTagErrorFromCStruct(napi_env env, OMITTED_CLOSING_TAG_ERROR_T* omitted_closing_tag_error);
|
|
32
|
+
napi_value UnclosedOpenTagErrorFromCStruct(napi_env env, UNCLOSED_OPEN_TAG_ERROR_T* unclosed_open_tag_error);
|
|
33
|
+
napi_value UnclosedCloseTagErrorFromCStruct(napi_env env, UNCLOSED_CLOSE_TAG_ERROR_T* unclosed_close_tag_error);
|
|
34
|
+
napi_value UnclosedQuoteErrorFromCStruct(napi_env env, UNCLOSED_QUOTE_ERROR_T* unclosed_quote_error);
|
|
35
|
+
napi_value MissingAttributeValueErrorFromCStruct(napi_env env, MISSING_ATTRIBUTE_VALUE_ERROR_T* missing_attribute_value_error);
|
|
36
|
+
napi_value UnclosedERBTagErrorFromCStruct(napi_env env, UNCLOSED_ERB_TAG_ERROR_T* unclosed_erb_tag_error);
|
|
37
|
+
napi_value StrayERBClosingTagErrorFromCStruct(napi_env env, STRAY_ERB_CLOSING_TAG_ERROR_T* stray_erb_closing_tag_error);
|
|
38
|
+
napi_value NestedERBTagErrorFromCStruct(napi_env env, NESTED_ERB_TAG_ERROR_T* nested_erb_tag_error);
|
|
28
39
|
|
|
29
40
|
#endif
|
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
extern "C" {
|
|
7
7
|
#include "../extension/libherb/include/ast_nodes.h"
|
|
8
8
|
#include "../extension/libherb/include/herb.h"
|
|
9
|
-
#include "../extension/libherb/include/io.h"
|
|
10
9
|
#include "../extension/libherb/include/location.h"
|
|
11
10
|
#include "../extension/libherb/include/position.h"
|
|
12
11
|
#include "../extension/libherb/include/range.h"
|
|
13
12
|
#include "../extension/libherb/include/token.h"
|
|
14
13
|
#include "../extension/libherb/include/util/hb_array.h"
|
|
15
14
|
#include "../extension/libherb/include/util/hb_buffer.h"
|
|
15
|
+
#include "../extension/libherb/include/util/hb_string.h"
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
#include "error_helpers.h"
|
|
@@ -47,6 +47,12 @@ napi_value CreateString(napi_env env, const char* str) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
napi_value CreateStringFromHbString(napi_env env, hb_string_T string) {
|
|
50
|
+
if (hb_string_is_null(string)) {
|
|
51
|
+
napi_value null_value;
|
|
52
|
+
napi_get_null(env, &null_value);
|
|
53
|
+
return null_value;
|
|
54
|
+
}
|
|
55
|
+
|
|
50
56
|
napi_value result;
|
|
51
57
|
napi_create_string_utf8(env, string.data, string.length, &result);
|
|
52
58
|
return result;
|
|
@@ -104,45 +110,19 @@ napi_value CreateToken(napi_env env, token_T* token) {
|
|
|
104
110
|
napi_value result;
|
|
105
111
|
napi_create_object(env, &result);
|
|
106
112
|
|
|
107
|
-
|
|
108
|
-
napi_value value = token->value ? CreateString(env, token->value) : nullptr;
|
|
109
|
-
if (value) {
|
|
110
|
-
napi_set_named_property(env, result, "value", value);
|
|
111
|
-
} else {
|
|
112
|
-
napi_value null_value;
|
|
113
|
-
napi_get_null(env, &null_value);
|
|
114
|
-
napi_set_named_property(env, result, "value", null_value);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Range
|
|
113
|
+
napi_value value = CreateStringFromHbString(env, token->value);
|
|
118
114
|
napi_value range = CreateRange(env, token->range);
|
|
119
|
-
napi_set_named_property(env, result, "range", range);
|
|
120
|
-
|
|
121
|
-
// Location
|
|
122
115
|
napi_value location = CreateLocation(env, token->location);
|
|
123
|
-
|
|
116
|
+
napi_value type = CreateStringFromHbString(env, token_type_to_string(token->type));
|
|
124
117
|
|
|
125
|
-
|
|
126
|
-
|
|
118
|
+
napi_set_named_property(env, result, "value", value);
|
|
119
|
+
napi_set_named_property(env, result, "range", range);
|
|
120
|
+
napi_set_named_property(env, result, "location", location);
|
|
127
121
|
napi_set_named_property(env, result, "type", type);
|
|
128
122
|
|
|
129
123
|
return result;
|
|
130
124
|
}
|
|
131
125
|
|
|
132
|
-
napi_value ReadFileToString(napi_env env, const char* file_path) {
|
|
133
|
-
char* content = herb_read_file(file_path);
|
|
134
|
-
if (!content) {
|
|
135
|
-
napi_throw_error(env, nullptr, "Failed to read file");
|
|
136
|
-
return nullptr;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
napi_value result = CreateString(env, content);
|
|
140
|
-
|
|
141
|
-
free(content);
|
|
142
|
-
|
|
143
|
-
return result;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
126
|
napi_value CreateLexResult(napi_env env, hb_array_T* tokens, napi_value source) {
|
|
147
127
|
napi_value result, tokens_array, errors_array, warnings_array;
|
|
148
128
|
|
|
@@ -151,10 +131,10 @@ napi_value CreateLexResult(napi_env env, hb_array_T* tokens, napi_value source)
|
|
|
151
131
|
napi_create_array(env, &errors_array);
|
|
152
132
|
napi_create_array(env, &warnings_array);
|
|
153
133
|
|
|
154
|
-
// Add tokens to array
|
|
155
134
|
if (tokens) {
|
|
156
135
|
for (size_t i = 0; i < hb_array_size(tokens); i++) {
|
|
157
136
|
token_T* token = (token_T*)hb_array_get(tokens, i);
|
|
137
|
+
|
|
158
138
|
if (token) {
|
|
159
139
|
napi_value token_obj = CreateToken(env, token);
|
|
160
140
|
napi_set_element(env, tokens_array, i, token_obj);
|
|
@@ -170,15 +150,15 @@ napi_value CreateLexResult(napi_env env, hb_array_T* tokens, napi_value source)
|
|
|
170
150
|
return result;
|
|
171
151
|
}
|
|
172
152
|
|
|
173
|
-
napi_value CreateParseResult(napi_env env, AST_DOCUMENT_NODE_T* root, napi_value source) {
|
|
153
|
+
napi_value CreateParseResult(napi_env env, AST_DOCUMENT_NODE_T* root, napi_value source, parser_options_T* options) {
|
|
174
154
|
napi_value result, errors_array, warnings_array;
|
|
175
155
|
|
|
176
156
|
napi_create_object(env, &result);
|
|
177
157
|
napi_create_array(env, &errors_array);
|
|
178
158
|
napi_create_array(env, &warnings_array);
|
|
179
159
|
|
|
180
|
-
// Convert the AST to a JavaScript object
|
|
181
160
|
napi_value ast_value;
|
|
161
|
+
|
|
182
162
|
if (root) {
|
|
183
163
|
ast_value = NodeFromCStruct(env, (AST_NODE_T*)root);
|
|
184
164
|
} else {
|
|
@@ -190,5 +170,28 @@ napi_value CreateParseResult(napi_env env, AST_DOCUMENT_NODE_T* root, napi_value
|
|
|
190
170
|
napi_set_named_property(env, result, "warnings", warnings_array);
|
|
191
171
|
napi_set_named_property(env, result, "errors", errors_array);
|
|
192
172
|
|
|
173
|
+
napi_value options_object;
|
|
174
|
+
napi_create_object(env, &options_object);
|
|
175
|
+
|
|
176
|
+
napi_value strict_value, track_whitespace_value, analyze_value, action_view_helpers_value, prism_program_value, prism_nodes_value, prism_nodes_deep_value;
|
|
177
|
+
|
|
178
|
+
napi_get_boolean(env, options->strict, &strict_value);
|
|
179
|
+
napi_get_boolean(env, options->track_whitespace, &track_whitespace_value);
|
|
180
|
+
napi_get_boolean(env, options->analyze, &analyze_value);
|
|
181
|
+
napi_get_boolean(env, options->action_view_helpers, &action_view_helpers_value);
|
|
182
|
+
napi_get_boolean(env, options->prism_program, &prism_program_value);
|
|
183
|
+
napi_get_boolean(env, options->prism_nodes, &prism_nodes_value);
|
|
184
|
+
napi_get_boolean(env, options->prism_nodes_deep, &prism_nodes_deep_value);
|
|
185
|
+
|
|
186
|
+
napi_set_named_property(env, options_object, "strict", strict_value);
|
|
187
|
+
napi_set_named_property(env, options_object, "track_whitespace", track_whitespace_value);
|
|
188
|
+
napi_set_named_property(env, options_object, "analyze", analyze_value);
|
|
189
|
+
napi_set_named_property(env, options_object, "action_view_helpers", action_view_helpers_value);
|
|
190
|
+
napi_set_named_property(env, options_object, "prism_program", prism_program_value);
|
|
191
|
+
napi_set_named_property(env, options_object, "prism_nodes", prism_nodes_value);
|
|
192
|
+
napi_set_named_property(env, options_object, "prism_nodes_deep", prism_nodes_deep_value);
|
|
193
|
+
|
|
194
|
+
napi_set_named_property(env, result, "options", options_object);
|
|
195
|
+
|
|
193
196
|
return result;
|
|
194
197
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
extern "C" {
|
|
7
7
|
#include "../extension/libherb/include/ast_nodes.h"
|
|
8
|
+
#include "../extension/libherb/include/herb.h"
|
|
8
9
|
#include "../extension/libherb/include/util/hb_array.h"
|
|
9
10
|
#include "../extension/libherb/include/util/hb_string.h"
|
|
10
11
|
}
|
|
@@ -12,9 +13,8 @@ extern "C" {
|
|
|
12
13
|
char* CheckString(napi_env env, napi_value value);
|
|
13
14
|
napi_value CreateString(napi_env env, const char* str);
|
|
14
15
|
napi_value CreateStringFromHbString(napi_env env, hb_string_T string);
|
|
15
|
-
napi_value ReadFileToString(napi_env env, const char* file_path);
|
|
16
16
|
napi_value CreateLexResult(napi_env env, hb_array_T* tokens, napi_value source);
|
|
17
|
-
napi_value CreateParseResult(napi_env env, AST_DOCUMENT_NODE_T* root, napi_value source);
|
|
17
|
+
napi_value CreateParseResult(napi_env env, AST_DOCUMENT_NODE_T* root, napi_value source, parser_options_T* options);
|
|
18
18
|
|
|
19
19
|
napi_value CreateLocation(napi_env env, location_T location);
|
|
20
20
|
napi_value CreateToken(napi_env env, token_T* token);
|
package/extension/herb.cpp
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
extern "C" {
|
|
2
|
-
#include "../extension/libherb/include/analyze.h"
|
|
3
2
|
#include "../extension/libherb/include/ast_nodes.h"
|
|
3
|
+
#include "../extension/libherb/include/extract.h"
|
|
4
4
|
#include "../extension/libherb/include/herb.h"
|
|
5
5
|
#include "../extension/libherb/include/location.h"
|
|
6
6
|
#include "../extension/libherb/include/range.h"
|
|
7
7
|
#include "../extension/libherb/include/token.h"
|
|
8
|
+
#include "../extension/libherb/include/util/hb_allocator.h"
|
|
8
9
|
#include "../extension/libherb/include/util/hb_array.h"
|
|
9
10
|
#include "../extension/libherb/include/util/hb_buffer.h"
|
|
10
11
|
}
|
|
@@ -31,34 +32,19 @@ napi_value Herb_lex(napi_env env, napi_callback_info info) {
|
|
|
31
32
|
char* string = CheckString(env, args[0]);
|
|
32
33
|
if (!string) { return nullptr; }
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
free(string);
|
|
39
|
-
|
|
40
|
-
return result;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
napi_value Herb_lex_file(napi_env env, napi_callback_info info) {
|
|
44
|
-
size_t argc = 1;
|
|
45
|
-
napi_value args[1];
|
|
46
|
-
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
|
47
|
-
|
|
48
|
-
if (argc < 1) {
|
|
49
|
-
napi_throw_error(env, nullptr, "Wrong number of arguments");
|
|
35
|
+
hb_allocator_T allocator;
|
|
36
|
+
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
|
|
37
|
+
free(string);
|
|
38
|
+
napi_throw_error(env, nullptr, "Failed to initialize allocator");
|
|
50
39
|
return nullptr;
|
|
51
40
|
}
|
|
52
41
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
hb_array_T* tokens = herb_lex_file(file_path);
|
|
57
|
-
napi_value source_value = ReadFileToString(env, file_path);
|
|
58
|
-
napi_value result = CreateLexResult(env, tokens, source_value);
|
|
42
|
+
hb_array_T* tokens = herb_lex(string, &allocator);
|
|
43
|
+
napi_value result = CreateLexResult(env, tokens, args[0]);
|
|
59
44
|
|
|
60
|
-
herb_free_tokens(&tokens);
|
|
61
|
-
|
|
45
|
+
herb_free_tokens(&tokens, &allocator);
|
|
46
|
+
hb_allocator_destroy(&allocator);
|
|
47
|
+
free(string);
|
|
62
48
|
|
|
63
49
|
return result;
|
|
64
50
|
}
|
|
@@ -76,8 +62,7 @@ napi_value Herb_parse(napi_env env, napi_callback_info info) {
|
|
|
76
62
|
char* string = CheckString(env, args[0]);
|
|
77
63
|
if (!string) { return nullptr; }
|
|
78
64
|
|
|
79
|
-
parser_options_T
|
|
80
|
-
parser_options_T opts = {0};
|
|
65
|
+
parser_options_T parser_options = HERB_DEFAULT_PARSER_OPTIONS;
|
|
81
66
|
|
|
82
67
|
if (argc >= 2) {
|
|
83
68
|
napi_valuetype valuetype;
|
|
@@ -85,35 +70,110 @@ napi_value Herb_parse(napi_env env, napi_callback_info info) {
|
|
|
85
70
|
|
|
86
71
|
if (valuetype == napi_object) {
|
|
87
72
|
napi_value track_whitespace_prop;
|
|
88
|
-
bool
|
|
89
|
-
napi_has_named_property(env, args[1], "track_whitespace", &
|
|
73
|
+
bool has_track_whitespace_prop;
|
|
74
|
+
napi_has_named_property(env, args[1], "track_whitespace", &has_track_whitespace_prop);
|
|
90
75
|
|
|
91
|
-
if (
|
|
76
|
+
if (has_track_whitespace_prop) {
|
|
92
77
|
napi_get_named_property(env, args[1], "track_whitespace", &track_whitespace_prop);
|
|
93
78
|
bool track_whitespace_value;
|
|
94
79
|
napi_get_value_bool(env, track_whitespace_prop, &track_whitespace_value);
|
|
95
80
|
|
|
96
81
|
if (track_whitespace_value) {
|
|
97
|
-
|
|
98
|
-
|
|
82
|
+
parser_options.track_whitespace = true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
napi_value analyze_prop;
|
|
87
|
+
bool has_analyze_prop;
|
|
88
|
+
napi_has_named_property(env, args[1], "analyze", &has_analyze_prop);
|
|
89
|
+
|
|
90
|
+
if (has_analyze_prop) {
|
|
91
|
+
napi_get_named_property(env, args[1], "analyze", &analyze_prop);
|
|
92
|
+
bool analyze_value;
|
|
93
|
+
napi_get_value_bool(env, analyze_prop, &analyze_value);
|
|
94
|
+
|
|
95
|
+
if (!analyze_value) {
|
|
96
|
+
parser_options.analyze = false;
|
|
99
97
|
}
|
|
100
98
|
}
|
|
99
|
+
|
|
100
|
+
napi_value strict_prop;
|
|
101
|
+
bool has_strict_prop;
|
|
102
|
+
napi_has_named_property(env, args[1], "strict", &has_strict_prop);
|
|
103
|
+
|
|
104
|
+
if (has_strict_prop) {
|
|
105
|
+
napi_get_named_property(env, args[1], "strict", &strict_prop);
|
|
106
|
+
bool strict_value;
|
|
107
|
+
napi_get_value_bool(env, strict_prop, &strict_value);
|
|
108
|
+
parser_options.strict = strict_value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
napi_value action_view_helpers_prop;
|
|
112
|
+
bool has_action_view_helpers_prop;
|
|
113
|
+
napi_has_named_property(env, args[1], "action_view_helpers", &has_action_view_helpers_prop);
|
|
114
|
+
|
|
115
|
+
if (has_action_view_helpers_prop) {
|
|
116
|
+
napi_get_named_property(env, args[1], "action_view_helpers", &action_view_helpers_prop);
|
|
117
|
+
bool action_view_helpers_value;
|
|
118
|
+
napi_get_value_bool(env, action_view_helpers_prop, &action_view_helpers_value);
|
|
119
|
+
parser_options.action_view_helpers = action_view_helpers_value;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
napi_value prism_nodes_prop;
|
|
123
|
+
bool has_prism_nodes_prop;
|
|
124
|
+
napi_has_named_property(env, args[1], "prism_nodes", &has_prism_nodes_prop);
|
|
125
|
+
|
|
126
|
+
if (has_prism_nodes_prop) {
|
|
127
|
+
napi_get_named_property(env, args[1], "prism_nodes", &prism_nodes_prop);
|
|
128
|
+
bool prism_nodes_value;
|
|
129
|
+
napi_get_value_bool(env, prism_nodes_prop, &prism_nodes_value);
|
|
130
|
+
parser_options.prism_nodes = prism_nodes_value;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
napi_value prism_nodes_deep_prop;
|
|
134
|
+
bool has_prism_nodes_deep_prop;
|
|
135
|
+
napi_has_named_property(env, args[1], "prism_nodes_deep", &has_prism_nodes_deep_prop);
|
|
136
|
+
|
|
137
|
+
if (has_prism_nodes_deep_prop) {
|
|
138
|
+
napi_get_named_property(env, args[1], "prism_nodes_deep", &prism_nodes_deep_prop);
|
|
139
|
+
bool prism_nodes_deep_value;
|
|
140
|
+
napi_get_value_bool(env, prism_nodes_deep_prop, &prism_nodes_deep_value);
|
|
141
|
+
parser_options.prism_nodes_deep = prism_nodes_deep_value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
napi_value prism_program_prop;
|
|
145
|
+
bool has_prism_program_prop;
|
|
146
|
+
napi_has_named_property(env, args[1], "prism_program", &has_prism_program_prop);
|
|
147
|
+
|
|
148
|
+
if (has_prism_program_prop) {
|
|
149
|
+
napi_get_named_property(env, args[1], "prism_program", &prism_program_prop);
|
|
150
|
+
bool prism_program_value;
|
|
151
|
+
napi_get_value_bool(env, prism_program_prop, &prism_program_value);
|
|
152
|
+
parser_options.prism_program = prism_program_value;
|
|
153
|
+
}
|
|
101
154
|
}
|
|
102
155
|
}
|
|
103
156
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
157
|
+
hb_allocator_T allocator;
|
|
158
|
+
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
|
|
159
|
+
free(string);
|
|
160
|
+
napi_throw_error(env, nullptr, "Failed to initialize allocator");
|
|
161
|
+
return nullptr;
|
|
162
|
+
}
|
|
107
163
|
|
|
108
|
-
|
|
164
|
+
AST_DOCUMENT_NODE_T* root = herb_parse(string, &parser_options, &allocator);
|
|
165
|
+
napi_value result = CreateParseResult(env, root, args[0], &parser_options);
|
|
166
|
+
|
|
167
|
+
ast_node_free((AST_NODE_T *) root, &allocator);
|
|
168
|
+
hb_allocator_destroy(&allocator);
|
|
109
169
|
free(string);
|
|
110
170
|
|
|
111
171
|
return result;
|
|
112
172
|
}
|
|
113
173
|
|
|
114
|
-
napi_value
|
|
115
|
-
size_t argc =
|
|
116
|
-
napi_value args[
|
|
174
|
+
napi_value Herb_extract_ruby(napi_env env, napi_callback_info info) {
|
|
175
|
+
size_t argc = 2;
|
|
176
|
+
napi_value args[2];
|
|
117
177
|
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
|
118
178
|
|
|
119
179
|
if (argc < 1) {
|
|
@@ -121,28 +181,72 @@ napi_value Herb_parse_file(napi_env env, napi_callback_info info) {
|
|
|
121
181
|
return nullptr;
|
|
122
182
|
}
|
|
123
183
|
|
|
124
|
-
char*
|
|
125
|
-
if (!
|
|
184
|
+
char* string = CheckString(env, args[0]);
|
|
185
|
+
if (!string) { return nullptr; }
|
|
126
186
|
|
|
127
|
-
|
|
187
|
+
hb_allocator_T allocator;
|
|
188
|
+
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
|
|
189
|
+
free(string);
|
|
190
|
+
napi_throw_error(env, nullptr, "Failed to initialize allocator");
|
|
191
|
+
return nullptr;
|
|
192
|
+
}
|
|
128
193
|
|
|
129
|
-
|
|
130
|
-
if (!string) {
|
|
131
|
-
|
|
194
|
+
hb_buffer_T output;
|
|
195
|
+
if (!hb_buffer_init(&output, strlen(string), &allocator)) {
|
|
196
|
+
hb_allocator_destroy(&allocator);
|
|
197
|
+
free(string);
|
|
198
|
+
napi_throw_error(env, nullptr, "Failed to initialize buffer");
|
|
132
199
|
return nullptr;
|
|
133
200
|
}
|
|
134
201
|
|
|
135
|
-
|
|
136
|
-
napi_value result = CreateParseResult(env, root, source_value);
|
|
202
|
+
herb_extract_ruby_options_T extract_options = HERB_EXTRACT_RUBY_DEFAULT_OPTIONS;
|
|
137
203
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
204
|
+
if (argc >= 2) {
|
|
205
|
+
napi_valuetype valuetype;
|
|
206
|
+
napi_typeof(env, args[1], &valuetype);
|
|
207
|
+
|
|
208
|
+
if (valuetype == napi_object) {
|
|
209
|
+
napi_value prop;
|
|
210
|
+
bool has_prop;
|
|
211
|
+
|
|
212
|
+
napi_has_named_property(env, args[1], "semicolons", &has_prop);
|
|
213
|
+
if (has_prop) {
|
|
214
|
+
napi_get_named_property(env, args[1], "semicolons", &prop);
|
|
215
|
+
bool value;
|
|
216
|
+
napi_get_value_bool(env, prop, &value);
|
|
217
|
+
extract_options.semicolons = value;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
napi_has_named_property(env, args[1], "comments", &has_prop);
|
|
221
|
+
if (has_prop) {
|
|
222
|
+
napi_get_named_property(env, args[1], "comments", &prop);
|
|
223
|
+
bool value;
|
|
224
|
+
napi_get_value_bool(env, prop, &value);
|
|
225
|
+
extract_options.comments = value;
|
|
226
|
+
}
|
|
141
227
|
|
|
228
|
+
napi_has_named_property(env, args[1], "preserve_positions", &has_prop);
|
|
229
|
+
if (has_prop) {
|
|
230
|
+
napi_get_named_property(env, args[1], "preserve_positions", &prop);
|
|
231
|
+
bool value;
|
|
232
|
+
napi_get_value_bool(env, prop, &value);
|
|
233
|
+
extract_options.preserve_positions = value;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
herb_extract_ruby_to_buffer_with_options(string, &output, &extract_options, &allocator);
|
|
239
|
+
|
|
240
|
+
napi_value result;
|
|
241
|
+
napi_create_string_utf8(env, output.value, NAPI_AUTO_LENGTH, &result);
|
|
242
|
+
|
|
243
|
+
hb_buffer_free(&output);
|
|
244
|
+
hb_allocator_destroy(&allocator);
|
|
245
|
+
free(string);
|
|
142
246
|
return result;
|
|
143
247
|
}
|
|
144
248
|
|
|
145
|
-
napi_value
|
|
249
|
+
napi_value Herb_extract_html(napi_env env, napi_callback_info info) {
|
|
146
250
|
size_t argc = 1;
|
|
147
251
|
napi_value args[1];
|
|
148
252
|
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
|
@@ -155,24 +259,33 @@ napi_value Herb_extract_ruby(napi_env env, napi_callback_info info) {
|
|
|
155
259
|
char* string = CheckString(env, args[0]);
|
|
156
260
|
if (!string) { return nullptr; }
|
|
157
261
|
|
|
262
|
+
hb_allocator_T allocator;
|
|
263
|
+
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
|
|
264
|
+
free(string);
|
|
265
|
+
napi_throw_error(env, nullptr, "Failed to initialize allocator");
|
|
266
|
+
return nullptr;
|
|
267
|
+
}
|
|
268
|
+
|
|
158
269
|
hb_buffer_T output;
|
|
159
|
-
if (!hb_buffer_init(&output, strlen(string))) {
|
|
270
|
+
if (!hb_buffer_init(&output, strlen(string), &allocator)) {
|
|
271
|
+
hb_allocator_destroy(&allocator);
|
|
160
272
|
free(string);
|
|
161
273
|
napi_throw_error(env, nullptr, "Failed to initialize buffer");
|
|
162
274
|
return nullptr;
|
|
163
275
|
}
|
|
164
276
|
|
|
165
|
-
|
|
277
|
+
herb_extract_html_to_buffer(string, &output, &allocator);
|
|
166
278
|
|
|
167
279
|
napi_value result;
|
|
168
280
|
napi_create_string_utf8(env, output.value, NAPI_AUTO_LENGTH, &result);
|
|
169
281
|
|
|
170
|
-
|
|
282
|
+
hb_buffer_free(&output);
|
|
283
|
+
hb_allocator_destroy(&allocator);
|
|
171
284
|
free(string);
|
|
172
285
|
return result;
|
|
173
286
|
}
|
|
174
287
|
|
|
175
|
-
napi_value
|
|
288
|
+
napi_value Herb_parse_ruby(napi_env env, napi_callback_info info) {
|
|
176
289
|
size_t argc = 1;
|
|
177
290
|
napi_value args[1];
|
|
178
291
|
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
|
@@ -185,20 +298,27 @@ napi_value Herb_extract_html(napi_env env, napi_callback_info info) {
|
|
|
185
298
|
char* string = CheckString(env, args[0]);
|
|
186
299
|
if (!string) { return nullptr; }
|
|
187
300
|
|
|
188
|
-
|
|
189
|
-
|
|
301
|
+
herb_ruby_parse_result_T* parse_result = herb_parse_ruby(string, strlen(string));
|
|
302
|
+
|
|
303
|
+
if (!parse_result) {
|
|
190
304
|
free(string);
|
|
191
|
-
napi_throw_error(env, nullptr, "Failed to initialize buffer");
|
|
192
305
|
return nullptr;
|
|
193
306
|
}
|
|
194
307
|
|
|
195
|
-
|
|
308
|
+
pm_buffer_t buffer = { 0 };
|
|
309
|
+
pm_serialize(&parse_result->parser, parse_result->root, &buffer);
|
|
196
310
|
|
|
197
|
-
napi_value result;
|
|
198
|
-
napi_create_string_utf8(env, output.value, NAPI_AUTO_LENGTH, &result);
|
|
311
|
+
napi_value result = nullptr;
|
|
199
312
|
|
|
200
|
-
|
|
313
|
+
if (buffer.length > 0) {
|
|
314
|
+
void* data;
|
|
315
|
+
napi_create_buffer_copy(env, buffer.length, buffer.value, &data, &result);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
pm_buffer_free(&buffer);
|
|
319
|
+
herb_free_ruby_parse_result(parse_result);
|
|
201
320
|
free(string);
|
|
321
|
+
|
|
202
322
|
return result;
|
|
203
323
|
}
|
|
204
324
|
|
|
@@ -219,11 +339,10 @@ napi_value Init(napi_env env, napi_value exports) {
|
|
|
219
339
|
napi_property_descriptor descriptors[] = {
|
|
220
340
|
{ "parse", nullptr, Herb_parse, nullptr, nullptr, nullptr, napi_default, nullptr },
|
|
221
341
|
{ "lex", nullptr, Herb_lex, nullptr, nullptr, nullptr, napi_default, nullptr },
|
|
222
|
-
{ "parseFile", nullptr, Herb_parse_file, nullptr, nullptr, nullptr, napi_default, nullptr },
|
|
223
|
-
{ "lexFile", nullptr, Herb_lex_file, nullptr, nullptr, nullptr, napi_default, nullptr },
|
|
224
342
|
{ "extractRuby", nullptr, Herb_extract_ruby, nullptr, nullptr, nullptr, napi_default, nullptr },
|
|
225
343
|
{ "extractHTML", nullptr, Herb_extract_html, nullptr, nullptr, nullptr, napi_default, nullptr },
|
|
226
344
|
{ "version", nullptr, Herb_version, nullptr, nullptr, nullptr, napi_default, nullptr },
|
|
345
|
+
{ "parseRuby", nullptr, Herb_parse_ruby, nullptr, nullptr, nullptr, napi_default, nullptr },
|
|
227
346
|
};
|
|
228
347
|
|
|
229
348
|
napi_define_properties(env, exports, sizeof(descriptors) / sizeof(descriptors[0]), descriptors);
|