def test_or(self): self._compare('a|b', N.Or(N.Char('a'), N.Char('b'))) self._compare('ab|c', N.Or(N.Concat(N.Char('a'), N.Char('b')), N.Char('c'))) self._compare('a|bc', N.Or(N.Char('a'), N.Concat(N.Char('b'), N.Char('c')))) self._compare('a|b|c', N.Or(N.Or(N.Char('a'), N.Char('b')), N.Char('c'))) self._compare('|', N.Or(N.Empty(), N.Empty()))
def parse_concat(self): lineno = self.stream.current.lineno args = [self.parse_mul()] while self.stream.current.type == 'tilde': next(self.stream) args.append(self.parse_mul()) if len(args) == 1: return args[0] return nodes.Concat(args, lineno=lineno)
def test_concat(self): self._compare( 'abc', N.Concat(N.Concat(N.Char('a'), N.Char('b')), N.Char('c'))) self._compare( 'abcde', N.Concat( N.Concat( N.Concat(N.Concat(N.Char('a'), N.Char('b')), N.Char('c')), N.Char('d')), N.Char('e')))
def test_parentheses(self): self._compare('()', N.Empty()) self._compare('(ab)+', N.OneOrMore(N.Concat(N.Char('a'), N.Char('b')))) self._compare('a(b)+', N.Concat(N.Char('a'), N.OneOrMore(N.Char('b')))) parse.from_string('a(bcd*|efgh?(jk)+)*')
def test_unary(self): self._compare('a?', N.ZeroOrOne(N.Char('a'))) self._compare('ab+', N.Concat(N.Char('a'), N.OneOrMore(N.Char('b')))) self._compare('a+b', N.Concat(N.OneOrMore(N.Char('a')), N.Char('b')))