11import os
22from syncode .parsers import incremental_parser
3+ from syncode .parsers .itergen_parser import IGParser
34from syncode .parsers .python_parser import PythonIncrementalParser , PythonIndenter
45from syncode .parsers .go_parser import GoIncrementalParser
56import syncode .common as common
67from syncode .larkm .lark import Lark
78from syncode .parsers .grammars .grammar import Grammar
89
9- def create_parser (grammar : Grammar , parser = 'lalr' , ** kwargs ) -> incremental_parser .IncrementalParser :
10+ def create_parser (
11+ grammar : Grammar ,
12+ parser = 'lalr' ,
13+ use_symbol_pos_map = False ,
14+ ** kwargs
15+ ) -> incremental_parser .IncrementalParser :
1016 """
1117 Creates an incremental parser for the given grammar. The parser is cached for future use.
1218 parser (str, optional): The type of parser to use. Can be 'lalr' or 'lr'. Defaults to 'lalr'.
@@ -16,6 +22,10 @@ def create_parser(grammar: Grammar, parser='lalr', **kwargs) -> incremental_pars
1622 cache_filename = parser_cache_dir + f'{ grammar } _{ parser } _{ grammar .hash ()} _parser.pkl'
1723 os .makedirs (os .path .dirname (parser_cache_dir ), exist_ok = True )
1824
25+ # First check if we should use the IGParser with symbol position map
26+ if use_symbol_pos_map :
27+ return IGParser (base_parser , ** kwargs )
28+
1929 if grammar .name == 'python' :
2030 indenter = PythonIndenter ()
2131
@@ -27,6 +37,7 @@ def create_parser(grammar: Grammar, parser='lalr', **kwargs) -> incremental_pars
2737 return GoIncrementalParser (base_parser , ** kwargs )
2838 return incremental_parser .IncrementalParser (base_parser , ** kwargs )
2939
40+
3041def create_base_parser (grammar , parser = 'lalr' , indenter = None , cache_filename = None ):
3142 base_parser = Lark ( # This is the standard Lark parser
3243 grammar .ebnf ,
0 commit comments