annotate python/ppci/frontends/lexer.py @ 96:a350055d6119

movage
author windel
date Mon, 24 Dec 2012 13:30:12 +0100
parents python/libs/compiler/frontends/lexer.py@60cc36ef5a50
children
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 import collections
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 import re
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
3 from .errors import CompilerException
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
4
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
5 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
6 Lexical analyzer part. Splits the input character stream into tokens.
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
7 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
8
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
9 # Token is used in the lexical analyzer:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 Token = collections.namedtuple('Token', 'typ val row col')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12 keywords = ['and', 'array', 'begin', 'by', 'case', 'const', 'div', 'do', \
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13 'else', 'elsif', 'end', 'false', 'for', 'if', 'import', 'in', 'is', \
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
14 'mod', 'module', 'nil', 'not', 'of', 'or', 'pointer', 'procedure', \
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 'record', 'repeat', 'return', 'then', 'to', 'true', 'type', 'until', 'var', \
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
16 'while', 'asm' ]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
18 def tokenize(s):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20 Tokenizer, generates an iterator that
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 returns tokens!
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 This GREAT example was taken from python re doc page!
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 tok_spec = [
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 ('REAL', r'\d+\.\d+'),
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27 ('HEXNUMBER', r'0x[\da-fA-F]+'),
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 ('NUMBER', r'\d+'),
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 ('ID', r'[A-Za-z][A-Za-z\d_]*'),
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 ('NEWLINE', r'\n'),
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 ('SKIP', r'[ \t]'),
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32 ('COMMENTS', r'{.*}'),
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 ('LEESTEKEN', r':=|[\.,=:;\-+*\[\]/\(\)]|>=|<=|<>|>|<'),
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 ('STRING', r"'.*?'")
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 ]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 tok_re = '|'.join('(?P<%s>%s)' % pair for pair in tok_spec)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 gettok = re.compile(tok_re).match
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 line = 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 pos = line_start = 0
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 mo = gettok(s)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 while mo is not None:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42 typ = mo.lastgroup
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 val = mo.group(typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44 if typ == 'NEWLINE':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45 line_start = pos
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
46 line += 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47 elif typ == 'COMMENTS':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
48 pass
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
49 elif typ != 'SKIP':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
50 if typ == 'ID':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
51 if val in keywords:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
52 typ = val
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
53 elif typ == 'LEESTEKEN':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
54 typ = val
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
55 elif typ == 'NUMBER':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
56 val = int(val)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
57 elif typ == 'HEXNUMBER':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
58 val = int(val[2:], 16)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
59 typ = 'NUMBER'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
60 elif typ == 'REAL':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
61 val = float(val)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
62 elif typ == 'STRING':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
63 val = val[1:-1]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
64 yield Token(typ, val, line, mo.start()-line_start)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
65 pos = mo.end()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
66 mo = gettok(s, pos)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
67 if pos != len(s):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
68 col = pos - line_start
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
69 raise CompilerException('Unexpected character {0}'.format(s[pos]), line, col)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
70 yield Token('END', '', line, 0)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
71