summaryrefslogtreecommitdiff
path: root/willow/tools/willowc/lib/tokenizer.cpp
blob: 0c1f917e8ad00564821b51b154155626aa5e3fe0 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <tokenizer.hpp>

namespace willowc {

static inline bool is_space(unsigned char c) {
  return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
static inline bool is_digit(unsigned char c) { return c >= '0' && c <= '9'; }
static inline bool is_xdigit(unsigned char c) {
  return is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
static inline bool is_alpha(unsigned char c) {
  unsigned char x = static_cast<unsigned char>(c | 0x20);
  return x >= 'a' && x <= 'z';
}

static inline bool valid_id_start(int c) {
  return is_alpha(c) || c == '$' || c == '.' || c == '_' || c == '-';
}

bool Tokenizer::scan_id(bool accept_digits = true) {
  char c = peek();

  if (accept_digits && is_digit(c)) {
    // if it starts with a digit, must be all digits
    while (is_digit(peek()))
      skip();
    return true;
  }

  if (!valid_id_start(c))
    return false;

  while (valid_id_start(peek()) || isdigit(peek()))
    skip();

  return true;
}

Token Tokenizer::scan() {
  std::size_t start = this->offset;

  while (isspace(peek()))
    skip();

  TokenKind k = [&] {
    switch (peek()) {
    case '@':
      skip();
      if (scan_id(false))
        return TokenKind::Function;
      return TokenKind::Invalid;
    case '%':
      skip();
      if (scan_id())
        return TokenKind::Variable;
      return TokenKind::Invalid;
    case '^':
      skip();
      if (scan_id())
        return TokenKind::Label;
      return TokenKind::Invalid;
    case ',':
      skip();
      return TokenKind::Comma;
    case ';':
      skip();
      return TokenKind::Semicolon;
    case '(':
      skip();
      return TokenKind::LParen;
    case ')':
      skip();
      return TokenKind::RParen;
    case '{':
      skip();
      return TokenKind::LCurly;
    case '}':
      skip();
      return TokenKind::RCurly;
    case '=':
      skip();
      return TokenKind::Equals;
    case '-': {
      if (peek(1) == '>') {
        skip(2);
        return TokenKind::RArrow;
      }
      if (isdigit(peek(1))) {
        skip();
        if (scan_dec())
          return TokenKind::Constant;
      }
      return TokenKind::Invalid;
    }
    case '/': {
      skip();
      if (peek() != '/')
        return TokenKind::Invalid;

      skip();
      char c = eat();
      while (c != '\0' && c != '\n')
        c = eat();

      return TokenKind::Comment;
    }
    case '\0':
      return TokenKind::Eof;
    default: {
      if (is_digit(peek()))
        return scan_constant() ? TokenKind::Constant : TokenKind::Invalid;

      if (peek() == 'i') {
        skip();
        if (scan_dec())
          return TokenKind::Type;
      }

      if (isalpha(peek())) {
        skip();
        while (isalnum(peek()) || peek() == '.')
          skip();
        return TokenKind::Inst;
      }

      return TokenKind::Invalid;
    }
    }
  }();

  return Token{start, offset, k};
}

bool Tokenizer::scan_dec() {
  if (!is_digit(peek()))
    return false;
  skip();
  while (is_digit(peek()))
    skip();

  return true;
}

bool Tokenizer::scan_hex() {
  if (!is_xdigit(peek()))
    return false;
  skip();
  while (is_xdigit(peek()))
    skip();

  return true;
}

bool Tokenizer::scan_constant() {
  if (peek() == '-')
    skip();

  if (peek() == '0') {
    skip();
    if (is_digit(peek()))
      return false;
    if (peek() == 'x') {
      skip();
      return scan_hex();
    } else {
      return true; // 0
    }
  } else if (is_digit(peek())) {
    return scan_dec();
  }

  return false;
}

} // namespace willowc