9
9
package XML::Axk::Core ;
10
10
use XML::Axk::Base qw( :all) ;
11
11
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
+
12
53
# Wrapper around string eval, way up here so it can't see any of the
13
54
# lexicals below.
14
55
sub eval_nolex {
@@ -27,7 +68,12 @@ my $scriptnumber = 0;
27
68
# }}}1
28
69
# Loading =============================================================== {{{1
29
70
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
+
31
77
# TODO permit specifying an Ln?
32
78
# @param $self
33
79
# @param $fn {String} Filename to load
@@ -49,7 +95,12 @@ sub load_script_file {
49
95
50
96
} # load_script_file
51
97
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
+
53
104
# TODO permit specifying a specific Ln?
54
105
# @param $self
55
106
# @param $text {String} The source text, **which load_script_text may modify.**
@@ -71,53 +122,14 @@ sub load_script_text {
71
122
$fn =~ s { "} { -} g ;
72
123
# as far as I can tell, #line can't handle embedded quotes.
73
124
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
-
105
125
my $has_lang ;
106
126
my $curr_trailer ;
107
127
my $lines_added = 0;
108
128
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
118
130
119
131
# Split the file into individual Ln blocks
120
- while ( $text =~ m /$RE_Ln / g ) {
132
+ while ( $text =~ m /$RE_Pragma / g ) {
121
133
my @idxes =($- [0], $+ [0]);
122
134
my $lang = $1 ;
123
135
my $oldpos = pos ($text );
@@ -157,15 +169,15 @@ files.
157
169
158
170
# Does this language parse the source text itself?
159
171
my $want_text ;
160
- eval " require XML::Axk::L$lang " ;
172
+ eval " require XML::Axk::L::L $lang " ;
161
173
die " Can't find language $lang : $@ " if $@ ;
162
174
do {
163
175
no strict ' refs' ;
164
- $want_text = ${" XML::Axk::L${lang} ::C_WANT_TEXT" };
176
+ $want_text = ${" XML::Axk::L::L ${lang} ::C_WANT_TEXT" };
165
177
};
166
178
167
179
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 " ;
169
181
170
182
} else { # Harder case: give the Ln the source text
171
183
$curr_trailer =
@@ -174,7 +186,7 @@ files.
174
186
my $following_lineno = $curr_lineno +1;
175
187
# Number of first line of the text in that language
176
188
$replacement .=
177
- " use XML::Axk::L$lang \" $fn \" , $following_lineno , " .
189
+ " use XML::Axk::L::L $lang \" $fn \" , $following_lineno , " .
178
190
" <<'$curr_trailer ';\n " ;
179
191
}
180
192
@@ -187,6 +199,8 @@ files.
187
199
# say "pos = $oldpos; Delta pos = $length_delta";
188
200
pos ($text ) = $oldpos + $length_delta ;
189
201
}
202
+
203
+ %pragmas = (); # reset for next time through the loop
190
204
} # foreach Ln block
191
205
192
206
$text .= " \n " unless substr ($text , length ($text )-1) eq " \n " ;
@@ -378,7 +392,7 @@ sub new {
378
392
post_file => [], # List of \& to run after reading each file
379
393
post_all => [], # List of \& to run after reading the last file
380
394
381
- # Script parameters, indexed by language name (X::A::Ln).
395
+ # Script parameters, indexed by language name (X::A::L:: Ln).
382
396
# Format: { lang name => { varname with sigil => value, ... }, ... }
383
397
sp => {},
384
398
@@ -440,35 +454,6 @@ sub global_name {
440
454
441
455
=pod
442
456
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
-
472
457
=head2 load_script_file
473
458
474
459
=head2 load_script_text
0 commit comments