blib/lib/XML/XSH2/Grammar.pm untested

filter: Statementstmt 0.0% 0 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 20 CC 1 SCAR 6.9
CC
1
Cov
0%
compile
2.0
new
2.0
BEGIN
2.0
CRAP 2.0
1 # This file was automatically generated from src/xsh_grammar.xml on
2 # Mon Aug 17 11:04:58 2026
3  
4  
5 package XML::XSH2::Grammar;
6  
7 use strict;
8 use Parse::RecDescent;
9 use vars qw/$grammar/;
10  
11 $Parse::RecDescent::skip = '(\s|\n|#[^\n]*)*';
12 $grammar=<<'_EO_GRAMMAR_';
13  
14
15 command:
16 /(?=\s*[}{;]|\s*\Z)/ <commit> <reject>
17 | /assign\b|(?:local\b|my\b)?(?=\s*\$[a-zA-Z_][a-zA-Z0-9_]*\s*\s*(?:[\-\+\*\/%x.]|\|\||\&\&)?:?=)/ <commit> variable
18 ( /(?:[\-\+\*\/%x.]|\|\||\&\&)?=/ <commit> loose_exp
19 { ['xpath_assign',$item[3],$item[1]] }
20
21 | /\s*(?:[\-\+\*\/%x.]|\|\||\&\&)?:=/ command
22 { ['command_assign',$item[2],$item[1]] }
23
24 )
25 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,@{$item[4]},$item[1],$item[3]] }
26
27 | /(my)\b/ variable(s)
28 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'store_lex_variables',0,@{$item[2]}] }
29
30 | /(local)\b/ variable(s)
31 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'make_local',@{$item[2]}] }
32
33 | /(do)\b/ <commit> block
34 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'run_commands',$item[3],0] }
35
36 | /(if)\b/ <commit> exp command
37 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'if_statement',[$item[3],[$item[4]]]] }
38
39 | /(unless)\b/ <commit> exp command
40 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'unless_statement',$item[3],[$item[4]]] }
41
42 | /(while)\b/ <commit> exp command
43 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'while_statement',$item[3],[$item[4]]] }
44
45 | /(foreach|for)\b/ <commit> local_var_in(?) exp command
46 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'foreach_statement',$item[4],[$item[5]],@{$item[3]}] }
47
48 | /(stream)\b/ <commit>
49 (( /--input-file|:f/ filename { [ 'string/input-file' => $item[2] ] }
50 | /--input-pipe|:p/ filename { [ 'string/input-pipe' => $item[2] ] }
51 | /--input-string|:s/ exp { [ 'exp/input-string' => $item[2] ] }
52 | /--output-file|:F/ filename { [ 'string/output-file' => $item[2] ] }
53 | /--output-encoding|:E/ enc_string { [ 'string/output-encoding' => $item[2] ] }
54 | /--output-pipe|:P/ filename { [ 'string/output-pipe' => $item[2] ] }
55 | /--output-string|:S/ { [ '/output-string' => 1 ] }
56 )(s?) { [ map { @$_ } @{$item[1]} ] }
57 )
58 stream_select(s)
59 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'stream_process',$item[3],$item[4]] }
60
61 | /(undef|undefine)\b/ <commit> /\$?[a-zA-Z_][a-zA-Z0-9_]*/
62 {
63 [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'undefine',$item[3]];
64 }
65
66 | /(use)\b/ <commit> /XML::XSH2::(?:Inline|Compile)/
67 { 1 }
68
69 | /(test-mode|test_mode)/
70 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,"test-mode"] }
71
72 | /(run-mode|run_mode)/
73 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,"run-mode"] }
74
75 | /(?!(?:iterate|try|def|define)\b)/ /\.|[a-zA-Z_][-a-zA-Z0-9_]*/ exp_or_opt(s?)
76 {
77 bless
78 [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,$item[2],@{$item[3]}],
79 'XML::XSH2::Command'
80 }
81
82  
83 statement:
84 /(?=\s*[}{;])/ <commit> <reject>
85 | /(if)\b/ <commit> exp block elsif_block else_block
86 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'if_statement',[$item[3],$item[4]],@{$item[5]},@{$item[6]}] }
87
88 | /(unless)\b/ <commit> exp block else_block(?)
89 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'unless_statement',$item[3],$item[4],@{$item[5]}] }
90
91 | /(while)\b/ <commit> exp block
92 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'while_statement',$item[3],$item[4]] }
93
94 | /(foreach|for)\b/ <commit> local_var_in(?) exp block
95 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'foreach_statement',@item[4,5],@{$item[3]}] }
96
97 | /(try)\b/ <commit> block 'catch' local_var(?) block
98 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'try_catch',$item[3],$item[6],@{$item[5]}] }
99
100 | /(iterate)\b/ <commit> xpstep block
101 { [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'iterate',$item[4],@{$item[3]}] }
102
103 | /(def|define)\b/ <commit> ID
104 { XML::XSH2::Functions::is_command($item[3])?undef:1 }
105 variable(s?) block
106 {
107 [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'def',$item[3],$item[6],$item[5]]
108 }
109
110  
111 complex_command:
112 /(?=\s*[{}]|\s*\Z)/ <commit> <reject>
113 | /\s*;/ <commit>
114 | /(?=(?:foreach|for|if|unless|iterate|while|try|def|define)\b)/ statement <commit> trail(?)
115 {
116 if (scalar(@{$item[4]})) {
117 if ($item[4][0][0] eq 'pipe') {
118 $return=[$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'pipe_command',[$item[2]],$item[4][0][1]]
119 } else {
120 $return=[$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'string_pipe_command',[$item[2]],$item[4][0][1]]
121 }
122 } else {
123 $return=$item[2]
124 }
125 }
126
127 | command <commit> trail(?) /\s*;|(?=\s*}|\s*\Z)/
128 {
129 if (scalar(@{$item[3]})) {
130 if ($item[3][0][0] eq 'pipe') {
131 $return=[$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'pipe_command',[$item[1]],$item[3][0][1]]
132 } else {
133 $return=[$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'string_pipe_command',[$item[1]],$item[3][0][1]]
134 }
135 } else {
136 $return=$item[1]
137 }
138 }
139
140 | <error:Parse error near: "}.substr($text,0,40).qq{ ...">
141  
142 block:
143 '{' <commit> complex_command(s) '}'
144 { [grep ref,@{$item[3]}] }
145
146  
147 exp_or_opt:
148 /(?=\s*[};]|\s*\Z)/ <commit> <reject>
149 | option
150 | exp
151  
152 option:
153 /:[[:alnum:]]|--[-_[:alnum:]]+/
154  
155 inline_doc_string:
156 /'[a-zA-Z_][a-zA-Z0-9_]*'|"[a-zA-Z_][a-zA-Z0-9_]*"|\([a-zA-Z_][a-zA-Z0-9_]*\)|\{[a-zA-Z_][a-zA-Z0-9_]*\}|[a-zA-Z_][a-zA-Z0-9_]*/
157 { [($item[1]=~/^(['"({])?(.*?)(['")}])?$/)] }
158
159  
160 inline_doc:
161 /<</ inline_doc_string <skip:""> /.*\n/ /(.|\n)*?\n$item[2][1]\s*(\n|$)/
162 {
163 $text=$item[4].$text;
164 local $_=$item[5]; s/\n$item[2][1]\s*$//;
165 my $paren = $item[2][0];
166 $paren = '"' if $paren eq "";
167 if ($paren eq "'") {
168 $paren = '"';
169 s{(\\)(.|\n)|(\$)}{ (defined($3) and $3 eq '$') ? "\\\$"
170 : ((defined($2) and $2 eq "\\")
171 ? "\\\\" : ((defined($2) and $2 eq "'") ? "'" : (
172 (defined($2) and $2 eq '$') ? "\\\\\\$2" :
173 "\\\\$2"))) }eg;
174 } "<<".$paren.$_;
175 }
176
177  
178 enc_string:
179 exp
180  
181 ID:
182 /[a-zA-Z_][a-zA-Z0-9_]*/
183  
184 filename:
185 exp
186  
187 nodename:
188 exp
189  
190 xpath:
191 /(?=['"])/ <commit> xpstring
192 { $item[3] }
193
194 | xpsimple <skip:""> xpcont(?)
195 { $item[1].join("",@{$item[3]}) }
196
197 | <error:expected XPath, but got "}.substr($text,0,40).qq{ ...">
198  
199 xpsimple:
200 /(?:
201 \$\{ (?: \$?[a-zA-Z_][a-zA-Z0-9_]* | \{.*?\} | \(.+?\) ) \} |
202 \$(?!\{) |
203 [^-:\$\[\]{}|"'\ \s();] |
204 -[^-\[\]{}|"'\ \s();]
205 )
206 (?:
207 \$\{ (?: \$?[a-zA-Z_][a-zA-Z0-9_]* | \{.*?\} | \(.+?\) ) \} |
208 \$(?!\{) |
209 [^\[\]\${}|"'\ \s();]
210 )*/x
211 | xpbrackets
212  
213 xpcont:
214 ( xpfilters
215 | xpbrackets
216 ) <skip:""> xpath(?)
217 { $item[1].join("",@{$item[3]}) }
218
219 | xpath
220 { $item[1] }
221
222  
223 xpfilters:
224 /(?=\[)/ xpfilter(s)
225 { join("",@{$item[2]}) }
226
227  
228 xpfilter:
229 '[' xpinter ']'
230 { "[$item[2]]" }
231
232  
233 xpbracket:
234 '(' <skip:""> xpinter ')'
235 { "($item[3])" }
236
237  
238 xpbrackets:
239 /(?=\()/ xpbracket <skip:""> xpfilters(?)
240 { join "",$item[2],@{$item[4]} }
241
242  
243 xpinter:
244 xps <skip:""> xpintercont(?)
245 { join("",$item[1],@{$item[3]}) }
246
247  
248 xpintercont:
249 ( xpfilters
250 | xpbrackets
251 ) <skip:""> xpinter(?)
252 { join("",$item[1],@{$item[3]}) }
253
254  
255 xps:
256 /(?: [^\$\[\]()'"};]+ |
257 \$(?!\{) |
258 \$\{ (?:\$?[a-zA-Z_][a-zA-Z0-9_]* |
259 \{.*?\} |
260 \(.+?\)
261 )
262 \} |
263 '(?:\$\{ (?: \$?[a-zA-Z_][a-zA-Z0-9_]* | \{.*?\} | \(.+?\)) \} |
264 \$(?!\{) | [^\$']
265 )*' |
266 "(?:\$\{ (?: \$?[a-zA-Z_][a-zA-Z0-9_]* | \{.*?\} | \(.+?\)) \} |
267 \$(?!\{) | [^\$"]
268 )*"
269 )*/x
270  
271 xpstring:
272 /'(?:\$\{(?:\$?[a-zA-Z_][a-zA-Z0-9_]*|\{.*?\}|\(.+?\))\}|\$(?!\{)|[^\$'])*' |
273 "(?:\$\{(?:\$?[a-zA-Z_][a-zA-Z0-9_]*|\{.*?\}|\(.+?\))\}|\$(?!\{)|[^\$"])*"/x
274  
275 perl_expression:
276
277 { $main::myline = $thisline; }
278 <reject>
279 | exp
280 { {local $^W=0; "\n# line $main::myline \"$XML::XSH2::Functions::SCRIPT\"\n".$item[1]} }
281
282  
283 perl_block:
284
285 { $main::myline = $thisline; }
286 <reject>
287 |
288 { $main::myline = $thisline; }
289 <reject>
290 | <perl_codeblock>
291 { {
292 $return=$item[1];
293 {
294 local $^W = 0; # don't warn about undefined contants
295 my $pos="# line $main::myline \"$XML::XSH2::Functions::SCRIPT\"\n";
296 $return=~s/^\{/\{\n$pos/;
297 }
298 } }
299
300  
301 loose_exp:
302 /^(?={)/ perl_block
303 { $item[2] }
304
305 | '&' block
306 { $item[2] }
307
308 | /^(?=<<)/ inline_doc
309 { $item[2] }
310
311 | xpinter
312  
313 exp:
314 /^(?={)/ perl_block
315 { $item[2] }
316
317 | '&' block
318 { $item[2] }
319
320 | /^(?=<<)/ inline_doc
321 { $item[2] }
322
323 | xpath
324  
325 variable:
326 /\$[a-zA-Z_][a-zA-Z0-9_]*/
327  
328 eof:
329 /\Z/
330 { 1; }
331
332  
333 startrule:
334 shell <commit> eof
335 { $item[1] }
336
337 | complex_command(s) <commit> eof
338 { $item[1] }
339
340  
341 trail:
342 /(?=\s*[};]|\s*\Z)/ <commit> <reject>
343 | '|>' <commit> variable
344 { ['var',$item[3]] }
345
346 | '|' <commit> shline
347 { ['pipe',$item[3]] }
348
349  
350 shline_nosc:
351 /([^;()\\"'\|]|\|[^>]|\\.|\"([^\"\\]|\\.)*\"|\'([^\'\\]|\\\'|\\\\|\\[^\'\\])*\')*/
352  
353 shline_inter:
354 /([^()\\"']|\\.|\"([^\"\\]|\\.)*\"|\'([^\'\\]|\\\'|\\\\|\\[^\'\\])*\')*/
355  
356 shline_bracket:
357 '(' shline_inter shline_bracket(?) shline_inter ')'
358 { join("",'(',$item[2],@{$item[3]},$item[4],')') }
359
360  
361 shline:
362 shline_nosc shline_bracket(?) shline_nosc
363 { join("",$item[1],@{$item[2]},$item[3]) }
364
365  
366 shell:
367 /!\s*/ <commit> /.*/
368 { [[$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'sh_noev',$item[3]]] }
369
370 | <error?:Parse error near: "! }.substr($text,0,40).qq{ ..."> <reject>
371  
372 elsif_block:
373 /(elsif)\b/ <commit> exp block elsif_block
374 { [[$item[3],$item[4]],@{$item[5]}] }
375
376 | /(?!elsif)/
377 { [] }
378
379 | <uncommit> <error:Parse error near keyword elsif: "}.substr($text,0,40).qq{ ...">
380  
381 else_block:
382 /(else)\b/ <commit> block
383 { [[undef,$item[3]]] }
384
385 | /(?!else)/
386 { [] }
387
388 | <uncommit> <error:Parse error near keyword else: "}.substr($text,0,40).qq{ ...">
389  
390 local_var:
391 ( /(local|my)\s/
392 )(?) variable
393 { [$item[2],@{$item[1]}] }
394
395  
396 local_var_in:
397 local_var 'in'
398 { $item[1] }
399
400  
401 undef:
402 /(undef|undefine)\b/ <commit> /\$?[a-zA-Z_][a-zA-Z0-9_]*/
403 {
404 [$thisline,$thiscolumn,$thisoffset,$XML::XSH2::Functions::SCRIPT,'undefine',$item[3]];
405 }
406
407  
408 param:
409 /[^=\s]+/ '=' exp
410 { [$item[1],$item[3]] }
411
412  
413 nodetype:
414 /element|attribute|attributes|text|cdata|pi|comment|chunk|entity_reference/
415  
416 loc:
417 /after\s/
418 { "after" }
419
420 | /before\s/
421 { "before" }
422
423 | /(in)?to\s/
424 { "into" }
425
426 | /(append(ing)?|as\s+(a\s+)child(\s+of)?)\s/
427 { "append" }
428
429 | /(prepend(ing)?|(as\s+)(the\s+)first(\s+child(\s+of)?)?)\s/
430 { "prepend" }
431
432 | /(replace|instead( of)?)\s/
433 { "replace" }
434
435  
436 xpaxis:
437 /[-a-z]+::/
438  
439 xpnodetest:
440 /node\(\)|text\(\)|comment\(\)|processing-instruction\(\s*(?:"[^"]*"\s*|'[^'*]'\s*)?\)|[^\(\[\/\"\'\&\;\s]+/
441  
442 xplocationstep:
443 xpaxis(?) xpnodetest
444 { [ (@{$item[1]} ? $item[1][0] : 'child::'),$item[3] ] }
445
446  
447 xpstep:
448 xplocationstep <skip:""> xpfilter(?)
449 { [ @{$item[1]}, @{$item[3]}] }
450
451  
452 stream_select:
453 /select\s/ xpath block
454 { [$item[2],$item[3]] }
455
456  
457  
458  
459 _EO_GRAMMAR_
460  
461 sub compile {
462 Parse::RecDescent->Precompile($grammar,"XML::XSH2::Parser");
463 }
464  
465 sub new {
466 return new Parse::RecDescent ($grammar);
467 }
468  
469 1;
470  
471