Skip to content

Commit 62e0f57

Browse files
committed
rule name is now passed to AST
1 parent edf9018 commit 62e0f57

18 files changed

+80
-24
lines changed

Diff for: src/speg_visitor.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ SPEG_actions.prototype.parsing_rule = function(node) {
3636
var rule = node.children[4];
3737
return {
3838
name: node.children[0].match,
39-
parser: rule
39+
parser: function(state) {
40+
var ast = rule(state);
41+
if (ast) {
42+
ast.rule = node.children[0].match;
43+
}
44+
return ast;
45+
}
4046
}
4147
};
4248

Diff for: test/speg_fixtures/pegjs_example.peg.json

+19
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
],
1818
"end_position": 1,
1919
"match": "2",
20+
"rule": "Integer",
2021
"start_position": 0,
2122
"type": "one_or_more"
2223
}
2324
],
2425
"end_position": 1,
2526
"match": "2",
27+
"rule": "Factor",
2628
"start_position": 0,
2729
"type": "ordered_choice"
2830
},
@@ -41,6 +43,7 @@
4143
],
4244
"end_position": 2,
4345
"match": " ",
46+
"rule": "_",
4447
"start_position": 1,
4548
"type": "zero_or_more"
4649
},
@@ -69,6 +72,7 @@
6972
],
7073
"end_position": 4,
7174
"match": " ",
75+
"rule": "_",
7276
"start_position": 3,
7377
"type": "zero_or_more"
7478
},
@@ -86,6 +90,7 @@
8690
"children": [],
8791
"end_position": 5,
8892
"match": null,
93+
"rule": "_",
8994
"start_position": 5,
9095
"type": "zero_or_more"
9196
},
@@ -106,12 +111,14 @@
106111
],
107112
"end_position": 6,
108113
"match": "3",
114+
"rule": "Integer",
109115
"start_position": 5,
110116
"type": "one_or_more"
111117
}
112118
],
113119
"end_position": 6,
114120
"match": "3",
121+
"rule": "Factor",
115122
"start_position": 5,
116123
"type": "ordered_choice"
117124
},
@@ -125,6 +132,7 @@
125132
],
126133
"end_position": 6,
127134
"match": "3",
135+
"rule": "Term",
128136
"start_position": 5,
129137
"type": "sequence"
130138
},
@@ -143,6 +151,7 @@
143151
],
144152
"end_position": 7,
145153
"match": " ",
154+
"rule": "_",
146155
"start_position": 6,
147156
"type": "zero_or_more"
148157
},
@@ -171,6 +180,7 @@
171180
],
172181
"end_position": 9,
173182
"match": " ",
183+
"rule": "_",
174184
"start_position": 8,
175185
"type": "zero_or_more"
176186
},
@@ -189,12 +199,14 @@
189199
],
190200
"end_position": 10,
191201
"match": "4",
202+
"rule": "Integer",
192203
"start_position": 9,
193204
"type": "one_or_more"
194205
}
195206
],
196207
"end_position": 10,
197208
"match": "4",
209+
"rule": "Factor",
198210
"start_position": 9,
199211
"type": "ordered_choice"
200212
},
@@ -208,6 +220,7 @@
208220
],
209221
"end_position": 10,
210222
"match": "4",
223+
"rule": "Term",
211224
"start_position": 9,
212225
"type": "sequence"
213226
}
@@ -225,13 +238,15 @@
225238
}
226239
],
227240
"end_position": 10,
241+
"rule": "Expression",
228242
"match": "3 + 4",
229243
"start_position": 5,
230244
"type": "sequence"
231245
},
232246
{
233247
"children": [],
234248
"end_position": 10,
249+
"rule": "_",
235250
"match": null,
236251
"start_position": 10,
237252
"type": "zero_or_more"
@@ -250,6 +265,7 @@
250265
}
251266
],
252267
"end_position": 11,
268+
"rule": "Factor",
253269
"match": "(3 + 4)",
254270
"start_position": 4,
255271
"type": "ordered_choice"
@@ -269,6 +285,7 @@
269285
],
270286
"end_position": 11,
271287
"match": "2 * (3 + 4)",
288+
"rule": "Term",
272289
"start_position": 0,
273290
"type": "sequence"
274291
},
@@ -283,6 +300,7 @@
283300
"end_position": 11,
284301
"match": "2 * (3 + 4)",
285302
"start_position": 0,
303+
"rule": "Expression",
286304
"type": "sequence"
287305
},
288306
{
@@ -294,6 +312,7 @@
294312
}
295313
],
296314
"end_position": 11,
315+
"rule": "Math",
297316
"match": "2 * (3 + 4)",
298317
"start_position": 0,
299318
"type": "sequence"

