Exemple #1
0
    def test_parseinfo_directive(self):
        grammar = '''
            @@parseinfo
            @@parseinfo :: True

            test = value:"test" $;
        '''
        model = grako.compile(grammar, "test")
        ast = model.parse("test")
        self.assertIsNotNone(ast.parseinfo)

        code = codegen(model)
        self.assertTrue('parseinfo=True' in code)
        compile(code, 'test.py', EXEC)

        grammar = '''
            @@parseinfo :: False

            test = value:"test" $;
        '''
        model = grako.compile(grammar, "test")
        ast = model.parse("test")
        self.assertIsNone(ast.parseinfo)

        code = codegen(model)
        self.assertTrue('parseinfo=False' in code)
        compile(code, 'test.py', EXEC)
Exemple #2
0
 def MakeTest(self, exp, caseNo: int):    
     test = Test()                  
     model = compile(exp)        
     test.StartTest(caseNo, 1, 100, 'Grako', lambda w: self.TestPositive(model, ' '.join(w)), lambda w: self.TestNegative(model, ' '.join(w)))
     test.StartTest(caseNo, 101, 1000, 'Grako', lambda w: self.TestPositive(model, ' '.join(w)), lambda w: self.TestNegative(model, ' '.join(w)))
     test.StartTest(caseNo, 1001, 10000, 'Grako', lambda w: self.TestPositive(model, ' '.join(w)), lambda w: self.TestNegative(model, ' '.join(w)))
     test.StartTest(caseNo, 10001, 100000, 'Grako', lambda w: self.TestPositive(model, ' '.join(w)), lambda w: self.TestNegative(model, ' '.join(w)))        
Exemple #3
0
    def test_eol_comments_re_directive(self):
        grammar = '''
            @@eol_comments :: /#.*?$/

            test = "test" $;
        '''
        model = grako.compile(grammar, "test")
        code = codegen(model)
        compile(code, 'test.py', EXEC)
Exemple #4
0
    def test_whitespace_directive(self):
        grammar = '''
            @@whitespace :: /[\t ]+/

            test = "test" $;
        '''
        model = grako.compile(grammar, "test")
        code = codegen(model)
        compile('test.py', code, EXEC)
Exemple #5
0
    def test_left_recursion_directive(self):
        grammar = '''
            @@left_recursion :: False

            test = "test" $;
        '''
        model = grako.compile(grammar, "test")
        self.assertFalse(model.directives.get('left_recursion'))
        self.assertFalse(model.left_recursion)

        code = codegen(model)
        compile('test.py', code, EXEC)
Exemple #6
0
def test(s=SAMPLE):
    with open(s) as dsl:
        src = dsl.read()
        print('input DSL:')
        print(src)
        sem = SparqlSemantics()
        g = compile(SPARQL)
        ast = g.parse(src,
                      eol_comments_re="#.*?$",
                      semantics=sem,
                      nameguard=False)
        print('output AST:')
        pprint(ast)
        return ast
Exemple #7
0
    def test_grammar_directive(self):
        grammar = '''
            @@grammar :: Test

            start = test $;
            test = "test";
        '''
        model = grako.compile(grammar=grammar)
        self.assertEqual('Test', model.directives.get('grammar'))
        self.assertEqual('Test', model.name)

        code = codegen(model)
        module = compile(code, 'test.py', EXEC)

        assert 'TestParser' in module.co_names
Exemple #8
0
def main():
    grammar = regex_parser.translate('(a|b)*')
    model = grako.compile(grammar, 'Regexp')
    model.parse('aaabbaba', 'S0')
    try:
        model.parse('aaaCbbaba', 'S0')
        raise Exception('Should not have parsed!')
    except grako.exceptions.FailedParse:
        pass
    print('Grammar:', file=sys.stderr)
    print(grammar)
    sys.stdout.flush()
    with open(PARSER_FILENAME, 'w') as f:
        f.write(codegen(model))
    print('Generated parser saved as:', PARSER_FILENAME, file=sys.stderr)
    print(file=sys.stderr)
Exemple #9
0
    def test_rule_name(self):
        grammar = '''
            @@grammar :: Test

            start = test $;
            test = "test";
        '''
        model = grako.compile(grammar=grammar)
        self.assertEqual('Test', model.directives.get('grammar'))
        self.assertEqual('Test', model.name)

        ast = model.parse("test")
        self.assertEqual(ast, "test")

        ast = grako.parse(grammar, "test", rule_name='start')
        self.assertEqual(ast, "test")
Exemple #10
0
    def test_rule_name(self):
        grammar = '''
            @@grammar :: Test

            start = test $;
            test = "test";
        '''
        model = grako.compile(grammar=grammar)
        self.assertEqual('Test', model.directives.get('grammar'))
        self.assertEqual('Test', model.name)

        ast = model.parse("test")
        self.assertEqual(ast, "test")

        ast = grako.parse(grammar, "test", rule_name='start')
        self.assertEqual(ast, "test")
Exemple #11
0
    def test_whitespace_no_newlines(self):
        grammar = """
            @@whitespace :: /[\t ]+/
            # this is just a token with any character but space and newline
            # it should finish before it capture space or newline character
            token = /[^ \n]+/;
            # expect whitespace to capture spaces between tokens, but newline should be captured afterwards
            token2 = {token}* /\n/;
            # document is just list of this strings of tokens
            document = {@+:token2}* $;
        """
        text = trim("""\
            a b
            c d
            e f
        """)

        expected = [
            [
                [
                    "a",
                    "b"
                ],
                "\n"
            ],
            [
                [
                    "c",
                    "d"
                ],
                "\n"
            ],
            [
                [
                    "e",
                    "f"
                ],
                "\n"
            ]
        ]

        model = grako.compile(grammar, "document")
        ast = model.parse(text, start='document')
        self.assertEqual(expected, ast)