Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sci.regex.lexer;
- import java.util.*;
- public final class Lexer {
- @FunctionalInterface
- private interface StateFn {
- StateFn next(final Lexer lexer);
- }
- private String input;
- private int start;
- private int pos;
- private int width;
- private List<Token> result;
- public Lexer(final String input) {
- this.input = input;
- this.width = 1;
- this.result = new ArrayList<>();
- }
- private String current() {
- return this.input.substring(this.start, this.pos);
- }
- private void emit(final TokenType type) {
- this.result.add(new Token(type, this.current()));
- this.start = this.pos;
- }
- private char next() {
- if(this.pos >= this.input.length()) {
- this.width = 0;
- return 0;
- }
- final char c = this.input.charAt(this.pos);
- this.pos++;
- return c;
- }
- private char peek() {
- final char c = this.next();
- this.prev();
- return c;
- }
- private void prev() {
- this.pos--;
- }
- private void ignore() {
- this.start = this.pos;
- }
- private boolean accept(final String valid) {
- if(valid.contains(String.valueOf(this.next()))) {
- return true;
- }
- this.prev();
- return false;
- }
- private void acceptRun(final String valid) {
- while(valid.contains(String.valueOf(this.next())))
- ;
- this.prev();
- }
- private void run() {
- StateFn state = Lexer::lexAny;
- while(state != null) {
- state = state.next(this);
- }
- }
- public Token[] tokenize() {
- this.run();
- return this.result.toArray(new Token[this.result.size()]);
- }
- private static StateFn lexAny(final Lexer lexer) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement