pmacs3/parse.py

162 lines
3.9 KiB
Python
Raw Permalink Normal View History

import re
2009-02-02 09:44:32 -05:00
import lex
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):
if not tokens:
return []
return self._match(tokens)
def _match(self, tokens):
2009-02-02 09:44:32 -05:00
raise Exception("unimplemented")
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):
for token in tokens:
if token.name in self.names:
return [1]
return []
class String(Rule):
def __init__(self, s):
self.string = s
def _match(self, tokens):
for token in tokens:
if token.string == self.string:
return [1]
return []
class Strings(Rule):
def __init__(self, ss):
self.strings = ss
def _match(self, tokens):
for token in tokens:
if token.string in self.strings:
return [1]
return []
class Stringre(Rule):
def __init__(self, r):
self.regex = re.compile(r)
def _match(self, tokens):
for token in tokens:
if self.regex.match(token.string):
return [1]
return []
2009-02-02 09:44:32 -05:00
class Match(Rule):
def __init__(self, *args):
self.args = args
def method(self, tok, name, string):
return tok.match(name, string)
2009-02-02 09:44:32 -05:00
def match(self, tokens):
if not tokens:
return []
elif self.method(tokens[0], *self.args):
return [1]
2009-02-02 09:44:32 -05:00
else:
return []
2009-02-02 09:44:32 -05:00
class Matchs(Match):
def method(self, tok, name, strings):
return tok.matchs(name, strings)
2009-02-02 09:44:32 -05:00
class Matchp(Match):
def method(self, tok, pairs):
return tok.matchp(pairs)
2009-02-02 09:44:32 -05:00
class Fqmatch(Match):
def method(self, tok, name, string):
return tok.fqmatch(name, string)
2009-02-02 09:44:32 -05:00
class Fqmatchs(Match):
def method(self, name, strings, nocase=False):
return tok.fqmatchs(name, strings, nocase)
2009-02-02 09:44:32 -05:00
class Fqmatchp(Match):
def method(self, name, pairs):
return tok.fqmatchp(pairs)
2009-02-02 09:44:32 -05:00
class And(Rule):
def match(self, tokens):
n = 0
2009-02-02 09:44:32 -05:00
for r in self.rules:
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):
n = 0
2009-02-02 09:44:32 -05:00
for r in self.rules:
result = r.match(tokens[n:])
if result:
return result
n += result[0]
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):
n = 0
for _ in range(0, self.minimum):
result = self.rule.match(tokens[n:])
if not result:
return []
n += result[0]
for _ 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
class Optional(Repeat):
2009-02-02 09:44:32 -05:00
def __init__(self, rule):
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):
if tokens:
return []
else:
return [0]