Skip to content

Commit 9b5669f

Browse files
committed
proper escaping for errors
1 parent ae6a10b commit 9b5669f

File tree

10 files changed

+45
-2
lines changed

10 files changed

+45
-2
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
"no-shadow": "error",
181181
"no-shadow-restricted-names": "error",
182182
"no-spaced-func": "error",
183-
"no-sync": "error",
183+
"no-sync": "warn",
184184
"no-tabs": "off",
185185
"no-template-curly-in-string": "error",
186186
"no-ternary": "off",

src/rd_parser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ function call_rule_by_name(name) {
320320
var rule = state.rules.filter(function(r){
321321
return r.name === name;
322322
})[0];
323+
if (!rule) {
324+
throw new Error('failed to find rule by name - ' + name);
325+
}
323326
var ast = rule.parser(state);
324327
return ast;
325328
}

src/speg_parser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ function parsing_string() {
183183
return rd.action('parsing_string', rd.sequence([
184184
rd.string('"'),
185185
rd.one_or_more(rd.ordered_choice([
186+
rd.string('\\\\'),
186187
rd.string('\\"'),
187188
rd.regex_char('[^"]')
188189
])),

src/speg_visitor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ SPEG_actions.prototype.parsing_optional = function(node) {
138138
};
139139

140140
SPEG_actions.prototype.parsing_string = function(node) {
141-
return rd.string(node.children[1].match);
141+
return rd.string(node.children[1].match
142+
.replace(/\\\\/g, '\\')
143+
.replace(/\\"/g, '"')
144+
);
142145
};
143146

144147
SPEG_actions.prototype.parsing_regex_char = function(node) {

test/speg_fixtures/escape.peg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GRAMMAR test
2+
3+
a -> "\\";

test/speg_fixtures/escape.peg.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "string",
3+
"start_position": 0,
4+
"match": "\\",
5+
"rule": "a",
6+
"end_position": 1
7+
}

test/speg_fixtures/escape.peg.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\

test/speg_fixtures/stringstring.peg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GRAMMAR test
2+
3+
a -> "\"" "A";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "sequence",
3+
"rule": "a",
4+
"start_position": 0,
5+
"children": [
6+
{
7+
"type": "string",
8+
"start_position": 0,
9+
"match": "\"",
10+
"end_position": 1
11+
},
12+
{
13+
"type": "string",
14+
"start_position": 1,
15+
"match": "A",
16+
"end_position": 2
17+
}
18+
],
19+
"match": "\"A",
20+
"end_position": 2
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"A

0 commit comments

Comments
 (0)