Skip to content

Commit 32bca38

Browse files
authored
Merge pull request #38 from stasm/fix-compare-locales
Parser fixes required to update compare-locales to 0.6
2 parents 7b80850 + 1f3aeb6 commit 32bca38

10 files changed

+65
-29
lines changed

fluent/syntax/ast.py

-3
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,7 @@ def __init__(self, content=None, **kwargs):
285285

286286
class Comment(BaseComment):
287287
def __init__(self, content=None, **kwargs):
288-
zero_four_style = kwargs.pop("zero_four_style", False)
289288
super(Comment, self).__init__(content, **kwargs)
290-
if zero_four_style:
291-
self.zero_four_style = True
292289

293290

294291
class GroupComment(BaseComment):

fluent/syntax/parser.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ def get_entry(self, ps):
101101
ps.expect_char('\n' if ps.current() else None)
102102

103103
if ps.current_is('['):
104-
self.skip_section(ps)
105-
if comment:
106-
return ast.GroupComment(comment.content)
107-
return None
104+
group_comment = self.get_group_comment_from_section(ps, comment)
105+
if comment and self.with_spans:
106+
# The Group Comment should start where the section comment
107+
# starts.
108+
group_comment.span.start = comment.span.start
109+
return group_comment
108110

109111
if ps.is_entry_id_start() \
110112
and (comment is None or isinstance(comment, ast.Comment)):
@@ -182,7 +184,8 @@ def get_comment(self, ps):
182184
elif level == 2:
183185
return ast.ResourceComment(content)
184186

185-
def skip_section(self, ps):
187+
@with_span
188+
def get_group_comment_from_section(self, ps, comment):
186189
ps.expect_char('[')
187190
ps.expect_char('[')
188191

@@ -195,8 +198,12 @@ def skip_section(self, ps):
195198
ps.expect_char(']')
196199
ps.expect_char(']')
197200

198-
ps.skip_inline_ws()
199-
ps.next()
201+
if comment:
202+
return ast.GroupComment(comment.content)
203+
204+
# A Section without a comment is like an empty Group Comment.
205+
# Semantically it ends the previous group and starts a new one.
206+
return ast.GroupComment('')
200207

201208
@with_span
202209
def get_message(self, ps, comment):
@@ -231,7 +238,6 @@ def get_message(self, ps, comment):
231238

232239
@with_span
233240
def get_attribute(self, ps):
234-
ps.expect_indent()
235241
ps.expect_char('.')
236242

237243
key = self.get_identifier(ps)
@@ -250,6 +256,7 @@ def get_attributes(self, ps):
250256
attrs = []
251257

252258
while True:
259+
ps.expect_indent()
253260
attr = self.get_attribute(ps)
254261
attrs.append(attr)
255262

@@ -287,8 +294,6 @@ def get_variant_key(self, ps):
287294

288295
@with_span
289296
def get_variant(self, ps, has_default):
290-
ps.expect_indent()
291-
292297
default_index = False
293298

294299
if ps.current_is('*'):
@@ -315,6 +320,7 @@ def get_variants(self, ps):
315320
has_default = False
316321

317322
while True:
323+
ps.expect_indent()
318324
variant = self.get_variant(ps, has_default)
319325

320326
if variant.default:

tests/syntax/fixtures_structure/elements_indent.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
},
127127
"span": {
128128
"type": "Span",
129-
"start": 37,
129+
"start": 42,
130130
"end": 61
131131
}
132132
}

tests/syntax/fixtures_structure/section.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
{
22
"type": "Resource",
3-
"body": [],
3+
"body": [
4+
{
5+
"type": "GroupComment",
6+
"annotations": [],
7+
"content": "",
8+
"span": {
9+
"type": "Span",
10+
"start": 1,
11+
"end": 24
12+
}
13+
}
14+
],
415
"span": {
516
"type": "Span",
617
"start": 0,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Comment
2+
[[ Section ]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "Resource",
3+
"body": [
4+
{
5+
"type": "GroupComment",
6+
"annotations": [],
7+
"content": "Comment",
8+
"span": {
9+
"type": "Span",
10+
"start": 0,
11+
"end": 24
12+
}
13+
}
14+
],
15+
"span": {
16+
"type": "Span",
17+
"start": 0,
18+
"end": 25
19+
}
20+
}

tests/syntax/fixtures_structure/sparse-messages.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
},
8787
"span": {
8888
"type": "Span",
89-
"start": 23,
89+
"start": 30,
9090
"end": 47
9191
}
9292
}
@@ -162,7 +162,7 @@
162162
},
163163
"span": {
164164
"type": "Span",
165-
"start": 102,
165+
"start": 110,
166166
"end": 125
167167
}
168168
}
@@ -267,7 +267,7 @@
267267
"default": false,
268268
"span": {
269269
"type": "Span",
270-
"start": 152,
270+
"start": 164,
271271
"end": 173
272272
}
273273
},
@@ -304,7 +304,7 @@
304304
"default": true,
305305
"span": {
306306
"type": "Span",
307-
"start": 173,
307+
"start": 186,
308308
"end": 196
309309
}
310310
}

tests/syntax/fixtures_structure/syntax_zero_four.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"span": {
4949
"type": "Span",
50-
"start": 4,
50+
"start": 9,
5151
"end": 24
5252
}
5353
}
@@ -105,7 +105,7 @@
105105
},
106106
"span": {
107107
"type": "Span",
108-
"start": 30,
108+
"start": 35,
109109
"end": 50
110110
}
111111
},
@@ -141,7 +141,7 @@
141141
},
142142
"span": {
143143
"type": "Span",
144-
"start": 50,
144+
"start": 55,
145145
"end": 70
146146
}
147147
}

tests/syntax/fixtures_structure/term.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"default": true,
5656
"span": {
5757
"type": "Span",
58-
"start": 19,
58+
"start": 27,
5959
"end": 48
6060
}
6161
},
@@ -92,7 +92,7 @@
9292
"default": false,
9393
"span": {
9494
"type": "Span",
95-
"start": 48,
95+
"start": 57,
9696
"end": 78
9797
}
9898
}
@@ -149,7 +149,7 @@
149149
},
150150
"span": {
151151
"type": "Span",
152-
"start": 84,
152+
"start": 89,
153153
"end": 108
154154
}
155155
}
@@ -347,7 +347,7 @@
347347
"default": false,
348348
"span": {
349349
"type": "Span",
350-
"start": 220,
350+
"start": 229,
351351
"end": 289
352352
}
353353
},
@@ -409,7 +409,7 @@
409409
"default": false,
410410
"span": {
411411
"type": "Span",
412-
"start": 289,
412+
"start": 298,
413413
"end": 358
414414
}
415415
},
@@ -480,7 +480,7 @@
480480
"default": true,
481481
"span": {
482482
"type": "Span",
483-
"start": 358,
483+
"start": 366,
484484
"end": 431
485485
}
486486
}

tests/syntax/fixtures_structure/variant_with_empty_pattern.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"default": true,
7272
"span": {
7373
"type": "Span",
74-
"start": 17,
74+
"start": 25,
7575
"end": 36
7676
}
7777
}

0 commit comments

Comments
 (0)