[libparser-mgc-perl] 03/12: Import of PEVANS/Parser-MGC-0.03 from CPAN.
Jonas Smedegaard
dr at jones.dk
Sat Dec 17 17:37:00 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag PEVANS
in repository libparser-mgc-perl.
commit 25f85062460be63f8ce5360ad74c960bdcffd737
Author: Paul Evans <leonerd at leonerd.org.uk>
Date: Sun Dec 12 17:37:46 2010 +0000
Import of PEVANS/Parser-MGC-0.03 from CPAN.
gitpan-cpan-distribution: Parser-MGC
gitpan-cpan-version: 0.03
gitpan-cpan-path: PEVANS/Parser-MGC-0.03.tar.gz
gitpan-cpan-author: PEVANS
gitpan-cpan-maturity: released
---
Build.PL | 1 +
Changes | 7 ++++++
META.yml | 6 +++--
Makefile.PL | 1 +
README | 71 ++++++++++++++++++++++++++++++++++++++++++++++------
lib/Parser/MGC.pm | 74 ++++++++++++++++++++++++++++++++++++++++++++++++-------
t/02expect.t | 6 ++++-
7 files changed, 147 insertions(+), 19 deletions(-)
diff --git a/Build.PL b/Build.PL
index f98f30d..45549a9 100644
--- a/Build.PL
+++ b/Build.PL
@@ -6,6 +6,7 @@ use Module::Build;
my $build = Module::Build->new(
module_name => 'Parser::MGC',
requires => {
+ 'File::Slurp' => 0,
},
build_requires => {
'Test::More' => 0,
diff --git a/Changes b/Changes
index 6b61864..58675b5 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,12 @@
Revision history for Parser-MGC
+0.03 CHANGES:
+ * Expanded documentation, more examples
+
+ BUGFIXES:
+ * Regexp quoting fix for perl >= 5.13.6
+ * Declare dependency on File::Slurp
+
0.02 CHANGES:
* ->expect now returns the consumed string
* ->token_int recognises negative integers
diff --git a/META.yml b/META.yml
index f4262cd..cb085ae 100644
--- a/META.yml
+++ b/META.yml
@@ -15,7 +15,9 @@ name: Parser-MGC
provides:
Parser::MGC:
file: lib/Parser/MGC.pm
- version: 0.02
+ version: 0.03
+requires:
+ File::Slurp: 0
resources:
license: http://dev.perl.org/licenses/
-version: 0.02
+version: 0.03
diff --git a/Makefile.PL b/Makefile.PL
index 1c06b3a..e9136a7 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -5,6 +5,7 @@ WriteMakefile
'NAME' => 'Parser::MGC',
'VERSION_FROM' => 'lib/Parser/MGC.pm',
'PREREQ_PM' => {
+ 'File::Slurp' => 0,
'Test::More' => 0
},
'INSTALLDIRS' => 'site',
diff --git a/README b/README
index bfd056f..5130c07 100644
--- a/README
+++ b/README
@@ -91,7 +91,7 @@ METHODS
$parser->fail( $message )
Aborts the current parse attempt with the given message string. The
failure message will include the current line and column position, and
- will include the line of input that failed.
+ the line of input that failed.
$eos = $parser->at_eos
Returns true if the input string is at the end of the string.
@@ -103,14 +103,27 @@ STRUCTURE-FORMING METHODS
argument.
$ret = $parser->maybe( $code )
- Attempts to execute the given $code reference in scalar context, passing
- in no arguments, and returning what it returned. If the code fails to
- parse by calling the "fail" method then none of the input string will be
- consumed; the current parsing position will be restored. "undef" will be
- returned in this case.
+ Attempts to execute the given $code reference in scalar context, and
+ returns what it returned. If the code fails to parse by calling the
+ "fail" method then none of the input string will be consumed; the
+ current parsing position will be restored. "undef" will be returned in
+ this case.
This may be considered to be similar to the "?" regexp qualifier.
+ sub parse_declaration
+ {
+ my $self = shift;
+
+ [ $self->parse_type,
+ $self->token_ident,
+ $self->maybe( sub {
+ $self->expect( "=" );
+ $self->parse_expression
+ } ),
+ ];
+ }
+
$ret = $parser->scope_of( $start, $code, $stop )
Expects to find the $start pattern, then attempts to execute the given
$code reference, then expects to find the $stop pattern. Returns
@@ -120,6 +133,13 @@ STRUCTURE-FORMING METHODS
token parsing methods as an end-of-scope marker; causing them to raise a
failure if called at the end of a scope.
+ sub parse_block
+ {
+ my $self = shift;
+
+ $self->scope_of( "{", sub { $self->parse_statements }, "}" );
+ }
+
$ret = $parser->list_of( $sep, $code )
Expects to find a list of instances of something parsed by $code,
separated by the $sep pattern. Returns an ARRAY ref containing a list of
@@ -129,6 +149,13 @@ STRUCTURE-FORMING METHODS
that is, that the scope ended before any item instances were parsed from
it.
+ sub parse_numbers
+ {
+ my $self = shift;
+
+ $self->list_of( ",", sub { $self->token_int } );
+ }
+
$ret = $parser->sequence_of( $code )
A shortcut for calling "list_of" with an empty string as separator;
expects to find at least one instance of something parsed by $code,
@@ -137,6 +164,13 @@ STRUCTURE-FORMING METHODS
This may be considered to be similar to the "+" or "*" regexp
qualifiers.
+ sub parse_statements
+ {
+ my $self = shift;
+
+ $self->sequence_of( sub { $self->parse_statement } );
+ }
+
$ret = $parser->one_of( @codes )
Expects that one of the given code references can parse something from
the input, returning what it returned. Each code reference may indicate
@@ -145,6 +179,17 @@ STRUCTURE-FORMING METHODS
This may be considered to be similar to the "|" regexp operator for
forming alternations of possible parse trees.
+ sub parse_statement
+ {
+ my $self = shift;
+
+ $self->one_of(
+ sub { $self->parse_declaration; $self->expect(";") },
+ sub { $self->parse_expression; $self->expect(";") },
+ sub { $self->parse_block },
+ );
+ }
+
$parser->commit
Calling this method will cancel the backtracking behaviour of the
innermost "maybe" or "one_of" structure forming method. That is, if
@@ -155,7 +200,19 @@ STRUCTURE-FORMING METHODS
alternation has been determined, ensuring that any further failures are
raised as real exceptions, rather than by attempting other alternatives.
- TODO: Code example with commit inside one_of
+ sub parse_statement
+ {
+ my $self = shift;
+
+ $self->one_of(
+ ...
+ sub {
+ $self->scope_of( "{",
+ sub { $self->commit; $self->parse_statements; },
+ "}" ),
+ },
+ );
+ }
TOKEN PARSING METHODS
The following methods attempt to consume some part of the input string,
diff --git a/lib/Parser/MGC.pm b/lib/Parser/MGC.pm
index 3b6675b..fc4039b 100644
--- a/lib/Parser/MGC.pm
+++ b/lib/Parser/MGC.pm
@@ -8,7 +8,7 @@ package Parser::MGC;
use strict;
use warnings;
-our $VERSION = '0.02';
+our $VERSION = '0.03';
use Carp;
@@ -217,8 +217,8 @@ sub where
=head2 $parser->fail( $message )
Aborts the current parse attempt with the given message string. The failure
-message will include the current line and column position, and will include
-the line of input that failed.
+message will include the current line and column position, and the line of
+input that failed.
=cut
@@ -266,14 +266,26 @@ which will be passed the actual C<$parser> object as its first argument.
=head2 $ret = $parser->maybe( $code )
-Attempts to execute the given C<$code> reference in scalar context, passing in
-no arguments, and returning what it returned. If the code fails to parse by
-calling the C<fail> method then none of the input string will be consumed; the
-current parsing position will be restored. C<undef> will be returned in this
-case.
+Attempts to execute the given C<$code> reference in scalar context, and
+returns what it returned. If the code fails to parse by calling the C<fail>
+method then none of the input string will be consumed; the current parsing
+position will be restored. C<undef> will be returned in this case.
This may be considered to be similar to the C<?> regexp qualifier.
+ sub parse_declaration
+ {
+ my $self = shift;
+
+ [ $self->parse_type,
+ $self->token_ident,
+ $self->maybe( sub {
+ $self->expect( "=" );
+ $self->parse_expression
+ } ),
+ ];
+ }
+
=cut
sub maybe
@@ -306,6 +318,13 @@ While the code is being executed, the C<$stop> pattern will be used by the
token parsing methods as an end-of-scope marker; causing them to raise a
failure if called at the end of a scope.
+ sub parse_block
+ {
+ my $self = shift;
+
+ $self->scope_of( "{", sub { $self->parse_statements }, "}" );
+ }
+
=cut
sub scope_of
@@ -334,6 +353,13 @@ the return values from the C<$code>.
This method does not consider it an error if the returned list is empty; that
is, that the scope ended before any item instances were parsed from it.
+ sub parse_numbers
+ {
+ my $self = shift;
+
+ $self->list_of( ",", sub { $self->token_int } );
+ }
+
=cut
sub list_of
@@ -363,6 +389,13 @@ by skipped whitespace.
This may be considered to be similar to the C<+> or C<*> regexp qualifiers.
+ sub parse_statements
+ {
+ my $self = shift;
+
+ $self->sequence_of( sub { $self->parse_statement } );
+ }
+
=cut
sub sequence_of
@@ -382,6 +415,17 @@ to parse by calling the C<fail> method.
This may be considered to be similar to the C<|> regexp operator for forming
alternations of possible parse trees.
+ sub parse_statement
+ {
+ my $self = shift;
+
+ $self->one_of(
+ sub { $self->parse_declaration; $self->expect(";") },
+ sub { $self->parse_expression; $self->expect(";") },
+ sub { $self->parse_block },
+ );
+ }
+
=cut
sub one_of
@@ -417,7 +461,19 @@ Typically this will be called once the grammatical structure of an
alternation has been determined, ensuring that any further failures are raised
as real exceptions, rather than by attempting other alternatives.
- TODO: Code example with commit inside one_of
+ sub parse_statement
+ {
+ my $self = shift;
+
+ $self->one_of(
+ ...
+ sub {
+ $self->scope_of( "{",
+ sub { $self->commit; $self->parse_statements; },
+ "}" ),
+ },
+ );
+ }
=cut
diff --git a/t/02expect.t b/t/02expect.t
index b5a8e35..a6382e2 100644
--- a/t/02expect.t
+++ b/t/02expect.t
@@ -22,9 +22,13 @@ is_deeply( $parser->from_string( "hello world" ),
[ "hello", "world" ],
'"hello world"' );
+# Perl 5.13.6 changed the regexp form
+# Accept both old and new-style stringification
+my $modifiers = (qr/foobar/ =~ /\Q(?^/) ? '^' : '-xism';
+
ok( !eval { $parser->from_string( "goodbye world" ) }, '"goodbye world" fails' );
is( $@,
- qq[Expected (?-xism:hello) on line 1 at:\n] .
+ qq[Expected (?$modifiers:hello) on line 1 at:\n] .
qq[goodbye world\n] .
qq[^\n],
'Exception from "goodbye world" failure' );
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libparser-mgc-perl.git
More information about the Pkg-perl-cvs-commits
mailing list