-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexing.py
205 lines (125 loc) · 4.15 KB
/
lexing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <headingcell level=1>
# Lexical Analysis
# <markdowncell>
# Lexical analysis takes letters from a piece of source code and converts them into a stream of tokens which are values associated with a particular type. For example '3 + 4' might turn into the token (3, integer), ('', whitespace), (+, operator), (4, integer).
# <codecell>
simple_src = '4*6 + 9*(8-1)'
# <codecell>
import re
# <codecell>
from collections import namedtuple
# <markdowncell>
# A token is defined by it's name, a pattern which is used to match it and (optional) callable which converts from the textual value of the token into a value. If this callable is None then the raw textual value of the token is used.
# <codecell>
class TokenDef(namedtuple('TokenDef', ('name', 'pattern', 'value_filter'))):
def __repr__(self):
return 'TokenType.' + self.name
# <codecell>
class TokenType(object):
_defs = [
# operators
TokenDef('plus', '+', None),
TokenDef('minus', '-', None),
TokenDef('asterisk', '*', None),
TokenDef('slash', '/', None),
# other punctuation
TokenDef('left_paren', '(', None),
TokenDef('right_paren', ')', None),
# more complex tokens
TokenDef('integer', re.compile('[0-9]+'), int),
TokenDef('whitespace', re.compile('[ \t]+'), None),
]
# <codecell>
for def_ in TokenType._defs:
setattr(TokenType, def_.name, def_)
# <codecell>
TokenType.integer
# <markdowncell>
# Define a `Token` as a tuple of type (an attribute of 'TokenType'), a value and the slice of the input which it covers.
# <codecell>
Token = namedtuple('Token', ('type', 'value', 'slice'))
# <markdowncell>
# Define a function to return the first token from a piece of text.
# <codecell>
def first_token(text, start=0):
"""Takes some text and an optional starting position within that string and
returns a Token representing the longest match at the front of that
string starting from *start*."""
match_text = text[start:]
token = None
token_text = None
for type_ in TokenType._defs:
name, pattern, value_filter = type_
if pattern is None:
continue
elif isinstance(pattern, str):
if not match_text.startswith(pattern):
continue
match_value = pattern
else:
match = pattern.match(match_text)
if not match:
continue
match_value = match.group(0)
# see if the match value is longer than the current token's match value
if token_text is not None and len(token_text) >= len(match_value):
continue
token_text = match_value
if value_filter is not None:
match_value = value_filter(match_value)
token = Token(type_, match_value, slice(start, start + len(token_text)))
return token
# <codecell>
first_token(' ')
# <codecell>
first_token('6')
# <codecell>
first_token('68')
# <codecell>
first_token('68+')
# <codecell>
first_token('+')
# <markdowncell>
# The raw lexer repeatedly calls `first_token` on the text until all the text has been consumed.
# <codecell>
def lex_raw(text):
start = 0
while True:
if start >= len(text):
break
token = first_token(text, start)
yield token
start = token.slice.stop
# <markdowncell>
# Check that `lex_raw` is giving correct output.
# <codecell>
list(lex_raw('8+9'))
# <codecell>
list(lex_raw('8 +9'))
# <codecell>
list(lex_raw('8 + 7 / (6-8)'))
# <markdowncell>
# Define the actual function which should be exported from this module.
# <codecell>
lex = lex_raw
# <markdowncell>
# Check that the lexer can lex some real source code.
# <codecell>
src = '5+6*(8-1)/2-5'
# <codecell>
list(lex(src))
# <headingcell level=2>
# Skipping Whitespace
# <codecell>
def lex_skip_whitespace(text):
for token in lex_raw(text):
if token.type is TokenType.whitespace:
continue
yield token
# <codecell>
lex = lex_skip_whitespace
# <codecell>
list(lex('3 + 7'))
# <codecell>