Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class Lexer {
- public String code;
- public int cursorPosition;
- public Lexer(String code) {
- this.code = code;
- }
- public ArrayList<Token> lex() {
- ArrayList<Token> tokens = new ArrayList<Token>();
- cursorPosition = -1;
- try {
- while (true) {
- char currentChar;
- try {
- currentChar = getNext();
- } catch (Exception e) {
- break;
- }
- // Espaces, sauts de lignes, ...
- if (isWhitespace(currentChar)) {
- // Ne rien faire
- // +, =, (, ), [, ]
- } else if (isSymbol(currentChar)) {
- TokenType type = TokenType.getFromChar(currentChar);
- Token token = new Token(type, currentChar);
- tokens.add(token);
- // a..z | A..Z | _
- } else if (isAlpha(currentChar)) {
- String identifier = "";
- while (isAlpha(currentChar)) {
- identifier += currentChar;
- try {
- currentChar = getNext();
- } catch (Exception e) {
- break;
- }
- }
- cursorPosition -= 1;
- Token token = new Token(TokenType.TOK_IDENTIFIER, identifier);
- tokens.add(token);
- // 0..9
- } else if (isDigit(currentChar)) {
- String identifier = "";
- while (isDigit(currentChar)) {
- identifier += currentChar;
- try {
- currentChar = getNext();
- } catch (Exception e) {
- break;
- }
- }
- cursorPosition -= 1;
- Token token = new Token(TokenType.TOK_IDENTIFIER, identifier);
- tokens.add(token);
- }
- if (cursorPosition > code.length())
- break;
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- return tokens;
- }
- public char getNext() {
- cursorPosition += 1;
- return code.charAt(cursorPosition);
- }
- public boolean isWhitespace(char chr) {
- return chr == ' ' || chr == '\t' || chr == '\r' || chr == '\n';
- }
- /// = | + | ( | ) | [ | ]
- public boolean isSymbol(char chr) {
- return chr == '=' || chr == '+' || chr == '(' || chr == ')' || chr == '[' || chr == ']';
- }
- /// a .. z | A .. Z | _
- public boolean isAlpha(char chr) {
- return (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || chr == '_';
- }
- /// 0 .. 9
- public boolean isDigit(char chr) {
- return chr >= '0' && chr <= '9';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement