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 variable(self, text): """variable = identifier - reserved_words ;""" self._attempting(text) return exclusion( self.identifier, self.reserved_words )(text).compressed(TokenType.variable)
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_exclusion(self): c0 = P.alternation(["a", "b"]) c1 = P.exclusion(c0, "b") n0 = c1("a") self.assertEqual(n0.node_type, P.ParseNodeType.terminal) self.assertEqual(n0.value, "a") self.assertEqual(c0("b").value, "b") with self.assertRaises(P.DeadEnd): c1("ba")
def eol(self, text): """eol = { whitespace - "\n" } . "\n" ;""" self._attempting(text) return concatenation([ zero_or_more( exclusion( self.whitespace, "\n" ), ignore_whitespace=False ), "\n", ], ignore_whitespace=False)(text)
def single_string_char(self, text): """single_string_char = "\\" . all_characters - "'" | "\" . "'" | all_characters - "'" ;""" self._attempting(text) return alternation([ concatenation([ "\\\\", exclusion( self.all_characters, "'" ), ], ignore_whitespace=False), concatenation([ "\\", "'", ], ignore_whitespace=False), exclusion( self.all_characters, "'" ), ])(text).retyped(TokenType.single_string_char)
def double_string_char(self, text): """double_string_char = "\\" . all_characters - '"' | "\" . '"' | all_characters - '"' ;""" self._attempting(text) return alternation([ concatenation([ "\\\\", exclusion( self.all_characters, '"' ), ], ignore_whitespace=False), concatenation([ "\\", '"', ], ignore_whitespace=False), exclusion( self.all_characters, '"' ), ])(text).retyped(TokenType.double_string_char)
def number(self, text): """number = digit - "0" . {digit} ;""" self._attempting(text) return concatenation([ exclusion( self.digit, "0" ), zero_or_more( self.digit, ignore_whitespace=False ), ], ignore_whitespace=False)(text).compressed(TokenType.number)
def comment(self, text): """comment = "/*" . {all_characters - "*" | "*" . all_characters - "/"} . "*/" ;""" self._attempting(text) return concatenation([ "/*", zero_or_more( alternation([ exclusion( self.all_characters, "*" ), concatenation([ "*", exclusion( self.all_characters, "/" ), ], ignore_whitespace=False), ]), ignore_whitespace=False ), "*/", ], ignore_whitespace=False)(text).compressed(TokenType.comment)
def directive(self, text): """directive = "#" . { all_characters - "\n" } . eol ;""" self._attempting(text) return concatenation([ "#", zero_or_more( exclusion( self.all_characters, "\n" ), ignore_whitespace=False ), self.eol, ], ignore_whitespace=False)(text).compressed(TokenType.directive)
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 comment(self, text): """comment = "(*" . {printable - "*" | "*" . printable - ")"} . "*)" ;""" self._attempting(text) return concatenation([ "(*", zero_or_more( alternation([ exclusion( self.printable, "*" ), concatenation([ "*", exclusion( self.printable, ")" ), ], ignore_whitespace=False), ]), ignore_whitespace=False ), "*)", ], ignore_whitespace=False)(text).compressed(TokenType.comment)