annotate python/c3/lexer.py @ 287:1c7c1e619be8

File movage
author Windel Bouwman
date Thu, 21 Nov 2013 11:57:27 +0100
parents 05184b95fa16
children a747a45dcd78
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
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
9 keywords = ['and', 'or', 'not', 'true', 'false', \
148
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', \
221
848c4b15fd0b pointers
Windel Bouwman
parents: 213
diff changeset
12 'struct', 'cast', \
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 232
diff changeset
13 'import', 'module' ]
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
15 def tokenize(input_file):
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
16 """
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 Tokenizer, generates an iterator that
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 returns tokens!
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
20 Input is a file like object.
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
21
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 This GREAT example was taken from python re doc page!
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
23 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
24 filename = input_file.name if hasattr(input_file, 'name') else ''
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
25 s = input_file.read()
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
26 tok_spec = [
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 ('REAL', r'\d+\.\d+'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28 ('HEXNUMBER', r'0x[\da-fA-F]+'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29 ('NUMBER', r'\d+'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 ('NEWLINE', r'\n'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 ('SKIP', r'[ \t]'),
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
33 ('COMMENTS', r'//.*'),
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
34 ('LONGCOMMENTBEGIN', r'\/\*'),
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
35 ('LONGCOMMENTEND', r'\*\/'),
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 225
diff changeset
36 ('LEESTEKEN', r'==|->|<<|>>|[\.,=:;\-+*\[\]/\(\)]|>=|<=|<>|>|<|{|}|&|\^|\|'),
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
37 ('STRING', r"'.*?'")
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 ]
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
39 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
40 gettok = re.compile(tok_re).match
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
41 line = 1
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
42 pos = line_start = 0
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
43 mo = gettok(s)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
44 incomment = False
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
45 while mo is not None:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
46 typ = mo.lastgroup
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
47 val = mo.group(typ)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 if typ == 'NEWLINE':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
49 line_start = pos
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50 line += 1
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
51 elif typ == 'COMMENTS':
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52 pass
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
53 elif typ == 'LONGCOMMENTBEGIN':
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
54 incomment = True
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
55 elif typ == 'LONGCOMMENTEND':
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
56 incomment = False
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57 elif typ == 'SKIP':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
58 pass
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
59 elif incomment:
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
60 pass # Wait until we are not in a comment section
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
61 else:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
62 if typ == 'ID':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
63 if val in keywords:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
64 typ = val
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
65 elif typ == 'LEESTEKEN':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
66 typ = val
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
67 elif typ == 'NUMBER':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68 val = int(val)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
69 elif typ == 'HEXNUMBER':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70 val = int(val[2:], 16)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
71 typ = 'NUMBER'
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
72 elif typ == 'REAL':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
73 val = float(val)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
74 elif typ == 'STRING':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
75 val = val[1:-1]
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
76 loc = SourceLocation(filename, line, mo.start() - line_start, mo.end() - mo.start())
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
77 yield Token(typ, val, loc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78 pos = mo.end()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
79 mo = gettok(s, pos)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
80 if pos != len(s):
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
81 col = pos - line_start
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
82 loc = SourceLocation(filename, line, col, 1)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
83 raise CompilerError('Unexpected character "{0}"'.format(s[pos]), loc)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
84 loc = SourceLocation(filename, line, 0, 0)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
85 yield Token('END', '', loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
86