Skip to content

Commit

Permalink
Merge pull request #5144 from ibuclaw/isvalididentifier
Browse files Browse the repository at this point in the history
Move Lexer.isValidIdentifier static method to Identifier class
  • Loading branch information
9rnsr committed Sep 29, 2015
2 parents e598f18 + fdf64e1 commit 5b0471d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ public:
dst = modules; // and so this module goes into global module symbol table
/* Check to see if module name is a valid identifier
*/
if (!Lexer.isValidIdentifier(this.ident.toChars()))
if (!Identifier.isValidIdentifier(this.ident.toChars()))
error("has non-identifier characters in filename, use module declaration instead");
}
// Add internal used functions in 'object' module members.
Expand Down
31 changes: 31 additions & 0 deletions src/identifier.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

module ddmd.identifier;

import core.stdc.ctype;
import core.stdc.stdio;
import core.stdc.string;
import ddmd.globals;
Expand All @@ -16,6 +17,7 @@ import ddmd.root.outbuffer;
import ddmd.root.rootobject;
import ddmd.root.stringtable;
import ddmd.tokens;
import ddmd.utf;

/***********************************************************
*/
Expand Down Expand Up @@ -134,6 +136,35 @@ public:
return id;
}

/**********************************
* Determine if string is a valid Identifier.
* Returns:
* 0 invalid
*/
final static bool isValidIdentifier(const(char)* p)
{
size_t len;
size_t idx;
if (!p || !*p)
goto Linvalid;
if (*p >= '0' && *p <= '9') // beware of isdigit() on signed chars
goto Linvalid;
len = strlen(p);
idx = 0;
while (p[idx])
{
dchar_t dc;
const(char)* q = utf_decodeChar(cast(char*)p, len, &idx, &dc);
if (q)
goto Linvalid;
if (!((dc >= 0x80 && isUniAlpha(dc)) || isalnum(dc) || dc == '_'))
goto Linvalid;
}
return true;
Linvalid:
return false;
}

static Identifier lookup(const(char)* s, size_t len)
{
StringValue* sv = stringtable.lookup(s, len);
Expand Down
30 changes: 0 additions & 30 deletions src/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -2437,36 +2437,6 @@ public:
*dc = cast(char*)buf.extractData();
}

/**********************************
* Determine if string is a valid Identifier.
* Placed here because of commonality with Lexer functionality.
* Returns:
* 0 invalid
*/
final static bool isValidIdentifier(const(char)* p)
{
size_t len;
size_t idx;
if (!p || !*p)
goto Linvalid;
if (*p >= '0' && *p <= '9') // beware of isdigit() on signed chars
goto Linvalid;
len = strlen(p);
idx = 0;
while (p[idx])
{
dchar_t dc;
const(char)* q = utf_decodeChar(cast(char*)p, len, &idx, &dc);
if (q)
goto Linvalid;
if (!((dc >= 0x80 && isUniAlpha(dc)) || isalnum(dc) || dc == '_'))
goto Linvalid;
}
return true;
Linvalid:
return false;
}

/********************************************
* Combine two document comments into one,
* separated by a newline.
Expand Down
6 changes: 3 additions & 3 deletions src/mars.d
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ Language changes listed by -transition=id:
goto Lerror;
}
}
else if (Lexer.isValidIdentifier(p + 12))
else if (Identifier.isValidIdentifier(p + 12))
{
const(char)* ident = p + 12;
switch (strlen(ident))
Expand Down Expand Up @@ -857,7 +857,7 @@ Language changes listed by -transition=id:
goto Lerror;
DebugCondition.setGlobalLevel(cast(int)level);
}
else if (Lexer.isValidIdentifier(p + 7))
else if (Identifier.isValidIdentifier(p + 7))
DebugCondition.addGlobalIdent(p + 7);
else
goto Lerror;
Expand All @@ -883,7 +883,7 @@ Language changes listed by -transition=id:
goto Lerror;
VersionCondition.setGlobalLevel(cast(int)level);
}
else if (Lexer.isValidIdentifier(p + 9))
else if (Identifier.isValidIdentifier(p + 9))
VersionCondition.addGlobalIdent(p + 9);
else
goto Lerror;
Expand Down

0 comments on commit 5b0471d

Please sign in to comment.