Merge pull request #35 from blackwhale/range-based-lexer

Avoid array lookups where simple constants are fine
This commit is contained in:
Hackerpilot 2013-03-18 13:35:11 -07:00
commit aa406c76f2
2 changed files with 19 additions and 8 deletions

View File

@ -74,6 +74,8 @@ class LabeledStatement : NonEmptyStatementNoCaseNoDefault
interface ExpressionStatement : NonEmptyStatementNoCaseNoDefault {} interface ExpressionStatement : NonEmptyStatementNoCaseNoDefault {}
interface DeclarationStatement : NonEmptyStatementNoCaseNoDefault {} interface DeclarationStatement : NonEmptyStatementNoCaseNoDefault {}
/+
/** /**
* $(LINK2 http://dlang.org/statement.html#IfStatement) * $(LINK2 http://dlang.org/statement.html#IfStatement)
*/ */
@ -395,3 +397,6 @@ class Inherits : DeclDef
//FunctionDeclaration[] functions; //FunctionDeclaration[] functions;
} }
+/ +/
+/

View File

@ -393,7 +393,6 @@ L_advance:
// since this branch at most is taken once per file // since this branch at most is taken once per file
_empty = true; _empty = true;
return; return;
// pragma(msg, generateCaseTrie(
mixin(generateCaseTrie( mixin(generateCaseTrie(
"=", "TokenType.assign", "=", "TokenType.assign",
"@", "TokenType.at", "@", "TokenType.at",
@ -485,7 +484,7 @@ L_advance:
if (!src.canPeek()) if (!src.canPeek())
{ {
current.type = TokenType.dot; current.type = TokenType.dot;
current.value = getTokenValue(TokenType.dot); current.value = tokenValue!(TokenType.dot);
return; return;
} }
switch (src.peek()) switch (src.peek())
@ -501,13 +500,15 @@ L_advance:
{ {
current.type = TokenType.vararg; current.type = TokenType.vararg;
nextCharNonLF(); nextCharNonLF();
current.value = tokenValue!(TokenType.vararg);
} }
current.value = getTokenValue(current.type); else
current.value = tokenValue!(TokenType.slice);
return; return;
default: default:
nextCharNonLF(); nextCharNonLF();
current.type = TokenType.dot; current.type = TokenType.dot;
current.value = getTokenValue(TokenType.dot); current.value = tokenValue!(TokenType.dot);
return; return;
} }
case '0': .. case '9': case '0': .. case '9':
@ -1464,7 +1465,7 @@ L_advance:
else else
{ {
current.type = TokenType.hash; current.type = TokenType.hash;
current.value = getTokenValue(TokenType.hash); current.value = tokenValue!(TokenType.hash);
} }
} }
@ -2705,6 +2706,11 @@ pure string getTokenValue(const TokenType type)
return tokenValues[type]; return tokenValues[type];
} }
template tokenValue(TokenType val)
{
enum tokenValue = getTokenValue(val);
}
private pure bool isNewline(ubyte ch) private pure bool isNewline(ubyte ch)
{ {
return ch == '\n' || ch == '\r'; return ch == '\n' || ch == '\r';
@ -2955,7 +2961,7 @@ string printCaseStatements(K, V)(TrieNode!(K,V) node, string indentString)
caseStatement ~= indentString; caseStatement ~= indentString;
caseStatement ~= "\t{\n"; caseStatement ~= "\t{\n";
caseStatement ~= indentString; caseStatement ~= indentString;
caseStatement ~= "\t\tcurrent.value = getTokenValue(current.type);\n"; caseStatement ~= "\t\tcurrent.value = tokenValue!("~node.children[k].value~");\n";
caseStatement ~= indentString; caseStatement ~= indentString;
caseStatement ~= "\t\tcurrent.type = " ~ node.children[k].value; caseStatement ~= "\t\tcurrent.type = " ~ node.children[k].value;
caseStatement ~= ";\n"; caseStatement ~= ";\n";
@ -2975,7 +2981,7 @@ string printCaseStatements(K, V)(TrieNode!(K,V) node, string indentString)
caseStatement ~= v.value; caseStatement ~= v.value;
caseStatement ~= ";\n"; caseStatement ~= ";\n";
caseStatement ~= indentString; caseStatement ~= indentString;
caseStatement ~= "\t\tcurrent.value = getTokenValue(current.type);\n"; caseStatement ~= "\t\tcurrent.value = tokenValue!("~v.value~");\n";
caseStatement ~= indentString; caseStatement ~= indentString;
caseStatement ~= "\t\treturn;\n"; caseStatement ~= "\t\treturn;\n";
caseStatement ~= indentString; caseStatement ~= indentString;
@ -2988,7 +2994,7 @@ string printCaseStatements(K, V)(TrieNode!(K,V) node, string indentString)
caseStatement ~= v.value; caseStatement ~= v.value;
caseStatement ~= ";\n"; caseStatement ~= ";\n";
caseStatement ~= indentString; caseStatement ~= indentString;
caseStatement ~= "\tcurrent.value = getTokenValue(current.type);\n"; caseStatement ~= "\tcurrent.value = tokenValue!("~v.value~");\n";
caseStatement ~= indentString; caseStatement ~= indentString;
caseStatement ~= "\treturn;\n"; caseStatement ~= "\treturn;\n";
} }