288
|
1 import re
|
191
|
2 from ppci import CompilerError, SourceLocation, Token
|
148
|
3
|
|
4 """
|
|
5 Lexical analyzer part. Splits the input character stream into tokens.
|
|
6 """
|
|
7
|
288
|
8 keywords = ['and', 'or', 'not', 'true', 'false',
|
315
|
9 'else', 'if', 'while', 'for', 'return',
|
305
|
10 'function', 'var', 'type', 'const',
|
|
11 'struct', 'cast',
|
|
12 'import', 'module']
|
148
|
13
|
293
|
14
|
|
15 class Lexer:
|
305
|
16 """ Generates a sequence of token from an input stream """
|
293
|
17 def __init__(self, diag):
|
|
18 self.diag = diag
|
148
|
19
|
306
|
20 def lex(self, source):
|
|
21 return self.tokenize(source)
|
|
22
|
293
|
23 def tokenize(self, input_file):
|
|
24 """
|
|
25 Tokenizer, generates an iterator that
|
|
26 returns tokens!
|
|
27
|
|
28 Input is a file like object.
|
287
|
29
|
293
|
30 This GREAT example was taken from python re doc page!
|
|
31 """
|
|
32 filename = input_file.name if hasattr(input_file, 'name') else ''
|
|
33 s = input_file.read()
|
|
34 input_file.close()
|
|
35 self.diag.addSource(filename, s)
|
|
36 tok_spec = [
|
|
37 ('REAL', r'\d+\.\d+'),
|
|
38 ('HEXNUMBER', r'0x[\da-fA-F]+'),
|
|
39 ('NUMBER', r'\d+'),
|
|
40 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
|
|
41 ('NEWLINE', r'\n'),
|
|
42 ('SKIP', r'[ \t]'),
|
|
43 ('COMMENTS', r'//.*'),
|
|
44 ('LONGCOMMENTBEGIN', r'\/\*'),
|
|
45 ('LONGCOMMENTEND', r'\*\/'),
|
315
|
46 ('LEESTEKEN', r'==|->|<<|>>|!=|\+\+|[\.,=:;\-+*\[\]/\(\)]|>=|<=|<>|>|<|{|}|&|\^|\|'),
|
293
|
47 ('STRING', r"'.*?'")
|
300
|
48 ]
|
293
|
49 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
|
|
50 gettok = re.compile(tok_re).match
|
|
51 line = 1
|
|
52 pos = line_start = 0
|
|
53 mo = gettok(s)
|
|
54 incomment = False
|
|
55 while mo is not None:
|
|
56 typ = mo.lastgroup
|
|
57 val = mo.group(typ)
|
|
58 if typ == 'NEWLINE':
|
|
59 line_start = pos
|
|
60 line += 1
|
|
61 elif typ == 'COMMENTS':
|
|
62 pass
|
|
63 elif typ == 'LONGCOMMENTBEGIN':
|
|
64 incomment = True
|
|
65 elif typ == 'LONGCOMMENTEND':
|
|
66 incomment = False
|
|
67 elif typ == 'SKIP':
|
|
68 pass
|
|
69 elif incomment:
|
305
|
70 pass # Wait until we are not in a comment section
|
293
|
71 else:
|
|
72 if typ == 'ID':
|
|
73 if val in keywords:
|
|
74 typ = val
|
|
75 elif typ == 'LEESTEKEN':
|
288
|
76 typ = val
|
293
|
77 elif typ == 'NUMBER':
|
|
78 val = int(val)
|
|
79 elif typ == 'HEXNUMBER':
|
|
80 val = int(val[2:], 16)
|
|
81 typ = 'NUMBER'
|
|
82 elif typ == 'REAL':
|
|
83 val = float(val)
|
|
84 elif typ == 'STRING':
|
|
85 val = val[1:-1]
|
300
|
86 loc = SourceLocation(filename, line, mo.start() - line_start,
|
305
|
87 mo.end() - mo.start())
|
293
|
88 yield Token(typ, val, loc)
|
|
89 pos = mo.end()
|
|
90 mo = gettok(s, pos)
|
|
91 if pos != len(s):
|
|
92 col = pos - line_start
|
|
93 loc = SourceLocation(filename, line, col, 1)
|
300
|
94 raise CompilerError('Unexpected: "{0}"'.format(s[pos]), loc)
|
293
|
95 loc = SourceLocation(filename, line, 0, 0)
|
|
96 yield Token('END', '', loc)
|