Skip to content
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
2 changes: 2 additions & 0 deletions fluent-syntax/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function getErrorMessage(code, args) {
return 'Expected variant key';
case 'E0014':
return 'Expected literal';
case 'E0015':
return 'Only one variant can be marked as default (*)';
default:
return code;
}
Expand Down
62 changes: 61 additions & 1 deletion fluent-syntax/src/ftlstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export class FTLParserStream extends ParserStream {
return true;
}

if (ch === '\n') {
// Unicode Character 'SYMBOL FOR NEWLINE' (U+2424)
throw new ParseError('E0003', '\u2424');
}

throw new ParseError('E0003', ch);
}

Expand All @@ -56,14 +61,18 @@ export class FTLParserStream extends ParserStream {

takeChar(f) {
const ch = this.ch;
if (f(ch)) {
if (ch !== undefined && f(ch)) {
this.next();
return ch;
}
return undefined;
}

isIDStart() {
if (this.ch === undefined) {
return false;
}

const cc = this.ch.charCodeAt(0);
return ((cc >= 97 && cc <= 122) || // a-z
(cc >= 65 && cc <= 90) || // A-Z
Expand Down Expand Up @@ -98,8 +107,15 @@ export class FTLParserStream extends ParserStream {

this.peek();

const ptr = this.getPeekIndex();

this.peekLineWS();

if (this.getPeekIndex() - ptr === 0) {
this.resetPeek();
return false;
}

if (this.currentPeekIs('*')) {
this.peek();
}
Expand All @@ -119,8 +135,15 @@ export class FTLParserStream extends ParserStream {

this.peek();

const ptr = this.getPeekIndex();

this.peekLineWS();

if (this.getPeekIndex() - ptr === 0) {
this.resetPeek();
return false;
}

if (this.currentPeekIs('.')) {
this.resetPeek();
return true;
Expand All @@ -130,15 +153,52 @@ export class FTLParserStream extends ParserStream {
return false;
}

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

this.peek();

const ptr = this.getPeekIndex();

this.peekLineWS();

if (this.getPeekIndex() - ptr === 0) {
this.resetPeek();
return false;
}

if (this.currentPeekIs('}') ||
this.currentPeekIs('.') ||
this.currentPeekIs('#') ||
this.currentPeekIs('[') ||
this.currentPeekIs('*') ||
this.currentPeekIs('{')) {
this.resetPeek();
return false;
}

this.resetPeek();
return true;
}

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

this.peek();

const ptr = this.getPeekIndex();

this.peekLineWS();

if (this.getPeekIndex() - ptr === 0) {
this.resetPeek();
return false;
}

if (this.currentPeekIs('#')) {
this.resetPeek();
return true;
Expand Down
14 changes: 7 additions & 7 deletions fluent-syntax/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function getMessage(ps, comment) {
}

if (pattern === undefined && attrs === undefined && tags === undefined) {
throw new ParseError('E0005', id);
throw new ParseError('E0005', id.name);
}

return new AST.Message(id, pattern, attrs, tags, comment);
Expand Down Expand Up @@ -254,6 +254,9 @@ function getVariants(ps) {
ps.skipLineWS();

if (ps.currentIs('*')) {
if (hasDefault) {
throw new ParseError('E0015');
}
ps.next();
defaultIndex = true;
hasDefault = true;
Expand Down Expand Up @@ -350,15 +353,12 @@ function getPattern(ps) {
break;
}

ps.peek();

if (!ps.currentPeekIs(' ')) {
ps.resetPeek();
if (!ps.isPeekNextLinePattern()) {
break;
}

ps.peekLineWS();
ps.skipToPeek();
ps.next();
ps.skipLineWS();

firstLine = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
key = { foo.23 }

//~ ERROR E0004, pos 12, args "a-zA-Z"

key = { foo. }

//~ ERROR E0004, pos 31, args "a-zA-Z"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo = Value
.attr = Value 2
//~ ERROR E0002, pos 12
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key = Value
.label =
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = Value
.label
//~ ERROR E0003, pos 22, args "="
14 changes: 14 additions & 0 deletions fluent-syntax/test/fixtures_behavior/broken_number.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
key = { -2.4.5 }
//~ ERROR E0003, pos 12, args "}"

key = { -2.4. }
//~ ERROR E0003, pos 30, args "}"

key = { -.4 }
//~ ERROR E0004, pos 44, args "0-9"

key = { -2..4 }
//~ ERROR E0004, pos 61, args "0-9"

key = { 24d }
//~ ERROR E0003, pos 77, args "}"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
key = { BUILTIN(23, ) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key = { BUILTIN(2: "foo") }
//~ ERROR E0009, pos 17
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key = { BUILTIN(key: foo) }
//~ ERROR E0006, pos 21, args "value"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This is a normal comment
/ but this is not
//~ ERROR E0003, pos 29, args "/"
1 change: 1 addition & 0 deletions fluent-syntax/test/fixtures_behavior/comment_with_eof.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This is a comment with no new line
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

/ Test
//~ ERROR E0003, pos 2, args "/"
3 changes: 3 additions & 0 deletions fluent-syntax/test/fixtures_behavior/leading_empty_lines.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


foo = Value
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@




foo = Value
3 changes: 3 additions & 0 deletions fluent-syntax/test/fixtures_behavior/multiline_string.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = { BUILTIN(key: "
text
") }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = Value
Value 2
//~ ERROR E0002, pos 12
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = Value
.2 = Foo
//~ ERROR E0004, pos 17, args "a-zA-Z"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = { $num

//~ ERROR E0003, pos 12, args "}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
key = Value
.label = Value
.accesskey = K
//~ ERROR E0002, pos 31
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
key = Value
#tag
#tag2
//~ ERROR E0002, pos 21
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ This is a broken section ]]
//~ ERROR E0003, pos 1, args "["
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[ This is
a broken section]]
//~ ERROR E0003, pos 10, args "]"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[ This is a correct section ]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[ This is a broken section ]
//~ ERROR E0003, pos 29, args "]"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key = { $foo $faa }
//~ ERROR E0003, pos 13, args "}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key = { $foo - }
//~ ERROR E0003, pos 13, args "}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
key = { $foo -> }
//~ ERROR E0003, pos 16, args "␤"

key = { $foo ->
}
//~ ERROR E0003, pos 39, args "["
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key = { $foo ->
//~ ERROR E0003, pos 16, args "["
2 changes: 2 additions & 0 deletions fluent-syntax/test/fixtures_behavior/single_char_id.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
k = Value
.l = Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo
//~ ERROR E0005, pos 3, args "foo"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
key = Value
.label = Foo
#masculine
//~ ERROR E0012, pos 28
3 changes: 3 additions & 0 deletions fluent-syntax/test/fixtures_behavior/tag_starts_from_nl.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = Value
#tag
//~ ERROR E0002, pos 12
3 changes: 3 additions & 0 deletions fluent-syntax/test/fixtures_behavior/unknown_entry_start.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

8Foo = Foo
//~ ERROR E0002, pos 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = { $foo ->
*[
//~ ERROR E0004, pos 22, args "a-zA-Z"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key = { foo[] }
//~ ERROR E0004, pos 12, args "a-zA-Z"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
key = {
*[one] Value
}
//~ ERROR E0004, pos 7, args "a-zA-Z"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = {
*[-2] Foo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = {
*[one]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
key = {
*[ one] Foo
}
//~ ERROR E0004, pos 18, args "a-zA-Z"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
key = {
*[New York] Nowy Jork
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
key = {
*[one] Foo
*[two] Two
}
//~ ERROR E0015, pos 27
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo =
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "Resource",
"body": [
{
"type": "Message",
"span": {
"type": "Span",
"start": 0,
"end": 6
},
"annotations": [],
"id": {
"type": "Identifier",
"name": "foo"
},
"value": {
"type": "Pattern",
"elements": []
},
"attributes": null,
"tags": null,
"comment": null
}
],
"comment": null
}
2 changes: 2 additions & 0 deletions fluent-syntax/test/fixtures_structure/resource_comment.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This is a resource wide comment
// It's multiline
14 changes: 14 additions & 0 deletions fluent-syntax/test/fixtures_structure/resource_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": "Resource",
"body": [],
"comment": {
"type": "Comment",
"span": {
"type": "Span",
"start": 0,
"end": 53
},
"annotations": [],
"content": "This is a resource wide comment\nIt's multiline"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This is a comment
// This comment is multiline
//
Loading