-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathLexer.java
More file actions
155 lines (134 loc) · 5.53 KB
/
Lexer.java
File metadata and controls
155 lines (134 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*BEGIN_COPYRIGHT_BLOCK
*
* Copyright (c) 2001-2019, JavaPLT group at Rice University (drjava@rice.edu). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the names of DrJava, the JavaPLT group, Rice University, nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software is Open Source Initiative approved Open Source Software. Open Source Initative Approved is a trademark
* of the Open Source Initiative.
*
* This file is part of DrJava. Download the current version of this project from http://www.drjava.org/ or
* http://sourceforge.net/projects/drjava/
*
* END_COPYRIGHT_BLOCK*/
package edu.rice.cs.util.sexp;
import java.util.*;
import java.io.*;
class Lexer extends StreamTokenizer {
public HashMap<String,Tokens.SExpToken> wordTable = new HashMap<String,Tokens.SExpToken>();
private Tokens.SExpToken buffer;
public Lexer(File file) throws FileNotFoundException{
this(new BufferedReader(new FileReader(file)));
}
public Lexer(Reader reader) {
super(new BufferedReader(reader));
initLexer();
}
private void initLexer() {
// configure StreamTokenizer portion of this
resetSyntax();
parseNumbers();
slashSlashComments(true);
wordChars('!','\'');
wordChars('*','~');
quoteChar('"');
ordinaryChars('(',')');
whitespaceChars(0,' ');
commentChar(';');
initWordTable();
buffer = null; // buffer initially empty
}
/** Skips through the input stream until an EOL is encountered
* @throws IOException if an IO operation fails
*/
public void flush() throws IOException {
eolIsSignificant(true);
while (nextToken() != TT_EOL) ; // eat tokens until EOL
eolIsSignificant(false);
}
/** Performs a nextToken() operation from StreamTokenizer except for throwing
* an unchecked LexingException instead of a checked IOException
* @return the token type
*/
private int getToken() {
try {
int tokenType = nextToken();
return tokenType;
} catch(IOException e) {
throw new LexingException("Unable to read the data from the given input");
}
}
/** @return the next Tokens.SExpToken without consuming it */
public Tokens.SExpToken peek() {
if (buffer == null) buffer = readToken();
return buffer;
}
/** Reads the next Tokens.SExpToken from the input stream and consumes it.
* @return the Tokens.SExpToken object representing this Tokens.SExpToken */
public Tokens.SExpToken readToken() {
if (buffer != null) {
Tokens.SExpToken token = buffer;
buffer = null; // clear buffer
return token;
}
int tokenType = getToken();
// Process the Tokens.SExpToken returned by StreamTokenizer
switch (tokenType) {
case TT_NUMBER:
return new Tokens.NumberToken(nval);
case TT_WORD:
String s = sval.toLowerCase();
Tokens.SExpToken regToken = wordTable.get(s);
if (regToken == null) return new Tokens.WordToken(sval);
return regToken;
case TT_EOF: return null;
case '(': return Tokens.LeftParenToken.ONLY;
case ')': return Tokens.RightParenToken.ONLY;
case '"': return new Tokens.QuotedTextToken(sval);
case '\\':
// int t = getToken();
// if (t == '"') {
// return new Tokens.WordToken("\"");
// }
// else if (t == '\\') {
// return new Tokens.WordToken("\\");
// }
// else if (t == ' ') {
// return new Tokens.WordToken(" ");
// }
// else if (t == 'n') {
// return new Tokens.WordToken("\n");
// }
// else if (t == 't') {
// return new Tokens.WordToken("\t");
// }
// else {
// pushBack();
return Tokens.BackSlashToken.ONLY;
// throw new SExpParseException("Invalid escape sequence: \\" + (char)t);
default:
return new Tokens.WordToken("" + (char)tokenType);
}
}
/** Initialize the word table used by the lexer to classify Tokens */
private void initWordTable() {
// initialize wordTable
wordTable.put("true", Tokens.BooleanToken.TRUE);
wordTable.put("false", Tokens.BooleanToken.FALSE);
}
}