0
|
1 /++
|
|
2 Author: Aziz Köksal
|
|
3 License: GPL2
|
|
4 +/
|
|
5 module Lexer;
|
3
|
6 import Token;
|
2
|
7 import std.stdio;
|
0
|
8
|
|
9 /// ASCII character properties table.
|
2
|
10 static const int ptable[256] = [
|
|
11 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
12 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,23,23,23,23,23,23,23,23,22,22, 0, 0, 0, 0, 0, 0,
|
|
13 0,28,28,28,28,28,28,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24, 0, 0, 0, 0,16,
|
|
14 0,28,28,28,28,28,28,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24, 0, 0, 0, 0, 0,
|
|
15 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
17 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
19 ];
|
0
|
20
|
|
21 enum CProperty
|
|
22 {
|
1
|
23 Octal = 1,
|
0
|
24 Digit = 1<<1,
|
|
25 Hex = 1<<2,
|
|
26 Alpha = 1<<3,
|
|
27 Identifier = 1<<4
|
|
28 }
|
|
29
|
|
30 int isoctal(char c) { return ptable[c] & CProperty.Octal; }
|
|
31 int isdigit(char c) { return ptable[c] & CProperty.Digit; }
|
|
32 int ishexad(char c) { return ptable[c] & CProperty.Hex; }
|
|
33 int isalpha(char c) { return ptable[c] & CProperty.Alpha; }
|
|
34 int isalnum(char c) { return ptable[c] & (CProperty.Alpha | CProperty.Digit); }
|
|
35 int isident(char c) { return ptable[c] & CProperty.Identifier; }
|
2
|
36 /+
|
0
|
37 static this()
|
|
38 {
|
2
|
39 // Initialize character properties table.
|
0
|
40 for (int i; i < ptable.length; ++i)
|
|
41 {
|
|
42 if ('0' <= i && i <= '7')
|
|
43 ptable[i] |= CProperty.Octal;
|
|
44 if ('0' <= i && i <= '9')
|
|
45 ptable[i] |= CProperty.Digit;
|
|
46 if (isdigit(i) || 'a' <= i && i <= 'f' || 'A' <= i && i <= 'F')
|
|
47 ptable[i] |= CProperty.Hex;
|
|
48 if ('a' <= i && i <= 'z' || 'A' <= i && i <= 'Z')
|
|
49 ptable[i] |= CProperty.Alpha;
|
|
50 if (isalnum(i) || i == '_')
|
1
|
51 ptable[i] |= CProperty.Identifier;
|
0
|
52 }
|
2
|
53 // Print a formatted array literal.
|
|
54 char[] array = "[\n";
|
|
55 for (int i; i < ptable.length; ++i)
|
|
56 {
|
|
57 char c = ptable[i];
|
|
58 array ~= std.string.format("%2d,", c, ((i+1) % 32) ? "":"\n");
|
|
59 }
|
|
60 array.length = array.length - 2; // remove ",\n"
|
|
61 array ~= "\n]";
|
|
62 writefln(array);
|
0
|
63 }
|
2
|
64 +/
|
0
|
65
|
|
66 class Lexer
|
|
67 {
|
3
|
68 Token t;
|
|
69 public void scan(ref Token t)
|
|
70 {
|
0
|
71
|
3
|
72 }
|
|
73 public TOK nextToken()
|
|
74 {
|
|
75 return TOK.max;
|
|
76 }
|
0
|
77 } |