aboutsummaryrefslogtreecommitdiff
path: root/neb/lexer.py
blob: e9142bbf81cf4252a7105f52261a6fe9ce822598 (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
from .structs import TokenType, Token
from .exceptions import LexError
import sys


types = {
    ":int": TokenType.INT_TYPE,
    ":float": TokenType.FLOAT_TYPE,
    ":number": TokenType.NUMBER_TYPE,
    ":string": TokenType.STRING_TYPE,
    ":list": TokenType.LIST_TYPE,
    ":any": TokenType.ANY_TYPE,
    ":literal": TokenType.LITERAL_TYPE,
    ":bool": TokenType.BOOL_TYPE }

keywords = {
    "def": TokenType.DEF,
    "lambda": TokenType.LAMBDA,
    "&": TokenType.MANY,
    "func": TokenType.FUNC }


WHITESPACE = [" ", "\n", "\t"]
SEPARATORS = WHITESPACE + [")", "]", "}"]
DIGITS = list("0123456789")

def lex(data):
    start = 0
    current = 0
    line = 1
    end = len(data)

    tokens = []
    while current < end:
        char = data[current]
        if char == ";":
            while char != "\n" and current < end:
                current += 1
                char = data[current]
            continue
        if char == "\n":
            line += 1
        if char in WHITESPACE:
            current += 1
            continue
        elif char == "(":
            tokens.append(Token(TokenType.OPEN_PAREN, "(", None, line))
        elif char == ")":
            tokens.append(Token(TokenType.CLOSE_PAREN, ")", None, line))
        elif char == "[":
            tokens.append(Token(TokenType.OPEN_BRACKET, "[", None, line))
        elif char == "]":
            tokens.append(Token(TokenType.CLOSE_BRACKET, "]", None, line))
        elif char == "{":
            tokens.append(Token(TokenType.OPEN_BRACE, "{", None, line))
        elif char == "}":
            tokens.append(Token(TokenType.CLOSE_BRACE, "}", None, line))
        elif char == ":":
            tokens.append(Token(TokenType.COLON, ":", None, line))
        # numbers
        #elif char in DIGITS or char == ".":
        elif char in DIGITS:
            tok, length = get_number(data[current:], line)
            tokens.append(tok)
            current += length
        # strings
        elif char == '"':
            tok, length, offset = get_string(data[current+1:], line)
            tokens.append(tok)
            current += length
            line += offset
        # bools
        elif char == "#":
            tok, length = get_bool(data[current+1:], line)
            tokens.append(tok)
            current += length
        # single quotes
        elif char == "'":
            tokens.append(Token(TokenType.APOSTROPHE, "'", None, line))
        # symbols
        else:
            tok, length = get_symbol(data[current:], line)
            if tok.text in keywords:
                tok.type_ = keywords[tok.text]
            tokens.append(tok)
            current += length

        current += 1
    tokens.append(Token(TokenType.EOF, "", None, line))
    return tokens

def get_number(data, line):
    counter = 0
    value = ""
    is_float = False
    char = data[counter]
    while char not in SEPARATORS:
        if char in DIGITS:
            value += char
        elif char == ".":
            if is_float:
                raise LexError("too many '.' in number", line)
            is_float = True
            value += char
        else:
            raise Exception(f"invalid number: {value}")
        counter += 1
        if counter >= len(data):
            break
        char = data[counter]
    if is_float:
        return Token(TokenType.FLOAT, value, float(value), line), counter - 1
    else:
        return Token(TokenType.INT, value, int(value), line), counter - 1


def get_string(data, line):
    offset = 0
    counter = 0
    string = ""
    while data[counter] != '"':
        if data[counter] == "\n":
            offset += 1

        # look ahead to see if it's a double quote
        if data[counter] == "\\" and \
                    len(data) > counter and \
                    data[counter+1] == '"':
            string += '"'
            counter += 1
        else:
            string += data[counter]
        counter += 1
        if counter >= len(data):
            raise Exception("couldn't parse string")
    string = string.encode().decode("unicode_escape")
    return Token(TokenType.STRING, str(string), str(string), line), counter + 1, offset

def get_bool(data, line):
    counter = 0
    value = ""
    while data[counter] not in SEPARATORS:
        value += data[counter]
        counter += 1
        if counter >= len(data):
            break
    if value == "true":
        return Token(TokenType.TRUE, "#true", True, line), 4
    elif value == "false":
        return Token(TokenType.FALSE, "#false", False, line), 5
    else:
        raise LexError("couldn't parse boolean", line)

def get_symbol(data, line):
    counter = 0
    value = ""
    while data[counter] not in SEPARATORS:
        value += data[counter]
        counter += 1
        if counter >= len(data):
            break
    return Token(TokenType.SYMBOL, value, None, line), counter - 1