@@ -117,6 +117,16 @@ def keyword_helper(self, keyword: str) -> FunctionCall:
117
117
comment = f"token='{ keyword } '" ,
118
118
)
119
119
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
+
120
130
def visit_NameLeaf (self , node : NameLeaf ) -> FunctionCall :
121
131
name = node .value
122
132
if name in self .non_exact_tokens :
@@ -154,7 +164,10 @@ def visit_NameLeaf(self, node: NameLeaf) -> FunctionCall:
154
164
def visit_StringLeaf (self , node : StringLeaf ) -> FunctionCall :
155
165
val = ast .literal_eval (node .value )
156
166
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 )
158
171
else :
159
172
assert val in self .exact_tokens , f"{ node .value } is not a known literal"
160
173
type = self .exact_tokens [val ]
@@ -656,8 +669,9 @@ def handle_alt_normal(self, node: Alt, is_gather: bool, rulename: Optional[str])
656
669
self .print ("{" )
657
670
# We have parsed successfully all the conditions for the option.
658
671
with self .indent ():
672
+ node_str = str (node ).replace ('"' , '\\ "' )
659
673
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 } "));'
661
675
)
662
676
# Prepare to emmit the rule action and do so
663
677
if node .action and "EXTRA" in node .action :
@@ -710,8 +724,9 @@ def visit_Alt(
710
724
self .print (f"{{ // { node } " )
711
725
with self .indent ():
712
726
self ._check_for_errors ()
727
+ node_str = str (node ).replace ('"' , '\\ "' )
713
728
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 } "));'
715
730
)
716
731
# Prepare variable declarations for the alternative
717
732
vars = self .collect_vars (node )
@@ -733,9 +748,10 @@ def visit_Alt(
733
748
self .handle_alt_normal (node , is_gather , rulename )
734
749
735
750
self .print ("p->mark = _mark;" )
751
+ node_str = str (node ).replace ('"' , '\\ "' )
736
752
self .print (
737
753
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 } "));'
739
755
)
740
756
if "_cut_var" in vars :
741
757
self .print ("if (_cut_var) {" )
0 commit comments