Skip to content

Forbid tabs as inline blanks #167

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 2 commits into from
Aug 20, 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
3 changes: 2 additions & 1 deletion spec/fluent.ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ quote ::= "\""
*/
regular_char ::= [!-\uD7FF\uE000-\uFFFD]
text_char ::= blank_inline
| "\u0009"
| /\\u[0-9a-fA-F]{4}/
| (backslash backslash)
| (backslash "{")
Expand All @@ -95,7 +96,7 @@ quoted_text_char ::= (text_char - quote)
digit ::= [0-9]

/* Whitespace */
blank_inline ::= ("\u0020" | "\u0009")+
blank_inline ::= "\u0020"+
line_end ::= "\u000D\u000A"
| "\u000A"
| EOF
Expand Down
9 changes: 7 additions & 2 deletions syntax/abstract.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,18 @@ function join_of_type(Type, ...elements) {
}
}

const LEADING_BLANK = /^[ \n\r]*/;
const TRAILING_BLANK = /[ \n\r]*$/;

function trim_text_at_extremes(element, index, array) {
if (element instanceof FTL.TextElement) {
if (index === 0) {
element.value = element.value.trimLeft();
element.value = element.value.replace(
LEADING_BLANK, "");
}
if (index === array.length - 1) {
element.value = element.value.trimRight();
element.value = element.value.replace(
TRAILING_BLANK, "");
}
}
return element;
Expand Down
5 changes: 2 additions & 3 deletions syntax/grammar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ let regular_char =
let text_char = defer(() =>
either(
blank_inline,
string("\u0009"),
regex(/\\u[0-9a-fA-F]{4}/),
sequence(
backslash,
Expand Down Expand Up @@ -467,9 +468,7 @@ let digit = charset("0-9");
/* Whitespace */
let blank_inline =
repeat1(
either(
string("\u0020"),
string("\u0009")))
string("\u0020"))
.map(join);

let line_end =
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/tab.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# OK (tab after = is part of the value)
key01 = Value 01

# Error (tab before =)
key02 = Value 02

# Error (tab is not a valid indent)
key03 =
This line isn't properly indented.

# Partial Error (tab is not a valid indent)
key04 =
This line is indented by 4 spaces,
whereas this line by 1 tab.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This exhibits the same behavior as when a broken attribute is encountered. The body of the message up until the syntax error parses fine. Essentially, it's the same situation as:

key04 =
    This line is indented by 4 spaces,
junk here

70 changes: 70 additions & 0 deletions test/fixtures/tab.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"type": "Resource",
"body": [
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "key01"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": "\tValue 01"
}
]
},
"attributes": [],
"comment": {
"type": "Comment",
"content": "OK (tab after = is part of the value)"
}
},
{
"type": "Comment",
"content": "Error (tab before =)"
},
{
"type": "Junk",
"annotations": [],
"content": "key02\t= Value 02\n"
},
{
"type": "Comment",
"content": "Error (tab is not a valid indent)"
},
{
"type": "Junk",
"annotations": [],
"content": "key03 =\n\tThis line isn't properly indented.\n"
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "key04"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": "This line is indented by 4 spaces,"
}
]
},
"attributes": [],
"comment": {
"type": "Comment",
"content": "Partial Error (tab is not a valid indent)"
}
},
{
"type": "Junk",
"annotations": [],
"content": "\twhereas this line by 1 tab.\n"
}
]
}