Skip to content

Commit

Permalink
fix: 修复一些解析错误
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Jan 17, 2018
1 parent b079256 commit 91dae25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
14 changes: 14 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,17 @@ test("Invalid expression", t => {
});
t.deepEqual(r, "Hello Mary {{}");
});

test("Invalid expression", t => {
const r = compiler("Hello {{name}} {{} abc", {
name: "Mary"
});
t.deepEqual(r, "Hello Mary {{} abc");
});

test("Invalid expression", t => {
const r = compiler("Hello {{name}} {{}\n}abc", {
name: "Mary"
});
t.deepEqual(r, "Hello Mary {{}\n}abc");
});
17 changes: 12 additions & 5 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,22 @@ function parser(tokens) {
token = tokens[current]; // 如果是表达式, 那么这个的token的值应该为 }
nextToken = tokens[++current]; // 如果是表达式,那么这个token的值也应该为 }, 以 }} 结尾

current++; // 指针往前一格

// 如果表达式不完整 {{name}

// 此时的token,要么是}, 要么是undefined
// 那么它只作为普通的字符串
if (!token) {
// 虽然前面有{{,但是有吗没有相应的字段了
// 所以,它依旧只是普通的字符串
current++;
return {
type: "StringLiteral",
value: "{{" + node.params.map(n => n.value).join("")
};
}

if (!nextToken) {
current++;
return {
type: "StringLiteral",
value: token.value
Expand All @@ -120,12 +120,19 @@ function parser(tokens) {
// 此时,已经能确定是 {{} 的形式

// 如果下一个token,不是}
// 比如 {{name}abc nextToken.value = a
// 那么它也只是普通的字符串而已
if (nextToken.type !== "paren" || nextToken.value !== "}") {
throw new Error("Invalid Expression.");
return {
type: "StringLiteral",
value:
"{{" +
node.params.map(n => n.value).join("") +
"}" +
nextToken.value
};
}

current++;

return node;
case "}":
// 如果字符串是 }, 那么自是普通的字符串, {{}}的表达式,已经在 {的操作中处理了
Expand Down

0 comments on commit 91dae25

Please sign in to comment.