| 1 |
import py |
| 2 |
from rpython.rlib.parsing import deterministic, regex |
| 3 |
|
| 4 |
class Token(object): |
| 5 |
def __init__(self, name, source, source_pos): |
| 6 |
self.name = name |
| 7 |
self.source = source |
| 8 |
self.source_pos = source_pos |
| 9 |
|
| 10 |
def copy(self): |
| 11 |
return self.__class__(self.name, self.source, self.source_pos) |
| 12 |
|
| 13 |
def __eq__(self, other): |
| 14 |
|
| 15 |
return self.__dict__ == other.__dict__ |
| 16 |
|
| 17 |
def __ne__(self, other): |
| 18 |
|
| 19 |
return not self == other |
| 20 |
|
| 21 |
def __repr__(self): |
| 22 |
return "Token(%r, %r, %r)" % (self.name, self.source, self.source_pos) |
| 23 |
|
| 24 |
class SourcePos(object): |
| 25 |
"""An object to record position in source code.""" |
| 26 |
def __init__(self, i, lineno, columnno): |
| 27 |
self.i = i |
| 28 |
self.lineno = lineno |
| 29 |
self.columnno = columnno |
| 30 |
|
| 31 |
def copy(self): |
| 32 |
return SourcePos(self.i, self.lineno, self.columnno) |
| 33 |
|
| 34 |
def __eq__(self, other): |
| 35 |
|
| 36 |
return self.__dict__ == other.__dict__ |
| 37 |
|
| 38 |
def __ne__(self, other): |
| 39 |
|
| 40 |
return not self == other |
| 41 |
|
| 42 |
def __repr__(self): |
| 43 |
return "SourcePos(%r, %r, %r)" % (self.i, self.lineno, self.columnno) |
| 44 |
|
| 45 |
class Lexer(object): |
| 46 |
def __init__(self, token_regexs, names, ignore=None): |
| 47 |
self.token_regexs = token_regexs |
| 48 |
self.names = names |
| 49 |
self.rex = regex.LexingOrExpression(token_regexs, names) |
| 50 |
automaton = self.rex.make_automaton() |
| 51 |
self.automaton = automaton.make_deterministic(names) |
| 52 |
self.automaton.optimize() |
| 53 |
if ignore is None: |
| 54 |
ignore = [] |
| 55 |
for ign in ignore: |
| 56 |
assert ign in names |
| 57 |
self.ignore = dict.fromkeys(ignore) |
| 58 |
self.matcher = self.automaton.make_lexing_code() |
| 59 |
|
| 60 |
def get_runner(self, text, eof=False, token_class=None): |
| 61 |
return LexingDFARunner(self.matcher, self.automaton, text, |
| 62 |
self.ignore, eof, token_class=token_class) |
| 63 |
|
| 64 |
def tokenize(self, text, eof=False): |
| 65 |
"""Return a list of Token's from text.""" |
| 66 |
r = self.get_runner(text, eof) |
| 67 |
result = [] |
| 68 |
while 1: |
| 69 |
try: |
| 70 |
tok = r.find_next_token() |
| 71 |
result.append(tok) |
| 72 |
except StopIteration: |
| 73 |
break |
| 74 |
return result |
| 75 |
|
| 76 |
def get_dummy_repr(self): |
| 77 |
return '%s\nlexer = DummyLexer(recognize, %r, %r)' % ( |
| 78 |
py.code.Source(self.matcher), |
| 79 |
self.automaton, |
| 80 |
self.ignore) |
| 81 |
|
| 82 |
def __getstate__(self): |
| 83 |
return (self.token_regexs, self.names, self.ignore) |
| 84 |
|
| 85 |
def __setstate__(self, args): |
| 86 |
self.__init__(*args) |
| 87 |
|
| 88 |
|
| 89 |
class DummyLexer(Lexer): |
| 90 |
def __init__(self, matcher, automaton, ignore): |
| 91 |
self.token_regexs = None |
| 92 |
self.names = None |
| 93 |
self.rex = None |
| 94 |
self.automaton = automaton |
| 95 |
self.ignore = ignore |
| 96 |
self.matcher = matcher |
| 97 |
|
| 98 |
class AbstractLexingDFARunner(deterministic.DFARunner): |
| 99 |
i = 0 |
| 100 |
def __init__(self, matcher, automaton, text, eof=False): |
| 101 |
self.automaton = automaton |
| 102 |
self.state = 0 |
| 103 |
self.text = text |
| 104 |
self.last_matched_state = 0 |
| 105 |
self.last_matched_index = -1 |
| 106 |
self.eof = eof |
| 107 |
self.matcher = matcher |
| 108 |
self.lineno = 0 |
| 109 |
self.columnno = 0 |
| 110 |
|
| 111 |
def find_next_token(self): |
| 112 |
while 1: |
| 113 |
self.state = 0 |
| 114 |
start = self.last_matched_index + 1 |
| 115 |
assert start >= 0 |
| 116 |
|
| 117 |
|
| 118 |
if start == len(self.text) and self.eof: |
| 119 |
self.last_matched_index += 1 |
| 120 |
return self.make_token(start, -1, "", eof=True) |
| 121 |
elif start >= len(self.text): |
| 122 |
raise StopIteration |
| 123 |
|
| 124 |
i = self.inner_loop(start) |
| 125 |
if i < 0: |
| 126 |
i = ~i |
| 127 |
stop = self.last_matched_index + 1 |
| 128 |
assert stop >= 0 |
| 129 |
if start == stop: |
| 130 |
source_pos = self.token_position_class(i - 1, self.lineno, self.columnno) |
| 131 |
raise deterministic.LexerError(self.text, self.state, |
| 132 |
source_pos) |
| 133 |
source = self.text[start:stop] |
| 134 |
result = self.make_token(start, self.last_matched_state, source) |
| 135 |
self.adjust_position(source) |
| 136 |
if self.ignore_token(self.last_matched_state): |
| 137 |
continue |
| 138 |
return result |
| 139 |
if self.last_matched_index == i - 1: |
| 140 |
source = self.text[start: ] |
| 141 |
result = self.make_token(start, self.last_matched_state, source) |
| 142 |
self.adjust_position(source) |
| 143 |
if self.ignore_token(self.last_matched_state): |
| 144 |
if self.eof: |
| 145 |
self.last_matched_index += 1 |
| 146 |
return self.make_token(i, -1, "", eof=True) |
| 147 |
else: |
| 148 |
raise StopIteration |
| 149 |
return result |
| 150 |
source_pos = self.token_position_class(i - 1, self.lineno, self.columnno) |
| 151 |
raise deterministic.LexerError(self.text, self.state, source_pos) |
| 152 |
|
| 153 |
def adjust_position(self, token): |
| 154 |
"""Update the line# and col# as a result of this token.""" |
| 155 |
newlines = token.count("\n") |
| 156 |
self.lineno += newlines |
| 157 |
if newlines==0: |
| 158 |
self.columnno += len(token) |
| 159 |
else: |
| 160 |
self.columnno = token.rfind("\n") |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
def inner_loop(self, i): |
| 179 |
return self.matcher(self, i) |
| 180 |
|
| 181 |
next = find_next_token |
| 182 |
|
| 183 |
def __iter__(self): |
| 184 |
return self |
| 185 |
|
| 186 |
class LexingDFARunner(AbstractLexingDFARunner): |
| 187 |
def __init__(self, matcher, automaton, text, ignore, eof=False, |
| 188 |
token_class=None): |
| 189 |
|
| 190 |
if not token_class: |
| 191 |
self.token_class = Token |
| 192 |
self.token_position_class = SourcePos |
| 193 |
|
| 194 |
else: |
| 195 |
self.token_class = token_class |
| 196 |
self.token_position_class = token_class.source_position_class |
| 197 |
|
| 198 |
AbstractLexingDFARunner.__init__(self, matcher, automaton, text, eof) |
| 199 |
self.ignore = ignore |
| 200 |
|
| 201 |
def ignore_token(self, state): |
| 202 |
return self.automaton.names[state] in self.ignore |
| 203 |
|
| 204 |
def make_token(self, index, state, text, eof=False): |
| 205 |
assert (eof and state == -1) or 0 <= state < len(self.automaton.names) |
| 206 |
|
| 207 |
source_pos = self.token_position_class(index, self.lineno, self.columnno) |
| 208 |
if eof: |
| 209 |
return self.token_class("EOF", "EOF", source_pos) |
| 210 |
|
| 211 |
return self.token_class(self.automaton.names[self.last_matched_state], |
| 212 |
text, source_pos) |