Mercurial > projects > dil
annotate src/dil/lexer/Funcs.d @ 806:bcb74c9b895c
Moved out files in the trunk folder to the root.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sun, 09 Mar 2008 00:12:19 +0100 |
parents | trunk/src/dil/lexer/Funcs.d@3b34f6a95a27 |
children |
rev | line source |
---|---|
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
1 /++ |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
2 Author: Aziz Köksal |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
3 License: GPL3 |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
4 +/ |
577
9e811db780a6
Moved LexerFuncs.d to package 'lexer'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
533
diff
changeset
|
5 module dil.lexer.Funcs; |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
6 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
7 const char[3] LS = \u2028; /// Unicode line separator. |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
8 const dchar LSd = 0x2028; /// ditto |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
9 const char[3] PS = \u2029; /// Unicode paragraph separator. |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
10 const dchar PSd = 0x2029; /// ditto |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
11 static assert(LS[0] == PS[0] && LS[1] == PS[1]); |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
12 |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
13 const dchar _Z_ = 26; /// Control+Z. |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
14 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
15 /// Returns: true if d is a Unicode line or paragraph separator. |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
16 bool isUnicodeNewlineChar(dchar d) |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
17 { |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
18 return d == LSd || d == PSd; |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
19 } |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
20 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
21 /// Returns: true if p points to a line or paragraph separator. |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
22 bool isUnicodeNewline(char* p) |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
23 { |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
24 return *p == LS[0] && p[1] == LS[1] && (p[2] == LS[2] || p[2] == PS[2]); |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
25 } |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
26 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
27 /// Returns: true if p points to the start of a Newline. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
28 /// Newline: \n | \r | \r\n | LS | PS |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
29 bool isNewline(char* p) |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
30 { |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
31 return *p == '\n' || *p == '\r' || isUnicodeNewline(p); |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
32 } |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
33 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
34 /// Returns: true if c is a Newline character. |
533
2a8d0ed0d71e
Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
496
diff
changeset
|
35 bool isNewline(dchar c) |
2a8d0ed0d71e
Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
496
diff
changeset
|
36 { |
2a8d0ed0d71e
Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
496
diff
changeset
|
37 return c == '\n' || c == '\r' || isUnicodeNewlineChar(c); |
2a8d0ed0d71e
Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
496
diff
changeset
|
38 } |
2a8d0ed0d71e
Improved error reporting in dil.Converter.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
496
diff
changeset
|
39 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
40 /// Returns: true if p points to an EOF character. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
41 /// EOF: 0 | _Z_ |
496
5a607597dc22
Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
494
diff
changeset
|
42 bool isEOF(dchar c) |
5a607597dc22
Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
494
diff
changeset
|
43 { |
5a607597dc22
Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
494
diff
changeset
|
44 return c == 0 || c == _Z_; |
5a607597dc22
Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
494
diff
changeset
|
45 } |
5a607597dc22
Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
494
diff
changeset
|
46 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
47 /// Returns: true if p points to the first character of an EndOfLine. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
48 /// EndOfLine: Newline | EOF |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
49 bool isEndOfLine(char* p) |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
50 { |
496
5a607597dc22
Improved error recovery in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
494
diff
changeset
|
51 return isNewline(p) || isEOF(*p); |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
52 } |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
53 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
54 /// Scans a Newline and sets p one character past it. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
55 /// Returns: '\n' if found or 0 otherwise. |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
56 dchar scanNewline(ref char* p) |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
57 { |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
58 switch (*p) |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
59 { |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
60 case '\r': |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
61 if (p[1] == '\n') |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
62 ++p; |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
63 case '\n': |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
64 ++p; |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
65 return '\n'; |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
66 default: |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
67 if (isUnicodeNewline(p)) |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
68 { |
494
9a7ca8c56e59
Refactored a few things in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
490
diff
changeset
|
69 p += 3; |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
70 return '\n'; |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
71 } |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
72 } |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
73 return 0; |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
74 } |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
75 |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
76 /// ASCII character properties table. |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
77 static const int ptable[256] = [ |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
78 0, 0, 0, 0, 0, 0, 0, 0, 0,32, 0,32,32, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
80 32, 0, 0x2200, 0, 0, 0, 0, 0x2700, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
81 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 0, 0, 0, 0, 0, 0x3f00, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
82 0,12,12,12,12,12,12, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
83 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0x5c00, 0, 0,16, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
84 0, 0x70c, 0x80c,12,12,12, 0xc0c, 8, 8, 8, 8, 8, 8, 8, 0xa08, 8, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
85 8, 8, 0xd08, 8, 0x908, 8, 0xb08, 8, 8, 8, 8, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
94 ]; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
95 |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
96 /// Enumeration of character property flags. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
97 enum CProperty |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
98 { |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
99 Octal = 1, /// 0-7 |
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
100 Digit = 1<<1, /// 0-9 |
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
101 Hex = 1<<2, /// 0-9a-fA-F |
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
102 Alpha = 1<<3, /// a-zA-Z |
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
103 Underscore = 1<<4, /// _ |
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
104 Whitespace = 1<<5 /// ' ' \t \v \f |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
105 } |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
106 |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
107 const uint EVMask = 0xFF00; // Bit mask for escape value. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
108 |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
109 private alias CProperty CP; |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
110 /// Returns: true if c is an octal digit. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
111 int isoctal(char c) { return ptable[c] & CP.Octal; } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
112 /// Returns: true if c is a decimal digit. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
113 int isdigit(char c) { return ptable[c] & CP.Digit; } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
114 /// Returns: true if c is a hexadecimal digit. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
115 int ishexad(char c) { return ptable[c] & CP.Hex; } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
116 /// Returns: true if c is a letter. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
117 int isalpha(char c) { return ptable[c] & CP.Alpha; } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
118 /// Returns: true if c is an alphanumeric. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
119 int isalnum(char c) { return ptable[c] & (CP.Alpha | CP.Digit); } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
120 /// Returns: true if c is the beginning of a D identifier (only ASCII.) |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
121 int isidbeg(char c) { return ptable[c] & (CP.Alpha | CP.Underscore); } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
122 /// Returns: true if c is a D identifier character (only ASCII.) |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
123 int isident(char c) { return ptable[c] & (CP.Alpha | CP.Underscore | CP.Digit); } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
124 /// Returns: true if c is a whitespace character. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
125 int isspace(char c) { return ptable[c] & CP.Whitespace; } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
126 /// Returns: the escape value for c. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
127 int char2ev(char c) { return ptable[c] >> 8; /*(ptable[c] & EVMask) >> 8;*/ } |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
715
diff
changeset
|
128 /// Returns: true if c is an ASCII character. |
715
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
129 int isascii(uint c) { return c < 128; } |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
130 |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
131 version(gen_ptable) |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
132 static this() |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
133 { |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
134 alias ptable p; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
135 assert(p.length == 256); |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
136 // Initialize character properties table. |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
137 for (int i; i < p.length; ++i) |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
138 { |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
139 p[i] = 0; // Reset |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
140 if ('0' <= i && i <= '7') |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
141 p[i] |= CP.Octal; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
142 if ('0' <= i && i <= '9') |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
143 p[i] |= CP.Digit | CP.Hex; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
144 if ('a' <= i && i <= 'f' || 'A' <= i && i <= 'F') |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
145 p[i] |= CP.Hex; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
146 if ('a' <= i && i <= 'z' || 'A' <= i && i <= 'Z') |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
147 p[i] |= CP.Alpha; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
148 if (i == '_') |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
149 p[i] |= CP.Underscore; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
150 if (i == ' ' || i == '\t' || i == '\v' || i == '\f') |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
151 p[i] |= CP.Whitespace; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
152 } |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
153 // Store escape sequence values in second byte. |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
154 assert(CProperty.max <= ubyte.max, "character property flags and escape value byte overlap."); |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
155 p['\''] |= 39 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
156 p['"'] |= 34 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
157 p['?'] |= 63 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
158 p['\\'] |= 92 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
159 p['a'] |= 7 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
160 p['b'] |= 8 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
161 p['f'] |= 12 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
162 p['n'] |= 10 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
163 p['r'] |= 13 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
164 p['t'] |= 9 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
165 p['v'] |= 11 << 8; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
166 // Print a formatted array literal. |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
167 char[] array = "[\n"; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
168 foreach (i, c; ptable) |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
169 { |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
170 array ~= Format((c>255?" 0x{0:x},":"{0,2},"), c) ~ (((i+1) % 16) ? "":"\n"); |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
171 } |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
172 array[$-2..$] = "\n]"; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
173 Stdout(array).newline; |
b6c6baa41267
Moved character properties table to dil.lexer.Funcs.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
577
diff
changeset
|
174 } |