1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#!/usr/bin/env perl
# This is an example Pegex parser that parses itself! Each word is captured and
# uppercased, then added to the tree.
#
# This is a good script to start with to try new ideas, as it is
# self-contained.
use Pegex::Parser;
use FindBin '$Script';
use IO::All;
use YAML::PP;
# Pegex parsing needs:
#
# * A parser object
# * A grammar object
# * A receiver object
# * Some input to parse
#
# Try the debug option to see everything in detail.
sub main {
my $parser = Pegex::Parser->new(
grammar => SelfGrammar->new,
receiver => SelfTree->new,
debug => 1,
);
my $input = io->file($Script)->all;
my $tree = $parser->parse($input);
print YAML::PP::Dump $tree;
}
# A custom grammar class:
{
package SelfGrammar;
use Pegex::Base;
extends 'Pegex::Grammar';
use constant text => <<'...';
self: word* %% +
word: / ( NS+ ) /
...
}
# A custom receiver class:
{
package SelfTree;
use Pegex::Base;
extends 'Pegex::Tree';
sub got_word {
my ($self, $got) = @_;
uc $got;
}
}
main(@ARGV);
|