Mercurial > projects > dil
changeset 382:66477017cb95
Applied fixes to isNonReservedIdentifier().
Return false if ident is empty.
Before entering the foreach loop, it must be checked if ident starts with a valid identifier character.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Mon, 10 Sep 2007 17:36:58 +0200 |
parents | 7b5c6c2c6a79 |
children | 6a5fc22cae34 |
files | trunk/src/dil/Lexer.d |
diffstat | 1 files changed, 24 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/trunk/src/dil/Lexer.d Sat Sep 08 20:57:01 2007 +0000 +++ b/trunk/src/dil/Lexer.d Mon Sep 10 17:36:58 2007 +0200 @@ -1578,7 +1578,7 @@ foreach(k; keywords) idtable[k.str] = k; } -/+ +/+ // Not needed anymore because tokens are stored in a linked list. struct State { Lexer lexer; @@ -1648,6 +1648,7 @@ unittest { + writefln("Testing method Lexer.peek()"); string sourceText = "unittest { }"; auto lx = new Lexer(sourceText, null); @@ -1660,7 +1661,6 @@ assert(next.type == TOK.RBrace); lx.peek(next); assert(next.type == TOK.EOF); - writefln("end of peek() unittest"); } Token* getTokens() @@ -1672,16 +1672,34 @@ static bool isNonReservedIdentifier(char[] ident) { + if (ident.length == 0) + return false; + static Identifier[string] reserved_ids_table; if (reserved_ids_table is null) foreach(k; keywords) reserved_ids_table[k.str] = k; + size_t idx = 1; // Index to the 2nd character in ident. + dchar isFirstCharUniAlpha() + { + idx = 0; + // NB: decode() could throw an Exception which would be + // caught by the next try-catch-block. + return isUniAlpha(std.utf.decode(ident, idx)); + } + try - foreach (dchar c; ident) - if (!isident(c) && !isUniAlpha(c)) - return false; - catch (Exception e) + { + if (isidbeg(ident[0]) || + ident[0] & 128 && isFirstCharUniAlpha()) + { + foreach (dchar c; ident[idx..$]) + if (!isident(c) && !isUniAlpha(c)) + return false; + } + } + catch (Exception) return false; return !(ident in reserved_ids_table);