annotate python/c3/lexer.py @ 288:a747a45dcd78

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