Debian Codegraph @

inc/Marpa/R2/Display.pm

raw
1 # Copyright 2022 Jeffrey Kegler
2 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
3 # redistribute it and/or modify it under the terms of the GNU Lesser
4 # General Public License as published by the Free Software Foundation,
5 # either version 3 of the License, or (at your option) any later version.
6 #
7 # Marpa::R2 is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser
13 # General Public License along with Marpa::R2. If not, see
14 # http://www.gnu.org/licenses/.
15
16 package Marpa::R2::Display;
17
18 use 5.010001;
19 use strict;
20 use warnings;
21 use Fatal qw(open close);
22 use YAML::XS;
23 use Data::Dumper; # for debugging
24 use Carp;
25
26 package Marpa::R2::Display::Internal;
27
28 use English qw( -no_match_vars );
29
30 sub Marpa::R2::Display::new {
31 my ($class) = @_;
32 my $self = {};
33 $self->{displays} = {};
34 $self->{ignored_displays} = [];
35 return bless $self, $class;
36 } ## end sub Marpa::R2::Display::new
37
38 @Marpa::R2::Display::Internal::DISPLAY_SPECS = qw(
39 start-after-line end-before-line perltidy normalize-whitespace name
40 remove-display-indent
41 remove-blank-last-line
42 partial flatten inline
43 ignore
44 );
45
46 sub Marpa::R2::Display::read {
47 my ( $self, $data_arg, $file_name ) = @_;
48 my @lines;
49 GET_LINES: {
50 if ( not ref $data_arg ) {
51 $file_name //= $data_arg;
52 open my $fh, q{<}, $data_arg;
53 @lines = <$fh>;
54 close $fh;
55 last GET_LINES;
56 } ## end if ( not ref $data_arg )
57 $file_name //= q{?};
58 @lines = split /\n/xms, ${$data_arg};
59 } ## end GET_LINES:
60
61 chomp @lines;
62 my @verbatim_lines;
63 my $in_pod = 0;
64 my $in_verbatim = 0;
65 my $in_begin;
66 POD_LINE: for my $zero_based_line ( 0 .. $#lines ) {
67 my $line = $lines[$zero_based_line];
68 if ( $in_pod and $line =~ /\A=cut/xms ) {
69 $in_pod = 0;
70 $in_verbatim = 0;
71 $in_begin = undef;
72 next POD_LINE;
73 } ## end if ( $in_pod and $line =~ /\A=cut/xms )
74 if ( not $in_pod and $line =~ /\A=[a-zA-Z]/xms ) {
75 $in_pod = 1;
76 }
77 next POD_LINE if not $in_pod;
78
79 # at this point out $in_pod indicates if we are
80 # in a pod sequence
81 if ( $in_pod and not $in_begin and $line =~ /\A=begin\s+(.*)/xms ) {
82 my $begin_identifier = $1;
83 if ( $begin_identifier !~ /\A:/xms ) {
84 $in_begin = $begin_identifier;
85 }
86 } ## end if ( $in_pod and not $in_begin and $line =~ ...)
87 if ( $in_begin and $line =~ /\A=end\s+(.*)/xms ) {
88 my $begin_identifier = $1;
89 if ( $begin_identifier eq $in_begin ) {
90 $in_begin = undef;
91 }
92 } ## end if ( $in_begin and $line =~ /\A=end\s+(.*)/xms )
93
94 # Don't look for verbatim paragraph inside begin
95 next POD_LINE if $in_begin;
96
97 # Is this the start of a verbatim paragraph?
98 if ( not $in_verbatim and $line =~ /\A[ \t]/xms ) {
99 $in_verbatim = 1;
100 }
101 if ( $in_verbatim and $line =~ /\A[ \t]*\z/xms ) {
102 $in_verbatim = 0;
103 }
104 if ($in_verbatim) {
105 $verbatim_lines[ $zero_based_line + 1 ] = $line;
106 }
107 } ## end for my $zero_based_line ( 0 .. $#lines )
108 LINE: for my $zero_based_line ( 0 .. $#lines ) {
109 my $line = $lines[$zero_based_line];
110
111 my $display_spec;
112 my $display_spec_line_number = $zero_based_line + 1;
113 if ( $line =~ /^[#] \s+ Marpa::R2[:][:]Display/xms ) {
114
115 my $yaml = q{};
116 while ( ( my $yaml_line = $lines[ ++$zero_based_line ] )
117 =~ /^[#]/xms )
118 {
119 $yaml .= "$yaml_line\n";
120 }
121 if ( $yaml =~ / \S /xms ) {
122 $yaml =~ s/^ [#] \s? //xmsg;
123 local $main::EVAL_ERROR = undef;
124 my $eval_ok =
125 eval { $display_spec = YAML::XS::Load($yaml); 1 };
126 if ( not $eval_ok ) {
127 say {*STDERR} $main::EVAL_ERROR
128 or Carp::croak("Cannot print: $ERRNO");
129 say {*STDERR}
130 "Fatal error in YAML Display spec at $file_name, line "
131 . ( $display_spec_line_number + 1 )
132 or Carp::croak("Cannot print: $ERRNO");
133 } ## end if ( not $eval_ok )
134 } ## end if ( $yaml =~ / \S /xms )
135 } ## end if ( $line =~ /^[#] \s+ Marpa::R2[:][:]Display/xms )
136
137 if ( $line =~ /^[=]for \s+ Marpa::R2[:][:]Display/xms ) {
138
139 my $yaml = q{};
140 while (
141 ( my $yaml_line = $lines[ ++$zero_based_line ] ) =~ /\S/xms )
142 {
143 $yaml .= "$yaml_line\n";
144 }
145 if ( $yaml =~ / \S /xms ) {
146 local $main::EVAL_ERROR = undef;
147 my $eval_ok =
148 eval { $display_spec = YAML::XS::Load($yaml); 1 };
149 if ( not $eval_ok ) {
150 say {*STDERR} $main::EVAL_ERROR
151 or Carp::croak("Cannot print: $ERRNO");
152 say {*STDERR}
153 "Fatal error in YAML Display spec at $file_name, line "
154 . ( $display_spec_line_number + 1 )
155 or Carp::croak("Cannot print: $ERRNO");
156 } ## end if ( not $eval_ok )
157 } ## end if ( $yaml =~ / \S /xms )
158 } ## end if ( $line =~ /^[=]for \s+ Marpa::R2[:][:]Display/xms)
159
160 next LINE if not defined $display_spec;
161
162 SPEC: for my $spec ( keys %{$display_spec} ) {
163 next SPEC
164 if $spec ~~ \@Marpa::R2::Display::Internal::DISPLAY_SPECS;
165 say {*STDERR}
166 qq{Warning: Unknown display spec "$spec" in $file_name, line $display_spec_line_number}
167 or Carp::croak("Cannot print: $ERRNO");
168 } ## end for my $spec ( keys %{$display_spec} )
169
170 my $content;
171 my $content_start_line;
172 my $content_end_line;
173 if ( defined( my $end_pattern = $display_spec->{'end-before-line'} ) )
174 {
175 my $end_pat = qr/$end_pattern/xms;
176 if (defined(
177 my $start_pattern = $display_spec->{'start-after-line'}
178 )
179 )
180 {
181 my $start_pat = qr/$start_pattern/xms;
182 PRE_CONTENT_LINE: while (1) {
183 my $pre_content_line = $lines[ ++$zero_based_line ];
184 if ( not defined $pre_content_line ) {
185 say {*STDERR}
186 qq{Warning: Pattern "$start_pattern" never found, },
187 qq{started looking at $file_name, line $display_spec_line_number}
188 or Carp::croak("Cannot print: $ERRNO");
189 return $self;
190 } ## end if ( not defined $pre_content_line )
191 last PRE_CONTENT_LINE
192 if $pre_content_line =~ /$start_pat/xms;
193 } ## end while (1)
194 } ## end if ( defined( my $start_pattern = $display_spec->{...}))
195
196 CONTENT_LINE: while (1) {
197 my $content_line = $lines[ ++$zero_based_line ];
198 if ( not defined $content_line ) {
199 say {*STDERR}
200 qq{Warning: Pattern "$end_pattern" never found, },
201 qq{started looking at $file_name, line $display_spec_line_number}
202 or Carp::croak("Cannot print: $ERRNO");
203 } ## end if ( not defined $content_line )
204 last CONTENT_LINE if $content_line =~ /$end_pat/xms;
205 $content .= "$content_line\n";
206 $content_end_line = $zero_based_line + 1;
207 $content_start_line //= $zero_based_line + 1;
208 } ## end while (1)
209 } ## end if ( defined( my $end_pattern = $display_spec->{...}))
210
211 if ( not defined $content ) {
212 CONTENT_LINE: while (1) {
213 my $content_line = $lines[ ++$zero_based_line ];
214 if ( not defined $content_line ) {
215 say {*STDERR}
216 q{Warning: Pattern "Marpa::R2::Display::End" never found,}
217 . qq{started looking at $file_name, line $display_spec_line_number}
218 or Carp::croak("Cannot print: $ERRNO");
219 return $self;
220 } ## end if ( not defined $content_line )
221 last CONTENT_LINE
222 if $content_line
223 =~ /^[=]for \s+ Marpa::R2[:][:]Display[:][:]End\b/xms;
224 last CONTENT_LINE
225 if $content_line
226 =~ /^[#] \s* Marpa::R2[:][:]Display[:][:]End\b/xms;
227 $content .= "$content_line\n";
228 $content_end_line = $zero_based_line + 1;
229 $content_start_line //= $zero_based_line + 1;
230 } ## end while (1)
231 } ## end if ( not defined $content )
232
233 $content //= '!?! No Content Found !?!';
234
235 my $display_spec_name = $display_spec->{name};
236 my $ignore = $display_spec->{ignore};
237 if ( not $display_spec_name and not $ignore ) {
238 say {*STDERR} q{Warning: Unnamed display }
239 . qq{at $file_name, line $display_spec_line_number}
240 or Carp::croak("Cannot print: $ERRNO");
241 next LINE;
242 } ## end if ( not $display_spec_name and not $ignore )
243
244 $display_spec->{filename} = $file_name;
245 $display_spec->{display_spec_line} = $display_spec_line_number;
246 $display_spec->{content} = $content;
247 $display_spec->{content_start_line} = $content_start_line;
248 $display_spec->{content_end_line} = $content_end_line;
249 $display_spec->{line} = $content_start_line
250 // $display_spec_line_number;
251
252 $verbatim_lines[$_] = undef
253 for $display_spec->{line} .. $display_spec->{content_end_line};
254
255 if ( not $ignore ) {
256 push @{ $self->{displays}->{$display_spec_name} }, $display_spec;
257 next LINE;
258 }
259
260 push @{ $self->{ignored_displays} }, $display_spec;
261
262 } ## end for my $zero_based_line ( 0 .. $#lines )
263
264 $self->{verbatim_lines}->{$file_name} = \@verbatim_lines;
265
266 return $self;
267
268 } ## end sub Marpa::R2::Display::read
269
270 1;