2009-02-03 09:56:22 -05:00
|
|
|
import re
|
2009-02-02 09:44:32 -05:00
|
|
|
import lex
|
|
|
|
|
2009-02-03 09:56:22 -05:00
|
|
|
MAX_MAX = 1024
|
|
|
|
|
2009-02-02 09:44:32 -05:00
|
|
|
class Rule(object):
|
|
|
|
def __init__(self, *rules):
|
|
|
|
self.rules = rules
|
|
|
|
def match(self, tokens):
|
2009-02-03 09:56:22 -05:00
|
|
|
if not tokens:
|
|
|
|
return []
|
|
|
|
return self._match(tokens)
|
|
|
|
def _match(self, tokens):
|
2009-02-02 09:44:32 -05:00
|
|
|
raise Exception("unimplemented")
|
|
|
|
|
2009-02-03 09:56:22 -05:00
|
|
|
class Any(Rule):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
def _match(self, tokens):
|
|
|
|
if tokens:
|
|
|
|
return [1]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
class Name(Rule):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
def _match(self, tokens):
|
|
|
|
if tokens[0].name == self.name:
|
|
|
|
return [1]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
class Names(Rule):
|
|
|
|
def __init__(self, names):
|
|
|
|
self.names = names
|
|
|
|
def _match(self, tokens):
|
|
|
|
if token.name in self.names:
|
|
|
|
return [1]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
class String(Rule):
|
|
|
|
def __init__(self, s):
|
|
|
|
self.string = s
|
|
|
|
def _match(self, tokens):
|
|
|
|
if token.string == self.string:
|
|
|
|
return [1]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
class Strings(Rule):
|
|
|
|
def __init__(self, ss):
|
|
|
|
self.strings = ss
|
|
|
|
def _match(self, tokens):
|
|
|
|
if token.string in self.strings:
|
|
|
|
return [1]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
class Stringre(Rule):
|
|
|
|
def __init__(self, r):
|
|
|
|
self.regex = re.compile(r)
|
|
|
|
def _match(self, tokens):
|
|
|
|
if self.regex.match(token.string):
|
|
|
|
return [1]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2009-02-02 09:44:32 -05:00
|
|
|
class Match(Rule):
|
|
|
|
method = lex.Token.match
|
|
|
|
def __init__(self, *args):
|
|
|
|
self.args = args
|
|
|
|
def match(self, tokens):
|
|
|
|
if not tokens:
|
2009-02-03 09:56:22 -05:00
|
|
|
return []
|
|
|
|
elif self.method(tokens[0], *self.args):
|
|
|
|
return [1]
|
2009-02-02 09:44:32 -05:00
|
|
|
else:
|
2009-02-03 09:56:22 -05:00
|
|
|
return []
|
2009-02-02 09:44:32 -05:00
|
|
|
class Matchs(Match):
|
|
|
|
method = lex.Token.matchs
|
|
|
|
class Matchp(Match):
|
|
|
|
method = lex.Token.matchp
|
|
|
|
|
|
|
|
class Fqmatch(Match):
|
|
|
|
method = lex.Token.fqmatch
|
|
|
|
class Fqmatchs(Match):
|
|
|
|
method = lex.Token.fqmatchs
|
|
|
|
class Fqmatchp(Match):
|
|
|
|
method = lex.Token.fqmatchp
|
|
|
|
|
|
|
|
class And(Rule):
|
|
|
|
def match(self, tokens):
|
2009-02-03 09:56:22 -05:00
|
|
|
n = 0
|
2009-02-02 09:44:32 -05:00
|
|
|
for r in self.rules:
|
2009-02-03 09:56:22 -05:00
|
|
|
result = r.match(tokens[n:])
|
|
|
|
if not result:
|
|
|
|
return []
|
|
|
|
n += result[0]
|
|
|
|
return [n]
|
2009-02-02 09:44:32 -05:00
|
|
|
class Or(Rule):
|
|
|
|
def match(self, tokens):
|
|
|
|
for r in self.rules:
|
2009-02-03 09:56:22 -05:00
|
|
|
result = r.match(tokens[n:])
|
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
return []
|
2009-02-02 09:44:32 -05:00
|
|
|
|
|
|
|
class Repeat(Rule):
|
|
|
|
def __init__(self, rule, minimum, maximum):
|
|
|
|
self.rule = rule
|
|
|
|
self.minimum = minimum
|
|
|
|
self.maximum = maximum
|
|
|
|
def match(self, tokens):
|
2009-02-03 09:56:22 -05:00
|
|
|
n = 0
|
2009-02-02 09:44:32 -05:00
|
|
|
for i in range(0, self.minimum):
|
2009-02-03 09:56:22 -05:00
|
|
|
result = self.rule.match(tokens[n:])
|
|
|
|
if not result:
|
|
|
|
return []
|
|
|
|
n += result[0]
|
|
|
|
for i in range(self.minimum, self.maximum):
|
|
|
|
result = self.rule.match(tokens[n:])
|
|
|
|
if not result:
|
|
|
|
return [n]
|
|
|
|
n += result[0]
|
|
|
|
return [n]
|
2009-02-02 09:44:32 -05:00
|
|
|
|
2009-02-03 09:56:22 -05:00
|
|
|
class Optional(Repeat):
|
2009-02-02 09:44:32 -05:00
|
|
|
def __init__(self, rule):
|
2009-02-03 09:56:22 -05:00
|
|
|
self.rule = rule
|
|
|
|
self.minimum = 0
|
|
|
|
self.maximum = 1
|
|
|
|
class Star(Repeat):
|
|
|
|
def __init__(self, rule):
|
|
|
|
self.rule = rule
|
|
|
|
self.minimum = 0
|
|
|
|
self.maximum = MAX_MAX
|
|
|
|
class Plus(Repeat):
|
|
|
|
def __init__(self, rule):
|
|
|
|
self.rule = rule
|
|
|
|
self.minimum = 1
|
|
|
|
self.maximum = MAX_MAX
|
|
|
|
|
2009-02-02 09:44:32 -05:00
|
|
|
class End(Rule):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
def match(self, tokens):
|
2009-02-03 09:56:22 -05:00
|
|
|
if tokens:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return [0]
|