Permalink
Browse files

Perlito5 - grammar - new module Perlito5::Grammar::Number

  • Loading branch information...
1 parent 18819e6 commit d7ff69fe4296b7366ee8ebeaec68910632470e7b @fglock committed Sep 13, 2013
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
1,883 perlito5.pl

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,6 +1,7 @@
package Perlito5::Grammar;
use Perlito5::Grammar::Expression;
+use Perlito5::Grammar::Statement;
use Perlito5::Grammar::Control;
use Perlito5::Grammar::String;
use Perlito5::Grammar::Sigil;
@@ -10,6 +11,7 @@ use Perlito5::Grammar::Space;
use Perlito5::Grammar::Print;
use Perlito5::Grammar::Map;
use Perlito5::Grammar::Attribute;
+use Perlito5::Grammar::Number;
sub word {
substr( $_[1], $_[2], 1 ) =~ m/\w/
@@ -21,16 +23,6 @@ sub word {
: 0;
}
-sub digit {
- substr( $_[1], $_[2], 1 ) =~ m/\d/
- ? {
- str => $_[1],
- from => $_[2],
- to => $_[2] + 1,
- }
- : 0;
-}
-
sub ident {
return
if substr( $_[1], $_[2], 1 ) !~ m/\w/
@@ -105,7 +97,7 @@ token opt_type {
token var_sigil { \$ |\% |\@ |\& | \* };
-token var_name { <full_ident> | <digit> };
+token var_name { <full_ident> | <Perlito5::Grammar::Number.digit> };
token var_ident {
<var_sigil> <optional_namespace_before_ident> <var_name>
@@ -118,44 +110,6 @@ token var_ident {
}
};
-token exponent {
- [ 'e' | 'E' ] [ '+' | '-' | '' ] [ '_' | \d ]+
-};
-
-token val_num {
- [
- | \. \d [ '_' | \d]* <.exponent>? # .10 .10e10
- | \d [ '_' | \d]* [ <.exponent> | \. <!before \. > [ '_' | \d]* <.exponent>? ]
- ]
- {
- my $s = Perlito5::Match::flat($MATCH);
- $s =~ s/_//g;
- $MATCH->{capture} = Perlito5::AST::Val::Num->new( num => $s )
- }
-};
-
-token digits {
- \d+
-};
-
-token val_int {
- [ '0' ['x'|'X'] <.word>+ # XXX test for hex number
- | '0' ['b'|'B'] [ '_' | '0' | '1' ]+
- | '0' [ '_' | \d]+ # XXX test for octal number
- ]
- { $MATCH->{capture} = Perlito5::AST::Val::Int->new( int => oct(lc(Perlito5::Match::flat($MATCH))) ) }
- | \d [ '_' | \d]*
- {
- my $s = Perlito5::Match::flat($MATCH);
- $s =~ s/_//g;
- $MATCH->{capture} = Perlito5::AST::Val::Int->new( int => $s )
- }
-};
-
-token val_version {
- ['v']? <.digits> [ '.' <.digits> [ '.' <.digits> ]? ]?
-};
-
my @PKG;
sub exp_stmts {
my $self = $_[0];
@@ -5,17 +5,6 @@ use Perlito5::Grammar::Bareword;
use Perlito5::Grammar::Attribute;
use Perlito5::Grammar::Statement;
-Perlito5::Grammar::Precedence::add_term( '.' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '0' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '1' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '2' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '3' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '4' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '5' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '6' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '7' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '8' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
-Perlito5::Grammar::Precedence::add_term( '9' => sub { Perlito5::Grammar::Expression->term_digit( $_[0], $_[1] ) } );
Perlito5::Grammar::Precedence::add_term( 'my' => sub { Perlito5::Grammar::Expression->term_declarator( $_[0], $_[1] ) } );
Perlito5::Grammar::Precedence::add_term( 'our' => sub { Perlito5::Grammar::Expression->term_declarator( $_[0], $_[1] ) } );
Perlito5::Grammar::Precedence::add_term( 'eval' => sub { Perlito5::Grammar::Expression->term_eval( $_[0], $_[1] ) } );
@@ -384,11 +373,6 @@ token term_arrow {
]
};
-token term_digit {
- <Perlito5::Grammar.val_num> { $MATCH->{capture} = [ 'term', Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.val_num"}) ] } # 123.456
- | <Perlito5::Grammar.val_int> { $MATCH->{capture} = [ 'term', Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.val_int"}) ] } # 123
-};
-
token term_ternary {
'?' <ternary5_parse> ':'
{ $MATCH->{capture} = [ 'op', '? :', Perlito5::Match::flat($MATCH->{ternary5_parse}) ] }
@@ -0,0 +1,107 @@
+
+package Perlito5::Grammar::Number;
+use strict;
+use Perlito5::Grammar::Precedence;
+
+Perlito5::Grammar::Precedence::add_term( '.' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '0' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '1' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '2' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '3' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '4' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '5' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '6' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '7' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '8' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+Perlito5::Grammar::Precedence::add_term( '9' => sub { Perlito5::Grammar::Number->term_digit( $_[0], $_[1] ) } );
+
+
+token term_digit {
+ <Perlito5::Grammar::Number.val_num>
+ # 123.456
+ { $MATCH->{capture} = [ 'term', Perlito5::Match::flat($MATCH->{"Perlito5::Grammar::Number.val_num"}) ] }
+ | <Perlito5::Grammar::Number.val_int>
+ # 123
+ { $MATCH->{capture} = [ 'term', Perlito5::Match::flat($MATCH->{"Perlito5::Grammar::Number.val_int"}) ] }
+};
+
+sub digit {
+ substr( $_[1], $_[2], 1 ) =~ m/\d/
+ ? {
+ str => $_[1],
+ from => $_[2],
+ to => $_[2] + 1,
+ }
+ : 0;
+}
+
+token exponent {
+ [ 'e' | 'E' ] [ '+' | '-' | '' ] [ '_' | \d ]+
+};
+
+token val_num {
+ [
+ | \. \d [ '_' | \d]* <.exponent>? # .10 .10e10
+ | \d [ '_' | \d]* [ <.exponent> | \. <!before \. > [ '_' | \d]* <.exponent>? ]
+ ]
+ {
+ my $s = Perlito5::Match::flat($MATCH);
+ $s =~ s/_//g;
+ $MATCH->{capture} = Perlito5::AST::Val::Num->new( num => $s )
+ }
+};
+
+token digits {
+ \d+
+};
+
+token val_int {
+ [ '0' ['x'|'X'] <.Perlito5::Grammar.word>+ # XXX test for hex number
+ | '0' ['b'|'B'] [ '_' | '0' | '1' ]+
+ | '0' [ '_' | \d]+ # XXX test for octal number
+ ]
+ { $MATCH->{capture} = Perlito5::AST::Val::Int->new( int => oct(lc(Perlito5::Match::flat($MATCH))) ) }
+ | \d [ '_' | \d]*
+ {
+ my $s = Perlito5::Match::flat($MATCH);
+ $s =~ s/_//g;
+ $MATCH->{capture} = Perlito5::AST::Val::Int->new( int => $s )
+ }
+};
+
+token val_version {
+ ['v']? <.digits> [ '.' <.digits> [ '.' <.digits> ]? ]?
+};
+
+1;
+
+=begin
+
+=head1 NAME
+
+Perlito5::Grammar::Number - Parser and AST generator for Perlito
+
+=head1 SYNOPSIS
+
+ term_print($str)
+
+=head1 DESCRIPTION
+
+This module parses source code for Perl 5 statements and generates Perlito5 AST.
+
+=head1 AUTHORS
+
+Flavio Soibelmann Glock <fglock@gmail.com>.
+The Pugs Team E<lt>perl6-compiler@perl.orgE<gt>.
+
+=head1 COPYRIGHT
+
+Copyright 2013 by Flavio Soibelmann Glock and others.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+See L<http://www.perl.com/perl/misc/Artistic.html>
+
+=end
+
@@ -110,10 +110,10 @@ token rule_term {
{ $MATCH->{capture} = Perlito5::Rul::Block->new( closure => Perlito5::Match::flat($MATCH->{parsed_code}) ) }
| \\
[
- | 'c' \[ <Perlito5::Grammar.digits> \]
- { $MATCH->{capture} = Perlito5::Rul::Constant->new( constant => chr( Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.digits"}) ) ) }
- | 'c' <Perlito5::Grammar.digits>
- { $MATCH->{capture} = Perlito5::Rul::Constant->new( constant => chr( Perlito5::Match::flat($MATCH->{"Perlito5::Grammar.digits"}) ) ) }
+ | 'c' \[ <Perlito5::Grammar::Number.digits> \]
+ { $MATCH->{capture} = Perlito5::Rul::Constant->new( constant => chr( Perlito5::Match::flat($MATCH->{"Perlito5::Grammar::Number.digits"}) ) ) }
+ | 'c' <Perlito5::Grammar::Number.digits>
+ { $MATCH->{capture} = Perlito5::Rul::Constant->new( constant => chr( Perlito5::Match::flat($MATCH->{"Perlito5::Grammar::Number.digits"}) ) ) }
| <any>
# \e \E
{ $MATCH->{capture} = Perlito5::Rul::SpecialChar->new( char => Perlito5::Match::flat($MATCH->{any}) ) }
@@ -105,14 +105,14 @@ token start_of_line {
[ ' ' | \t ]*
'line'
[ ' ' | \t ]+
- <Perlito5::Grammar.digits>
+ <Perlito5::Grammar::Number.digits>
[ ' ' | \t ]*
# TODO: optional filename (specified with or without quotes)
<.to_eol>
{
- $Perlito5::LINE_NUMBER = 0 + Perlito5::Match::flat($MATCH->{'Perlito5::Grammar.digits'});
+ $Perlito5::LINE_NUMBER = 0 + Perlito5::Match::flat($MATCH->{'Perlito5::Grammar::Number.digits'});
# TODO: filename
# $Perlito5::FILE_NAME = ...;
@@ -24,7 +24,7 @@ token use_decl { 'use' | 'no' };
token term_use {
<use_decl> <.Perlito5::Grammar::Space.ws>
[
- <Perlito5::Grammar.val_version>
+ <Perlito5::Grammar::Number.val_version>
{
# TODO - check version

0 comments on commit d7ff69f

Please sign in to comment.