Fix bug with ignoring whitespaces at end of expression

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-11-28 13:28:03 -08:00
parent 31f4062b60
commit a59d1fa97e
2 changed files with 52 additions and 43 deletions

View File

@@ -15,6 +15,14 @@ namespace Orchard.Tests.Modules.Scripting {
});
}
[Test]
public void ParserShouldIgnoreWhitespaces() {
var tree = new Parser(" true \n ").Parse();
CheckTree(tree, new object[] {
"const", true,
});
}
[Test]
public void ParserShouldUnderstandBinaryExpressions() {
var tree = new Parser("true+true").Parse();

View File

@@ -14,52 +14,53 @@ namespace Orchard.Scripting.Compiler {
}
public Token NextToken() {
if (Eof())
return CreateToken(TokenKind.Eof);
while (true) {
if (Eof())
return CreateToken(TokenKind.Eof);
LexAgain:
_startTokenIndex = _index;
char ch = Character();
switch (ch) {
case '(':
NextCharacter();
return CreateToken(TokenKind.OpenParen);
case ')':
NextCharacter();
return CreateToken(TokenKind.CloseParen);
case ',':
NextCharacter();
return CreateToken(TokenKind.Comma);
case '+':
NextCharacter();
return CreateToken(TokenKind.Plus);
case '-':
NextCharacter();
return CreateToken(TokenKind.Minus);
case '*':
NextCharacter();
return CreateToken(TokenKind.Mul);
case '/':
NextCharacter();
return CreateToken(TokenKind.Div);
case '"':
return LexStringLiteral();
case '\'':
return LexSingleQuotedStringLiteral();
}
_startTokenIndex = _index;
char ch = Character();
switch (ch) {
case '(':
NextCharacter();
return CreateToken(TokenKind.OpenParen);
case ')':
NextCharacter();
return CreateToken(TokenKind.CloseParen);
case ',':
NextCharacter();
return CreateToken(TokenKind.Comma);
case '+':
NextCharacter();
return CreateToken(TokenKind.Plus);
case '-':
NextCharacter();
return CreateToken(TokenKind.Minus);
case '*':
NextCharacter();
return CreateToken(TokenKind.Mul);
case '/':
NextCharacter();
return CreateToken(TokenKind.Div);
case '"':
return LexStringLiteral();
case '\'':
return LexSingleQuotedStringLiteral();
}
if (IsDigitCharacter(ch)) {
return LexInteger();
}
else if (IsIdentifierCharacter(ch)) {
return LexIdentifierOrKeyword();
}
else if (IsWhitespaceCharacter(ch)) {
NextCharacter();
goto LexAgain;
}
if (IsDigitCharacter(ch)) {
return LexInteger();
}
else if (IsIdentifierCharacter(ch)) {
return LexIdentifierOrKeyword();
}
else if (IsWhitespaceCharacter(ch)) {
NextCharacter();
continue;
}
return CreateToken(TokenKind.Invalid, "Unrecognized character");
return CreateToken(TokenKind.Invalid, "Unrecognized character");
}
}
private Token LexIdentifierOrKeyword() {