Skip to content

Commit 9b5669f

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

10 files changed

+45
-2
lines changed

Diff for: .eslintrc.json

+1-1
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",

Diff for: src/rd_parser.js

+3
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
}

Diff for: src/speg_parser.js

+1
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
])),

Diff for: src/speg_visitor.js

+4-1
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) {

Diff for: test/speg_fixtures/escape.peg

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

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

+7
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+
}

Diff for: test/speg_fixtures/escape.peg.txt

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

Diff for: test/speg_fixtures/stringstring.peg

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

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

+21
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+
}

Diff for: test/speg_fixtures/stringstring.peg.txt

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

0 commit comments

Comments
 (0)