Skip to content

Commit

Permalink
Parser syntax sparse bl (#76)
Browse files Browse the repository at this point in the history
* Handle sparse messages in fluent-syntax

* Fix tests affected by the sparse message parsing change

* Handle sparse messages in fluent runtime

* Fix linting errors

* Apply reviewers feedback

* One more indent
  • Loading branch information
zbraniecki committed Oct 4, 2017
1 parent 64ef212 commit 7e52517
Show file tree
Hide file tree
Showing 10 changed files with 613 additions and 26 deletions.
32 changes: 31 additions & 1 deletion fluent-syntax/src/ftlstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ export class FTLParserStream extends ParserStream {
}
}

peekBlankLines() {
while (true) {
const lineStart = this.getPeekIndex();

this.peekInlineWS();

if (this.currentPeekIs('\n')) {
this.peek();
} else {
this.resetPeek(lineStart);
break;
}
}
}

skipInlineWS() {
while (this.ch) {
if (!includes(INLINE_WS, this.ch)) {
Expand All @@ -54,6 +69,13 @@ export class FTLParserStream extends ParserStream {
throw new ParseError('E0003', ch);
}

expectIndent() {
this.expectChar('\n');
this.skipBlankLines();
this.expectChar(' ');
this.skipInlineWS();
}

takeCharIf(ch) {
if (this.ch === ch) {
this.next();
Expand Down Expand Up @@ -94,6 +116,8 @@ export class FTLParserStream extends ParserStream {

this.peek();

this.peekBlankLines();

const ptr = this.getPeekIndex();

this.peekInlineWS();
Expand Down Expand Up @@ -122,6 +146,8 @@ export class FTLParserStream extends ParserStream {

this.peek();

this.peekBlankLines();

const ptr = this.getPeekIndex();

this.peekInlineWS();
Expand All @@ -140,13 +166,15 @@ export class FTLParserStream extends ParserStream {
return false;
}

isPeekNextLinePattern() {
isPeekNextNonBlankLinePattern() {
if (!this.currentPeekIs('\n')) {
return false;
}

this.peek();

this.peekBlankLines();

const ptr = this.getPeekIndex();

this.peekInlineWS();
Expand Down Expand Up @@ -176,6 +204,8 @@ export class FTLParserStream extends ParserStream {

this.peek();

this.peekBlankLines();

const ptr = this.getPeekIndex();

this.peekInlineWS();
Expand Down
25 changes: 10 additions & 15 deletions fluent-syntax/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export default class FluentParser {
if (ps.currentIs('=')) {
ps.next();
ps.skipInlineWS();
ps.skipBlankLines();

pattern = this.getPattern(ps);
}
Expand Down Expand Up @@ -241,8 +242,7 @@ export default class FluentParser {
const attrs = [];

while (true) {
ps.expectChar('\n');
ps.skipInlineWS();
ps.expectIndent();

const attr = this.getAttribute(ps);
attrs.push(attr);
Expand All @@ -264,8 +264,7 @@ export default class FluentParser {
const tags = [];

while (true) {
ps.expectChar('\n');
ps.skipInlineWS();
ps.expectIndent();

const tag = this.getTag(ps);
tags.push(tag);
Expand Down Expand Up @@ -340,8 +339,7 @@ export default class FluentParser {
let hasDefault = false;

while (true) {
ps.expectChar('\n');
ps.skipInlineWS();
ps.expectIndent();

const variant = this.getVariant(ps, hasDefault);

Expand Down Expand Up @@ -419,7 +417,7 @@ export default class FluentParser {
ps.skipInlineWS();

// Special-case: trim leading whitespace and newlines.
if (ps.isPeekNextLinePattern()) {
if (ps.isPeekNextNonBlankLinePattern()) {
ps.skipBlankLines();
ps.skipInlineWS();
}
Expand All @@ -429,7 +427,7 @@ export default class FluentParser {

// The end condition for getPattern's while loop is a newline
// which is not followed by a valid pattern continuation.
if (ch === '\n' && !ps.isPeekNextLinePattern()) {
if (ch === '\n' && !ps.isPeekNextNonBlankLinePattern()) {
break;
}

Expand All @@ -456,7 +454,7 @@ export default class FluentParser {
}

if (ch === '\n') {
if (!ps.isPeekNextLinePattern()) {
if (!ps.isPeekNextNonBlankLinePattern()) {
return new AST.TextElement(buffer);
}

Expand Down Expand Up @@ -498,9 +496,7 @@ export default class FluentParser {
if (ps.isPeekNextLineVariantStart()) {
const variants = this.getVariants(ps);

ps.expectChar('\n');
ps.expectChar(' ');
ps.skipInlineWS();
ps.expectIndent();

return new AST.SelectExpression(null, variants);
}
Expand All @@ -523,13 +519,12 @@ export default class FluentParser {

const variants = this.getVariants(ps);


if (variants.length === 0) {
throw new ParseError('E0011');
}

ps.expectChar('\n');
ps.expectChar(' ');
ps.skipInlineWS();
ps.expectIndent();

return new AST.SelectExpression(selector, variants);
}
Expand Down
13 changes: 10 additions & 3 deletions fluent-syntax/src/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,16 @@ export class ParserStream {
return ret === ch;
}

resetPeek() {
this.peekIndex = this.index;
this.peekEnd = this.iterEnd;
resetPeek(pos) {
if (pos) {
if (pos < this.peekIndex) {
this.peekEnd = false;
}
this.peekIndex = pos;
} else {
this.peekIndex = this.index;
this.peekEnd = this.iterEnd;
}
}

skipToPeek() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
key = { $foo ->
//~ ERROR E0003, pos 16, args "["
//~ ERROR E0003, pos 16, args " "
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"elements": [],
"span": {
"type": "Span",
"start": 6,
"end": 6
"start": 7,
"end": 7
}
},
"attributes": [],
Expand All @@ -28,7 +28,7 @@
"span": {
"type": "Span",
"start": 0,
"end": 6
"end": 7
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions fluent-syntax/test/fixtures_structure/placeable_at_eol.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
],
"span": {
"type": "Span",
"start": 6,
"start": 7,
"end": 131
}
},
Expand Down Expand Up @@ -127,7 +127,7 @@
],
"span": {
"type": "Span",
"start": 139,
"start": 140,
"end": 184
}
},
Expand Down
46 changes: 46 additions & 0 deletions fluent-syntax/test/fixtures_structure/sparse-messages.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
key =
Value
key2


.attr = Attribute


key3 =
Value
Value2
Value 4
Value3
.attr2 = Attr 2
key4 = Value
#tag1
#tag2
key5 = Value 5
key6 = {
[one] One
*[two] Two
}
Loading

0 comments on commit 7e52517

Please sign in to comment.