Пример #1
0
 def test_parser_scratch(self):
   source = """expression = number , [operator , expression] ;
               number = ? get_digit ? ;
               repeated = 3 * number ;
            """
   parser = Parser()
   grammar = parser.parse(source).trimmed().flattened().flattened(flatten)
Пример #2
0
  def test_parser(self):
    source = """expression = number , [operator , expression] ;
                number = ? get_digit ? ;
                (* a comment for you! *)
             """
    parser = Parser()
    grammar = parser.parse(source).trimmed().flattened().flattened(flatten)

    # pprint_parser_tree(grammar, source_len=len(source))

    self.assertEqual(list(c.node_type for c in grammar.children),
                     [TokenType.rule, TokenType.rule, TokenType.comment])
Пример #3
0
  def test_parser_2(self):
    with open(os.path.join(FIXTURES_ROOT, "ebnf.ebnf")) as rf:
      source = rf.read()

    parser = Parser()
    grammar = parser.parse(source).trimmed().flattened().flattened(flatten)

    # pprint_parser_tree(grammar, source_len=len(source))

    rules = {n.children[0].value for n in grammar.children if n.is_type(TokenType.rule)}
    expected = {"grammar", "comment", "rule", "identifier", "expression", "expression_terminal",
                "option_group", "repetition_group", "grouping_group", "special_handling", "number",
                "terminal", "operator", "op_mult", "op_add", "alpha_character", "digit",
                "printable"}
    self.assertEqual(rules & expected, expected)