def terminal(self, text): """terminal = '"' . (printable - '"') + . '"' | "'" . (printable - "'") + . "'" ;""" self._attempting(text) return alternation([ concatenation([ '"', one_or_more( exclusion( self.printable, '"' ), ignore_whitespace=False ), '"', ], ignore_whitespace=False), concatenation([ "'", one_or_more( exclusion( self.printable, "'" ), ignore_whitespace=False ), "'", ], ignore_whitespace=False), ])(text).compressed(TokenType.terminal)
def number(self, text): """number = ["-"] . ("0" | digit - "0" . {digit}) . ["." . digit +] ;""" self._attempting(text) return concatenation([ option( "-" ), alternation([ "0", concatenation([ exclusion( self.digit, "0" ), zero_or_more( self.digit, ignore_whitespace=False ), ], ignore_whitespace=False), ]), option( concatenation([ ".", one_or_more( self.digit, ignore_whitespace=False ), ], ignore_whitespace=False) ), ], ignore_whitespace=False)(text).compressed(TokenType.number)
def test_one_or_more(self): c0 = P.one_or_more("a") n0 = c0("ab") self.assertEqual(n0.node_type, P.ParseNodeType.repetition) self.assertEqual(len(n0.children), 1) n1 = c0("aaa") self.assertEqual(len(n1.children), 3) with self.assertRaises(P.DeadEnd): c0("baa")
def switch(self, text): """switch = switch_subject , switch_case + , [switch_default] ; s""" self._attempting(text) return concatenation([ self.switch_subject, one_or_more( self.switch_case, ignore_whitespace=True ), option( self.switch_default ), ], ignore_whitespace=True)(text).retyped(TokenType.switch)
def complex_identifier(self, text): """complex_identifier = "[" . (all_characters - "]") + . "]" ;""" self._attempting(text) return concatenation([ "[", one_or_more( exclusion( self.all_characters, "]" ), ignore_whitespace=False ), "]", ], ignore_whitespace=False)(text).retyped(TokenType.complex_identifier)
def expression(self, text): """expression = {operator} , expression_terminal , {operator + , expression} ;""" self._attempting(text) return concatenation([ zero_or_more( self.operator, ignore_whitespace=True ), self.expression_terminal, zero_or_more( concatenation([ one_or_more( self.operator, ignore_whitespace=True ), self.expression, ], ignore_whitespace=True), ignore_whitespace=True ), ], ignore_whitespace=True)(text).retyped(TokenType.expression)