annotate python/ppci/c3/lexer.py @ 383:173e20a47fda

Added linker description loader
author Windel Bouwman
date Sun, 27 Apr 2014 17:40:39 +0200
parents b8ad45b3a573
children 6ae782a085e0
rev   line source
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
1 import re
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 163
diff changeset
2 from ppci import CompilerError, SourceLocation, Token
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 353
diff changeset
3 from baselex import BaseLexer
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
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
9 keywords = ['and', 'or', 'not', 'true', 'false',
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 306
diff changeset
10 'else', 'if', 'while', 'for', 'return',
305
0615b5308710 Updated docs
Windel Bouwman
parents: 300
diff changeset
11 'function', 'var', 'type', 'const',
0615b5308710 Updated docs
Windel Bouwman
parents: 300
diff changeset
12 'struct', 'cast',
0615b5308710 Updated docs
Windel Bouwman
parents: 300
diff changeset
13 'import', 'module']
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
15
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
16 class Lexer:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 300
diff changeset
17 """ Generates a sequence of token from an input stream """
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
18 def __init__(self, diag):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
19 self.diag = diag
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
21 def lex(self, source):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
22 return self.tokenize(source)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
23
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
24 def tokenize(self, input_file):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
25 """
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
26 Tokenizer, generates an iterator that
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
27 returns tokens!
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
28
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
29 Input is a file like object.
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
30
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
31 This GREAT example was taken from python re doc page!
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
32 """
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
33 filename = input_file.name if hasattr(input_file, 'name') else ''
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
34 s = input_file.read()
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
35 input_file.close()
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
36 self.diag.addSource(filename, s)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
37 tok_spec = [
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
38 ('REAL', r'\d+\.\d+'),
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
39 ('HEXNUMBER', r'0x[\da-fA-F]+'),
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
40 ('NUMBER', r'\d+'),
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
41 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
42 ('NEWLINE', r'\n'),
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
43 ('SKIP', r'[ \t]'),
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
44 ('COMMENTS', r'//.*'),
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
45 ('LONGCOMMENTBEGIN', r'\/\*'),
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
46 ('LONGCOMMENTEND', r'\*\/'),
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 306
diff changeset
47 ('LEESTEKEN', r'==|->|<<|>>|!=|\+\+|[\.,=:;\-+*\[\]/\(\)]|>=|<=|<>|>|<|{|}|&|\^|\|'),
353
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 322
diff changeset
48 ('STRING', r'".*?"')
300
Windel Bouwman
parents: 293
diff changeset
49 ]
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
50 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
51 gettok = re.compile(tok_re).match
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
52 line = 1
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
53 pos = line_start = 0
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
54 mo = gettok(s)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
55 incomment = False
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
56 while mo is not None:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
57 typ = mo.lastgroup
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
58 val = mo.group(typ)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
59 if typ == 'NEWLINE':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
60 line_start = pos
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
61 line += 1
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
62 elif typ == 'COMMENTS':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
63 pass
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
64 elif typ == 'LONGCOMMENTBEGIN':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
65 incomment = True
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
66 elif typ == 'LONGCOMMENTEND':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
67 incomment = False
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
68 elif typ == 'SKIP':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
69 pass
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
70 elif incomment:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 300
diff changeset
71 pass # Wait until we are not in a comment section
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
72 else:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
73 if typ == 'ID':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
74 if val in keywords:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
75 typ = val
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
76 elif typ == 'LEESTEKEN':
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
77 typ = val
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
78 elif typ == 'NUMBER':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
79 val = int(val)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
80 elif typ == 'HEXNUMBER':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
81 val = int(val[2:], 16)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
82 typ = 'NUMBER'
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
83 elif typ == 'REAL':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
84 val = float(val)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
85 elif typ == 'STRING':
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
86 val = val[1:-1]
300
Windel Bouwman
parents: 293
diff changeset
87 loc = SourceLocation(filename, line, mo.start() - line_start,
305
0615b5308710 Updated docs
Windel Bouwman
parents: 300
diff changeset
88 mo.end() - mo.start())
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
89 yield Token(typ, val, loc)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
90 pos = mo.end()
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
91 mo = gettok(s, pos)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
92 if pos != len(s):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
93 col = pos - line_start
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
94 loc = SourceLocation(filename, line, col, 1)
300
Windel Bouwman
parents: 293
diff changeset
95 raise CompilerError('Unexpected: "{0}"'.format(s[pos]), loc)
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
96 loc = SourceLocation(filename, line, 0, 0)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 288
diff changeset
97 yield Token('END', '', loc)