Skip to content

Commit eef9417

Browse files
authored
Implement header skipping in TokenStream
Added functionality to skip headers in the token stream.
1 parent 7bdb725 commit eef9417

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

src/main/java/com/scanner/project/TokenStream.java

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,59 @@
11
package com.scanner.project;
22
// TokenStream.java
33

4-
// Implementation of the Scanner for JAY
5-
6-
// This code DOES NOT implement a scanner for JAY yet. You have to complete
7-
// the code and also make sure it implements a scanner for JAY - not something
8-
// else.
9-
104
import java.io.BufferedReader;
115
import java.io.FileNotFoundException;
126
import java.io.FileReader;
137
import java.io.IOException;
148

159
public class TokenStream {
1610

17-
// READ THE COMPLETE FILE FIRST
18-
// You will need to adapt it to KAY, NOT JAY
19-
20-
// Instance variables
21-
private boolean isEof = false; // is end of file
22-
private char nextChar = ' '; // next character in input stream
11+
private boolean isEof = false;
12+
private char nextChar = ' ';
2313
private BufferedReader input;
2414

25-
// This function was added to make the demo file work
2615
public boolean isEoFile() {
2716
return isEof;
2817
}
2918

30-
// Constructor
31-
// Pass a filename for the program text as a source for the TokenStream.
3219
public TokenStream(String fileName) {
3320
try {
3421
input = new BufferedReader(new FileReader(fileName));
3522
nextChar = readChar();
3623
} catch (FileNotFoundException e) {
3724
System.out.println("File not found: " + fileName);
38-
// System.exit(1); // Removed to allow ScannerDemo to continue
39-
// running after the input file is not found.
4025
isEof = true;
4126
}
4227
}
4328

44-
public Token nextToken() { // Main function of the scanner
29+
public Token nextToken() {
4530
skipWhiteSpace();
31+
32+
// --- FIX START: Skip headers ---
33+
while (nextChar == '[') {
34+
// Consume everything until ']'
35+
while (nextChar != ']' && !isEof) {
36+
nextChar = readChar();
37+
}
38+
// Consume the closing ']'
39+
if (!isEof) nextChar = readChar();
40+
// Skip any whitespace after the header
41+
skipWhiteSpace();
42+
}
43+
// --- FIX END ---
44+
4645
if (isEof) {
4746
Token eof = new Token();
4847
eof.setType("EOF");
4948
eof.setValue("");
5049
return eof;
5150
}
52-
// Return next token type and value.
5351
Token t = new Token();
54-
t.setType("Other"); // For now it is Other
52+
t.setType("Other");
5553
t.setValue("");
5654

57-
// First check for whitespaces and bypass them
58-
59-
60-
// Then check for a comment, and bypass it
61-
// but remember that / may also be a division operator.
55+
// Check for comments
6256
while (nextChar == '/') {
63-
// Changed if to while to avoid the 2nd line being printed when
64-
// there are two comment lines in a row.
6557
nextChar = readChar();
6658
if (nextChar == '/') {
6759
while (!isEndOfLine(nextChar) && !isEof) {
@@ -75,32 +67,29 @@ public Token nextToken() { // Main function of the scanner
7567
return eof;
7668
}
7769
} else {
78-
// A slash followed by anything else must be an operator.
7970
t.setValue("/");
8071
t.setType("Operator");
8172
return t;
8273
}
8374
}
8475

85-
// Then check for an operator; this part of the code should recover 2-character
86-
// operators as well as 1-character ones.
8776
if (isOperator(nextChar)) {
8877
t.setType("Operator");
8978
t.setValue("" + nextChar);
9079
switch (nextChar) {
9180
case '<':
9281
t.setType("Operator");
93-
nextChar = readChar();
94-
if (nextChar == '=') {
95-
t.setValue("<=");
96-
nextChar = readChar();
97-
} else if (nextChar == '>') { // Add this block
98-
t.setValue("<>");
99-
nextChar = readChar();
100-
} else {
101-
t.setValue("<");
102-
}
103-
return t;
82+
nextChar = readChar();
83+
if (nextChar == '=') {
84+
t.setValue("<=");
85+
nextChar = readChar();
86+
} else if (nextChar == '>') {
87+
t.setValue("<>");
88+
nextChar = readChar();
89+
} else {
90+
t.setValue("<");
91+
}
92+
return t;
10493

10594
case '>':
10695
t.setType("Operator");
@@ -147,7 +136,6 @@ public Token nextToken() { // Main function of the scanner
147136
t.setValue(":");
148137
return t;
149138
case '|':
150-
// Look for ||
151139
nextChar = readChar();
152140
if (nextChar == '|') {
153141
t.setType("Operator");
@@ -171,30 +159,26 @@ public Token nextToken() { // Main function of the scanner
171159
t.setValue("&");
172160
return t;
173161

174-
default: // all other operators
162+
default:
175163
t.setType("Operator");
176164
nextChar = readChar();
177165
return t;
178166
}
179167
}
180168

181-
// Then check for a separator
182169
if (isSeparator(nextChar)) {
183170
t.setType("Separator");
184171
t.setValue("" + nextChar);
185172
nextChar = readChar();
186173
return t;
187174
}
188175

189-
// Then check for an identifier, keyword, or literal.
190176
if (isLetter(nextChar)) {
191-
// Set to an identifier
192177
t.setType("Identifier");
193178
while (isLetter(nextChar) || isDigit(nextChar)) {
194179
t.setValue(t.getValue() + nextChar);
195180
nextChar = readChar();
196181
}
197-
// now see if this is a keyword
198182
if (isKeyword(t.getValue())) {
199183
t.setType("Keyword");
200184
} else if (t.getValue().equals("true") || t.getValue().equals("false")) {
@@ -203,14 +187,12 @@ public Token nextToken() { // Main function of the scanner
203187
return t;
204188
}
205189

206-
if (isDigit(nextChar)) { // check for integer literals
190+
if (isDigit(nextChar)) {
207191
t.setType("Literal");
208192
while (isDigit(nextChar)) {
209193
t.setValue(t.getValue() + nextChar);
210194
nextChar = readChar();
211195
}
212-
// An Integer-Literal is to be only followed by a space,
213-
// an operator, or a separator.
214196
return t;
215197
}
216198

@@ -253,12 +235,11 @@ private boolean isEndOfLine(char c) {
253235
return (c == '\r' || c == '\n' || c == '\f');
254236
}
255237

256-
private boolean isEndOfToken(char c) { // Is the value a seperate token?
238+
private boolean isEndOfToken(char c) {
257239
return (isWhiteSpace(c) || isOperator(c) || isSeparator(c) || isEof);
258240
}
259241

260242
private void skipWhiteSpace() {
261-
// check for whitespaces, and bypass them
262243
while (!isEof && isWhiteSpace(nextChar)) {
263244
nextChar = readChar();
264245
}

0 commit comments

Comments
 (0)