Inject a private _tok template to allow user-defined tok() template to have an arbitrary name.

This commit is contained in:
Andrej Mitrovic 2014-02-13 10:31:54 +01:00
parent 54118e905f
commit 236403ab31
1 changed files with 9 additions and 13 deletions

View File

@ -155,12 +155,7 @@ unittest
{ {
/// Fix https://github.com/Hackerpilot/Dscanner/issues/96 /// Fix https://github.com/Hackerpilot/Dscanner/issues/96
alias IdType = TokenIdType!(["foo"], ["bar"], ["doo"]); alias IdType = TokenIdType!(["foo"], ["bar"], ["doo"]);
alias tok(string token) = TokenId!(IdType, ["foo"], ["bar"], ["doo"], token);
template tok(string token)
{
alias tok = TokenId!(IdType, ["foo"], ["bar"], ["doo"], token);
}
alias str = tokenStringRepresentation!(IdType, ["foo"], ["bar"], ["doo"]); alias str = tokenStringRepresentation!(IdType, ["foo"], ["bar"], ["doo"]);
static assert(str(tok!"foo") == "foo"); static assert(str(tok!"foo") == "foo");
@ -373,7 +368,8 @@ mixin template Lexer(Token, alias defaultTokenFunction,
alias tokenSeparatingFunction, alias tokenHandlers, alias tokenSeparatingFunction, alias tokenHandlers,
alias staticTokens, alias dynamicTokens, alias possibleDefaultTokens) alias staticTokens, alias dynamicTokens, alias possibleDefaultTokens)
{ {
alias IDType = typeof(Token.type); private alias _IDType = typeof(Token.type);
private alias _tok(string symbol) = TokenId!(_IDType, staticTokens, dynamicTokens, possibleDefaultTokens, symbol);
static assert (tokenHandlers.length % 2 == 0, "Each pseudo-token must" static assert (tokenHandlers.length % 2 == 0, "Each pseudo-token must"
~ " have a corresponding handler function name."); ~ " have a corresponding handler function name.");
@ -433,7 +429,7 @@ mixin template Lexer(Token, alias defaultTokenFunction,
else if (staticTokens.countUntil(tokens[0]) >= 0) else if (staticTokens.countUntil(tokens[0]) >= 0)
{ {
return " range.popFront();\n" return " range.popFront();\n"
~ " return Token(tok!\"" ~ escape(tokens[0]) ~ "\", null, line, column, index);\n"; ~ " return Token(_tok!\"" ~ escape(tokens[0]) ~ "\", null, line, column, index);\n";
} }
else if (pseudoTokens.countUntil(tokens[0]) >= 0) else if (pseudoTokens.countUntil(tokens[0]) >= 0)
{ {
@ -474,7 +470,7 @@ mixin template Lexer(Token, alias defaultTokenFunction,
if (token.length <= 8) if (token.length <= 8)
{ {
code ~= " range.popFrontN(" ~ text(token.length) ~ ");\n"; code ~= " range.popFrontN(" ~ text(token.length) ~ ");\n";
code ~= " return Token(tok!\"" ~ escape(token) ~ "\", null, line, column, index);\n"; code ~= " return Token(_tok!\"" ~ escape(token) ~ "\", null, line, column, index);\n";
} }
else else
{ {
@ -489,7 +485,7 @@ mixin template Lexer(Token, alias defaultTokenFunction,
code ~= " if (tokenSeparatingFunction(" ~ text(token.length) ~ "))\n"; code ~= " if (tokenSeparatingFunction(" ~ text(token.length) ~ "))\n";
code ~= " {\n"; code ~= " {\n";
code ~= " range.popFrontN(" ~ text(token.length) ~ ");\n"; code ~= " range.popFrontN(" ~ text(token.length) ~ ");\n";
code ~= " return Token(tok!\"" ~ escape(token) ~ "\", null, line, column, index);\n"; code ~= " return Token(_tok!\"" ~ escape(token) ~ "\", null, line, column, index);\n";
code ~= " }\n"; code ~= " }\n";
code ~= " else\n"; code ~= " else\n";
code ~= " goto default;\n"; code ~= " goto default;\n";
@ -499,7 +495,7 @@ mixin template Lexer(Token, alias defaultTokenFunction,
code ~= " if (range.peek(" ~ text(token.length - 1) ~ ") == \"" ~ escape(token) ~"\" && isSeparating(" ~ text(token.length) ~ "))\n"; code ~= " if (range.peek(" ~ text(token.length - 1) ~ ") == \"" ~ escape(token) ~"\" && isSeparating(" ~ text(token.length) ~ "))\n";
code ~= " {\n"; code ~= " {\n";
code ~= " range.popFrontN(" ~ text(token.length) ~ ");\n"; code ~= " range.popFrontN(" ~ text(token.length) ~ ");\n";
code ~= " return Token(tok!\"" ~ escape(token) ~ "\", null, line, column, index);\n"; code ~= " return Token(_tok!\"" ~ escape(token) ~ "\", null, line, column, index);\n";
code ~= " }\n"; code ~= " }\n";
code ~= " else\n"; code ~= " else\n";
code ~= " goto default;\n"; code ~= " goto default;\n";
@ -531,7 +527,7 @@ mixin template Lexer(Token, alias defaultTokenFunction,
*/ */
bool empty() pure const nothrow @property bool empty() pure const nothrow @property
{ {
return _front.type == tok!"\0"; return _front.type == _tok!"\0";
} }
static string escape(string input) static string escape(string input)
@ -575,7 +571,7 @@ mixin template Lexer(Token, alias defaultTokenFunction,
Token advance() pure Token advance() pure
{ {
if (range.empty) if (range.empty)
return Token(tok!"\0"); return Token(_tok!"\0");
immutable size_t index = range.index; immutable size_t index = range.index;
immutable size_t column = range.column; immutable size_t column = range.column;
immutable size_t line = range.line; immutable size_t line = range.line;