Skip to content

Require = after the id (but not really) #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fluent-react/examples/text-input/src/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const MESSAGES_ALL = {
'pl': `
hello = Cześć { $username }!
hello-no-name = Witaj nieznajomy!
type-name
type-name =
.placeholder = Twoje imię
`,
'en-US': `
hello = Hello, { $username }!
hello-no-name = Hello, stranger!
type-name
type-name =
.placeholder = Your name
`,
};
Expand Down
45 changes: 33 additions & 12 deletions fluent-syntax/src/ftlstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ import { ParseError } from './errors';
import { includes } from './util';

const INLINE_WS = [' ', '\t'];
const SPECIAL_LINE_START_CHARS = ['}', '.', '[', '*'];

export class FTLParserStream extends ParserStream {
skipInlineWS() {
while (this.ch) {
if (!includes(INLINE_WS, this.ch)) {
break;
}
this.next();
}
}

peekInlineWS() {
let ch = this.currentPeek();
while (ch) {
Expand Down Expand Up @@ -46,13 +56,9 @@ export class FTLParserStream extends ParserStream {
}
}

skipInlineWS() {
while (this.ch) {
if (!includes(INLINE_WS, this.ch)) {
break;
}
this.next();
}
skipIndent() {
this.skipBlankLines();
this.skipInlineWS();
}

expectChar(ch) {
Expand Down Expand Up @@ -125,6 +131,24 @@ export class FTLParserStream extends ParserStream {
return isDigit;
}

isCharPatternStart(ch) {
return !includes(SPECIAL_LINE_START_CHARS, ch);
}

isPeekPatternStart() {
this.peekInlineWS();

const ch = this.currentPeek();

if (ch === '\n') {
return this.isPeekNextLinePatternStart();
}

const isPattern = this.isCharPatternStart(this.currentPeek());
this.resetPeek();
return isPattern;
}

isPeekNextLineZeroFourStyleComment() {
if (!this.currentPeekIs('\n')) {
return false;
Expand Down Expand Up @@ -234,7 +258,7 @@ export class FTLParserStream extends ParserStream {
return false;
}

isPeekNextNonBlankLinePattern() {
isPeekNextLinePatternStart() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the isPeekNextLine* methods only consider non-blank lines so I renamed this one to match the naming of all others.

if (!this.currentPeekIs('\n')) {
return false;
}
Expand All @@ -252,10 +276,7 @@ export class FTLParserStream extends ParserStream {
return false;
}

if (this.currentPeekIs('}') ||
this.currentPeekIs('.') ||
this.currentPeekIs('[') ||
this.currentPeekIs('*')) {
if (!this.isCharPatternStart(this.currentPeek())) {
this.resetPeek();
return false;
}
Expand Down
51 changes: 21 additions & 30 deletions fluent-syntax/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export default class FluentParser {
entries.push(entry);
}

ps.skipInlineWS();
ps.skipBlankLines();
}

Expand Down Expand Up @@ -258,12 +257,15 @@ export default class FluentParser {
let pattern;
let attrs;

// XXX Syntax 0.4 compatibility.
// XXX Replace with ps.expectChar('=').
if (ps.currentIs('=')) {
ps.next();
ps.skipInlineWS();
ps.skipBlankLines();

pattern = this.getPattern(ps);
if (ps.isPeekPatternStart()) {
ps.skipIndent();
pattern = this.getPattern(ps);
}
}

if (ps.isPeekNextLineAttributeStart()) {
Expand All @@ -278,29 +280,27 @@ export default class FluentParser {
}

getAttribute(ps) {
ps.expectIndent();
ps.expectChar('.');

const key = this.getPublicIdentifier(ps);

ps.skipInlineWS();
ps.expectChar('=');
ps.skipInlineWS();

const value = this.getPattern(ps);

if (value === undefined) {
throw new ParseError('E0006', 'value');
if (ps.isPeekPatternStart()) {
ps.skipIndent();
const value = this.getPattern(ps);
return new AST.Attribute(key, value);
}

return new AST.Attribute(key, value);
throw new ParseError('E0006', 'value');
}

getAttributes(ps) {
const attrs = [];

while (true) {
ps.expectIndent();

const attr = this.getAttribute(ps);
attrs.push(attr);

Expand Down Expand Up @@ -349,6 +349,8 @@ export default class FluentParser {
}

getVariant(ps, hasDefault) {
ps.expectIndent();

let defaultIndex = false;

if (ps.currentIs('*')) {
Expand All @@ -366,24 +368,20 @@ export default class FluentParser {

ps.expectChar(']');

ps.skipInlineWS();

const value = this.getPattern(ps);

if (!value) {
throw new ParseError('E0006', 'value');
if (ps.isPeekPatternStart()) {
ps.skipIndent();
const value = this.getPattern(ps);
return new AST.Variant(key, value, defaultIndex);
}

return new AST.Variant(key, value, defaultIndex);
throw new ParseError('E0006', 'value');
}

getVariants(ps) {
const variants = [];
let hasDefault = false;

while (true) {
ps.expectIndent();

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

if (variant.default) {
Expand Down Expand Up @@ -459,18 +457,12 @@ export default class FluentParser {
const elements = [];
ps.skipInlineWS();

// Special-case: trim leading whitespace and newlines.
if (ps.isPeekNextNonBlankLinePattern()) {
Copy link
Contributor Author

@stasm stasm Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed any more because calls to getPattern are guarded by isPeekPatternStart, followed by skipIndent. This effectively fixes Bug 1406880 - Decide where multiline Pattern spans should start.

ps.skipBlankLines();
ps.skipInlineWS();
}

let ch;
while ((ch = ps.current())) {

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

Expand All @@ -491,13 +483,12 @@ export default class FluentParser {

let ch;
while ((ch = ps.current())) {

if (ch === '{') {
return new AST.TextElement(buffer);
}

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

Expand Down
2 changes: 1 addition & 1 deletion fluent-syntax/src/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ function serializeMessage(message) {
}

parts.push(serializeIdentifier(message.id));
parts.push(' =');

if (message.value) {
parts.push(' =');
parts.push(serializeValue(message.value));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
key = Value
.label =
//~ ERROR E0006, pos 24, args "value"
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
key = {
key1 = {
*[one] {""}
}

err1 = {
*[one]
}
//~ ERROR E0006, pos 51, args "value"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
key1 = Value 1
.attr =

key2 =
.attr =

key3 =
.attr1 = Attr 1
.attr2 =

key4 =
.attr1 =
.attr2 = Attr 2

key5 =
.attr1 =
.attr2 =
Loading