herb 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.
- checksums.yaml +4 -4
- data/Makefile +8 -5
- data/config.yml +26 -6
- data/ext/herb/error_helpers.c +57 -3
- data/ext/herb/error_helpers.h +1 -1
- data/ext/herb/extconf.rb +1 -0
- data/ext/herb/extension.c +10 -24
- data/ext/herb/extension_helpers.c +3 -3
- data/ext/herb/extension_helpers.h +1 -1
- data/ext/herb/nodes.c +72 -37
- data/herb.gemspec +0 -2
- data/lib/herb/ast/helpers.rb +11 -0
- data/lib/herb/ast/node.rb +15 -6
- data/lib/herb/ast/nodes.rb +609 -392
- data/lib/herb/cli.rb +31 -0
- data/lib/herb/colors.rb +82 -0
- data/lib/herb/engine/compiler.rb +140 -14
- data/lib/herb/engine/debug_visitor.rb +1 -5
- data/lib/herb/engine/parser_error_overlay.rb +1 -1
- data/lib/herb/engine.rb +8 -14
- data/lib/herb/errors.rb +166 -56
- data/lib/herb/location.rb +2 -2
- data/lib/herb/project.rb +86 -21
- data/lib/herb/token.rb +14 -2
- data/lib/herb/version.rb +1 -1
- data/lib/herb.rb +1 -0
- data/sig/herb/ast/helpers.rbs +3 -0
- data/sig/herb/ast/node.rbs +12 -5
- data/sig/herb/ast/nodes.rbs +124 -62
- data/sig/herb/colors.rbs +35 -0
- data/sig/herb/engine/compiler.rbs +23 -1
- data/sig/herb/errors.rbs +74 -20
- data/sig/herb/token.rbs +8 -0
- data/sig/herb_c_extension.rbs +1 -1
- data/sig/serialized_ast_errors.rbs +8 -0
- data/src/analyze.c +420 -171
- data/src/analyze_helpers.c +5 -0
- data/src/analyze_missing_end.c +147 -0
- data/src/analyze_transform.c +196 -0
- data/src/analyzed_ruby.c +23 -2
- data/src/ast_node.c +5 -5
- data/src/ast_nodes.c +179 -179
- data/src/ast_pretty_print.c +232 -232
- data/src/element_source.c +7 -6
- data/src/errors.c +246 -126
- data/src/extract.c +92 -34
- data/src/herb.c +37 -49
- data/src/html_util.c +34 -96
- data/src/include/analyze.h +10 -2
- data/src/include/analyze_helpers.h +3 -0
- data/src/include/analyzed_ruby.h +4 -2
- data/src/include/ast_node.h +2 -2
- data/src/include/ast_nodes.h +67 -66
- data/src/include/ast_pretty_print.h +2 -2
- data/src/include/element_source.h +3 -1
- data/src/include/errors.h +30 -14
- data/src/include/extract.h +4 -4
- data/src/include/herb.h +6 -7
- data/src/include/html_util.h +4 -5
- data/src/include/lexer.h +1 -3
- data/src/include/lexer_peek_helpers.h +14 -14
- data/src/include/lexer_struct.h +3 -2
- data/src/include/macros.h +4 -0
- data/src/include/parser.h +12 -6
- data/src/include/parser_helpers.h +25 -15
- data/src/include/pretty_print.h +38 -28
- data/src/include/token.h +5 -8
- data/src/include/utf8.h +3 -2
- data/src/include/util/hb_arena.h +31 -0
- data/src/include/util/hb_arena_debug.h +8 -0
- data/src/include/util/hb_array.h +33 -0
- data/src/include/util/hb_buffer.h +34 -0
- data/src/include/util/hb_string.h +29 -0
- data/src/include/util/hb_system.h +9 -0
- data/src/include/util.h +3 -14
- data/src/include/version.h +1 -1
- data/src/include/visitor.h +1 -1
- data/src/io.c +7 -4
- data/src/lexer.c +61 -88
- data/src/lexer_peek_helpers.c +35 -37
- data/src/main.c +19 -23
- data/src/parser.c +282 -201
- data/src/parser_helpers.c +46 -40
- data/src/parser_match_tags.c +316 -0
- data/src/pretty_print.c +82 -106
- data/src/token.c +18 -65
- data/src/utf8.c +4 -4
- data/src/util/hb_arena.c +179 -0
- data/src/util/hb_arena_debug.c +237 -0
- data/src/{array.c → util/hb_array.c} +26 -27
- data/src/util/hb_buffer.c +203 -0
- data/src/util/hb_string.c +85 -0
- data/src/util/hb_system.c +30 -0
- data/src/util.c +29 -99
- data/src/visitor.c +54 -54
- data/templates/ext/herb/error_helpers.c.erb +3 -3
- data/templates/ext/herb/error_helpers.h.erb +1 -1
- data/templates/ext/herb/nodes.c.erb +11 -6
- data/templates/java/error_helpers.c.erb +75 -0
- data/templates/java/error_helpers.h.erb +20 -0
- data/templates/java/nodes.c.erb +97 -0
- data/templates/java/nodes.h.erb +23 -0
- data/templates/java/org/herb/ast/Errors.java.erb +121 -0
- data/templates/java/org/herb/ast/NodeVisitor.java.erb +14 -0
- data/templates/java/org/herb/ast/Nodes.java.erb +220 -0
- data/templates/java/org/herb/ast/Visitor.java.erb +56 -0
- data/templates/javascript/packages/node/extension/error_helpers.cpp.erb +8 -8
- data/templates/javascript/packages/node/extension/error_helpers.h.erb +1 -1
- data/templates/javascript/packages/node/extension/nodes.cpp.erb +9 -9
- data/templates/javascript/packages/node/extension/nodes.h.erb +1 -1
- data/templates/lib/herb/ast/nodes.rb.erb +28 -16
- data/templates/lib/herb/errors.rb.erb +17 -12
- data/templates/rust/src/ast/nodes.rs.erb +220 -0
- data/templates/rust/src/errors.rs.erb +216 -0
- data/templates/rust/src/nodes.rs.erb +374 -0
- data/templates/src/analyze_missing_end.c.erb +36 -0
- data/templates/src/analyze_transform.c.erb +24 -0
- data/templates/src/ast_nodes.c.erb +14 -14
- data/templates/src/ast_pretty_print.c.erb +36 -36
- data/templates/src/errors.c.erb +31 -31
- data/templates/src/include/ast_nodes.h.erb +10 -9
- data/templates/src/include/ast_pretty_print.h.erb +2 -2
- data/templates/src/include/errors.h.erb +6 -6
- data/templates/src/parser_match_tags.c.erb +38 -0
- data/templates/src/visitor.c.erb +4 -4
- data/templates/template.rb +22 -3
- data/templates/wasm/error_helpers.cpp.erb +9 -9
- data/templates/wasm/error_helpers.h.erb +1 -1
- data/templates/wasm/nodes.cpp.erb +9 -9
- data/templates/wasm/nodes.h.erb +1 -1
- data/vendor/prism/Rakefile +4 -1
- data/vendor/prism/config.yml +2 -1
- data/vendor/prism/include/prism/ast.h +31 -1
- data/vendor/prism/include/prism/diagnostic.h +1 -0
- data/vendor/prism/include/prism/version.h +3 -3
- data/vendor/prism/src/diagnostic.c +3 -1
- data/vendor/prism/src/prism.c +130 -71
- data/vendor/prism/src/util/pm_string.c +6 -8
- data/vendor/prism/templates/include/prism/ast.h.erb +2 -0
- data/vendor/prism/templates/java/org/prism/Loader.java.erb +2 -2
- data/vendor/prism/templates/javascript/src/deserialize.js.erb +2 -2
- data/vendor/prism/templates/lib/prism/serialize.rb.erb +2 -2
- data/vendor/prism/templates/sig/prism.rbs.erb +4 -0
- data/vendor/prism/templates/src/diagnostic.c.erb +1 -0
- metadata +34 -20
- data/lib/herb/libherb/array.rb +0 -51
- data/lib/herb/libherb/ast_node.rb +0 -50
- data/lib/herb/libherb/buffer.rb +0 -56
- data/lib/herb/libherb/extract_result.rb +0 -20
- data/lib/herb/libherb/lex_result.rb +0 -32
- data/lib/herb/libherb/libherb.rb +0 -52
- data/lib/herb/libherb/parse_result.rb +0 -20
- data/lib/herb/libherb/token.rb +0 -46
- data/lib/herb/libherb.rb +0 -35
- data/src/buffer.c +0 -241
- data/src/include/array.h +0 -33
- data/src/include/buffer.h +0 -39
- data/src/include/json.h +0 -28
- data/src/include/memory.h +0 -12
- data/src/json.c +0 -205
- data/src/memory.c +0 -53
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
use crate::errors::{AnyError, ErrorNode};
|
|
2
|
+
use crate::{Location, Token};
|
|
3
|
+
use colored::*;
|
|
4
|
+
|
|
5
|
+
fn escape_string(s: &str) -> String {
|
|
6
|
+
s.replace('\\', "\\\\")
|
|
7
|
+
.replace('\n', "\\n")
|
|
8
|
+
.replace('\r', "\\r")
|
|
9
|
+
.replace('\t', "\\t")
|
|
10
|
+
.replace('"', "\\\"")
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
fn format_string_value(value: &str) -> String {
|
|
14
|
+
format!("\"{}\"", escape_string(value)).green().to_string()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
fn format_token_value(token: &Option<Token>) -> String {
|
|
18
|
+
token.as_ref().map(|t| t.tree_inspect()).unwrap_or_else(|| "∅".magenta().to_string())
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
fn format_bool_value(value: bool) -> String {
|
|
22
|
+
value.to_string().magenta().bold().to_string()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
fn format_node_value<T: Node + ?Sized>(node: &Option<Box<T>>, prefix: &str, add_spacing: bool) -> String {
|
|
26
|
+
if let Some(ref n) = node {
|
|
27
|
+
let mut output = String::new();
|
|
28
|
+
output.push('\n');
|
|
29
|
+
output.push_str(&inspect_node_field(n.as_ref(), prefix));
|
|
30
|
+
if add_spacing {
|
|
31
|
+
output.push_str(&format!("{}\n", prefix));
|
|
32
|
+
}
|
|
33
|
+
output
|
|
34
|
+
} else {
|
|
35
|
+
format!("{}\n", "∅".magenta())
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fn format_array_value(array: &[AnyNode], prefix: &str) -> String {
|
|
40
|
+
inspect_array(array, prefix)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
fn format_errors_field(errors: &[AnyError], prefix: &str) -> String {
|
|
44
|
+
inspect_errors(errors, prefix)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn inspect_errors(errors: &[AnyError], prefix: &str) -> String {
|
|
48
|
+
if errors.is_empty() {
|
|
49
|
+
return String::new();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let mut output = String::new();
|
|
53
|
+
output.push_str(&format!("{} {}: {}\n",
|
|
54
|
+
"├──".white(),
|
|
55
|
+
"errors".red().bold(),
|
|
56
|
+
format!("({} error{})", errors.len(), if errors.len() == 1 { "" } else { "s" }).dimmed()
|
|
57
|
+
));
|
|
58
|
+
|
|
59
|
+
for (i, error) in errors.iter().enumerate() {
|
|
60
|
+
let is_last = i == errors.len() - 1;
|
|
61
|
+
let symbol = if is_last { "└── " } else { "├── " };
|
|
62
|
+
let next_prefix_str = if is_last { " " } else { "│ " };
|
|
63
|
+
let next_prefix = next_prefix_str.white().to_string();
|
|
64
|
+
|
|
65
|
+
let tree = error.tree_inspect();
|
|
66
|
+
let tree = tree.trim_end_matches('\n');
|
|
67
|
+
output.push_str(&format!("{}{}{}\n", prefix, symbol.white(), tree.replace('\n', &format!("\n{}{}", prefix, next_prefix))));
|
|
68
|
+
|
|
69
|
+
if !is_last {
|
|
70
|
+
output.push_str(&format!("{}{}\n", prefix, "│ ".white()));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
output.push_str(&format!("{}\n", prefix));
|
|
75
|
+
output
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn inspect_array(array: &[AnyNode], prefix: &str) -> String {
|
|
79
|
+
if array.is_empty() {
|
|
80
|
+
return format!("{}\n", "[]".dimmed());
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let mut output = String::new();
|
|
84
|
+
output.push_str(&format!("{}\n", format!("({} item{})", array.len(), if array.len() == 1 { "" } else { "s" }).dimmed()));
|
|
85
|
+
|
|
86
|
+
for (i, item) in array.iter().enumerate() {
|
|
87
|
+
let is_last = i == array.len() - 1;
|
|
88
|
+
let symbol = if is_last { "└── " } else { "├── " };
|
|
89
|
+
let next_prefix_str = if is_last { " " } else { "│ " };
|
|
90
|
+
let next_prefix = next_prefix_str.white().to_string();
|
|
91
|
+
|
|
92
|
+
let tree = item.tree_inspect();
|
|
93
|
+
let tree = tree.trim_end_matches('\n');
|
|
94
|
+
|
|
95
|
+
output.push_str(prefix);
|
|
96
|
+
output.push_str(&symbol.white().to_string());
|
|
97
|
+
output.push_str(&tree.replace('\n', &format!("\n{}{}", prefix, next_prefix)));
|
|
98
|
+
output.push('\n');
|
|
99
|
+
|
|
100
|
+
if !is_last {
|
|
101
|
+
output.push_str(prefix);
|
|
102
|
+
output.push_str(&next_prefix);
|
|
103
|
+
output.push('\n');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
output
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
fn inspect_node_field<T: Node + ?Sized>(node: &T, prefix: &str) -> String {
|
|
111
|
+
let tree = node.tree_inspect();
|
|
112
|
+
let tree = tree.trim_end_matches('\n');
|
|
113
|
+
|
|
114
|
+
let lines: Vec<&str> = tree.split('\n').collect();
|
|
115
|
+
if lines.is_empty() {
|
|
116
|
+
return format!("{}\n", "∅".magenta());
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let mut result = String::new();
|
|
120
|
+
result.push_str(prefix);
|
|
121
|
+
result.push_str(&"└── ".white().to_string());
|
|
122
|
+
result.push_str(lines[0]);
|
|
123
|
+
result.push('\n');
|
|
124
|
+
|
|
125
|
+
for line in lines.iter().skip(1) {
|
|
126
|
+
if line.is_empty() {
|
|
127
|
+
result.push_str(prefix);
|
|
128
|
+
result.push('\n');
|
|
129
|
+
} else {
|
|
130
|
+
result.push_str(prefix);
|
|
131
|
+
result.push_str(" ");
|
|
132
|
+
result.push_str(line);
|
|
133
|
+
result.push('\n');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
result
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
pub trait Node {
|
|
141
|
+
fn node_type(&self) -> &str;
|
|
142
|
+
fn location(&self) -> &Location;
|
|
143
|
+
fn errors(&self) -> &[AnyError];
|
|
144
|
+
fn child_nodes(&self) -> Vec<&dyn Node>;
|
|
145
|
+
fn recursive_errors(&self) -> Vec<&dyn ErrorNode>;
|
|
146
|
+
fn tree_inspect(&self) -> String;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#[derive(Debug, Clone)]
|
|
150
|
+
pub enum AnyNode {
|
|
151
|
+
<%- nodes.each do |node| -%>
|
|
152
|
+
<%= node.name %>(<%= node.name %>),
|
|
153
|
+
<%- end -%>
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
impl AnyNode {
|
|
157
|
+
pub fn node_type(&self) -> &str {
|
|
158
|
+
match self {
|
|
159
|
+
<%- nodes.each do |node| -%>
|
|
160
|
+
AnyNode::<%= node.name %>(n) => &n.node_type,
|
|
161
|
+
<%- end -%>
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
pub fn location(&self) -> &Location {
|
|
166
|
+
match self {
|
|
167
|
+
<%- nodes.each do |node| -%>
|
|
168
|
+
AnyNode::<%= node.name %>(n) => &n.location,
|
|
169
|
+
<%- end -%>
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
pub fn errors(&self) -> &[AnyError] {
|
|
174
|
+
match self {
|
|
175
|
+
<%- nodes.each do |node| -%>
|
|
176
|
+
AnyNode::<%= node.name %>(n) => &n.errors,
|
|
177
|
+
<%- end -%>
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
pub fn tree_inspect(&self) -> String {
|
|
182
|
+
match self {
|
|
183
|
+
<%- nodes.each do |node| -%>
|
|
184
|
+
AnyNode::<%= node.name %>(n) => n.tree_inspect(),
|
|
185
|
+
<%- end -%>
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
pub fn child_nodes(&self) -> Vec<&dyn Node> {
|
|
190
|
+
match self {
|
|
191
|
+
<%- nodes.each do |node| -%>
|
|
192
|
+
AnyNode::<%= node.name %>(n) => n.child_nodes(),
|
|
193
|
+
<%- end -%>
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
pub fn recursive_errors(&self) -> Vec<&dyn ErrorNode> {
|
|
198
|
+
match self {
|
|
199
|
+
<%- nodes.each do |node| -%>
|
|
200
|
+
AnyNode::<%= node.name %>(n) => n.recursive_errors(),
|
|
201
|
+
<%- end -%>
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
impl Node for AnyNode {
|
|
207
|
+
fn node_type(&self) -> &str {
|
|
208
|
+
self.node_type()
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
fn location(&self) -> &Location {
|
|
212
|
+
self.location()
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
fn errors(&self) -> &[AnyError] {
|
|
216
|
+
self.errors()
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
fn child_nodes(&self) -> Vec<&dyn Node> {
|
|
220
|
+
self.child_nodes()
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
fn recursive_errors(&self) -> Vec<&dyn ErrorNode> {
|
|
224
|
+
self.recursive_errors()
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
fn tree_inspect(&self) -> String {
|
|
228
|
+
self.tree_inspect()
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
<%- nodes.each do |node| -%>
|
|
233
|
+
#[derive(Debug, Clone)]
|
|
234
|
+
pub struct <%= node.name %> {
|
|
235
|
+
pub node_type: String,
|
|
236
|
+
pub location: Location,
|
|
237
|
+
pub errors: Vec<AnyError>,
|
|
238
|
+
<%- node.fields.each do |field| -%>
|
|
239
|
+
<%- case field -%>
|
|
240
|
+
<%- when Herb::Template::StringField -%>
|
|
241
|
+
pub <%= field.name %>: String,
|
|
242
|
+
<%- when Herb::Template::TokenField -%>
|
|
243
|
+
pub <%= field.name %>: Option<Token>,
|
|
244
|
+
<%- when Herb::Template::BooleanField -%>
|
|
245
|
+
pub <%= field.name %>: bool,
|
|
246
|
+
<%- when Herb::Template::ArrayField -%>
|
|
247
|
+
pub <%= field.name %>: Vec<AnyNode>,
|
|
248
|
+
<%- when Herb::Template::NodeField -%>
|
|
249
|
+
<%- if field.specific_kind && field.specific_kind != "Node" -%>
|
|
250
|
+
pub <%= field.name %>: Option<Box<<%= field.specific_kind %>>>,
|
|
251
|
+
<%- else -%>
|
|
252
|
+
pub <%= field.name %>: Option<Box<AnyNode>>,
|
|
253
|
+
<%- end -%>
|
|
254
|
+
<%- when Herb::Template::ElementSourceField -%>
|
|
255
|
+
pub <%= field.name %>: String,
|
|
256
|
+
<%- end -%>
|
|
257
|
+
<%- end -%>
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
impl Node for <%= node.name %> {
|
|
261
|
+
fn node_type(&self) -> &str {
|
|
262
|
+
&self.node_type
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
fn location(&self) -> &Location {
|
|
266
|
+
&self.location
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
fn errors(&self) -> &[AnyError] {
|
|
270
|
+
&self.errors
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
fn child_nodes(&self) -> Vec<&dyn Node> {
|
|
274
|
+
<%- has_children = node.fields.any? { |field|
|
|
275
|
+
case field
|
|
276
|
+
when Herb::Template::ArrayField
|
|
277
|
+
true
|
|
278
|
+
when Herb::Template::NodeField
|
|
279
|
+
true
|
|
280
|
+
else
|
|
281
|
+
false
|
|
282
|
+
end
|
|
283
|
+
} -%>
|
|
284
|
+
<%- if has_children -%>
|
|
285
|
+
let mut children: Vec<&dyn Node> = Vec::new();
|
|
286
|
+
<%- node.fields.each do |field| -%>
|
|
287
|
+
<%- case field -%>
|
|
288
|
+
<%- when Herb::Template::ArrayField -%>
|
|
289
|
+
children.extend(self.<%= field.name %>.iter().map(|n| n as &dyn Node));
|
|
290
|
+
<%- when Herb::Template::NodeField -%>
|
|
291
|
+
if let Some(ref node) = self.<%= field.name %> {
|
|
292
|
+
<%- if field.specific_kind && field.specific_kind != "Node" -%>
|
|
293
|
+
children.push(node.as_ref() as &dyn Node);
|
|
294
|
+
<%- else -%>
|
|
295
|
+
children.push(node.as_ref() as &dyn Node);
|
|
296
|
+
<%- end -%>
|
|
297
|
+
}
|
|
298
|
+
<%- end -%>
|
|
299
|
+
<%- end -%>
|
|
300
|
+
children
|
|
301
|
+
<%- else -%>
|
|
302
|
+
Vec::new()
|
|
303
|
+
<%- end -%>
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
fn recursive_errors(&self) -> Vec<&dyn ErrorNode> {
|
|
307
|
+
<%- has_children = node.fields.any? { |field|
|
|
308
|
+
case field
|
|
309
|
+
when Herb::Template::ArrayField
|
|
310
|
+
true
|
|
311
|
+
when Herb::Template::NodeField
|
|
312
|
+
true
|
|
313
|
+
else
|
|
314
|
+
false
|
|
315
|
+
end
|
|
316
|
+
} -%>
|
|
317
|
+
<%- if has_children -%>
|
|
318
|
+
let mut all_errors: Vec<&dyn ErrorNode> = Vec::new();
|
|
319
|
+
all_errors.extend(self.errors.iter().map(|e| e as &dyn ErrorNode));
|
|
320
|
+
|
|
321
|
+
<%- node.fields.each do |field| -%>
|
|
322
|
+
<%- case field -%>
|
|
323
|
+
<%- when Herb::Template::ArrayField -%>
|
|
324
|
+
for child in &self.<%= field.name %> {
|
|
325
|
+
all_errors.extend(child.recursive_errors());
|
|
326
|
+
}
|
|
327
|
+
<%- when Herb::Template::NodeField -%>
|
|
328
|
+
if let Some(ref node) = self.<%= field.name %> {
|
|
329
|
+
all_errors.extend(node.recursive_errors());
|
|
330
|
+
}
|
|
331
|
+
<%- end -%>
|
|
332
|
+
<%- end -%>
|
|
333
|
+
|
|
334
|
+
all_errors
|
|
335
|
+
<%- else -%>
|
|
336
|
+
self.errors.iter().map(|e| e as &dyn ErrorNode).collect()
|
|
337
|
+
<%- end -%>
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
fn tree_inspect(&self) -> String {
|
|
341
|
+
let mut output = String::new();
|
|
342
|
+
|
|
343
|
+
output.push_str(&format!("{} {} {}\n",
|
|
344
|
+
"@".white(),
|
|
345
|
+
"<%= node.name %>".yellow().bold(),
|
|
346
|
+
format!("(location: {})", self.location).dimmed()
|
|
347
|
+
));
|
|
348
|
+
output.push_str(&format_errors_field(&self.errors, &<%- if node.fields.any? -%>"│ "<%- else -%>" "<%- end -%>.white().to_string()));
|
|
349
|
+
<%- if node.fields.any? -%>
|
|
350
|
+
<%- node.fields.each_with_index do |field, index| -%>
|
|
351
|
+
<%- is_last = index == node.fields.length - 1 -%>
|
|
352
|
+
<%- symbol = is_last ? "└── " : "├── " -%>
|
|
353
|
+
<%- case field -%>
|
|
354
|
+
<%- when Herb::Template::StringField, Herb::Template::ElementSourceField -%>
|
|
355
|
+
output.push_str(&format!("{}{}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_string_value(&self.<%= field.name %>)));
|
|
356
|
+
<%- when Herb::Template::TokenField -%>
|
|
357
|
+
output.push_str(&format!("{}{}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_token_value(&self.<%= field.name %>)));
|
|
358
|
+
<%- when Herb::Template::BooleanField -%>
|
|
359
|
+
output.push_str(&format!("{}{}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_bool_value(self.<%= field.name %>)));
|
|
360
|
+
<%- when Herb::Template::ArrayField -%>
|
|
361
|
+
output.push_str(&format!("{}{}: {}", "<%= symbol %>".white(), "<%= field.name %>".white(), format_array_value(&self.<%= field.name %>, &"<%= is_last ? " " : "│ " %>".white().to_string())));
|
|
362
|
+
<%- when Herb::Template::NodeField -%>
|
|
363
|
+
output.push_str(&format!("{}{}: {}", "<%= symbol %>".white(), "<%= field.name %>".white(), format_node_value(&self.<%= field.name %>, &"<%= is_last ? " " : "│ " %>".white().to_string(), <%= !is_last %>)));
|
|
364
|
+
<%- end -%>
|
|
365
|
+
<%- end -%>
|
|
366
|
+
<%- else -%>
|
|
367
|
+
output.push_str(&format!("{} {}\n", "└──".white(), "(no fields)".dimmed()));
|
|
368
|
+
<%- end -%>
|
|
369
|
+
|
|
370
|
+
output
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
<%- end -%>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#include "include/analyze_helpers.h"
|
|
2
|
+
#include "include/errors.h"
|
|
3
|
+
|
|
4
|
+
<%-
|
|
5
|
+
nodes_with_end_node = nodes.select do |node|
|
|
6
|
+
node.fields.any? { |field| field.name == "end_node" && field.is_a?(Herb::Template::NodeField) }
|
|
7
|
+
end
|
|
8
|
+
-%>
|
|
9
|
+
|
|
10
|
+
void check_erb_node_for_missing_end(const AST_NODE_T* node) {
|
|
11
|
+
switch (node->type) {
|
|
12
|
+
<%- nodes_with_end_node.each do |node| -%>
|
|
13
|
+
<%- keyword = node.name.gsub(/^ERB/, '').gsub(/Match|Node$/, '').downcase -%>
|
|
14
|
+
case <%= node.type %>: {
|
|
15
|
+
const <%= node.struct_type %>* <%= node.human %> = (const <%= node.struct_type %>*) node;
|
|
16
|
+
|
|
17
|
+
if (<%= node.human %>->end_node == NULL) {
|
|
18
|
+
append_missingerb_end_tag_error(
|
|
19
|
+
<%- if keyword == "block" -%>
|
|
20
|
+
"ERB block",
|
|
21
|
+
<%- else -%>
|
|
22
|
+
"`<" "%" " <%= keyword %> " "%" ">`",
|
|
23
|
+
<%- end -%>
|
|
24
|
+
<%= node.human %>->tag_opening->location.start,
|
|
25
|
+
<%= node.human %>->tag_closing->location.end,
|
|
26
|
+
node->errors
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
<%- end -%>
|
|
34
|
+
default: break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#include "include/analyze.h"
|
|
2
|
+
#include "include/visitor.h"
|
|
3
|
+
|
|
4
|
+
bool transform_erb_nodes(const AST_NODE_T* node, void* data) {
|
|
5
|
+
analyze_ruby_context_T* context = (analyze_ruby_context_T*) data;
|
|
6
|
+
context->parent = (AST_NODE_T*) node;
|
|
7
|
+
|
|
8
|
+
<%- nodes.each do |node| -%>
|
|
9
|
+
<%- node.fields.each do |field| -%>
|
|
10
|
+
<%- if field.is_a?(Herb::Template::ArrayField) && field.name != "errors" -%>
|
|
11
|
+
if (node->type == <%= node.type %>) {
|
|
12
|
+
<%= node.struct_type %>* <%= node.human %> = (<%= node.struct_type %>*) node;
|
|
13
|
+
hb_array_T* old_array = <%= node.human %>-><%= field.name %>;
|
|
14
|
+
<%= node.human %>-><%= field.name %> = rewrite_node_array((AST_NODE_T*) node, <%= node.human %>-><%= field.name %>, context);
|
|
15
|
+
hb_array_free(&old_array);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
<%- end -%>
|
|
19
|
+
<%- end -%>
|
|
20
|
+
<%- end -%>
|
|
21
|
+
herb_visit_child_nodes(node, transform_erb_nodes, data);
|
|
22
|
+
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
@@ -4,16 +4,16 @@
|
|
|
4
4
|
#include <prism.h>
|
|
5
5
|
|
|
6
6
|
#include "include/analyzed_ruby.h"
|
|
7
|
-
#include "include/array.h"
|
|
8
7
|
#include "include/ast_node.h"
|
|
9
8
|
#include "include/ast_nodes.h"
|
|
10
9
|
#include "include/errors.h"
|
|
11
10
|
#include "include/token.h"
|
|
12
11
|
#include "include/util.h"
|
|
12
|
+
#include "include/util/hb_array.h"
|
|
13
13
|
|
|
14
14
|
<%- nodes.each do |node| -%>
|
|
15
15
|
<%- node_arguments = node.fields.any? ? node.fields.map { |field| [field.c_type, " ", field.name].join } : [] -%>
|
|
16
|
-
<%- arguments = node_arguments + ["position_T start_position", "position_T end_position", "
|
|
16
|
+
<%- arguments = node_arguments + ["position_T start_position", "position_T end_position", "hb_array_T* errors"] -%>
|
|
17
17
|
|
|
18
18
|
<%= node.struct_type %>* ast_<%= node.human %>_init(<%= arguments.join(", ") %>) {
|
|
19
19
|
<%= node.struct_type %>* <%= node.human %> = malloc(sizeof(<%= node.struct_type %>));
|
|
@@ -49,36 +49,36 @@
|
|
|
49
49
|
}
|
|
50
50
|
<%- end -%>
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
hb_string_T ast_node_type_to_string(AST_NODE_T* node) {
|
|
53
53
|
switch (node->type) {
|
|
54
54
|
<%- nodes.each do |node| -%>
|
|
55
|
-
case <%= node.type %>: return "<%= node.type %>";
|
|
55
|
+
case <%= node.type %>: return hb_string("<%= node.type %>");
|
|
56
56
|
<%- end -%>
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
return "Unknown ast_node_type_T";
|
|
59
|
+
return hb_string("Unknown ast_node_type_T");
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
hb_string_T ast_node_human_type(AST_NODE_T* node) {
|
|
63
63
|
switch (node->type) {
|
|
64
64
|
<%- nodes.each do |node| -%>
|
|
65
|
-
case <%= node.type %>: return "<%= node.name %>";
|
|
65
|
+
case <%= node.type %>: return hb_string("<%= node.name %>");
|
|
66
66
|
<%- end -%>
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
return "Unknown ast_node_type_T";
|
|
69
|
+
return hb_string("Unknown ast_node_type_T");
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
void ast_free_base_node(AST_NODE_T* node) {
|
|
73
73
|
if (node == NULL) { return; }
|
|
74
74
|
|
|
75
75
|
if (node->errors) {
|
|
76
|
-
for (size_t i = 0; i <
|
|
77
|
-
ERROR_T* child =
|
|
76
|
+
for (size_t i = 0; i < hb_array_size(node->errors); i++) {
|
|
77
|
+
ERROR_T* child = hb_array_get(node->errors, i);
|
|
78
78
|
if (child != NULL) { error_free(child); }
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
hb_array_free(&node->errors);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
free(node);
|
|
@@ -99,12 +99,12 @@ static void ast_free_<%= node.human %>(<%= node.struct_type %>* <%= node.human %
|
|
|
99
99
|
ast_node_free((AST_NODE_T*) <%= node.human %>-><%= field.name %>);
|
|
100
100
|
<%- when Herb::Template::ArrayField -%>
|
|
101
101
|
if (<%= node.human %>-><%= field.name %> != NULL) {
|
|
102
|
-
for (size_t i = 0; i <
|
|
103
|
-
AST_NODE_T* child =
|
|
102
|
+
for (size_t i = 0; i < hb_array_size(<%= node.human %>-><%= field.name %>); i++) {
|
|
103
|
+
AST_NODE_T* child = hb_array_get(<%= node.human %>-><%= field.name %>, i);
|
|
104
104
|
if (child) { ast_node_free(child); }
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
|
|
107
|
+
hb_array_free(&<%= node.human %>-><%= field.name %>);
|
|
108
108
|
}
|
|
109
109
|
<%- when Herb::Template::StringField -%>
|
|
110
110
|
if (<%= node.human %>-><%= field.name %> != NULL) { free((char*) <%= node.human %>-><%= field.name %>); }
|
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
#include "include/ast_node.h"
|
|
2
2
|
#include "include/ast_nodes.h"
|
|
3
|
-
#include "include/buffer.h"
|
|
4
3
|
#include "include/errors.h"
|
|
5
4
|
#include "include/pretty_print.h"
|
|
6
5
|
#include "include/token_struct.h"
|
|
7
6
|
#include "include/util.h"
|
|
7
|
+
#include "include/util/hb_buffer.h"
|
|
8
8
|
|
|
9
9
|
#include <stdio.h>
|
|
10
10
|
#include <stdlib.h>
|
|
11
11
|
#include <stdbool.h>
|
|
12
12
|
|
|
13
|
-
void ast_pretty_print_node(AST_NODE_T* node, const size_t indent, const size_t relative_indent,
|
|
13
|
+
void ast_pretty_print_node(AST_NODE_T* node, const size_t indent, const size_t relative_indent, hb_buffer_T* buffer) {
|
|
14
14
|
if (!node) { return; }
|
|
15
15
|
|
|
16
16
|
bool print_location = true;
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
hb_buffer_append(buffer, "@ ");
|
|
19
|
+
hb_buffer_append_string(buffer, ast_node_human_type(node));
|
|
20
|
+
hb_buffer_append(buffer, " ");
|
|
21
21
|
|
|
22
22
|
if (print_location) { pretty_print_location(node->location, buffer); }
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
hb_buffer_append(buffer, "\n");
|
|
25
25
|
|
|
26
26
|
switch (node->type) {
|
|
27
27
|
<%- nodes.each do |node| -%>
|
|
@@ -33,58 +33,58 @@ void ast_pretty_print_node(AST_NODE_T* node, const size_t indent, const size_t r
|
|
|
33
33
|
<%- last = index == node.fields.length - 1 -%>
|
|
34
34
|
<%- case field -%>
|
|
35
35
|
<%- when Herb::Template::TokenField -%>
|
|
36
|
-
pretty_print_token_property(<%= node.human %>-><%= field.name %>, "<%= field.name %>", indent, relative_indent, <%= last %>, buffer);
|
|
36
|
+
pretty_print_token_property(<%= node.human %>-><%= field.name %>, hb_string("<%= field.name %>"), indent, relative_indent, <%= last %>, buffer);
|
|
37
37
|
<%- when Herb::Template::ArrayField -%>
|
|
38
|
-
pretty_print_array("<%= field.name %>", <%= node.human %>-><%= field.name %>, indent, relative_indent, <%= last %>, buffer);
|
|
38
|
+
pretty_print_array(hb_string("<%= field.name %>"), <%= node.human %>-><%= field.name %>, indent, relative_indent, <%= last %>, buffer);
|
|
39
39
|
<%- when Herb::Template::BooleanField -%>
|
|
40
|
-
pretty_print_boolean_property("<%= field.name %>", <%= node.human %>-><%= field.name %>, indent, relative_indent, <%= last %>, buffer);
|
|
40
|
+
pretty_print_boolean_property(hb_string("<%= field.name %>"), <%= node.human %>-><%= field.name %>, indent, relative_indent, <%= last %>, buffer);
|
|
41
41
|
<%- when Herb::Template::ElementSourceField -%>
|
|
42
|
-
pretty_print_string_property(element_source_to_string(<%= node.human %>-><%= field.name %>), "<%= field.name %>", indent, relative_indent, <%= last %>, buffer);
|
|
42
|
+
pretty_print_string_property(element_source_to_string(<%= node.human %>-><%= field.name %>), hb_string("<%= field.name %>"), indent, relative_indent, <%= last %>, buffer);
|
|
43
43
|
<%- when Herb::Template::StringField -%>
|
|
44
|
-
pretty_print_string_property(<%= node.human %>-><%= field.name
|
|
44
|
+
pretty_print_string_property(hb_string(<%= node.human %>-><%= field.name %>), hb_string("<%= field.name %>"), indent, relative_indent, <%= last %>, buffer);
|
|
45
45
|
<%- when Herb::Template::PrismNodeField -%>
|
|
46
|
-
pretty_print_string_property("<%= field.name %>", "<%= field.name %>", indent, relative_indent, <%= last %>, buffer);
|
|
46
|
+
pretty_print_string_property(hb_string("<%= field.name %>"), hb_string("<%= field.name %>"), indent, relative_indent, <%= last %>, buffer);
|
|
47
47
|
<%- when Herb::Template::NodeField -%>
|
|
48
48
|
|
|
49
|
-
pretty_print_label("<%= field.name %>", indent, relative_indent, <%= last %>, buffer);
|
|
49
|
+
pretty_print_label(hb_string("<%= field.name %>"), indent, relative_indent, <%= last %>, buffer);
|
|
50
50
|
|
|
51
51
|
if (<%= node.human %>-><%= field.name %>) {
|
|
52
|
-
|
|
52
|
+
hb_buffer_append(buffer, "\n");
|
|
53
53
|
pretty_print_indent(buffer, indent);
|
|
54
54
|
pretty_print_indent(buffer, relative_indent + 1);
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
hb_buffer_append(buffer, "└── ");
|
|
57
57
|
ast_pretty_print_node((AST_NODE_T*) <%= node.human %>-><%= field.name %>, indent, relative_indent + 2, buffer);
|
|
58
58
|
} else {
|
|
59
|
-
|
|
59
|
+
hb_buffer_append(buffer, " ∅\n");
|
|
60
60
|
}
|
|
61
|
-
|
|
61
|
+
hb_buffer_append(buffer, "\n");
|
|
62
62
|
|
|
63
63
|
<%- when Herb::Template::AnalyzedRubyField -%>
|
|
64
64
|
if (<%= node.human %>-><%= field.name %>) {
|
|
65
|
-
pretty_print_boolean_property("if_node", <%= node.human %>-><%= field.name %>->has_if_node, indent, relative_indent, false, buffer);
|
|
66
|
-
pretty_print_boolean_property("elsif_node", <%= node.human %>-><%= field.name %>->has_elsif_node, indent, relative_indent, false, buffer);
|
|
67
|
-
pretty_print_boolean_property("else_node", <%= node.human %>-><%= field.name %>->has_else_node, indent, relative_indent, false, buffer);
|
|
68
|
-
pretty_print_boolean_property("end", <%= node.human %>-><%= field.name %>->has_end, indent, relative_indent, false, buffer);
|
|
69
|
-
pretty_print_boolean_property("block_node", <%= node.human %>-><%= field.name %>->has_block_node, indent, relative_indent, false, buffer);
|
|
70
|
-
pretty_print_boolean_property("block_closing", <%= node.human %>-><%= field.name %>->has_block_closing, indent, relative_indent, false, buffer);
|
|
71
|
-
pretty_print_boolean_property("case_node", <%= node.human %>-><%= field.name %>->has_case_node, indent, relative_indent, false, buffer);
|
|
72
|
-
pretty_print_boolean_property("when_node", <%= node.human %>-><%= field.name %>->has_when_node, indent, relative_indent, false, buffer);
|
|
73
|
-
pretty_print_boolean_property("for_node", <%= node.human %>-><%= field.name %>->has_for_node, indent, relative_indent, false, buffer);
|
|
74
|
-
pretty_print_boolean_property("while_node", <%= node.human %>-><%= field.name %>->has_while_node, indent, relative_indent, false, buffer);
|
|
75
|
-
pretty_print_boolean_property("until_node", <%= node.human %>-><%= field.name %>->has_until_node, indent, relative_indent, false, buffer);
|
|
76
|
-
pretty_print_boolean_property("begin_node", <%= node.human %>-><%= field.name %>->has_begin_node, indent, relative_indent, false, buffer);
|
|
77
|
-
pretty_print_boolean_property("rescue_node", <%= node.human %>-><%= field.name %>->has_rescue_node, indent, relative_indent, false, buffer);
|
|
78
|
-
pretty_print_boolean_property("ensure_node", <%= node.human %>-><%= field.name %>->has_ensure_node, indent, relative_indent, <%= last %>, buffer);
|
|
79
|
-
pretty_print_boolean_property("unless_node", <%= node.human %>-><%= field.name %>->has_unless_node, indent, relative_indent, <%= last %>, buffer);
|
|
65
|
+
pretty_print_boolean_property(hb_string("if_node"), <%= node.human %>-><%= field.name %>->has_if_node, indent, relative_indent, false, buffer);
|
|
66
|
+
pretty_print_boolean_property(hb_string("elsif_node"), <%= node.human %>-><%= field.name %>->has_elsif_node, indent, relative_indent, false, buffer);
|
|
67
|
+
pretty_print_boolean_property(hb_string("else_node"), <%= node.human %>-><%= field.name %>->has_else_node, indent, relative_indent, false, buffer);
|
|
68
|
+
pretty_print_boolean_property(hb_string("end"), <%= node.human %>-><%= field.name %>->has_end, indent, relative_indent, false, buffer);
|
|
69
|
+
pretty_print_boolean_property(hb_string("block_node"), <%= node.human %>-><%= field.name %>->has_block_node, indent, relative_indent, false, buffer);
|
|
70
|
+
pretty_print_boolean_property(hb_string("block_closing"), <%= node.human %>-><%= field.name %>->has_block_closing, indent, relative_indent, false, buffer);
|
|
71
|
+
pretty_print_boolean_property(hb_string("case_node"), <%= node.human %>-><%= field.name %>->has_case_node, indent, relative_indent, false, buffer);
|
|
72
|
+
pretty_print_boolean_property(hb_string("when_node"), <%= node.human %>-><%= field.name %>->has_when_node, indent, relative_indent, false, buffer);
|
|
73
|
+
pretty_print_boolean_property(hb_string("for_node"), <%= node.human %>-><%= field.name %>->has_for_node, indent, relative_indent, false, buffer);
|
|
74
|
+
pretty_print_boolean_property(hb_string("while_node"), <%= node.human %>-><%= field.name %>->has_while_node, indent, relative_indent, false, buffer);
|
|
75
|
+
pretty_print_boolean_property(hb_string("until_node"), <%= node.human %>-><%= field.name %>->has_until_node, indent, relative_indent, false, buffer);
|
|
76
|
+
pretty_print_boolean_property(hb_string("begin_node"), <%= node.human %>-><%= field.name %>->has_begin_node, indent, relative_indent, false, buffer);
|
|
77
|
+
pretty_print_boolean_property(hb_string("rescue_node"), <%= node.human %>-><%= field.name %>->has_rescue_node, indent, relative_indent, false, buffer);
|
|
78
|
+
pretty_print_boolean_property(hb_string("ensure_node"), <%= node.human %>-><%= field.name %>->has_ensure_node, indent, relative_indent, <%= last %>, buffer);
|
|
79
|
+
pretty_print_boolean_property(hb_string("unless_node"), <%= node.human %>-><%= field.name %>->has_unless_node, indent, relative_indent, <%= last %>, buffer);
|
|
80
80
|
} else {
|
|
81
|
-
pretty_print_label("<%= field.name %>", indent, relative_indent, <%= last %>, buffer);
|
|
82
|
-
|
|
81
|
+
pretty_print_label(hb_string("<%= field.name %>"), indent, relative_indent, <%= last %>, buffer);
|
|
82
|
+
hb_buffer_append(buffer, " ∅\n");
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
<%- when Herb::Template::VoidPointerField -%>
|
|
86
|
-
pretty_print_label("<%= field.name %>", indent, relative_indent, <%= last %>, buffer);
|
|
87
|
-
|
|
86
|
+
pretty_print_label(hb_string("<%= field.name %>"), indent, relative_indent, <%= last %>, buffer);
|
|
87
|
+
hb_buffer_append(buffer, " ?\n");
|
|
88
88
|
|
|
89
89
|
<%- else -%>
|
|
90
90
|
<%= field.inspect %>
|