Skip to content

Commit 764e698

Browse files
authored
Don't include the terminal line break in Entries (#259)
Implement projectfluent/fluent#153.
1 parent 12ee312 commit 764e698

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+159
-365
lines changed

fluent-syntax/src/ast.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,10 @@ export class Resource extends SyntaxNode {
2626
}
2727
}
2828

29-
export class Entry extends SyntaxNode {
30-
constructor() {
31-
super();
32-
this.type = "Entry";
33-
this.annotations = [];
34-
}
35-
36-
addAnnotation(annot) {
37-
this.annotations.push(annot);
38-
}
39-
}
29+
/*
30+
* An abstract base class for useful elements of Resource.body.
31+
*/
32+
export class Entry extends SyntaxNode {}
4033

4134
export class Message extends Entry {
4235
constructor(id, value = null, attributes = [], comment = null) {
@@ -257,12 +250,17 @@ export class Function extends Identifier {
257250
}
258251
}
259252

260-
export class Junk extends Entry {
253+
export class Junk extends SyntaxNode {
261254
constructor(content) {
262255
super();
263256
this.type = "Junk";
257+
this.annotations = [];
264258
this.content = content;
265259
}
260+
261+
addAnnotation(annot) {
262+
this.annotations.push(annot);
263+
}
266264
}
267265

268266
export class Span extends BaseNode {

fluent-syntax/src/ftlstream.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ export class FTLParserStream extends ParserStream {
8585
}
8686

8787
expectLineEnd() {
88-
if (this.ch) {
89-
return this.expectChar("\n");
88+
if (this.ch === undefined) {
89+
// EOF is a valid line end in Fluent.
90+
return true;
9091
}
9192

92-
// EOF is a valid line end in Fluent.
93-
return true;
93+
return this.expectChar("\n");
9494
}
9595

9696
takeChar(f) {

fluent-syntax/src/parser.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ export default class FluentParser {
125125
const entryStartPos = ps.getIndex();
126126

127127
try {
128-
return this.getEntry(ps);
128+
const entry = this.getEntry(ps);
129+
ps.expectLineEnd();
130+
return entry;
129131
} catch (err) {
130132
if (!(err instanceof ParseError)) {
131133
throw err;
@@ -198,13 +200,6 @@ export default class FluentParser {
198200
}
199201
}
200202

201-
// Add the terminal line break.
202-
if (!ps.currentIs(undefined)) {
203-
content += ps.current();
204-
ps.next();
205-
}
206-
207-
208203
let Comment;
209204
switch (level) {
210205
case 0:
@@ -241,7 +236,6 @@ export default class FluentParser {
241236
throw new ParseError("E0005", id.name);
242237
}
243238

244-
ps.expectLineEnd();
245239
return new AST.Message(id, pattern, attrs);
246240
}
247241

@@ -262,7 +256,6 @@ export default class FluentParser {
262256
var attrs = this.getAttributes(ps);
263257
}
264258

265-
ps.expectLineEnd();
266259
return new AST.Term(id, value, attrs);
267260
}
268261

fluent-syntax/src/serializer.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ export default class FluentSerializer {
7575

7676

7777
function serializeComment(comment, prefix = "#") {
78-
// Remove the trailing newline.
79-
const content = comment.content.slice(0, comment.content.length - 1);
80-
const prefixed = content.split("\n").map(
78+
const prefixed = comment.content.split("\n").map(
8179
line => line.length ? `${prefix} ${line}` : prefix
8280
).join("\n");
83-
// Re-add the trailing newline.
81+
// Add the trailing newline.
8482
return `${prefixed}\n`;
8583
}
8684

fluent-syntax/test/behavior_test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ function getCodeName(code) {
2525
switch (code[0]) {
2626
case 'E':
2727
return `ERROR ${code}`;
28-
case 'W':
29-
return `WARNING ${code}`;
30-
case 'H':
31-
return `HINT ${code}`;
3228
default:
3329
throw new Error('Unknown Annotation code');
3430
}
@@ -53,7 +49,10 @@ export function serializeAnnotation(annot) {
5349
}
5450

5551
function toDirectives(annots, cur) {
56-
return annots.concat(cur.annotations.map(serializeAnnotation));
52+
if (cur.type === "Junk") {
53+
return annots.concat(cur.annotations.map(serializeAnnotation));
54+
}
55+
return annots;
5756
}
5857

5958
const fixtures = join(__dirname, 'fixtures_behavior');

fluent-syntax/test/entry_test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ suite('Parse entry', function() {
2323
],
2424
"type": "Pattern"
2525
},
26-
"annotations": [],
2726
"attributes": [],
2827
"type": "Message",
2928
"id": {
@@ -52,7 +51,6 @@ suite('Parse entry', function() {
5251
],
5352
"type": "Pattern"
5453
},
55-
"annotations": [],
5654
"attributes": [],
5755
"type": "Message",
5856
"id": {
@@ -110,7 +108,6 @@ suite('Parse entry', function() {
110108
],
111109
"type": "Pattern"
112110
},
113-
"annotations": [],
114111
"attributes": [],
115112
"type": "Message",
116113
"id": {
@@ -169,7 +166,6 @@ suite('Serialize entry', function() {
169166
],
170167
"type": "Pattern"
171168
},
172-
"annotations": [],
173169
"attributes": [],
174170
"type": "Message",
175171
"id": {

fluent-syntax/test/fixtures_reference/call_expressions.json

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"body": [
44
{
55
"type": "Message",
6-
"annotations": [],
76
"id": {
87
"type": "Identifier",
98
"name": "positional-args"
@@ -46,7 +45,6 @@
4645
},
4746
{
4847
"type": "Message",
49-
"annotations": [],
5048
"id": {
5149
"type": "Identifier",
5250
"name": "named-args"
@@ -96,7 +94,6 @@
9694
},
9795
{
9896
"type": "Message",
99-
"annotations": [],
10097
"id": {
10198
"type": "Identifier",
10299
"name": "dense-named-args"
@@ -146,7 +143,6 @@
146143
},
147144
{
148145
"type": "Message",
149-
"annotations": [],
150146
"id": {
151147
"type": "Identifier",
152148
"name": "mixed-args"
@@ -212,8 +208,7 @@
212208
},
213209
{
214210
"type": "Comment",
215-
"annotations": [],
216-
"content": "ERROR Positional arg must not follow keyword args\n"
211+
"content": "ERROR Positional arg must not follow keyword args"
217212
},
218213
{
219214
"type": "Junk",
@@ -222,8 +217,7 @@
222217
},
223218
{
224219
"type": "Comment",
225-
"annotations": [],
226-
"content": "ERROR Named arguments must be unique\n"
220+
"content": "ERROR Named arguments must be unique"
227221
},
228222
{
229223
"type": "Junk",
@@ -232,12 +226,10 @@
232226
},
233227
{
234228
"type": "GroupComment",
235-
"annotations": [],
236-
"content": "Whitespace around arguments\n"
229+
"content": "Whitespace around arguments"
237230
},
238231
{
239232
"type": "Message",
240-
"annotations": [],
241233
"id": {
242234
"type": "Identifier",
243235
"name": "sparse-inline-call"
@@ -288,7 +280,6 @@
288280
},
289281
{
290282
"type": "Message",
291-
"annotations": [],
292283
"id": {
293284
"type": "Identifier",
294285
"name": "empty-inline-call"
@@ -315,7 +306,6 @@
315306
},
316307
{
317308
"type": "Message",
318-
"annotations": [],
319309
"id": {
320310
"type": "Identifier",
321311
"name": "multiline-call"
@@ -366,7 +356,6 @@
366356
},
367357
{
368358
"type": "Message",
369-
"annotations": [],
370359
"id": {
371360
"type": "Identifier",
372361
"name": "sparse-multiline-call"
@@ -417,7 +406,6 @@
417406
},
418407
{
419408
"type": "Message",
420-
"annotations": [],
421409
"id": {
422410
"type": "Identifier",
423411
"name": "empty-multiline-call"
@@ -444,8 +432,7 @@
444432
},
445433
{
446434
"type": "GroupComment",
447-
"annotations": [],
448-
"content": "Syntax errors for multiline call expressions\n"
435+
"content": "Syntax errors for multiline call expressions"
449436
},
450437
{
451438
"type": "Junk",
@@ -489,12 +476,10 @@
489476
},
490477
{
491478
"type": "GroupComment",
492-
"annotations": [],
493-
"content": "Optional trailing comma\n"
479+
"content": "Optional trailing comma"
494480
},
495481
{
496482
"type": "Message",
497-
"annotations": [],
498483
"id": {
499484
"type": "Identifier",
500485
"name": "one-argument"
@@ -526,7 +511,6 @@
526511
},
527512
{
528513
"type": "Message",
529-
"annotations": [],
530514
"id": {
531515
"type": "Identifier",
532516
"name": "many-arguments"
@@ -566,7 +550,6 @@
566550
},
567551
{
568552
"type": "Message",
569-
"annotations": [],
570553
"id": {
571554
"type": "Identifier",
572555
"name": "inline-sparse-args"
@@ -606,7 +589,6 @@
606589
},
607590
{
608591
"type": "Message",
609-
"annotations": [],
610592
"id": {
611593
"type": "Identifier",
612594
"name": "mulitline-args"
@@ -642,7 +624,6 @@
642624
},
643625
{
644626
"type": "Message",
645-
"annotations": [],
646627
"id": {
647628
"type": "Identifier",
648629
"name": "mulitline-sparse-args"
@@ -678,8 +659,7 @@
678659
},
679660
{
680661
"type": "GroupComment",
681-
"annotations": [],
682-
"content": "Syntax errors for trailing comma\n"
662+
"content": "Syntax errors for trailing comma"
683663
},
684664
{
685665
"type": "Junk",
@@ -688,12 +668,10 @@
688668
},
689669
{
690670
"type": "GroupComment",
691-
"annotations": [],
692-
"content": "Whitespace in named arguments\n"
671+
"content": "Whitespace in named arguments"
693672
},
694673
{
695674
"type": "Message",
696-
"annotations": [],
697675
"id": {
698676
"type": "Identifier",
699677
"name": "sparse-named-arg"
@@ -754,8 +732,7 @@
754732
},
755733
{
756734
"type": "GroupComment",
757-
"annotations": [],
758-
"content": "Syntax errors for named arguments\n"
735+
"content": "Syntax errors for named arguments"
759736
},
760737
{
761738
"type": "Junk",

0 commit comments

Comments
 (0)