Advertisement
sci4me

Lexer

Mar 6th, 2016
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. package com.sci.regex.lexer;
  2.  
  3. import java.util.*;
  4.  
  5. public final class Lexer {
  6.     @FunctionalInterface
  7.     private interface StateFn {
  8.         StateFn next(final Lexer lexer);
  9.     }  
  10.    
  11.     private String input;
  12.     private int start;
  13.     private int pos;
  14.     private int width;
  15.     private List<Token> result;
  16.    
  17.     public Lexer(final String input) {
  18.         this.input = input;
  19.         this.width = 1;
  20.         this.result = new ArrayList<>();
  21.     }
  22.    
  23.     private String current() {
  24.         return this.input.substring(this.start, this.pos);
  25.     }
  26.    
  27.     private void emit(final TokenType type) {
  28.         this.result.add(new Token(type, this.current()));
  29.         this.start = this.pos;
  30.     }
  31.  
  32.     private char next() {
  33.         if(this.pos >= this.input.length()) {
  34.             this.width = 0;
  35.             return 0;
  36.         }  
  37.        
  38.         final char c = this.input.charAt(this.pos);
  39.         this.pos++;
  40.         return c;
  41.     }
  42.    
  43.     private char peek() {
  44.         final char c = this.next();
  45.         this.prev();
  46.         return c;
  47.     }
  48.    
  49.     private void prev() {
  50.         this.pos--;
  51.     }
  52.    
  53.     private void ignore() {
  54.         this.start = this.pos;
  55.     }
  56.    
  57.     private boolean accept(final String valid) {
  58.         if(valid.contains(String.valueOf(this.next()))) {
  59.             return true;
  60.         }
  61.         this.prev();
  62.         return false;
  63.     }
  64.    
  65.     private void acceptRun(final String valid) {
  66.         while(valid.contains(String.valueOf(this.next())))
  67.             ;
  68.         this.prev();
  69.     }
  70.    
  71.     private void run() {
  72.         StateFn state = Lexer::lexAny;
  73.         while(state != null) {
  74.             state = state.next(this);
  75.         }
  76.     }
  77.    
  78.     public Token[] tokenize() {
  79.         this.run();
  80.         return this.result.toArray(new Token[this.result.size()]);
  81.     }
  82.    
  83.     private static StateFn lexAny(final Lexer lexer) {
  84.         return null;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement