Skip to content

Commit 7673c50

Browse files
committed
tags support
1 parent 5910258 commit 7673c50

File tree

11 files changed

+101
-10
lines changed

11 files changed

+101
-10
lines changed

src/speg_parser.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,27 @@ function parsing_ordered_choice() {
8282
}
8383

8484
function parsing_sub_expression() {
85-
return rd.action('parsing_sub_expression', rd.ordered_choice([
86-
parsing_not_predicate(),
87-
parsing_and_predicate(),
88-
parsing_optional(),
89-
parsing_one_or_more(),
90-
parsing_zero_or_more(),
91-
parsing_group(),
92-
parsing_atomic_expression()
85+
return rd.action('parsing_sub_expression', rd.sequence([
86+
rd.zero_or_more(rd.sequence([
87+
tag(),
88+
rd.string(':')
89+
])),
90+
rd.ordered_choice([
91+
parsing_not_predicate(),
92+
parsing_and_predicate(),
93+
parsing_optional(),
94+
parsing_one_or_more(),
95+
parsing_zero_or_more(),
96+
parsing_group(),
97+
parsing_atomic_expression()
98+
])
99+
]));
100+
}
101+
102+
function tag() {
103+
return rd.action('noop', rd.sequence([
104+
rd.regex_char('[a-zA-Z_]'),
105+
rd.zero_or_more(rd.regex_char('[a-zA-Z_0-9]'))
93106
]));
94107
}
95108

src/speg_visitor.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,23 @@ SPEG_actions.prototype.parsing_ordered_choice = function(node) {
6161
};
6262

6363
SPEG_actions.prototype.parsing_sub_expression = function(node) {
64-
return node.children[0];
64+
return function() {
65+
var result = node.children[1].children[0].apply(this, arguments);
66+
if (result) {
67+
var tags = node.children[0].children.map(function(tag_node){
68+
return tag_node.children[0].match;
69+
});
70+
if (tags.length > 0) {
71+
if (result.tags) {
72+
result.tags = tags.concat(result.tags);
73+
} else {
74+
result.tags = tags;
75+
}
76+
}
77+
}
78+
return result;
79+
}
80+
return node.children[1].children[0];
6581
};
6682

6783
SPEG_actions.prototype.parsing_group = function(node) {

test/speg_fixtures/tag.peg

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GRAMMAR test
2+
3+
a -> tag:"A";

test/speg_fixtures/tag.peg.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "string",
3+
"start_position": 0,
4+
"match": "A",
5+
"tags": [
6+
"tag"
7+
],
8+
"end_position": 1
9+
}

test/speg_fixtures/tag.peg.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A

test/speg_fixtures/tags.peg

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
GRAMMAR test
2+
3+
a -> tag1:tag2:b ctag:c;
4+
b -> c;
5+
c -> last:"A";

test/speg_fixtures/tags.peg.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "sequence",
3+
"start_position": 0,
4+
"match": "AA",
5+
"children": [
6+
{
7+
"end_position": 1,
8+
"match": "A",
9+
"start_position": 0,
10+
"tags": [
11+
"tag1",
12+
"tag2",
13+
"last"
14+
],
15+
"type": "string"
16+
},
17+
{
18+
"end_position": 2,
19+
"match": "A",
20+
"start_position": 1,
21+
"tags": [
22+
"ctag",
23+
"last"
24+
],
25+
"type": "string"
26+
}
27+
],
28+
"end_position": 2
29+
}

test/speg_fixtures/tags.peg.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AA
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GRAMMAR tag
2+
3+
tag -> tag:"tag";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
GRAMMAR url
2+
3+
url -> scheme "://" host pathname search hash?;
4+
scheme -> "http" "s"?;
5+
host -> hostname port?;
6+
hostname -> segment ("." segment)*;
7+
segment -> [a-z0-9-]+;
8+
port -> ":" [0-9]+;
9+
pathname -> "/" [^ ?]*;
10+
search -> ("?" [^ #]*)?;
11+
hash -> "#" [^ ]*;

test/speg_parser.fixtures.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('speg_parser - fixtures - ', function() {
1414

1515
if (!ast) {
1616
console.log(JSON.stringify(speg.getLastExpectations()));
17-
console.log(speg.getLastError());
17+
console.log(speg.get_last_error());
1818
}
1919
ast.should.be.an('object');
2020
}

0 commit comments

Comments
 (0)