Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions jsone/prattparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SyntaxError(TemplateError):
@classmethod
def unexpected(cls, got, exp):
exp = ', '.join(sorted(exp))
return cls('Found {}, expected {}'.format(got, exp))
return cls('Found {}, expected {}'.format(got.value, exp))


Token = namedtuple('Token', ['kind', 'value', 'start', 'end'])
Expand Down Expand Up @@ -92,15 +92,15 @@ def parse(self, source):
# if there are any tokens remaining, that's an error..
token = pc.attempt()
if token:
raise SyntaxError.unexpected(token.kind, self.infix_rules)
raise SyntaxError.unexpected(token, self.infix_rules)
return result

def parseUntilTerminator(self, source, terminator):
pc = ParseContext(self, source, self._generate_tokens(source))
result = pc.parse()
token = pc.attempt()
if token.kind != terminator:
raise SyntaxError.unexpected(token.kind, [terminator])
raise SyntaxError.unexpected(token, [terminator])
return (result, token.start)

def _generate_tokens(self, source):
Expand Down Expand Up @@ -167,7 +167,7 @@ def require(self, *kinds):
if not token:
raise SyntaxError('Unexpected end of input')
if kinds and token.kind not in kinds:
raise SyntaxError.unexpected(token.kind, kinds)
raise SyntaxError.unexpected(token, kinds)
return token

def parse(self, precedence=None):
Expand All @@ -176,7 +176,7 @@ def parse(self, precedence=None):
token = self.require()
prefix_rule = parser.prefix_rules.get(token.kind)
if not prefix_rule:
raise SyntaxError.unexpected(token.kind, parser.prefix_rules)
raise SyntaxError.unexpected(token, parser.prefix_rules)
left = prefix_rule(parser, token, self)
while self.next_token:
kind = self.next_token.kind
Expand Down
8 changes: 4 additions & 4 deletions specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ result: 10.5
title: hex literal
context: {}
template: {$eval: '0xff'}
error: true
error: 'SyntaxError: Found xff, expected !=, &&, (, *, **, +, -, ., /, <, <=, ==, >, >=, [, in, ||'
---
title: string literal with single quote
context: {}
Expand All @@ -1661,12 +1661,12 @@ result: 'three!'
title: string literal escape with backslash (not supported)
context: {}
template: {$eval: '"backslash\\"maybe"'}
error: true
error: 'SyntaxError: Found maybe, expected !=, &&, (, *, **, +, -, ., /, <, <=, ==, >, >=, [, in, ||'
---
title: string literal escape with doubling (not supported)
context: {}
template: {$eval: '"doubled""maybe"'}
error: true
error: 'SyntaxError: Found "maybe", expected !=, &&, (, *, **, +, -, ., /, <, <=, ==, >, >=, [, in, ||'
---
title: boolean literals
context: {}
Expand Down Expand Up @@ -1701,7 +1701,7 @@ result: 'ab'
title: $eval must take string
context: {a: 3, b: 2}
template: {$eval: ['a', 'b']}
error: true
error: 'TemplateError: expression to be evaluated must be a string'
---
title: array access [index]
context: {a: [1,2,3,4]}
Expand Down
9 changes: 6 additions & 3 deletions src/prattparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ var assert = require('assert');
var {isString} = require('./type-utils');
var {SyntaxError, TemplateError} = require('./error');

let syntaxRuleError = (token, expects) => new SyntaxError(`Found '${token.value}' expected '${expects}'`, token);
let syntaxRuleError = (token, expects) => {
expects.sort();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to sort? Do values from tokens come sorted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They don't, and I think they come from a dictionary in some cases so they would be in random order. This is just for errors so I don't think efficiency is a big deal.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

return new SyntaxError(`Found ${token.value}, expected ${expects.join(', ')}`, token);
};

class PrattParser {
constructor(options = {}) {
Expand Down Expand Up @@ -49,7 +52,7 @@ class PrattParser {
let result = ctx.parse();
let next = ctx.attempt();
if (next) {
throw syntaxRuleError(next, Object.keys(this._infixRules).join(', '));
throw syntaxRuleError(next, Object.keys(this._infixRules));
}
return result;
}
Expand All @@ -64,7 +67,7 @@ class PrattParser {
throw new SyntaxError(`Found end of string, expected ${terminator}`,
{start: errorLocation, end: errorLocation});
} else if (next.kind !== terminator) {
throw syntaxRuleError(next, terminator);
throw syntaxRuleError(next, [terminator]);
}
return {result, offset: next.start};
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_prattparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def fail(input, message):
eq_(str(exc), "SyntaxError: " + message)

yield fail, 'x', 'Unexpected input: \'x\''
yield fail, '11', 'Found number, expected *, +, -'
yield fail, '12', 'Found 2, expected *, +, -'
yield fail, '(', 'Unexpected end of input'
yield fail, '1+', 'Unexpected end of input'
yield fail, ')', 'Found ), expected (, number'
Expand Down