XML/Axk/Preparse.pm

filter: Statementstmt 71.6% 48 / 67 Branchbran 45.0% 18 / 40 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 62.8% 76 / 121 CC 23 SCAR 27.0
CC
23
Cov
61%
assemble
20.6
pieces
17.7
preparse
4.6
CRAP 15.0
« XML/Axk/Vars/Scalar.pm ↑ Summary XML/Axk/SAX/BuildDOM2.pm »
1 #!/usr/bin/env perl
2 # Copyright (c) 2018 cxw42. All rights reserved. Artistic 2.
3
4 # Style note: Hungarian prefixes are used on scalars:
5 # "hr" (hash ref), "lr" (list ref), "sr" (string ref), "nr" (numeric ref),
6 # "dr" ("do," i.e., block ref), "ref" (unspecified ref),
7 # "b" or "is" (boolean), "s" (string)
8
9 package XML::Axk::Preparse;
10 3 ▶use XML::Axk::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 =encoding UTF-8
15
16 =head1 NAME
17
18 XML::Axk::Preparse - preparser for axk
19
20 =head1 SPECIFYING THE AXK LANGUAGE
21
22 An axk script can include a C<-Ln> pragma that specifies the axk
23 language in use. For example, C<-L1> (or, C<-L 1>, C<-L01>,
24 C<-L001>, ...) calls for language 1 (defined in
25 C<XML::Axk::L::L1>).
26
27 Similarly, a C<-Bn> pragma specifies the axk backend to use.
28
29 An axk script on disk without a C<-Ln> pragma is an error. This means
30 that the language version must be specified in the C<-Ln> form, not as
31 a direct C<use ...::Ln;> statement. This is so that C<-Ln> can expand
32 to something different depending on the language version, if
33 necessary. However, you can say `use...Ln` manually _in addition to_
34 the pragma (e.g., in a different package).
35
36 Multiple C<-Ln> pragmas are allowed in a file. This is so you can use
37 different language versions in different packages if you want to.
38 However, you do so at your own risk!
39
40 Command-line scripts without a C<-Ln> pragma use the latest version
41 automatically. That is, the behaviour is like perl's C<-E> rather than perl's
42 C<-e>. That risks breakage of inline scripts, but makes it easier to use axk
43 from the command line. If you are using axk in a shell script, specify the
44 C<-Ln> pragma at the beginning of your script or on the axk command line.
45 This is consistent with the requirement to list the version in your source
46 files.
47
48 =head2 Language formats
49
50 Languages can either be:
51
52 =over
53
54 =item C<[0-9]+>
55
56 A numeric language has leading 0s stripped from its name. E.g., C<-L012>
57 tries to use language C<12>.
58
59 Languages 1-9 are reserved for axk's use.
60
61 =item C<[a-zA-Z][a-zA-Z0-9\.]*>
62
63 An alphabetic language name is used as is, except that C<.> characters are
64 converted to C<::> module separators.
65
66 Language names that are all upper case, and that have no C<.> characters,
67 are reserved for axk's use.
68
69 =cut
70
71 =head1 ROUTINES
72
73 =head2 pieces
74
75 Split the given source text into language-specific pieces. Usage:
76
77 my $lrPieces = pieces(\$source_text[, $hrInitialPragmas]);
78 my ($lrPieces, $hasLang) = pieces(\$source_text[, $hrInitialPragmas]);
79
80 In the second form, it also tells you whether any Ln pragma is present
81 in the source text.
82
83 If you specify a C<$hrInitialPragma>, it will govern any lines before the
84 first pragma in the source text.
85
86 =cut
87
88 sub pieces {
89 1 ▶ my @retval;
Subroutine pieces: called CC=12 SCAR=28.7
Pod documented
90
91 1 ▶ my $srText = shift or croak('Need source text');
Branch: 1 branch
BranchTrueFalseTotal
my $srText = shift() or croak("Need source text") 0 1 1
92 1 ▶ croak 'Need a source reference' unless ref $srText eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $srText eq "SCALAR" 0 1 1
93
94 # Initial pragmas, if any
95 1 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
96 0 push @retval, { text => '', start => 1, pragmas => (shift) };
97 }
98
99 1 my $hasLang = false;
100
101 # Regex to match a pragma line. A pragma line can include up to two
102 # -L/-B items, generally one -L and one -B.
103 1 my $RE_Pragma_Item = q{
104 -(?<kind>[BL]|-backend|-language)\h*
105 (?:
106 0*(?<digits>\d+) # digit form
107 | (?<name>[a-zA-Z][a-zA-Z0-9\.]*) # alpha form, e.g., -Lfoo.bar.
108 )
109 \b
110 };
111
112 1 my $hrPragmas;
113 my $RE_Pragma = qr{
114 ^
115 # Leader: on a #! line, or first thing on any line
116 (?#!\H*\h.*?)?
117 (($RE_Pragma_Item)(?{
118 my $kind = $+{kind};
119 $kind = 'B' if $kind eq '-backend';
120 $kind = 'L' if $kind eq '-language';
121 $hrPragmas->{$kind} = { digits => $+{digits}, name => $+{name} };
122 })){1,2}
123 1 }mx;
124
125 # Main loop
126 1 open my $fh, '<', $srText;
127 1 ▶ LINE: while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1 1 2
128
129 1 ▶ MAYBE_PRAGMA: { if(/^(?:#!|-)/) { # fast bail
Branch: 1 branch
BranchTrueFalseTotal
if (/^(?:#!|-)/u) 1 0 1
130 1 $hrPragmas = {};
131 1 ▶ last MAYBE_PRAGMA unless /$RE_Pragma/;
Branch: 1 branch
BranchTrueFalseTotal
unless /$RE_Pragma/u 0 1 1
132
133 #say "Saw pragma";
134 1 ▶ $hrPragmas->{name} =~ s/\./::/g if $hrPragmas->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $hrPragmas->{"name"} 0 1 1
135 1 push @retval, { text => '' , start => $.+1, pragmas => $hrPragmas };
136 1 ▶ $hasLang = true if $hrPragmas->{L};
Branch: 1 branch
BranchTrueFalseTotal
if $hrPragmas->{"L"} 1 0 1
137 1 next LINE;
138 }}
139
140 # Otherwise, normal line.
141 # TODO permit the caller to say what to do with lines before the first pragma
142 0 ▶ unless(/^\h*(#|$)/) { # Ignore blanks and comments before the
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\h*(#|$)/u) 0 0 0
143 # first Ln.
144 0 ▶ die "Source text can't come before a pragma line" unless @retval;
Branch: 1 branch
BranchTrueFalseTotal
unless @retval 0 0 0
145 }
146 0 $retval[-1]->{text} .= $_;
147 #say "Stashed $_";
148 }
149 1 close $fh;
150
151 1 ▶ return \@retval, $hasLang if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1 1
152 1 return \@retval;
153 } #pieces()
154
155 =head2 assemble
156
157 Assemble a script for C<eval> based on the results of a call to pieces().
158 Usage:
159
160 my $srNewText = assemble($filename, $lrPieces);
161
162 =cut
163
164 sub assemble {
165 1 ▶ my ($filename, $lrPieces) = @_ or croak("Need filename, pieces");
Subroutine assemble: called CC=9 SCAR=30.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my($filename, $lrPieces) = or croak("Need filename, pieces") 0 1 1
166 1 ▶ croak "Need pieces as a reference" unless ref $lrPieces eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $lrPieces eq "ARRAY" 0 1 1
167
168 1 $filename =~ s{"}{-}g;
169 # as far as I can tell, #line can't handle embedded quotes.
170
171 1 my $retval = '';
172 1 foreach my $hrPiece (@$lrPieces) {
173
174 1 ▶ die "-B not yet implemented" if $hrPiece->{pragmas}->{B};
Branch: 1 branch
BranchTrueFalseTotal
if $hrPiece->{"pragmas"}{"B"} 0 1 1
175
176 # Which language?
177 my $lang = ($hrPiece->{pragmas}->{L}->{digits} //
178 1 ▶ $hrPiece->{pragmas}->{L}->{name});
Condition33%1/3
$hrPiece->{"pragmas"}{"L"}{"digits"} // $hrPiece->{"pragmas"}{"L"}{"name"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$hrPiece->{"pragmas"}{"L"}{"digits"} // $hrPiece->{"pragmas"}{"L"}{"name"}
$hrPiece->{"pragmas"}{"L"}{"digits"} $hrPiece->{"pragmas"}{"L"}{"name"}
Truth table33%1/3
A // B
A= $hrPiece->{"pragmas"}{"L"}{"digits"}B= $hrPiece->{"pragmas"}{"L"}{"name"}
ABresult
1X1
011
000
179 1 ▶ unless(defined $lang) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lang) 0 1 1
180 0 $retval .= $hrPiece->{text};
181 0 next;
182 }
183 1 my $lang_module = "XML::Axk::L::L$lang";
184
185 # Does this language parse the source text itself?
186 1 my $want_text;
187 1 eval "require $lang_module";
188 1 ▶ die "Can't find language $lang: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
189 0 do {
190 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
191 0 $want_text = ${"${lang_module}::C_WANT_TEXT"};
192 };
193
194 0 ▶ unless($want_text) { # Easy case: the script's code is still Perl
Branch: 1 branch
BranchTrueFalseTotal
if ($want_text) { } 0 0 0
195 0 $retval .= "use $lang_module;\n";
196 0 $retval .= "#line $hrPiece->{start} \"$filename\"\n";
197 0 $retval .= $hrPiece->{text};
198
199 } else { # Harder case: give the Ln the source text
200 0 my $trailer =
201 "AXK_EMBEDDED_SOURCE_DO_NOT_TYPE_THIS_YOURSELF_OR_ELSE";
202
203 0 $retval .=
204 "use $lang_module \"$filename\", $hrPiece->{start}, " .
205 "<<'$trailer';\n";
206 0 $retval .= $hrPiece->{text};
207 0 $retval .= "\n$trailer\n";
208 # Don't need a #line because the next language will take care of it
209 }
210 }
211
212 0 return \$retval;
213 } #assemble()
214
215 =head2 preparse
216
217 Invokes pieces() and assemble(). Usage:
218
219 my $srTextOut = preparse($filename, $textIn);
220
221 C<textIn> can be a string or a string ref.
222
223 =cut
224
225 sub preparse {
226 1 ▶ my $filename = $_[0] or croak('Need filename');
Subroutine preparse: called CC=4 SCAR=15.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $filename = $_[0] or croak("Need filename") 0 1 1
227 1 ▶ my $srTextIn = $_[1] or croak('Need text');
Branch: 1 branch
BranchTrueFalseTotal
my $srTextIn = $_[1] or croak("Need text") 0 1 1
228 1 ▶ $srTextIn = \$_[1] unless ref $srTextIn eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $srTextIn eq "SCALAR" 1 0 1
229
230 1 my $lrPieces = pieces($srTextIn);
231 1 my $srTextOut = assemble($filename, $lrPieces);
232 0 return $srTextOut;
233 } #preparse()
234
235 3 1;
236 # vi: set ts=4 sts=4 sw=4 et ai fo-=ro foldmethod=marker: #
« XML/Axk/Vars/Scalar.pm ↑ Summary XML/Axk/SAX/BuildDOM2.pm »