Skip to content

Commit

Permalink
allow nested text expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jun 10, 2019
1 parent 45ad76f commit af0a86e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
16 changes: 13 additions & 3 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,23 @@
} else if (_char === "{") {
// If a sequence of characters begins with "{", process it as an
// expression token.
var expression = ""; // Consume the input until the end of the expression.
var expression = ""; // Keep a stack of opened objects.

var opened = 0; // Consume the input until the end of the expression.

for (i += 1; i < input.length; i++) {
var _char2 = input[i];

if (_char2 === "}") {
break;
if (_char2 === "{") {
opened += 1;
expression += _char2;
} else if (_char2 === "}") {
if (opened === 0) {
break;
} else {
opened -= 1;
expression += _char2;
}
} else {
expression += _char2;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/moon/dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions packages/moon/src/compiler/lexer/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,23 @@ export function lex(input) {
// expression token.
let expression = "";

// Keep a stack of opened objects.
let opened = 0;

// Consume the input until the end of the expression.
for (i += 1; i < input.length; i++) {
const char = input[i];

if (char === "}") {
break;
if (char === "{") {
opened += 1;
expression += char;
} else if (char === "}") {
if (opened === 0) {
break;
} else {
opened -= 1;
expression += char;
}
} else {
expression += char;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/moon/test/compiler/lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test("lex text inside tag", () => {
})

test("lex expression", () => {
expect(lex(`{data + 1}`)).toEqual([{"attributes": {"": {"value": "data.data + 1", isStatic: false}}, "closed": true, "type": "tagOpen", "value": "text"}]);
expect(lex(`{data + 1 + {foo: true, bar: false}}`)).toEqual([{"attributes": {"": {"value": "data.data + 1 + {foo: true, bar: false}", isStatic: false}}, "closed": true, "type": "tagOpen", "value": "text"}]);
});

test("lex attributes", () => {
Expand Down

0 comments on commit af0a86e

Please sign in to comment.