1 package simpledb.parse;
2
3 import java.util.*;
4 import java.io.*;
5
6
7
8
9
10 public class Lexer {
11 private Collection<String> keywords;
12 private StreamTokenizer tok;
13
14
15
16
17
18 public Lexer(String s) {
19 initKeywords();
20 tok = new StreamTokenizer(new StringReader(s));
21 tok.ordinaryChar('.');
22 tok.lowerCaseMode(true);
23 nextToken();
24 }
25
26
27
28
29
30
31
32
33
34 public boolean matchDelim(char d) {
35 return d == (char)tok.ttype;
36 }
37
38
39
40
41
42 public boolean matchIntConstant() {
43 return tok.ttype == StreamTokenizer.TT_NUMBER;
44 }
45
46
47
48
49
50 public boolean matchStringConstant() {
51 return '\'' == (char)tok.ttype;
52 }
53
54
55
56
57
58
59 public boolean matchKeyword(String w) {
60 return tok.ttype == StreamTokenizer.TT_WORD && tok.sval.equals(w);
61 }
62
63
64
65
66
67 public boolean matchId() {
68 return tok.ttype==StreamTokenizer.TT_WORD && !keywords.contains(tok.sval);
69 }
70
71
72
73
74
75
76
77
78
79 public void eatDelim(char d) {
80 if (!matchDelim(d))
81 throw new BadSyntaxException();
82 nextToken();
83 }
84
85
86
87
88
89
90
91 public int eatIntConstant() {
92 if (!matchIntConstant())
93 throw new BadSyntaxException();
94 int i = (int) tok.nval;
95 nextToken();
96 return i;
97 }
98
99
100
101
102
103
104
105 public String eatStringConstant() {
106 if (!matchStringConstant())
107 throw new BadSyntaxException();
108 String s = tok.sval;
109 nextToken();
110 return s;
111 }
112
113
114
115
116
117
118
119 public void eatKeyword(String w) {
120 if (!matchKeyword(w))
121 throw new BadSyntaxException();
122 nextToken();
123 }
124
125
126
127
128
129
130
131
132 public String eatId() {
133 if (!matchId())
134 throw new BadSyntaxException();
135 String s = tok.sval;
136 nextToken();
137 return s;
138 }
139
140 private void nextToken() {
141 try {
142 tok.nextToken();
143 }
144 catch(IOException e) {
145 throw new BadSyntaxException();
146 }
147 }
148
149 private void initKeywords() {
150 keywords = Arrays.asList("select", "from", "where", "and",
151 "insert", "into", "values", "delete", "update", "set",
152 "create", "table", "int", "varchar", "view", "as", "index", "on");
153 }
154 }