diff --git a/regexp.py b/regexp.py --- a/regexp.py +++ b/regexp.py @@ -35,9 +35,7 @@ class Symbol(Token): return self.value -class Asterisk(Token): - is_skippable = True - +class Plus(Token): def __init__(self, content: Token): self.content = content @@ -54,6 +52,13 @@ class Asterisk(Token): yield (x, y) def __str__(self): + return str(self.content) + "+" + + +class Asterisk(Plus): + is_skippable = True + + def __str__(self): return str(self.content) + "*" @@ -120,6 +125,10 @@ def parse(pattern, offset=0): token = res.pop() res.append(Asterisk(token)) i += 1 + elif c == "+": + token = res.pop() + res.append(Plus(token)) + i += 1 else: res.append(Symbol(i+offset, c)) i += 1 @@ -169,8 +178,9 @@ class Regexp: if __name__ == "__main__": tests = ["a", "ab", "aabb", "abab", "abcd", "abcbcdbcd"] - for pattern in ["a*b*", "(ab)*", "a((bc)*d)*"]: + for pattern in ["a*b*", "a+b+", "(ab)*", "(ab)+", "a((bc)*d)*"]: print("#", pattern) + r = Regexp(pattern) for t in tests: - print(t, Regexp(pattern).match(t)) + print(t, r.match(t)) print()