1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 07:50:09 -08:00

Correctly handle un-matched tokens and end-of-string situations.

This commit is contained in:
Danila Fedorin 2017-07-26 13:24:46 -07:00
parent 76705ed92b
commit 93e892f92e

View File

@ -87,11 +87,12 @@ public class Lexer<T> {
int index = startAt; int index = startAt;
ArrayList<Match<T>> matches = new ArrayList<>(); ArrayList<Match<T>> matches = new ArrayList<>();
Match<T> lastMatch = null; Match<T> lastMatch = null;
while((lastMatch = lexOne(from, index, compare)) != null && index < from.length()){ while(index < from.length() && (lastMatch = lexOne(from, index, compare)) != null){
if(lastMatch.getTo() == lastMatch.getFrom()) return null; if(lastMatch.getTo() == lastMatch.getFrom()) return null;
matches.add(lastMatch); matches.add(lastMatch);
index += lastMatch.getTo() - lastMatch.getFrom(); index += lastMatch.getTo() - lastMatch.getFrom();
} }
if(lastMatch == null) return null;
return matches; return matches;
} }