summaryrefslogtreecommitdiff
path: root/src/main/java/no/eliashaugsbakk/kompilator/tokenization/Lexer.java
blob: 53c7b533edd1f61f04c3e6d912fa517174a1aa42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package no.eliashaugsbakk.kompilator.tokenization;

import static java.lang.Character.isLetterOrDigit;
import static no.eliashaugsbakk.kompilator.tokenization.LexerState.IN_STRING;
import static no.eliashaugsbakk.kompilator.tokenization.LexerState.IN_TYPE;
import static no.eliashaugsbakk.kompilator.tokenization.LexerState.IN_WORD;
import static no.eliashaugsbakk.kompilator.tokenization.LexerState.NORMAL;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.ASSIGN;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.COMMA;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.EOF;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.IDENTIFIER;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.KEYWORD;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.LPAREN;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.NULLABLE;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.RPAREN;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.SEMICOLON;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.STRING_LITERAL;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.TYPE;
import static no.eliashaugsbakk.kompilator.tokenization.TokenType.COLON;

import java.util.ArrayList;
import java.util.List;

public class Lexer {
  private char current;
  private int line = 1;
  private int column = 0;

  private final String input;
  private int position = 0;
  private final List<Token> tokens = new ArrayList<>();
  private final StringBuilder wordBuffer = new StringBuilder();
  LexerState state = NORMAL;


  public Lexer(String input) {
    this.input = input;
    this.current = input.charAt(position);
  }

  public List<Token> tokenize() {

    while (position < input.length()) {
      if (current == '\n') {
        line++;
        column = 0;
      } else {
        column++;
      }

      current = input.charAt(position);
      if (state == IN_STRING) {
        inString();
      } else if (!Character.isWhitespace(current)) {
        if (state == IN_WORD) {
          inWord();
        } else if (state == IN_TYPE) {
          inType();
        }

        if (state == NORMAL) {
          normal();
        }
      }
      position++;
    }
    tokens.add(new Token(EOF, "End of File", line, column));
    // tokens.forEach(token -> IO.println(token.type().toString() + ": " + token.value()));
    return tokens;
  }

  private void normal() {
    if (current == '"') {
      state = IN_STRING;
    } else if (isLetterOrDigit(current)) {
      state = IN_WORD;
      wordBuffer.append(current);

    } else if (current == ':') {
      tokens.add(new Token(COLON, ":", line, column));
      state = IN_TYPE;

    } else if (current == '=') {
      tokens.add(new Token(ASSIGN, "=", line, column));
    } else if (current == '(') {
      tokens.add(new Token(LPAREN, Character.toString(current), line, column));
    } else if (current == ')') {
      tokens.add(new Token(RPAREN, Character.toString(current), line, column));
    } else if (current == ';') {
      tokens.add(new Token(SEMICOLON, Character.toString(current), line, column));
    } else if (current == ',') {
      tokens.add(new Token(COMMA, Character.toString(current), line, column));
    }
  }

  private void inType() {
    if (current == '?') {
      state = NORMAL;
      tokens.add(new Token(TYPE, wordBuffer.toString(), line, column - wordBuffer.length()));
      tokens.add(new Token(NULLABLE, "?", line, column));
      clearWordBuffer();
    } else if (current == '=' || current == ';') {
      state = NORMAL;
      tokens.add(new Token(TYPE, wordBuffer.toString(), line, column - wordBuffer.length()));
      clearWordBuffer();
    } else {
      wordBuffer.append(current);
    }
  }

  private void inString() {
    if (current == '"') { // closing " gets skipped implicitly
      current = input.charAt(position);
      state = NORMAL;
      tokens.add(new Token(STRING_LITERAL, wordBuffer.toString(), line, column));
      wordBuffer.delete(0, wordBuffer.length());
    } else {
      wordBuffer.append(current);
    }
  }

  private void inWord() {
    if (!isLetterOrDigit(current) && current != '_') {
      state = NORMAL;
      characterizeWord();
    } else {
      wordBuffer.append(current);
    }

  }

  private void characterizeWord() {
    if (Keywords.KEYWORDS.contains(wordBuffer.toString())) {
      tokens.add(new Token(KEYWORD, wordBuffer.toString(), line, column - wordBuffer.length()));
    } else {
      tokens.add(new Token(IDENTIFIER, wordBuffer.toString(), line, column - wordBuffer.length()));
    }
    clearWordBuffer();
  }

  private void clearWordBuffer() {
    wordBuffer.delete(0, wordBuffer.length());
  }
}