From 629c93eca7e5370b1e3c7e01fec7caa770426c92 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Sun, 15 Dec 2013 14:01:11 -0800 Subject: [PATCH] Convert some token id functions to templates --- stdx/d/lexer.d | 2 +- stdx/d/parser.d | 6 ++---- stdx/lexer.d | 31 +++++++++++++++++++++---------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/stdx/d/lexer.d b/stdx/d/lexer.d index 26a0c3e..b1d1ab9 100644 --- a/stdx/d/lexer.d +++ b/stdx/d/lexer.d @@ -568,7 +568,7 @@ public struct DLexer(R) Token lexDecimal(LR)(ref LR range) { bool foundDot = range.front == '.'; - auto type = tok!"intLiteral"; + IdType type = tok!"intLiteral"; if (foundDot) { range.popFront(); diff --git a/stdx/d/parser.d b/stdx/d/parser.d index f47b6be..fca5a5a 100644 --- a/stdx/d/parser.d +++ b/stdx/d/parser.d @@ -1654,7 +1654,7 @@ class ClassFour(A, B) if (someTest()) : Super {}}c; break; case tok!"enum": if (startsWith(tok!"enum", tok!"identifier", tok!"(")) - goto _template; + goto case tok!"template"; node.enumDeclaration = parseEnumDeclaration(); if (node.enumDeclaration is null) return null; break; @@ -1722,7 +1722,6 @@ class ClassFour(A, B) if (someTest()) : Super {}}c; case tok!"struct": node.structDeclaration = parseStructDeclaration(); break; - _template: case tok!"template": node.templateDeclaration = parseTemplateDeclaration(); break; @@ -5736,12 +5735,11 @@ q{(int a, ...) || peekIs(tok!"("); index--; if (jump) - goto lParen; + goto case tok!"("; else break loop; case tok!"(": - lParen: auto newUnary = new UnaryExpression(); newUnary.functionCallExpression = parseFunctionCallExpression(node); node = newUnary; diff --git a/stdx/lexer.d b/stdx/lexer.d index d1e38c1..d18d11c 100644 --- a/stdx/lexer.d +++ b/stdx/lexer.d @@ -42,31 +42,44 @@ string TokenStringRepresentation(IdType, alias staticTokens, alias possibleDefau return null; } -IdType TokenId(IdType, alias staticTokens, alias dynamicTokens, - alias possibleDefaultTokens, string symbol)() @property +template TokenId(IdType, alias staticTokens, alias dynamicTokens, + alias possibleDefaultTokens, string symbol) { static if (symbol == "") - return 0; + { + enum id = 0; + alias id TokenId; + } 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 { enum i = staticTokens.countUntil(symbol); static if (i >= 0) + { enum id = i + 1; + alias id TokenId; + } else { enum ii = possibleDefaultTokens.countUntil(symbol); static if (ii >= 0) + { enum id = ii + staticTokens.length; + static assert (id >= 0 && id < IdType.max, "Invalid token: " ~ symbol); + alias id TokenId; + } else { enum dynamicId = dynamicTokens.countUntil(symbol); 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; } - - Token front() @property { return _front; @@ -188,11 +199,11 @@ mixin template Lexer(R, IDType, Token, alias isSeparating, alias defaultTokenFun post[pseudoTok!t] = fun; } - static IDType pseudoTok(string symbol)() nothrow pure @property + template pseudoTok(string symbol) { static assert (pseudoTokens.countUntil(symbol) >= 0); enum index = cast(IDType) pseudoTokens.countUntil(symbol); - return index; + alias index pseudoTok; } static string escape(string input)