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