Skip to content

Commit e001b9d

Browse files
author
Chris White
committed
Support --language and --backend pragmas; permit filename on the command line
- --language/--backend keep pragmas consistent with the command line - Permit filenames or scripts on the command line for flexibility
1 parent 561bda7 commit e001b9d

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

lib/XML/Axk/App.pm

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ my %CMDLINE_OPTS = (
3737
# They are listed in alphabetical order by option name,
3838
# lowercase before upper, although the code does not require that order.
3939

40+
#BACKEND => ['b', '|backend=s'], # TODO
41+
4042
#DUMP_VARS => ['d', '|dump-variables', false],
4143
#DEBUG => ['D','|debug', false],
4244
EVAL => ['e','|source=s@', $dr_save_source],
@@ -144,10 +146,15 @@ sub Main {
144146
return 0;
145147
}
146148

147-
# Treat the first non-option arg as a script if appropriate
149+
# Treat the first non-option arg as a script file if it exists
150+
# and is readable, otherwise as axk source.
148151
unless(@{$opts{SOURCES}}) {
149152
die "No scripts to run" unless @$lrArgs;
150-
push @{$opts{SOURCES}}, [false, shift @$lrArgs];
153+
my $option = shift @$lrArgs;
154+
push @{$opts{SOURCES}}, [
155+
-r $option && (-f $option || -l $option),
156+
$option
157+
];
151158
}
152159

153160
my $core = XML::Axk::Core->new(\%opts);
@@ -192,18 +199,24 @@ XML::Axk::App - awk-like XML processor, command-line interface
192199
193200
=head1 USAGE
194201
195-
axk [options] [--] [script] [input filename(s)]
202+
axk [options] [--] [input filename or script]
196203
197204
=head1 INPUTS
198205
199206
A filename of C<-> represents standard input. To actually process a file
200207
named C<->, you will need to use shell redirection (e.g., C<< axk < - >>).
201208
Standard input is the default if no input filenames are given.
202209
203-
The first non-option argument is a program if no -e or -f are given.
210+
The first non-option argument is used for program text if no -e or -f
211+
are given. If that argument names a readable regular file or symlink,
212+
an axk program will be loaded from that file. Otherwise, that argument
213+
is used as an axk program directly.
214+
204215
The script language version for a -e will default to the latest if the text
205216
on the command line doesn't specify a language version.
206217
218+
# TODO make -L and -B apply to following -e's
219+
207220
=head1 OPTIONS
208221
209222
=over

lib/XML/Axk/Core.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ sub load_script_text {
114114
# Text to wrap around the script
115115
my ($leader, $trailer) = ('', '');
116116

117+
#say "Text is $text";
117118
my ($lrPieces, $has_lang) = XML::Axk::Preparse::pieces(\$text,
118119
$add_Ln ? { L => {} } : undef);
119120

lib/XML/Axk/Preparse.pm

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,22 @@ first pragma in the source text.
6565
=cut
6666

6767
sub pieces {
68+
my @retval;
69+
6870
my $srText = shift or croak('Need source text');
6971
croak 'Need a source reference' unless ref $srText eq 'SCALAR';
7072

71-
my @retval;
73+
# Initial pragmas, if any
74+
if(@_) {
75+
push @retval, { text => '', start => 1, pragmas => (shift) };
76+
}
77+
7278
my $hasLang = false;
7379

7480
# Regex to match a pragma line. A pragma line can include up to two
7581
# -L/-B items, generally one -L and one -B.
7682
my $RE_Pragma_Item = q{
77-
-(?<kind>[BL])\h*
83+
-(?<kind>[BL]|-backend|-language)\h*
7884
(?:
7985
0*(?<digits>\d+) # digit form
8086
| (?<name>[a-zA-Z][a-zA-Z0-9\.]*) # alpha form, e.g., -Lfoo.bar.
@@ -88,28 +94,27 @@ sub pieces {
8894
# Leader: on a #! line, or first thing on any line
8995
(?#!\H*\h.*?)?
9096
(($RE_Pragma_Item)(?{
91-
$hrPragmas->{$+{kind}} = { digits => $+{digits}, name => $+{name} };
97+
my $kind = $+{kind};
98+
$kind = 'B' if $kind eq '-backend';
99+
$kind = 'L' if $kind eq '-language';
100+
$hrPragmas->{$kind} = { digits => $+{digits}, name => $+{name} };
92101
})){1,2}
93102
}mx;
94103

95-
# Initial pragmas, if any
96-
if(@_) {
97-
push @retval, { text => '', start => 1, pragmas => (shift) };
98-
}
99-
100104
# Main loop
101105
open my $fh, '<', $srText;
102-
while(<$fh>) {
106+
LINE: while(<$fh>) {
103107

104-
MAYBE_PRAGMA: if(/^(?:#!|-)/) { # fast bail
108+
MAYBE_PRAGMA: { if(/^(?:#!|-)/) { # fast bail
105109
$hrPragmas = {};
106110
last MAYBE_PRAGMA unless /$RE_Pragma/;
107111

112+
#say "Saw pragma";
108113
$hrPragmas->{name} =~ s/\./::/g if $hrPragmas->{name};
109114
push @retval, { text => '' , start => $.+1, pragmas => $hrPragmas };
110115
$hasLang = true if $hrPragmas->{L};
111-
next;
112-
}
116+
next LINE;
117+
}}
113118

114119
# Otherwise, normal line.
115120
# TODO permit the caller to say what to do with lines before the first pragma
@@ -118,6 +123,7 @@ sub pieces {
118123
die "Source text can't come before a pragma line" unless @retval;
119124
}
120125
$retval[-1]->{text} .= $_;
126+
#say "Stashed $_";
121127
}
122128
close $fh;
123129

t/01-preparse.t

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,36 @@ EOT
2828
like($$srTest, qr/^use XML::Axk::L::L1/);
2929
like($$srTest, qr/^#line.*foo/m);
3030

31+
$srTest = pp('foo', <<'EOT');
32+
#!x -L 1
33+
EOT
34+
like($$srTest, qr/^use XML::Axk::L::L1/);
35+
like($$srTest, qr/^#line.*foo/m);
36+
37+
$srTest = pp('foo', <<'EOT');
38+
#!x --language 1
39+
EOT
40+
like($$srTest, qr/^use XML::Axk::L::L1/);
41+
like($$srTest, qr/^#line.*foo/m);
42+
3143
$srTest = pp('foo', <<'EOT');
3244
-L1
3345
EOT
3446
like($$srTest, qr/^use XML::Axk::L::L1/);
3547
like($$srTest, qr/^#line.*foo/m);
3648

49+
$srTest = pp('foo', <<'EOT');
50+
-L 1
51+
EOT
52+
like($$srTest, qr/^use XML::Axk::L::L1/);
53+
like($$srTest, qr/^#line.*foo/m);
54+
55+
$srTest = pp('foo', <<'EOT');
56+
--language 1
57+
EOT
58+
like($$srTest, qr/^use XML::Axk::L::L1/);
59+
like($$srTest, qr/^#line.*foo/m);
60+
3761

3862
done_testing();
3963

0 commit comments

Comments
 (0)