package code.ast;
/**
* The Lexer class provides functionalities for parsing and iterating through
* a source string of text character by character.
*
* @author s-yamagiwa;
*/
public class Lexer {
private final String source;
private int position;
public Lexer(String source) {
this.source = source;
this.position = 0;
}
/**
* Retrieves the next character without advancing the position.
*
* @return The next character.
*/
public char peek() {
if (position >= source.length()) {
return '\0'; // End of line
}
return source.charAt(position);
}
/**
* Retrieves the next character after advancing the position.
*
* @return The next character.
*/
public char peekNext() {
if (position + 1 >= source.length()) {
return '\0'; // End of line
}
return source.charAt(position + 1);
}
/**
* Advances the position by one character.
*
* @return The character that was previously at the current position.
*/
public char advance() {
char current = peek();
position++;
return current;
}
}