Skip to content

Commit fcaec30

Browse files
authored
Merge pull request #18 from projectfluent/ast-mistakes
Port projectfluent/fluent.js#47 to python
2 parents a962169 + d560c40 commit fcaec30

11 files changed

+24
-13
lines changed

fluent/syntax/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_error_message(code, args):
1818
if code == 'E0004':
1919
return 'Expected a character from range: "{}"'.format(args[0])
2020
if code == 'E0005':
21-
msg = 'Expected entry "{}" to have a value, attributes or tags'
21+
msg = 'Expected entry "{}" to have a value or attributes'
2222
return msg.format(args[0])
2323
if code == 'E0006':
2424
return 'Expected field: "{}"'.format(args[0])

fluent/syntax/ftlstream.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .stream import ParserStream
33
from .errors import ParseError
44

5+
56
class FTLParserStream(ParserStream):
67
def peek_line_ws(self):
78
ch = self.current_peek()
@@ -187,7 +188,7 @@ def take_id_start(self):
187188
self.next()
188189
return ret
189190

190-
raise ParseError('E0004', 'a-zA-Z')
191+
raise ParseError('E0004', 'a-zA-Z_')
191192

192193
def take_id_char(self):
193194
def closure(ch):
@@ -212,5 +213,5 @@ def closure(ch):
212213
def take_digit(self):
213214
def closure(ch):
214215
cc = ord(ch)
215-
return (cc >= 48 and cc <= 57)
216+
return (cc >= 48 and cc <= 57)
216217
return self.take_char(closure)

fluent/syntax/parser.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import unicode_literals
2+
import re
23
from .ftlstream import FTLParserStream
34
from . import ast
45
from .errors import ParseError
@@ -173,7 +174,7 @@ def get_message(self, ps, comment):
173174
raise ParseError('E0012')
174175
tags = self.get_tags(ps)
175176

176-
if pattern is None and attrs is None and tags is None:
177+
if pattern is None and attrs is None:
177178
raise ParseError('E0005', id.name)
178179

179180
return ast.Message(id, pattern, attrs, tags, comment)
@@ -479,7 +480,13 @@ def get_selector_expression(self, ps):
479480

480481
ps.expect_char(')')
481482

482-
return ast.CallExpression(literal.id, args)
483+
if not re.match('^[A-Z_-]+$', literal.id.name):
484+
raise ParseError('E0008')
485+
486+
return ast.CallExpression(
487+
ast.Function(literal.id.name),
488+
args
489+
)
483490

484491
return literal
485492

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
key = { foo.23 }
22
3-
//~ ERROR E0004, pos 12, args "a-zA-Z"
3+
//~ ERROR E0004, pos 12, args "a-zA-Z_"
44

55
key = { foo. }
66
7-
//~ ERROR E0004, pos 31, args "a-zA-Z"
7+
//~ ERROR E0004, pos 31, args "a-zA-Z_"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
key = { no-caps-name() }
2+
3+
//~ ERROR E0008, pos 22
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
key = Value
22
.2 = Foo
3-
//~ ERROR E0004, pos 17, args "a-zA-Z"
3+
//~ ERROR E0004, pos 17, args "a-zA-Z_"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
bar = Bar {
2-
//~ ERROR E0004, pos 11, args "a-zA-Z"
2+
//~ ERROR E0004, pos 11, args "a-zA-Z_"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
key = { $foo ->
22
*[
3-
//~ ERROR E0004, pos 22, args "a-zA-Z"
3+
//~ ERROR E0004, pos 22, args "a-zA-Z_"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
key = { foo[] }
2-
//~ ERROR E0004, pos 12, args "a-zA-Z"
2+
//~ ERROR E0004, pos 12, args "a-zA-Z_"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
key = {
22
*[one] Value
33
}
4-
//~ ERROR E0004, pos 7, args "a-zA-Z"
4+
//~ ERROR E0004, pos 7, args "a-zA-Z_"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
key = {
22
*[ one] Foo
33
}
4-
//~ ERROR E0004, pos 18, args "a-zA-Z"
4+
//~ ERROR E0004, pos 18, args "a-zA-Z_"

0 commit comments

Comments
 (0)