Convert some token id functions to templates

This commit is contained in:
Hackerpilot 2013-12-15 14:01:11 -08:00
parent 8c4a87f563
commit 629c93eca7
3 changed files with 24 additions and 15 deletions

View File

@ -568,7 +568,7 @@ public struct DLexer(R)
Token lexDecimal(LR)(ref LR range) Token lexDecimal(LR)(ref LR range)
{ {
bool foundDot = range.front == '.'; bool foundDot = range.front == '.';
auto type = tok!"intLiteral"; IdType type = tok!"intLiteral";
if (foundDot) if (foundDot)
{ {
range.popFront(); range.popFront();

View File

@ -1654,7 +1654,7 @@ class ClassFour(A, B) if (someTest()) : Super {}}c;
break; break;
case tok!"enum": case tok!"enum":
if (startsWith(tok!"enum", tok!"identifier", tok!"(")) if (startsWith(tok!"enum", tok!"identifier", tok!"("))
goto _template; goto case tok!"template";
node.enumDeclaration = parseEnumDeclaration(); node.enumDeclaration = parseEnumDeclaration();
if (node.enumDeclaration is null) return null; if (node.enumDeclaration is null) return null;
break; break;
@ -1722,7 +1722,6 @@ class ClassFour(A, B) if (someTest()) : Super {}}c;
case tok!"struct": case tok!"struct":
node.structDeclaration = parseStructDeclaration(); node.structDeclaration = parseStructDeclaration();
break; break;
_template:
case tok!"template": case tok!"template":
node.templateDeclaration = parseTemplateDeclaration(); node.templateDeclaration = parseTemplateDeclaration();
break; break;
@ -5736,12 +5735,11 @@ q{(int a, ...)
|| peekIs(tok!"("); || peekIs(tok!"(");
index--; index--;
if (jump) if (jump)
goto lParen; goto case tok!"(";
else else
break loop; break loop;
case tok!"(": case tok!"(":
lParen:
auto newUnary = new UnaryExpression(); auto newUnary = new UnaryExpression();
newUnary.functionCallExpression = parseFunctionCallExpression(node); newUnary.functionCallExpression = parseFunctionCallExpression(node);
node = newUnary; node = newUnary;

View File

@ -42,31 +42,44 @@ string TokenStringRepresentation(IdType, alias staticTokens, alias possibleDefau
return null; return null;
} }
IdType TokenId(IdType, alias staticTokens, alias dynamicTokens, template TokenId(IdType, alias staticTokens, alias dynamicTokens,
alias possibleDefaultTokens, string symbol)() @property alias possibleDefaultTokens, string symbol)
{ {
static if (symbol == "") static if (symbol == "")
return 0; {
enum id = 0;
alias id TokenId;
}
else static if (symbol == "\0") else static if (symbol == "\0")
return 1 + staticTokens.length + dynamicTokens.length + possibleDefaultTokens.length; {
enum id = 1 + staticTokens.length + dynamicTokens.length + possibleDefaultTokens.length;
alias id TokenId;
}
else else
{ {
enum i = staticTokens.countUntil(symbol); enum i = staticTokens.countUntil(symbol);
static if (i >= 0) static if (i >= 0)
{
enum id = i + 1; enum id = i + 1;
alias id TokenId;
}
else else
{ {
enum ii = possibleDefaultTokens.countUntil(symbol); enum ii = possibleDefaultTokens.countUntil(symbol);
static if (ii >= 0) static if (ii >= 0)
{
enum id = ii + staticTokens.length; enum id = ii + staticTokens.length;
static assert (id >= 0 && id < IdType.max, "Invalid token: " ~ symbol);
alias id TokenId;
}
else else
{ {
enum dynamicId = dynamicTokens.countUntil(symbol); enum dynamicId = dynamicTokens.countUntil(symbol);
enum id = dynamicId >= 0 ? i + staticTokens.length + possibleDefaultTokens.length + dynamicId : -1; enum id = dynamicId >= 0 ? i + staticTokens.length + possibleDefaultTokens.length + dynamicId : -1;
static assert (id >= 0 && id < IdType.max, "Invalid token: " ~ symbol);
alias id TokenId;
} }
} }
static assert (id >= 0 && id < IdType.max, "Invalid token: " ~ symbol);
return id;
} }
} }
@ -166,8 +179,6 @@ mixin template Lexer(R, IDType, Token, alias isSeparating, alias defaultTokenFun
return code; return code;
} }
Token front() @property Token front() @property
{ {
return _front; return _front;
@ -188,11 +199,11 @@ mixin template Lexer(R, IDType, Token, alias isSeparating, alias defaultTokenFun
post[pseudoTok!t] = fun; post[pseudoTok!t] = fun;
} }
static IDType pseudoTok(string symbol)() nothrow pure @property template pseudoTok(string symbol)
{ {
static assert (pseudoTokens.countUntil(symbol) >= 0); static assert (pseudoTokens.countUntil(symbol) >= 0);
enum index = cast(IDType) pseudoTokens.countUntil(symbol); enum index = cast(IDType) pseudoTokens.countUntil(symbol);
return index; alias index pseudoTok;
} }
static string escape(string input) static string escape(string input)