def test_isolatedBracedEnd(self): my_parser = pushdown.Lark( r""" start: braced_free_input_string " {" constant_usage: CONSTANT_USAGE_ text_chunk: TEXT_CHUNK_ CONSTANT_USAGE_: /\$[^\n\$\:]+\:/ TEXT_CHUNK_: /(\\{|\\}|\\\$|[^\n{}\$])+/ braced_free_input_string: ( constant_usage | text_chunk )* ( braced_constant_usage_end | braced_text_chunk_end ) braced_constant_usage_end: BRACED_CONSTANT_USAGE_END_ braced_text_chunk_end: BRACED_TEXT_CHUNK_END_ BRACED_CONSTANT_USAGE_END_: /(?:\$[^\n\$\:]+\:)(?=(?: \u007b))/ BRACED_TEXT_CHUNK_END_: /(\\{|\\}|\\\$|[^\n{}\$])+(?=(?: \u007b))/ """, start='start', parser='lalr', lexer='contextual' ) tree = my_parser.parse( r"true$constant:\$|false {" ) self.assertTextEqual( r""" + start + braced_free_input_string + text_chunk [@1,0:3='true'<TEXT_CHUNK_>,1:1] + constant_usage [@2,4:13='$constant:'<CONSTANT_USAGE_>,1:5] + braced_text_chunk_end [@3,14:21='\\$|false'<BRACED_TEXT_CHUNK_END_>,1:15] """, tree.pretty(debug=True) )
def _getParser(self, log_level): ## The relative path the the pushdown grammar parser file from the current file grammar_file_path = get_relative_path( "grammars_grammar.pushdown", __file__ ) ## The parser used to build the Abstract Syntax Tree and parse the input text with open( grammar_file_path, "r", encoding='utf-8' ) as file: my_parser = pushdown.Lark( file.read(), start='language_syntax', parser='lalr', lexer='contextual', debug=log_level) return my_parser
def test_isolatedConstantUsage(self): my_parser = pushdown.Lark( r""" free_input_string: ( constant_usage | text_chunk )* ( constant_usage_end | text_chunk_end ) constant_usage: CONSTANT_USAGE_ text_chunk: TEXT_CHUNK_ constant_usage_end: CONSTANT_USAGE_END_ text_chunk_end: TEXT_CHUNK_END_ CONSTANT_USAGE_: /\$[^\n\$\:]+\:/ TEXT_CHUNK_: /(\\{|\\}|\\\$|[^\n{}\$])+/ CONSTANT_USAGE_END_: /(?:\$[^\n\$\:]+\:)(?=(?:\n|$))/ TEXT_CHUNK_END_: /(\\{|\\}|\\\$|[^\n{}\$])+(?=(?:\n|$))/ """, start='free_input_string', parser='lalr', lexer='contextual' ) tree = my_parser.parse( r"true$constant:\$|false" ) self.assertTextEqual( r""" + free_input_string + text_chunk [@1,0:3='true'<TEXT_CHUNK_>,1:1] + constant_usage [@2,4:13='$constant:'<CONSTANT_USAGE_>,1:5] + text_chunk_end [@3,14:21='\\$|false'<TEXT_CHUNK_END_>,1:15] """, tree.pretty(debug=True) )
#! /usr/bin/env python # -*- coding: utf-8 -*- import pushdown _parser = pushdown.Lark(r""" start: terminal_name terminal_name: TOKEN_NAME TOKEN_NAME: /ab/ """, start='start', parser='lalr', lexer='contextual') tree = _parser.parse("ab") print(tree) print(tree.pretty())
#! /usr/bin/env python # -*- coding: utf-8 -*- import pushdown _parser = pushdown.Lark(r""" start: a b a: "A" "B" b: "AB" """, start='start', parser='lalr', lexer='contextual') tree = _parser.parse("AB\nBABAB") print(tree.pretty())