@@ -117,6 +117,16 @@ def keyword_helper(self, keyword: str) -> FunctionCall:
117117 comment = f"token='{ keyword } '" ,
118118 )
119119
120+ def soft_keyword_helper (self , value : str ) -> FunctionCall :
121+ return FunctionCall (
122+ assigned_variable = "_keyword" ,
123+ function = "_PyPegen_expect_soft_keyword" ,
124+ arguments = ["p" , value ],
125+ return_type = "expr_ty" ,
126+ nodetype = NodeTypes .NAME_TOKEN ,
127+ comment = f"soft_keyword='{ value } '" ,
128+ )
129+
120130 def visit_NameLeaf (self , node : NameLeaf ) -> FunctionCall :
121131 name = node .value
122132 if name in self .non_exact_tokens :
@@ -154,7 +164,10 @@ def visit_NameLeaf(self, node: NameLeaf) -> FunctionCall:
154164 def visit_StringLeaf (self , node : StringLeaf ) -> FunctionCall :
155165 val = ast .literal_eval (node .value )
156166 if re .match (r"[a-zA-Z_]\w*\Z" , val ): # This is a keyword
157- return self .keyword_helper (val )
167+ if node .value .endswith ("'" ):
168+ return self .keyword_helper (val )
169+ else :
170+ return self .soft_keyword_helper (node .value )
158171 else :
159172 assert val in self .exact_tokens , f"{ node .value } is not a known literal"
160173 type = self .exact_tokens [val ]
@@ -656,8 +669,9 @@ def handle_alt_normal(self, node: Alt, is_gather: bool, rulename: Optional[str])
656669 self .print ("{" )
657670 # We have parsed successfully all the conditions for the option.
658671 with self .indent ():
672+ node_str = str (node ).replace ('"' , '\\ "' )
659673 self .print (
660- f'D(fprintf(stderr, "%*c+ { rulename } [%d-%d]: %s succeeded!\\ n", p->level, \' \' , _mark, p->mark, "{ node } "));'
674+ f'D(fprintf(stderr, "%*c+ { rulename } [%d-%d]: %s succeeded!\\ n", p->level, \' \' , _mark, p->mark, "{ node_str } "));'
661675 )
662676 # Prepare to emmit the rule action and do so
663677 if node .action and "EXTRA" in node .action :
@@ -710,8 +724,9 @@ def visit_Alt(
710724 self .print (f"{{ // { node } " )
711725 with self .indent ():
712726 self ._check_for_errors ()
727+ node_str = str (node ).replace ('"' , '\\ "' )
713728 self .print (
714- f'D(fprintf(stderr, "%*c> { rulename } [%d-%d]: %s\\ n", p->level, \' \' , _mark, p->mark, "{ node } "));'
729+ f'D(fprintf(stderr, "%*c> { rulename } [%d-%d]: %s\\ n", p->level, \' \' , _mark, p->mark, "{ node_str } "));'
715730 )
716731 # Prepare variable declarations for the alternative
717732 vars = self .collect_vars (node )
@@ -733,9 +748,10 @@ def visit_Alt(
733748 self .handle_alt_normal (node , is_gather , rulename )
734749
735750 self .print ("p->mark = _mark;" )
751+ node_str = str (node ).replace ('"' , '\\ "' )
736752 self .print (
737753 f"D(fprintf(stderr, \" %*c%s { rulename } [%d-%d]: %s failed!\\ n\" , p->level, ' ',\n "
738- f' p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "{ node } "));'
754+ f' p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "{ node_str } "));'
739755 )
740756 if "_cut_var" in vars :
741757 self .print ("if (_cut_var) {" )
0 commit comments