Advertisement
PifyZ

Untitled

Apr 21st, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Lexer {
  4.     public String code;
  5.     public int cursorPosition;
  6.  
  7.     public Lexer(String code) {
  8.         this.code = code;
  9.     }
  10.  
  11.     public ArrayList<Token> lex() {
  12.         ArrayList<Token> tokens = new ArrayList<Token>();
  13.  
  14.         cursorPosition = -1;
  15.  
  16.         try {
  17.             while (true) {
  18.                 char currentChar;
  19.  
  20.                 try {
  21.                     currentChar = getNext();
  22.                 } catch (Exception e) {
  23.                     break;
  24.                 }
  25.  
  26.                 // Espaces, sauts de lignes, ...
  27.                 if (isWhitespace(currentChar)) {
  28.                     // Ne rien faire
  29.                 // +, =, (, ), [, ]
  30.                 } else if (isSymbol(currentChar)) {
  31.                     TokenType type = TokenType.getFromChar(currentChar);
  32.                     Token token = new Token(type, currentChar);
  33.                     tokens.add(token);
  34.                 // a..z | A..Z | _
  35.                 } else if (isAlpha(currentChar)) {
  36.                     String identifier = "";
  37.  
  38.                     while (isAlpha(currentChar)) {
  39.                         identifier += currentChar;
  40.  
  41.                         try {
  42.                             currentChar = getNext();
  43.                         } catch (Exception e) {
  44.                             break;
  45.                         }
  46.                     }
  47.  
  48.                     cursorPosition -= 1;
  49.  
  50.                     Token token = new Token(TokenType.TOK_IDENTIFIER, identifier);
  51.  
  52.                     tokens.add(token);
  53.                 // 0..9
  54.                 } else if (isDigit(currentChar)) {
  55.                     String identifier = "";
  56.  
  57.                     while (isDigit(currentChar)) {
  58.                         identifier += currentChar;
  59.  
  60.                         try {
  61.                             currentChar = getNext();
  62.                         } catch (Exception e) {
  63.                             break;
  64.                         }
  65.                     }
  66.  
  67.                     cursorPosition -= 1;
  68.  
  69.                     Token token = new Token(TokenType.TOK_IDENTIFIER, identifier);
  70.  
  71.                     tokens.add(token);
  72.                 }
  73.  
  74.                 if (cursorPosition > code.length())
  75.                     break;
  76.             }
  77.         } catch (Exception e) {
  78.             System.out.println(e);
  79.         }
  80.  
  81.         return tokens;
  82.     }
  83.  
  84.     public char getNext() {
  85.         cursorPosition += 1;
  86.  
  87.         return code.charAt(cursorPosition);
  88.     }
  89.  
  90.     public boolean isWhitespace(char chr) {
  91.         return chr == ' ' || chr == '\t' || chr == '\r' || chr == '\n';
  92.     }
  93.  
  94.     /// = | + | ( | ) | [ | ]
  95.     public boolean isSymbol(char chr) {
  96.         return chr == '=' || chr == '+' || chr == '(' || chr == ')' || chr == '[' || chr == ']';
  97.     }
  98.  
  99.     /// a .. z | A .. Z | _
  100.     public boolean isAlpha(char chr) {
  101.         return (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || chr == '_';
  102.     }
  103.  
  104.     /// 0 .. 9
  105.     public boolean isDigit(char chr) {
  106.         return chr >= '0' && chr <= '9';
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement