Skip to content

Commit 6b7fdaf

Browse files
committed
Throw on valueless terms
1 parent 2d1c5e8 commit 6b7fdaf

File tree

10 files changed

+128
-37
lines changed

10 files changed

+128
-37
lines changed

fluent/syntax/errors.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ 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 or attributes'
21+
msg = 'Expected message "{}" to have a value or attributes'
2222
return msg.format(args[0])
2323
if code == 'E0006':
24-
return 'Expected field: "{}"'.format(args[0])
24+
msg = 'Expected term "{}" to have a value'
25+
return msg.format(args[0])
2526
if code == 'E0007':
2627
return 'Keyword cannot end with a whitespace'
2728
if code == 'E0008':
@@ -32,6 +33,8 @@ def get_error_message(code, args):
3233
return 'Expected one of the variants to be marked as default (*)'
3334
if code == 'E0011':
3435
return 'Expected at least one variant after "->"'
36+
if code == 'E0012':
37+
return 'Expected value'
3538
if code == 'E0013':
3639
return 'Expected variant key'
3740
if code == 'E0014':

fluent/syntax/parser.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,18 @@ def get_message(self, ps, comment):
215215
ps.skip_indent()
216216
pattern = self.get_pattern(ps)
217217

218+
if id.name.startswith('-') and pattern is None:
219+
raise ParseError('E0006', id.name)
220+
218221
if ps.is_peek_next_line_attribute_start():
219222
attrs = self.get_attributes(ps)
220223

221-
if pattern is None and attrs is None:
222-
raise ParseError('E0005', id.name)
223-
224224
if id.name.startswith('-'):
225225
return ast.Term(id, pattern, attrs, comment)
226226

227+
if pattern is None and attrs is None:
228+
raise ParseError('E0005', id.name)
229+
227230
return ast.Message(id, pattern, attrs, comment)
228231

229232
@with_span
@@ -241,7 +244,7 @@ def get_attribute(self, ps):
241244
value = self.get_pattern(ps)
242245
return ast.Attribute(key, value)
243246

244-
raise ParseError('E0006', 'value')
247+
raise ParseError('E0012')
245248

246249
def get_attributes(self, ps):
247250
attrs = []
@@ -305,7 +308,7 @@ def get_variant(self, ps, has_default):
305308
value = self.get_pattern(ps)
306309
return ast.Variant(key, value, default_index)
307310

308-
raise ParseError('E0006', 'value')
311+
raise ParseError('E0012')
309312

310313
def get_variants(self, ps):
311314
variants = []
@@ -573,7 +576,7 @@ def get_arg_val(self, ps):
573576
return self.get_number(ps)
574577
elif ps.current_is('"'):
575578
return self.get_string(ps)
576-
raise ParseError('E0006', 'value')
579+
raise ParseError('E0012')
577580

578581
@with_span
579582
def get_string(self, ps):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
key = Value
22
.label =
3-
//~ ERROR E0006, pos 24, args "value"
3+
//~ ERROR E0012, pos 24
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
key = { BUILTIN(key: foo) }
2-
//~ ERROR E0006, pos 21, args "value"
2+
//~ ERROR E0012, pos 21

tests/syntax/fixtures_behavior/term.ftl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ err2 = { $-foo }
2323

2424
err4 = { -brand() }
2525
//~ ERROR E0008, pos 340
26+
27+
-err5 =
28+
//~ ERROR E0006, pos 351, args "-err5"
29+
30+
-err6 =
31+
.attr = Attribute
32+
//~ ERROR E0006, pos 360, args "-err6"

tests/syntax/fixtures_behavior/variant_with_empty_pattern.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ key1 = {
55
err1 = {
66
*[one]
77
}
8-
//~ ERROR E0006, pos 51, args "value"
8+
//~ ERROR E0012, pos 51

tests/syntax/fixtures_structure/attribute_with_empty_pattern.json

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
"annotations": [
77
{
88
"type": "Annotation",
9-
"code": "E0006",
10-
"args": [
11-
"value"
12-
],
13-
"message": "Expected field: \"value\"",
9+
"code": "E0012",
10+
"args": [],
11+
"message": "Expected value",
1412
"span": {
1513
"type": "Span",
1614
"start": 26,
@@ -30,11 +28,9 @@
3028
"annotations": [
3129
{
3230
"type": "Annotation",
33-
"code": "E0006",
34-
"args": [
35-
"value"
36-
],
37-
"message": "Expected field: \"value\"",
31+
"code": "E0012",
32+
"args": [],
33+
"message": "Expected value",
3834
"span": {
3935
"type": "Span",
4036
"start": 46,
@@ -54,11 +50,9 @@
5450
"annotations": [
5551
{
5652
"type": "Annotation",
57-
"code": "E0006",
58-
"args": [
59-
"value"
60-
],
61-
"message": "Expected field: \"value\"",
53+
"code": "E0012",
54+
"args": [],
55+
"message": "Expected value",
6256
"span": {
6357
"type": "Span",
6458
"start": 87,
@@ -78,11 +72,9 @@
7872
"annotations": [
7973
{
8074
"type": "Annotation",
81-
"code": "E0006",
82-
"args": [
83-
"value"
84-
],
85-
"message": "Expected field: \"value\"",
75+
"code": "E0012",
76+
"args": [],
77+
"message": "Expected value",
8678
"span": {
8779
"type": "Span",
8880
"start": 108,
@@ -102,11 +94,9 @@
10294
"annotations": [
10395
{
10496
"type": "Annotation",
105-
"code": "E0006",
106-
"args": [
107-
"value"
108-
],
109-
"message": "Expected field: \"value\"",
97+
"code": "E0012",
98+
"args": [],
99+
"message": "Expected value",
110100
"span": {
111101
"type": "Span",
112102
"start": 149,

tests/syntax/fixtures_structure/message_with_empty_pattern.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"args": [
1111
"foo"
1212
],
13-
"message": "Expected entry \"foo\" to have a value or attributes",
13+
"message": "Expected message \"foo\" to have a value or attributes",
1414
"span": {
1515
"type": "Span",
1616
"start": 5,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-foo =
2+
.attr = Attribute
3+
4+
-bar =
5+
6+
-baz
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"type": "Resource",
3+
"body": [
4+
{
5+
"type": "Junk",
6+
"annotations": [
7+
{
8+
"type": "Annotation",
9+
"code": "E0006",
10+
"args": [
11+
"-foo"
12+
],
13+
"message": "Expected term \"-foo\" to have a value",
14+
"span": {
15+
"type": "Span",
16+
"start": 6,
17+
"end": 6
18+
}
19+
}
20+
],
21+
"content": "-foo =\n .attr = Attribute\n\n",
22+
"span": {
23+
"type": "Span",
24+
"start": 0,
25+
"end": 30
26+
}
27+
},
28+
{
29+
"type": "Junk",
30+
"annotations": [
31+
{
32+
"type": "Annotation",
33+
"code": "E0006",
34+
"args": [
35+
"-bar"
36+
],
37+
"message": "Expected term \"-bar\" to have a value",
38+
"span": {
39+
"type": "Span",
40+
"start": 36,
41+
"end": 36
42+
}
43+
}
44+
],
45+
"content": "-bar =\n\n",
46+
"span": {
47+
"type": "Span",
48+
"start": 30,
49+
"end": 38
50+
}
51+
},
52+
{
53+
"type": "Junk",
54+
"annotations": [
55+
{
56+
"type": "Annotation",
57+
"code": "E0006",
58+
"args": [
59+
"-baz"
60+
],
61+
"message": "Expected term \"-baz\" to have a value",
62+
"span": {
63+
"type": "Span",
64+
"start": 42,
65+
"end": 42
66+
}
67+
}
68+
],
69+
"content": "-baz\n",
70+
"span": {
71+
"type": "Span",
72+
"start": 38,
73+
"end": 43
74+
}
75+
}
76+
],
77+
"span": {
78+
"type": "Span",
79+
"start": 0,
80+
"end": 43
81+
}
82+
}

0 commit comments

Comments
 (0)