annotate python/c3/lexer.py @ 202:f22b431f4113

Added arm add instruction
author Windel Bouwman
date Sat, 15 Jun 2013 10:02:50 +0200
parents b01429a5d695
children de3a68f677a5
rev   line source
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
1 import collections, re
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 149
diff changeset
2
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 163
diff changeset
3 from ppci import CompilerError, SourceLocation, Token
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6 Lexical analyzer part. Splits the input character stream into tokens.
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
8
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
9 keywords = ['and', 'or', 'not','true', 'false', \
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 'else', 'if', 'while', 'return', \
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 152
diff changeset
11 'function', 'var', 'type', 'const', \
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 'import', 'package' ]
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 def tokenize(s):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 Tokenizer, generates an iterator that
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 returns tokens!
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 This GREAT example was taken from python re doc page!
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21 tok_spec = [
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 ('REAL', r'\d+\.\d+'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 ('HEXNUMBER', r'0x[\da-fA-F]+'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
24 ('NUMBER', r'\d+'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
26 ('NEWLINE', r'\n'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 ('SKIP', r'[ \t]'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28 ('COMMENTS', r'//.*'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29 ('LEESTEKEN', r'==|[\.,=:;\-+*\[\]/\(\)]|>=|<=|<>|>|<|{|}'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 ('STRING', r"'.*?'")
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 ]
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
33 gettok = re.compile(tok_re).match
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
34 line = 1
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
35 pos = line_start = 0
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
36 mo = gettok(s)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
37 while mo is not None:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 typ = mo.lastgroup
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 val = mo.group(typ)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 if typ == 'NEWLINE':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 line_start = pos
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 line += 1
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43 elif typ == 'COMMENTS':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
44 pass
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
45 elif typ == 'SKIP':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
46 pass
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
47 else:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 if typ == 'ID':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
49 if val in keywords:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50 typ = val
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
51 elif typ == 'LEESTEKEN':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52 typ = val
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
53 elif typ == 'NUMBER':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
54 val = int(val)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
55 elif typ == 'HEXNUMBER':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
56 val = int(val[2:], 16)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57 typ = 'NUMBER'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
58 elif typ == 'REAL':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
59 val = float(val)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
60 elif typ == 'STRING':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
61 val = val[1:-1]
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 152
diff changeset
62 loc = SourceLocation(line, mo.start()-line_start, mo.end() - mo.start())
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
63 yield Token(typ, val, loc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
64 pos = mo.end()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
65 mo = gettok(s, pos)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
66 if pos != len(s):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
67 col = pos - line_start
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68 pos = line
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 149
diff changeset
69 raise CompilerError('Unexpected character {0}'.format(s[pos]), pos)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
70 loc = SourceLocation(line, 0, 0)
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
71 yield Token('END', '', loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
72