Skip to content

Commit 0ada69c

Browse files
author
Eble
committed
Lexer:skip whitespace
1 parent 851f951 commit 0ada69c

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

src/main/java/com/ParserExample.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ public class ParserExample {
66

77
void parsePrint() throws Exception {
88
m_lexer.expect(com.compiler.TokenIntf.Type.PRINT);
9-
m_lexer.expect(com.compiler.TokenIntf.Type.WHITESPACE);
109
com.compiler.Token number = m_lexer.lookAhead();
1110
m_lexer.expect(com.compiler.TokenIntf.Type.INTEGER);
1211
m_lexer.expect(com.compiler.TokenIntf.Type.SEMICOLON);
13-
m_lexer.expect(com.compiler.TokenIntf.Type.WHITESPACE);
1412
System.out.println(number.m_value);
1513
}
1614

@@ -32,8 +30,7 @@ public static void main(String[] args) throws Exception {
3230
parser.parse("""
3331
PRINT 1;
3432
PRINT 2;
35-
PRINT 3;
36-
""");
33+
PRINT 3;""");
3734
}
3835

3936
}

src/main/java/com/compiler/Lexer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,13 @@ public Token lookAhead() {
190190
}
191191

192192
public void advance() throws Exception {
193-
m_currentToken = nextToken();
193+
Token token = nextToken();
194+
while (token.m_type == Token.Type.WHITESPACE ||
195+
token.m_type == Token.Type.MULTILINECOMMENT ||
196+
token.m_type == Token.Type.LINECOMMENT) {
197+
token = nextToken();
198+
}
199+
m_currentToken = token;
194200
}
195201

196202
public void expect(Token.Type tokenType) throws Exception {

0 commit comments

Comments
 (0)