Debian Codegraph @

lib/Marpa/R2.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;
17
18 use 5.010001;
19 use strict;
20 use warnings;
21
22 use vars qw($VERSION $STRING_VERSION @ISA $DEBUG);
23 $VERSION = '14.000000';
24 $STRING_VERSION = $VERSION;
25 ## no critic (BuiltinFunctions::ProhibitStringyEval)
26 $VERSION = eval $VERSION;
27 ## use critic
28 $DEBUG = 0;
29
30 use Carp;
31 use English qw( -no_match_vars );
32 use XSLoader;
33
34 use Marpa::R2::Version;
35
36 $Marpa::R2::USING_XS = 1;
37 $Marpa::R2::USING_PP = 0;
38 $Marpa::R2::LIBMARPA_FILE = '[built-in]';
39
40 LOAD_EXPLICIT_LIBRARY: {
41 last LOAD_EXPLICIT_LIBRARY if not $ENV{'MARPA_AUTHOR_TEST'};
42 my $file = $ENV{MARPA_LIBRARY};
43 last LOAD_EXPLICIT_LIBRARY if not $file;
44
45 require DynaLoader;
46 package DynaLoader;
47 my $bs = $file;
48 $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
49
50 if (-s $bs) { # only read file if it's not empty
51 # print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug;
52 eval { do $bs; };
53 warn "$bs: $@\n" if $@;
54 }
55
56 my $bootname = "marpa_g_new";
57 @DynaLoader::dl_require_symbols = ($bootname);
58
59 my $libref = dl_load_file($file, 0) or do {
60 require Carp;
61 Carp::croak("Can't load libmarpa library: '$file'" . dl_error());
62 };
63 push(@DynaLoader::dl_librefs,$libref); # record loaded object
64
65 my @unresolved = dl_undef_symbols();
66 if (@unresolved) {
67 require Carp;
68 Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
69 }
70
71 dl_find_symbol($libref, $bootname) or do {
72 require Carp;
73 Carp::croak("Can't find '$bootname' symbol in $file\n");
74 };
75
76 push(@DynaLoader::dl_shared_objects, $file); # record files loaded
77 $Marpa::R2::LIBMARPA_FILE = $file;
78 }
79
80 XSLoader::load( 'Marpa::R2', $Marpa::R2::STRING_VERSION );
81
82 if ( not $ENV{'MARPA_AUTHOR_TEST'} ) {
83 $Marpa::R2::DEBUG = 0;
84 }
85 else {
86 Marpa::R2::Thin::debug_level_set(1);
87 $Marpa::R2::DEBUG = 1;
88 }
89
90 sub version_ok {
91 my ($sub_module_version) = @_;
92 return 'not defined' if not defined $sub_module_version;
93 return "$sub_module_version does not match Marpa::R2::VERSION " . $VERSION
94 if $sub_module_version != $VERSION;
95 return;
96 } ## end sub version_ok
97
98 # Set up the error values
99 my @error_names = Marpa::R2::Thin::error_names();
100 for ( my $error = 0; $error <= $#error_names; ) {
101 my $current_error = $error;
102 (my $name = $error_names[$error] ) =~ s/\A MARPA_ERR_//xms;
103 no strict 'refs';
104 *{ "Marpa::R2::Error::$name" } = \$current_error;
105 # This shuts up the "used only once" warning
106 my $dummy = eval q{$} . 'Marpa::R2::Error::' . $name;
107 $error++;
108 }
109
110 my $version_result;
111 require Marpa::R2::Internal;
112 ( $version_result = version_ok($Marpa::R2::Internal::VERSION) )
113 and die 'Marpa::R2::Internal::VERSION ', $version_result;
114
115 require Marpa::R2::Grammar;
116 ( $version_result = version_ok($Marpa::R2::Grammar::VERSION) )
117 and die 'Marpa::R2::Grammar::VERSION ', $version_result;
118
119 require Marpa::R2::Recognizer;
120 ( $version_result = version_ok($Marpa::R2::Recognizer::VERSION) )
121 and die 'Marpa::R2::Recognizer::VERSION ', $version_result;
122
123 require Marpa::R2::Value;
124 ( $version_result = version_ok($Marpa::R2::Value::VERSION) )
125 and die 'Marpa::R2::Value::VERSION ', $version_result;
126
127 require Marpa::R2::MetaG;
128 ( $version_result = version_ok($Marpa::R2::MetaG::VERSION) )
129 and die 'Marpa::R2::MetaG::VERSION ', $version_result;
130
131 require Marpa::R2::SLG;
132 ( $version_result = version_ok($Marpa::R2::Scanless::G::VERSION) )
133 and die 'Marpa::R2::Scanless::G::VERSION ', $version_result;
134
135 require Marpa::R2::SLR;
136 ( $version_result = version_ok($Marpa::R2::Scanless::R::VERSION) )
137 and die 'Marpa::R2::Scanless::R::VERSION ', $version_result;
138
139 require Marpa::R2::MetaAST;
140 ( $version_result = version_ok($Marpa::R2::MetaAST::VERSION) )
141 and die 'Marpa::R2::MetaAST::VERSION ', $version_result;
142
143 require Marpa::R2::Stuifzand;
144 ( $version_result = version_ok($Marpa::R2::Stuifzand::VERSION) )
145 and die 'Marpa::R2::Stuifzand::VERSION ', $version_result;
146
147 require Marpa::R2::ASF;
148 ( $version_result = version_ok($Marpa::R2::ASF::VERSION) )
149 and die 'Marpa::R2::ASF::VERSION ', $version_result;
150
151 sub Marpa::R2::exception {
152 my $exception = join q{}, @_;
153 $exception =~ s/ \n* \z /\n/xms;
154 die($exception) if $Marpa::R2::JUST_DIE;
155 CALLER: for ( my $i = 0; 1; $i++) {
156 my ($package ) = caller($i);
157 last CALLER if not $package;
158 last CALLER if not 'Marpa::R2::' eq substr $package, 0, 11;
159 $Carp::Internal{ $package } = 1;
160 }
161 Carp::croak($exception, q{Marpa::R2 exception});
162 }
163
164 package Marpa::R2::Internal::X;
165
166 use overload (
167 q{""} => sub {
168 my ($self) = @_;
169 return $self->{message} // $self->{fallback_message};
170 },
171 fallback => 1
172 );
173
174 sub new {
175 my ( $class, @hash_ref_args ) = @_;
176 my %x_object = ();
177 for my $hash_ref_arg (@hash_ref_args) {
178 if ( ref $hash_ref_arg ne "HASH" ) {
179 my $ref_type = ref $hash_ref_arg;
180 my $ref_desc = $ref_type ? "ref to $ref_type" : "not a ref";
181 die
182 "Internal error: args to Marpa::R2::Internal::X->new is $ref_desc -- it should be hash ref";
183 } ## end if ( ref $hash_ref_arg ne "HASH" )
184 $x_object{$_} = $hash_ref_arg->{$_} for keys %{$hash_ref_arg};
185 } ## end for my $hash_ref_arg (@hash_ref_args)
186 my $name = $x_object{name};
187 die("Internal error: an excepion must have a name") if not $name;
188 $x_object{fallback_message} = qq{Exception "$name" thrown};
189 return bless \%x_object, $class;
190 } ## end sub new
191
192 sub name {
193 my ($self) = @_;
194 return $self->{name};
195 }
196
197 1;
198
199 # vim: set expandtab shiftwidth=4: