1 /// Author: Aziz Köksal
2 /// License: GPL3
3 /// $(Maturity high)
4 module dil.lexer.Identifier;
5 
6 import dil.lexer.TokensEnum,
7        dil.lexer.IDsEnum;
8 import dil.String;
9 import common;
10 
11 /// Represents an identifier as defined in the D specs.
12 ///
13 /// $(BNF
14 ////Identifier := IdStart IdChar*
15 ////   IdStart := "_" | Letter
16 ////    IdChar := IdStart | "0"-"9"
17 ////    Letter := UniAlpha
18 ////)
19 /// See_Also:
20 ///  Unicode alphas as defined in Unicode 5.0.0.
21 align(1)
22 struct Identifier_
23 {
24   string str; /// The UTF-8 string of the identifier.
25   TOK kind;   /// The token kind. Can be TOK.{KEYWORD, SpecialID, Identifier}.
26   IDK idKind; /// Only for predefined identifiers.
27 
28 immutable:
29   /// Constructs an Identifier.
30   this(string str, TOK kind, IDK idKind = IDK.init)
31   {
32     this.str = str;
33     this.kind = kind;
34     this.idKind = idKind;
35   }
36 
37   /// Calculates a hash for this id.
38   hash_t toHash()
39   {
40     return hashOf(str);
41   }
42 
43   /// Returns the string of this id.
44   string toString()
45   {
46     return str;
47   }
48 
49   /// Returns true if this id starts with prefix.
50   bool startsWith(string prefix)
51   {
52     return String(str).startsWith(prefix);
53   }
54 }
55 // pragma(msg, Identifier.sizeof.stringof);
56 
57 /// Identifiers are const by default.
58 alias Identifier = immutable(Identifier_);