1 package ExtUtils::XSpp::Parser;
7 use ExtUtils::XSpp::Grammar;
11 ExtUtils::XSpp::Parser - an XS++ parser
18 open my $in, "<", $file
19 or die "Failed to open '$file' for reading: $!";
24 =head2 ExtUtils::XSpp::Parser::new( file => path )
26 Create a new XS++ parser.
32 my $this = bless {}, $class;
35 $this->{FILE} = $args{file};
36 $this->{STRING} = $args{string};
37 $this->{PARSER} = ExtUtils::XSpp::Grammar->new;
42 =head2 ExtUtils::XSpp::Parser::parse
44 Parse the file data; returns true on success, false otherwise,
45 on failure C<get_errors> will return the list of errors.
53 $fh = _my_open( $this->{FILE} );
55 open $fh, '<', \$this->{STRING}
56 or die "Failed to create file handle from in-memory string";
60 my $parser = $this->{PARSER};
61 $parser->YYData->{LEX}{FH} = $fh;
62 $parser->YYData->{LEX}{BUFFER} = \$buf;
63 local $parser->YYData->{PARSER} = $this;
65 $this->{DATA} = $parser->YYParse( yylex => \&ExtUtils::XSpp::Grammar::yylex,
66 yyerror => \&ExtUtils::XSpp::Grammar::yyerror,
75 my $new_lex = { FH => _my_open( $file ),
77 NEXT => $this->{PARSER}->YYData->{LEX},
80 $this->{PARSER}->YYData->{LEX} = $new_lex;
83 =head2 ExtUtils::XSpp::Parser::get_data
85 Returns a list containing the parsed data. Each item of the list is
86 a subclass of C<ExtUtils::XSpp::Node>
92 die "'parse' must be called before calling 'get_data'"
93 unless exists $this->{DATA};
98 =head2 ExtUtils::XSpp::Parser::get_errors
100 Returns the parsing errors as an array.
107 return @{$this->{ERRORS}};
110 =head2 ExtUtils::XSpp::Parser::load_plugin
112 Loads the specified plugin and calls its C<register_plugin> method.
120 if (eval "require ExtUtils::XSpp::Plugin::$package;") {
121 $package = "ExtUtils::XSpp::Plugin::$package";
122 $package->register_plugin( $this );
124 elsif (eval "require $package;") {
125 $package->register_plugin( $this );
128 die "Could not load XS++ plugin '$package' (neither via the namespace "
129 ."'ExtUtils::XS++::Plugin::$package' nor via '$package'). Reason: $@";
134 =head2 ExtUtils::XSpp::Parser::add_post_process_plugin
136 Adds the specified plugin to be called after parsing is complete to
137 modify the parse tree before it is emitted.
141 sub add_post_process_plugin {
145 push @{$this->{PLUGINS}{POST_PROCESS}}, $plugin;
148 sub post_process_plugins { $_[0]->{PLUGINS}{POST_PROCESS} || [] }