@herb-tools/node 0.7.4 → 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.
- package/binding.gyp +8 -5
- package/dist/herb-node.esm.js +6 -6
- package/dist/herb-node.esm.js.map +1 -1
- package/extension/error_helpers.cpp +67 -9
- package/extension/error_helpers.h +4 -2
- package/extension/extension_helpers.cpp +20 -31
- package/extension/extension_helpers.h +7 -5
- package/extension/herb.cpp +10 -42
- package/extension/libherb/analyze.c +461 -249
- package/extension/libherb/analyze.h +10 -2
- package/extension/libherb/analyze_helpers.c +5 -0
- package/extension/libherb/analyze_helpers.h +3 -0
- package/extension/libherb/analyze_missing_end.c +147 -0
- package/extension/libherb/analyze_transform.c +196 -0
- package/extension/libherb/analyzed_ruby.c +23 -2
- package/extension/libherb/analyzed_ruby.h +4 -2
- package/extension/libherb/ast_node.c +14 -17
- package/extension/libherb/ast_node.h +4 -4
- package/extension/libherb/ast_nodes.c +180 -182
- package/extension/libherb/ast_nodes.h +69 -68
- package/extension/libherb/ast_pretty_print.c +233 -233
- package/extension/libherb/ast_pretty_print.h +3 -3
- package/extension/libherb/element_source.c +7 -6
- package/extension/libherb/element_source.h +3 -1
- package/extension/libherb/errors.c +273 -153
- package/extension/libherb/errors.h +43 -27
- package/extension/libherb/extract.c +92 -34
- package/extension/libherb/extract.h +4 -4
- package/extension/libherb/herb.c +37 -49
- package/extension/libherb/herb.h +6 -7
- package/extension/libherb/html_util.c +34 -96
- package/extension/libherb/html_util.h +4 -5
- package/extension/libherb/include/analyze.h +10 -2
- package/extension/libherb/include/analyze_helpers.h +3 -0
- package/extension/libherb/include/analyzed_ruby.h +4 -2
- package/extension/libherb/include/ast_node.h +4 -4
- package/extension/libherb/include/ast_nodes.h +69 -68
- package/extension/libherb/include/ast_pretty_print.h +3 -3
- package/extension/libherb/include/element_source.h +3 -1
- package/extension/libherb/include/errors.h +43 -27
- package/extension/libherb/include/extract.h +4 -4
- package/extension/libherb/include/herb.h +6 -7
- package/extension/libherb/include/html_util.h +4 -5
- package/extension/libherb/include/lexer.h +1 -3
- package/extension/libherb/include/lexer_peek_helpers.h +21 -19
- package/extension/libherb/include/lexer_struct.h +12 -10
- package/extension/libherb/include/location.h +10 -13
- package/extension/libherb/include/macros.h +4 -0
- package/extension/libherb/include/parser.h +12 -6
- package/extension/libherb/include/parser_helpers.h +26 -16
- package/extension/libherb/include/position.h +3 -14
- package/extension/libherb/include/pretty_print.h +38 -28
- package/extension/libherb/include/prism_helpers.h +1 -1
- package/extension/libherb/include/range.h +4 -13
- package/extension/libherb/include/token.h +5 -11
- package/extension/libherb/include/token_struct.h +2 -2
- package/extension/libherb/include/utf8.h +3 -2
- package/extension/libherb/include/util/hb_arena.h +31 -0
- package/extension/libherb/include/util/hb_arena_debug.h +8 -0
- package/extension/libherb/include/util/hb_array.h +33 -0
- package/extension/libherb/include/util/hb_buffer.h +34 -0
- package/extension/libherb/include/util/hb_string.h +29 -0
- package/extension/libherb/include/util/hb_system.h +9 -0
- package/extension/libherb/include/util.h +3 -14
- package/extension/libherb/include/version.h +1 -1
- package/extension/libherb/include/visitor.h +1 -1
- package/extension/libherb/io.c +7 -4
- package/extension/libherb/lexer.c +62 -88
- package/extension/libherb/lexer.h +1 -3
- package/extension/libherb/lexer_peek_helpers.c +42 -38
- package/extension/libherb/lexer_peek_helpers.h +21 -19
- package/extension/libherb/lexer_struct.h +12 -10
- package/extension/libherb/location.c +9 -37
- package/extension/libherb/location.h +10 -13
- package/extension/libherb/macros.h +4 -0
- package/extension/libherb/main.c +19 -23
- package/extension/libherb/parser.c +373 -313
- package/extension/libherb/parser.h +12 -6
- package/extension/libherb/parser_helpers.c +60 -54
- package/extension/libherb/parser_helpers.h +26 -16
- package/extension/libherb/parser_match_tags.c +316 -0
- package/extension/libherb/position.h +3 -14
- package/extension/libherb/pretty_print.c +88 -117
- package/extension/libherb/pretty_print.h +38 -28
- package/extension/libherb/prism_helpers.c +7 -7
- package/extension/libherb/prism_helpers.h +1 -1
- package/extension/libherb/range.c +2 -35
- package/extension/libherb/range.h +4 -13
- package/extension/libherb/token.c +36 -87
- package/extension/libherb/token.h +5 -11
- package/extension/libherb/token_struct.h +2 -2
- package/extension/libherb/utf8.c +4 -4
- package/extension/libherb/utf8.h +3 -2
- package/extension/libherb/util/hb_arena.c +179 -0
- package/extension/libherb/util/hb_arena.h +31 -0
- package/extension/libherb/util/hb_arena_debug.c +237 -0
- package/extension/libherb/util/hb_arena_debug.h +8 -0
- package/extension/libherb/{array.c → util/hb_array.c} +26 -27
- package/extension/libherb/util/hb_array.h +33 -0
- package/extension/libherb/util/hb_buffer.c +203 -0
- package/extension/libherb/util/hb_buffer.h +34 -0
- package/extension/libherb/util/hb_string.c +85 -0
- package/extension/libherb/util/hb_string.h +29 -0
- package/extension/libherb/util/hb_system.c +30 -0
- package/extension/libherb/util/hb_system.h +9 -0
- package/extension/libherb/util.c +29 -99
- package/extension/libherb/util.h +3 -14
- package/extension/libherb/version.h +1 -1
- package/extension/libherb/visitor.c +55 -55
- package/extension/libherb/visitor.h +1 -1
- package/extension/nodes.cpp +40 -40
- package/extension/nodes.h +2 -2
- package/extension/prism/include/prism/ast.h +31 -1
- package/extension/prism/include/prism/diagnostic.h +1 -0
- package/extension/prism/include/prism/version.h +3 -3
- package/extension/prism/src/diagnostic.c +3 -1
- package/extension/prism/src/prism.c +130 -71
- package/extension/prism/src/util/pm_string.c +6 -8
- package/package.json +3 -3
- package/extension/libherb/array.h +0 -33
- package/extension/libherb/buffer.c +0 -232
- package/extension/libherb/buffer.h +0 -39
- package/extension/libherb/include/array.h +0 -33
- package/extension/libherb/include/buffer.h +0 -39
- package/extension/libherb/include/json.h +0 -28
- package/extension/libherb/include/memory.h +0 -12
- package/extension/libherb/json.c +0 -205
- package/extension/libherb/json.h +0 -28
- package/extension/libherb/memory.c +0 -53
- package/extension/libherb/memory.h +0 -12
- package/extension/libherb/position.c +0 -33
|
@@ -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.
|
|
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,11 +21,13 @@ 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 {
|
|
27
29
|
error_type_T type;
|
|
28
|
-
location_T
|
|
30
|
+
location_T location;
|
|
29
31
|
char* message;
|
|
30
32
|
} ERROR_T;
|
|
31
33
|
|
|
@@ -84,26 +86,40 @@ typedef struct {
|
|
|
84
86
|
const char* level;
|
|
85
87
|
} RUBY_PARSE_ERROR_T;
|
|
86
88
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
void
|
|
99
|
-
|
|
100
|
-
void
|
|
101
|
-
|
|
102
|
-
void
|
|
103
|
-
|
|
104
|
-
void
|
|
105
|
-
|
|
106
|
-
void
|
|
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
|
+
|
|
99
|
+
UNEXPECTED_ERROR_T* unexpected_error_init(const char* description, const char* expected, const char* found, position_T start, position_T end);
|
|
100
|
+
void append_unexpected_error(const char* description, const char* expected, const char* found, position_T start, position_T end, hb_array_T* errors);
|
|
101
|
+
UNEXPECTED_TOKEN_ERROR_T* unexpected_token_error_init(token_type_T expected_type, token_T* found, position_T start, position_T end);
|
|
102
|
+
void append_unexpected_token_error(token_type_T expected_type, token_T* found, position_T start, position_T end, hb_array_T* errors);
|
|
103
|
+
MISSING_OPENING_TAG_ERROR_T* missing_opening_tag_error_init(token_T* closing_tag, position_T start, position_T end);
|
|
104
|
+
void append_missing_opening_tag_error(token_T* closing_tag, position_T start, position_T end, hb_array_T* errors);
|
|
105
|
+
MISSING_CLOSING_TAG_ERROR_T* missing_closing_tag_error_init(token_T* opening_tag, position_T start, position_T end);
|
|
106
|
+
void append_missing_closing_tag_error(token_T* opening_tag, position_T start, position_T end, hb_array_T* errors);
|
|
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);
|
|
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);
|
|
109
|
+
QUOTES_MISMATCH_ERROR_T* quotes_mismatch_error_init(token_T* opening_quote, token_T* closing_quote, position_T start, position_T end);
|
|
110
|
+
void append_quotes_mismatch_error(token_T* opening_quote, token_T* closing_quote, position_T start, position_T end, hb_array_T* errors);
|
|
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);
|
|
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);
|
|
113
|
+
UNCLOSED_ELEMENT_ERROR_T* unclosed_element_error_init(token_T* opening_tag, position_T start, position_T end);
|
|
114
|
+
void append_unclosed_element_error(token_T* opening_tag, position_T start, position_T end, hb_array_T* errors);
|
|
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);
|
|
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);
|
|
121
|
+
|
|
122
|
+
void error_init(ERROR_T* error, error_type_T type, position_T start, position_T end);
|
|
107
123
|
|
|
108
124
|
size_t error_sizeof(void);
|
|
109
125
|
error_type_T error_type(ERROR_T* error);
|
|
@@ -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,
|
|
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,
|
|
122
|
-
|
|
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,
|
|
10
|
-
|
|
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 <
|
|
14
|
-
const token_T* token =
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
45
|
-
|
|
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
|
-
|
|
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,
|
|
59
|
-
|
|
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 <
|
|
63
|
-
const token_T* token =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
136
|
+
hb_buffer_append(output, token->value);
|
|
83
137
|
} else {
|
|
84
|
-
|
|
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
|
-
|
|
147
|
+
hb_buffer_append_whitespace(output, range_length(token->range));
|
|
94
148
|
break;
|
|
95
149
|
}
|
|
96
150
|
|
|
97
151
|
default: {
|
|
98
|
-
|
|
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,
|
|
107
|
-
|
|
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 <
|
|
110
|
-
const token_T* token =
|
|
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:
|
|
116
|
-
default:
|
|
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
|
-
|
|
125
|
-
|
|
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
|
-
|
|
134
|
-
|
|
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 "
|
|
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,
|
|
12
|
-
void herb_extract_html_to_buffer(const char* source,
|
|
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,
|
|
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);
|
package/extension/libherb/herb.c
CHANGED
|
@@ -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
|
-
|
|
15
|
-
lexer_T
|
|
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
|
-
|
|
18
|
+
hb_array_T* tokens = hb_array_init(128);
|
|
18
19
|
|
|
19
|
-
while ((token = lexer_next_token(lexer))->type != TOKEN_EOF) {
|
|
20
|
-
|
|
20
|
+
while ((token = lexer_next_token(&lexer))->type != TOKEN_EOF) {
|
|
21
|
+
hb_array_append(tokens, token);
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
|
|
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
|
-
|
|
32
|
-
|
|
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
|
-
|
|
38
|
+
if (options != NULL) { parser_options = *options; }
|
|
35
39
|
|
|
36
|
-
|
|
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
|
-
|
|
49
|
+
hb_array_T* herb_lex_file(const char* path) {
|
|
42
50
|
char* source = herb_read_file(path);
|
|
43
|
-
|
|
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,
|
|
51
|
-
|
|
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
|
-
|
|
57
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
74
|
+
void herb_free_tokens(hb_array_T** tokens) {
|
|
87
75
|
if (!tokens || !*tokens) { return; }
|
|
88
76
|
|
|
89
|
-
for (size_t i = 0; i <
|
|
90
|
-
token_T* token =
|
|
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
|
-
|
|
82
|
+
hb_array_free(tokens);
|
|
95
83
|
}
|
|
96
84
|
|
|
97
85
|
const char* herb_version(void) {
|
package/extension/libherb/herb.h
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
20
|
-
|
|
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(
|
|
26
|
+
void herb_free_tokens(hb_array_T** tokens);
|
|
28
27
|
|
|
29
28
|
#ifdef __cplusplus
|
|
30
29
|
}
|