Skip to content

Commit 05a24c5

Browse files
author
Chris White
committed
Revamp parsing - work in progress
- Languages are now under XML::Axk::L to make a home for plugins - New XML::Axk::Preparse makes the Perl source to be evaluated (not counting headers or trailers)
1 parent 3b74de1 commit 05a24c5

File tree

9 files changed

+243
-87
lines changed

9 files changed

+243
-87
lines changed

L1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# NAME
22

3-
XML::Axk::Core::L1 - ack-like XML processor, language 1
3+
XML::Axk::Core::L1 - awk-like XML processor, language 1
44

55
# EXAMPLE
66

Makefile.PL

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ WriteMakefile(
3434
NAME => 'XML::Axk',
3535
AUTHOR => q{Christopher White <[email protected]>},
3636
VERSION_FROM => 'lib/XML/Axk/App.pm',
37-
ABSTRACT => 'ack-like XML processor',
37+
ABSTRACT => 'awk-like XML processor',
3838
LICENSE => 'artistic_2',
3939
EXE_FILES => [ 'bin/axk' ],
4040
MIN_PERL_VERSION => '5.018',

lib/XML/Axk/App.pm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ __END__
188188
189189
=head1 NAME
190190
191-
XML::Axk::App - ack-like XML processor, command-line interface
191+
XML::Axk::App - awk-like XML processor, command-line interface
192192
193193
=head1 USAGE
194194
@@ -208,6 +208,11 @@ on the command line doesn't specify a language version.
208208
209209
=over
210210
211+
=item -B, --backend B<bkend>
212+
213+
B<Not yet implemented:>
214+
Take input using the backend B<bkend>.
215+
211216
=item -e, --source B<text>
212217
213218
Run the axk code given as B<text>.

lib/XML/Axk/Core.pm

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,47 @@
99
package XML::Axk::Core;
1010
use XML::Axk::Base qw(:all);
1111

12+
=encoding UTF-8
13+
14+
=head1 NAME
15+
16+
XML::Axk::Core - awk-like XML processor, core
17+
18+
=head1 USAGE
19+
20+
my $core = XML::Axk::Core->new(\%opts);
21+
$core->load_script_file($filename);
22+
$core->load_script_text($source_text, $filename);
23+
$core->run(@input_filenames);
24+
25+
=head1 OPTIONS
26+
27+
A filename of C<-> represents standard input.
28+
29+
=head1 OVERVIEW
30+
31+
axk coordinates languages and backends to process XML files. Backends read
32+
XML input and provide it to an axk script. Languages are the way that
33+
script is expressed. Languages and backends can be mixed arbitrarily,
34+
as long as the backend provides the API the language needs.
35+
36+
A single axk script can include code in any number of languages, but can
37+
only use one backend. The first backend referenced (implicitly or
38+
explicitly) is the one used.
39+
40+
Each language has a default backend, but the user can specify a different
41+
backend using a pragma.
42+
43+
=head1 SUBROUTINES
44+
45+
=head2 XML::Axk::Core->new
46+
47+
Constructor. Takes a hash ref of options
48+
49+
=head1 METHODS
50+
51+
=cut
52+
1253
# Wrapper around string eval, way up here so it can't see any of the
1354
# lexicals below.
1455
sub eval_nolex {
@@ -27,7 +68,12 @@ my $scriptnumber = 0;
2768
# }}}1
2869
# Loading =============================================================== {{{1
2970

30-
# Load the named script file from disk, but do not execute it.
71+
=head2 load_script_file
72+
73+
Load the named script file from disk, but do not execute it.
74+
75+
=cut
76+
3177
# TODO permit specifying an Ln?
3278
# @param $self
3379
# @param $fn {String} Filename to load
@@ -49,7 +95,12 @@ sub load_script_file {
4995

5096
} #load_script_file
5197

52-
# Load the given text, but do not execute it.
98+
=head2 load_script_text
99+
100+
Load the given text, but do not execute it.
101+
102+
=cut
103+
53104
# TODO permit specifying a specific Ln?
54105
# @param $self
55106
# @param $text {String} The source text, **which load_script_text may modify.**
@@ -71,53 +122,14 @@ sub load_script_text {
71122
$fn =~ s{"}{-}g;
72123
# as far as I can tell, #line can't handle embedded quotes.
73124

74-
=pod
75-
76-
=head1 SPECIFYING THE AXK LANGUAGE VERSION
77-
78-
An axk script can include a C<Ln> pragma that specifies the axk
79-
language version in use. For example, C<L1> (or, C<L 1>, C<L01>,
80-
C<L001>, ...) calls for language version 1 (currently defined in
81-
C<XML::Axk::L1>). The C<Ln> must be the first non-whitespace item
82-
on a line.
83-
84-
An axk script on disk without a Ln pragma is an error. This means
85-
that the language version must be specified in the C<Ln> form, not as
86-
a direct C<use ...::Ln;> statement. This is so that C<Ln> can expand
87-
to something different depending on the language version, if
88-
necessary. However, you can say `use...Ln` manually _in addition to_
89-
the pragma (e.g., in a different package).
90-
91-
Multiple C<Ln> pragmas are allowed in a file. This is so you can use
92-
different language versions in different packages if you want to.
93-
However, you do so at your own risk!
94-
95-
Command-line scripts without a C<Ln> pragma use the latest version
96-
automatically. That is, the behaviour is like perl's C<-E> rather than
97-
perl's C<-e>. That risks breakage of inline scripts, but makes it easier
98-
to use axk from the command line. If you are using axk in a script,
99-
specify the C<Ln> pragma at the beginning of your script. This is
100-
consistent with the requirement to list the version in your source
101-
files.
102-
103-
=cut
104-
105125
my $has_lang;
106126
my $curr_trailer;
107127
my $lines_added = 0;
108128

109-
# Regex to match an Ln specification
110-
my $RE_Ln = qr{
111-
^\h*L\h* # has to start the line
112-
(?|
113-
(?:0*(\d+)) # digit form
114-
| (?:``(\w\+)) # alpha form, e.g., L``foo. I think ``
115-
) # is fairly unlikely in real text.
116-
\b\h*;? # permit trailers for ergonomics
117-
}mx;
129+
# TODO replace this with calls to Preparse routines
118130

119131
# Split the file into individual Ln blocks
120-
while( $text =~ m/$RE_Ln/g ) {
132+
while( $text =~ m/$RE_Pragma/g ) {
121133
my @idxes=($-[0], $+[0]);
122134
my $lang = $1;
123135
my $oldpos = pos($text);
@@ -157,15 +169,15 @@ files.
157169

158170
# Does this language parse the source text itself?
159171
my $want_text;
160-
eval "require XML::Axk::L$lang";
172+
eval "require XML::Axk::L::L$lang";
161173
die "Can't find language $lang: $@" if $@;
162174
do {
163175
no strict 'refs';
164-
$want_text = ${"XML::Axk::L${lang}::C_WANT_TEXT"};
176+
$want_text = ${"XML::Axk::L::L${lang}::C_WANT_TEXT"};
165177
};
166178

167179
unless($want_text) { # Easy case: the script's code is still Perl
168-
$replacement .= "use XML::Axk::L$lang;\n";
180+
$replacement .= "use XML::Axk::L::L$lang;\n";
169181

170182
} else { # Harder case: give the Ln the source text
171183
$curr_trailer =
@@ -174,7 +186,7 @@ files.
174186
my $following_lineno = $curr_lineno+1;
175187
# Number of first line of the text in that language
176188
$replacement .=
177-
"use XML::Axk::L$lang \"$fn\", $following_lineno, " .
189+
"use XML::Axk::L::L$lang \"$fn\", $following_lineno, " .
178190
"<<'$curr_trailer';\n";
179191
}
180192

@@ -187,6 +199,8 @@ files.
187199
#say "pos = $oldpos; Delta pos = $length_delta";
188200
pos($text) = $oldpos + $length_delta;
189201
}
202+
203+
%pragmas = (); # reset for next time through the loop
190204
} #foreach Ln block
191205

192206
$text .= "\n" unless substr($text, length($text)-1) eq "\n";
@@ -378,7 +392,7 @@ sub new {
378392
post_file => [], # List of \& to run after reading each file
379393
post_all => [], # List of \& to run after reading the last file
380394

381-
# Script parameters, indexed by language name (X::A::Ln).
395+
# Script parameters, indexed by language name (X::A::L::Ln).
382396
# Format: { lang name => { varname with sigil => value, ... }, ... }
383397
sp => {},
384398

@@ -440,35 +454,6 @@ sub global_name {
440454

441455
=pod
442456
443-
=encoding UTF-8
444-
445-
=head1 NAME
446-
447-
XML::Axk::Core - ack-like XML processor, core
448-
449-
=head1 VERSION
450-
451-
Version 0.1.0
452-
453-
=head1 USAGE
454-
455-
my $core = XML::Axk::Core->new(\%opts);
456-
$core->load_script_file($filename);
457-
$core->load_script_text($source_text, $filename);
458-
$core->run(@input_filenames);
459-
460-
=head1 OPTIONS
461-
462-
A filename of C<-> represents standard input.
463-
464-
=head1 SUBROUTINES
465-
466-
=head2 XML::Axk::Core->new
467-
468-
Constructor. Takes a hash ref of options
469-
470-
=head1 METHODS
471-
472457
=head2 load_script_file
473458
474459
=head2 load_script_text

lib/XML/Axk/L0.pm renamed to lib/XML/Axk/L/L0.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env perl
2-
# XML::Axk::L0 - DUMMY axk language, version 0
2+
# XML::Axk::L::L0 - DUMMY axk language, version 0
33
# Copyright (c) 2018 cxw42. All rights reserved. Artistic 2.
44
# This is not a real axk language - it exists for testing.
55

6-
package XML::Axk::L0;
6+
package XML::Axk::L::L0;
77
use XML::Axk::Base;
88

99
# Config

lib/XML/Axk/L1.pm renamed to lib/XML/Axk/L/L1.pm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env perl
2-
# XML::Axk::L1 - axk language, version 1
2+
# XML::Axk::L::L1 - axk language, version 1
33
# Copyright (c) 2018 cxw42. All rights reserved. Artistic 2.
44

55
# TODO:
@@ -9,7 +9,7 @@
99
# - Add a static method to XAC to get the next package name, so that each
1010
# `axk_script_*` is used by only one Core instance.
1111

12-
package XML::Axk::L1;
12+
package XML::Axk::L::L1;
1313
use XML::Axk::Base qw(:default now_names);
1414

1515
use XML::Axk::Matcher::XPath;
@@ -225,7 +225,7 @@ sub import {
225225
226226
=head1 NAME
227227
228-
XML::Axk::Core::L1 - ack-like XML processor, language 1
228+
XML::Axk::Core::L1 - awk-like XML processor, language 1
229229
230230
=head1 EXAMPLE
231231
File renamed without changes.

0 commit comments

Comments
 (0)