MpjvdZddlZddlmZmZddlmZddlmZm Z m Z ddl m Z ddl mZdd lmZdd lmZmZmZd ZGd d ZdZdeedeefdZdZdZdZdZGddZGddZy)z"Tree matcher based on Lark grammarN)ListDict) defaultdict)TreeTokenLark) ParserConf)ConfigurationError)earley)RuleTerminal NonTerminalc6|jxr |jSN)is_term filter_out)ts _/mnt/ssd/data/Dropbox/adrian/scripts/msg_venv/lib/python3.12/site-packages/lark/tree_matcher.pyis_discarded_terminalrs 99 %%ceZdZdZdZy)_MakeTreeMatchc ||_||_yr)name expansion)selfrrs r__init__z_MakeTreeMatch.__init__s "rct|j|}d|j_|j|j_|S)NT)rrmeta match_treerorig_expansion)rargsrs r__call__z_MakeTreeMatch.__call__s3 D !  $rN)__name__ __module__ __qualname__rr$rrrrs #rrci}|D]2}||}||vr||}|||}||kDs(|||<.|||<4t|jSr)listvalues)seq group_keycmp_keyditemkeyv1v2s r_best_from_groupr4sh Ao !8B3BBw#AcF  rrulesreturncJt|dd}|jd|S)Nc|Srr(rs rz(_best_rules_from_group..-sarc.t|j Srlenrr9s rr;z(_best_rules_from_group..-sC ..sS-r)r1)r4sort)r5s r_best_rules_from_grouprA,s% UK1L ME JJ-J. Lrct|tr't|j\}}|j|k(St|t r|t |jk(SJ||fr) isinstancerparse_rulenamerdatarrtype)termtokenr_argss r_matchrJ2sY%$TYY/ ezzT!! E5 !x +++4-5rcFt||t|j|S)N)alias)r rr)originr old_expansions rmake_recons_rulerO;s   ])S TTrcFt|t|jg|gSr)rOrr)rMrGs rmake_recons_rule_to_termrQ?s FXdii%8$9D6 BBrctjd|j\}}|xr-|jdDcgc]}|j c}}||fScc}w)zJParse rule names that may contain a template syntax (like rule{a, b, ...})z(\w+)(?:{(.+)})?,)rematchgroupssplitstrip)srargs_strar#s rrDrDCsTXX115<<>ND(  @HNN3,?@q@D :AsAceZdZdZdZy) ChildrenLexerc||_yrchildren)rr`s rrzChildrenLexer.__init__Ls   rc|jSrr_)r parser_states rlexzChildrenLexer.lexOs }}rN)r%r&r'rrcr(rrr]r]Ks !rr]cxeZdZUdZeeeefed<eeed<e ed<de fdZ deefdZ de ded e fd Z y ) TreeMatcherzMatch the elements of a tree node, based on an ontology provided by a Lark grammar. Supports templates and inlined rules (`rule{a, b,..}` and `_rule`) Initialize with an instance of Lark. rules_for_rootr5parserc|jjrJ|jjr|jjjrut |ds't |jj rdd|jj|jjt\|_ }}n/t|j|_ t|j}tt|_t|j#||_|jj%t'|j|_||_i|_y)NgrammarzGSource grammar not available from cached parser, use cache_grammar=TruezSource grammar not available!)optionsmaybe_placeholderspostlex always_accepthasattrr cachericompilestartsettokensr* terminalsr5rrf_build_recons_rulesreverserArg _parser_cache)rrgr5_extras rrzTreeMatcher.__init__^s>>4444 >> ! !fnn&<&<&J&J69-(,2NN,@,@*sggFegg)/)?)?@T@TVYV[)\ &DKv//0DK&E)$/$2259:  ,DJJ7  79rc#,K|Dchc]%}|jjs|j'}}tt}|D]7}|j s||jj |j 9|Dchc]}|j}}|Dchc]'}|jjds||vs||vr|)}}t}|D]b}|jDcgc](}t|s||vr|nt|j*} }| |jgk(r |j ^|j rt|j n |j}t|| |j} ||vrpt| dk7rb|j |jj | |j|vst#|||j%|j|jjds||vr| ;|j |jj | e|j'D]1\} } | D]} t#| t| t#| | 3ycc}wcc}wcc}wcc}ww)z>Convert tree-parsing/construction rules to tree-matching rules_Nr)rjexpand1rMrr*rLappendr startswithrrrrrrrOr>rfrQadditems)rr5r:expand1saliases rule_namessym nonterminalsseen recons_exprulerM rule_aliasesrLs rruzTreeMatcher._build_recons_ruleswsQ&+Aqyy/@/@AHHAAd# 2Aww!((1 2)..1ahh. .'1Z88..s3sh#QX.Z Zu ?A%&[[T!8Mc8R"% !4#(388:LLTJTahhZ'AGGO*+''+agg&qxxC#CQ[[ADh3z?a#7##CHH-44T:884'23<<HHSXX&88&&s+shJ''188>+ ?.%,MMO ; FL% K.v{57IJJ K*66: : ;GB/Z TsFJJJ"J.JJJ,J  J'-JB+JCJtreerulenamer6c0|rt|\}}|j|k(sJ|j} |j|}|jt|j |} | j|k(sJ| S#t$r|jt |j |z}|Dcic]}||jncc}w}}t|||g} tj|jj| td}||j|<YwxYw)aMatch the elements of `tree` to the symbols of rule `rulename`. Parameters: tree (Tree): the tree node to match rulename (str): The expected full rule name (including template args) Returns: Tree: an unreduced tree that matches `rulename` Raises: UnexpectedToken: If no match was found. Note: It's the callers' responsibility to match the tree recursively. T)resolve_ambiguity)rDrErwKeyErrorr5rArfrLr r Parserrg lexer_confrJparser]r`) rrrrrIrgr5r callbacksconfunreduced_trees rr!zTreeMatcher.match_trees  (2KD%99$ $$yyH 2''1F &||M$--,H(S""h... 2JJ!78K8KH8U!VVE7<rs( #*00&   $t*d  UCuur