|
8 | 8 |
|
9 | 9 | package XML::Axk::Core;
|
10 | 10 | use XML::Axk::Base qw(:all);
|
| 11 | +use XML::Axk::Preparse; |
| 12 | +use Data::Dumper; |
11 | 13 |
|
12 | 14 | =encoding UTF-8
|
13 | 15 |
|
@@ -82,11 +84,7 @@ sub load_script_file {
|
82 | 84 |
|
83 | 85 | my $fn = shift;
|
84 | 86 | open(my $fh, '<', $fn) or croak("Cannot open $fn");
|
85 |
| - my $contents; |
86 |
| - { |
87 |
| - local $/; |
88 |
| - $contents = <$fh>; |
89 |
| - } |
| 87 | + my $contents = do { local $/; <$fh> }; |
90 | 88 | close $fh;
|
91 | 89 |
|
92 | 90 | $self->load_script_text($contents, $fn, false);
|
@@ -116,104 +114,23 @@ sub load_script_text {
|
116 | 114 | # Text to wrap around the script
|
117 | 115 | my ($leader, $trailer) = ('', '');
|
118 | 116 |
|
119 |
| - # Prep the filename |
120 |
| - #$fn =~ s{\\}{\\\\}g; # This doesn't seem to be necessary based on |
121 |
| - # the regex given for #line in perlsyn. |
122 |
| - $fn =~ s{"}{-}g; |
123 |
| - # as far as I can tell, #line can't handle embedded quotes. |
124 |
| - |
125 |
| - my $has_lang; |
126 |
| - my $curr_trailer; |
127 |
| - my $lines_added = 0; |
128 |
| - |
129 |
| - # TODO replace this with calls to Preparse routines |
130 |
| - |
131 |
| - # Split the file into individual Ln blocks |
132 |
| - while( $text =~ m/$RE_Pragma/g ) { |
133 |
| - my @idxes=($-[0], $+[0]); |
134 |
| - my $lang = $1; |
135 |
| - my $oldpos = pos($text); |
136 |
| - my $length_delta = 0; # how much to adjust pos($text) by |
137 |
| - |
138 |
| - # Get line number in the present, possibly modified, text |
139 |
| - my $curr_lineno = 1 + ( () = substr($text, 0, $idxes[0]) =~ /\n/g ); |
140 |
| - # Thanks to ikegami, http://www.perlmonks.org/?node_id=968352 |
141 |
| - $curr_lineno -= $lines_added; |
142 |
| - |
143 |
| - # Ln must be followed by whitespace and a newline. |
144 |
| - # This is to keep the line numbering vaguely consistent. |
145 |
| - # However, the very last line does not have to end with a newline. |
146 |
| - # That makes it easier to use command-line scripts. |
147 |
| - my $didremove = (substr($text, $idxes[1]) =~ s/\A(\h*(?:\n|\Z))//); |
148 |
| - my $removed = $1; |
149 |
| - |
150 |
| - unless($didremove) { |
151 |
| - # Tell the caller where in the source file the problem is |
152 |
| - eval "#line $curr_lineno \"$fn\"\n" . |
153 |
| - "die(\"L$lang indicator must be on its own line\");"; |
154 |
| - die $@; |
155 |
| - } |
156 |
| - |
157 |
| - $length_delta -= length($removed); |
158 |
| - |
159 |
| - my $replacement = ''; |
160 |
| - $has_lang = true; |
161 |
| - |
162 |
| - # End an existing capture if we're switching languages |
163 |
| - if($curr_trailer) { |
164 |
| - $replacement .= "$curr_trailer\n" . |
165 |
| - "#line $curr_lineno \"$fn\"\n"; |
166 |
| - $curr_trailer=''; |
167 |
| - $lines_added += 2; |
168 |
| - } |
169 |
| - |
170 |
| - # Does this language parse the source text itself? |
171 |
| - my $want_text; |
172 |
| - eval "require XML::Axk::L::L$lang"; |
173 |
| - die "Can't find language $lang: $@" if $@; |
174 |
| - do { |
175 |
| - no strict 'refs'; |
176 |
| - $want_text = ${"XML::Axk::L::L${lang}::C_WANT_TEXT"}; |
177 |
| - }; |
178 |
| - |
179 |
| - unless($want_text) { # Easy case: the script's code is still Perl |
180 |
| - $replacement .= "use XML::Axk::L::L$lang;\n"; |
181 |
| - |
182 |
| - } else { # Harder case: give the Ln the source text |
183 |
| - $curr_trailer = |
184 |
| - "AXK_EMBEDDED_SOURCE_DO_NOT_TYPE_THIS_YOURSELF_OR_ELSE"; |
185 |
| - |
186 |
| - my $following_lineno = $curr_lineno+1; |
187 |
| - # Number of first line of the text in that language |
188 |
| - $replacement .= |
189 |
| - "use XML::Axk::L::L$lang \"$fn\", $following_lineno, " . |
190 |
| - "<<'$curr_trailer';\n"; |
191 |
| - } |
192 |
| - |
193 |
| - if($replacement) { |
194 |
| - substr($text, $idxes[0], $idxes[1] - $idxes[0]) = $replacement; |
195 |
| - $length_delta += (length($replacement) - ($idxes[1] - $idxes[0])); |
196 |
| - } |
197 |
| - |
198 |
| - if($length_delta) { |
199 |
| - #say "pos = $oldpos; Delta pos = $length_delta"; |
200 |
| - pos($text) = $oldpos + $length_delta; |
201 |
| - } |
202 |
| - |
203 |
| - %pragmas = (); # reset for next time through the loop |
204 |
| - } #foreach Ln block |
205 |
| - |
206 |
| - $text .= "\n" unless substr($text, length($text)-1) eq "\n"; |
207 |
| - $text .= "$curr_trailer\n" if $curr_trailer; |
| 117 | + my ($lrPieces, $has_lang) = XML::Axk::Preparse::pieces(\$text, |
| 118 | + $add_Ln ? { L => {} } : undef); |
208 | 119 |
|
| 120 | + #say "Has lang" if $has_lang; |
209 | 121 | unless($has_lang) {
|
210 | 122 | if($add_Ln) {
|
211 |
| - $leader = "use XML::Axk::L1;\n"; # To be updated over time |
| 123 | + $lrPieces->[0]->{pragmas}->{L}->{digits} = 1; # default language |
212 | 124 | } else {
|
213 |
| - croak "No language (Ln) specified in file $fn"; |
| 125 | + die "No language (Ln) specified in file $fn"; |
214 | 126 | }
|
215 | 127 | }
|
216 | 128 |
|
| 129 | + #say Dumper($lrPieces); |
| 130 | + my $srNewText = XML::Axk::Preparse::assemble($fn, $lrPieces); |
| 131 | + $text = $$srNewText; |
| 132 | + |
| 133 | + $text .= "\n" unless substr($text, length($text)-1) eq "\n"; |
217 | 134 |
|
218 | 135 | # Mark the filename for the sake of error messages.
|
219 | 136 | $leader .= ";\n#line 1 \"$fn\"\n";
|
|
0 commit comments