Diff for: test/speg_fixtures/simple_and_predicate.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "and_predicate",
3-
"start_position": 0,
3+
"start_position": 0,
4+
"rule": "a",
45
"children": [
56
{
67
"type": "string",

Diff for: test/speg_fixtures/simple_eof.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "sequence",
3-
"start_position": 0,
3+
"start_position": 0,
4+
"rule": "a",
45
"children": [
56
{
67
"type": "string",

Diff for: test/speg_fixtures/simple_not_predicate.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"type": "not_predicate",
33
"start_position": 0,
4-
"children": [],
4+
"children": [],
5+
"rule": "a",
56
"match": null,
67
"end_position": 0
78
}

Diff for: test/speg_fixtures/simple_one_or_more.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "one_or_more",
3-
"start_position": 0,
3+
"start_position": 0,
4+
"rule": "a",
45
"children": [
56
{
67
"type": "string",

Diff for: test/speg_fixtures/simple_optional.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "optional",
3-
"start_position": 0,
3+
"start_position": 0,
4+
"rule": "a",
45
"children": [
56
{
67
"type": "string",

Diff for: test/speg_fixtures/simple_ordered_choice.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "ordered_choice",
3-
"start_position": 0,
3+
"start_position": 0,
4+
"rule": "a",
45
"children": [
56
{
67
"type": "string",

Diff for: test/speg_fixtures/simple_regex.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "regex_char",
33
"start_position": 0,
4-
"match": "7",
4+
"match": "7",
5+
"rule": "a",
56
"end_position": 1
67
}

Diff for: test/speg_fixtures/simple_rule_call.peg.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"type": "string",
33
"start_position": 0,
4-
"match": "A",
4+
"match": "A",
5+
6+
"rule": "a",
57
"end_position": 1
68
}
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "string",
33
"start_position": 0,
4-
"match": "A",
4+
"match": "A",
5+
"rule": "a",
56
"end_position": 1
67
}

Diff for: test/speg_fixtures/simple_sequence.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"type": "sequence",
2+
"type": "sequence",
3+
"rule": "a",
34
"start_position": 0,
45
"children": [
56
{

Diff for: test/speg_fixtures/simple_string.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"type": "string",
2+
"type": "string",
3+
"rule": "a",
34
"start_position": 0,
45
"match": "A",
56
"end_position": 1

Diff for: test/speg_fixtures/simple_zero_or_more.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "zero_or_more",
3-
"start_position": 0,
3+
"start_position": 0,
4+
"rule": "a",
45
"children": [
56
{
67
"type": "string",

Diff for: test/speg_fixtures/tag.peg.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"type": "string",
33
"start_position": 0,
4-
"match": "A",
4+
"match": "A",
5+
"rule": "a",
56
"tags": [
67
"tag"
78
],

Diff for: test/speg_fixtures/tags.peg.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2-
"type": "sequence",
3-
"start_position": 0,
2+
"type": "sequence",
3+
"rule": "a",
4+
"start_position": 0,
45
"match": "AA",
56
"children": [
67
{
78
"end_position": 1,
9+
"rule": "b",
810
"match": "A",
911
"start_position": 0,
1012
"tags": [
@@ -17,6 +19,7 @@
1719
{
1820
"end_position": 2,
1921
"match": "A",
22+
"rule": "c",
2023
"start_position": 1,
2124
"tags": [
2225
"ctag",

0 commit comments

Comments
 (0)