Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement parsing for simple regexes
  • Loading branch information
sorear committed Jul 20, 2010
1 parent 007fab2 commit 663c423
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
@@ -1,8 +1,8 @@
STDBASE:=$(shell pwd)/STD_checkout
STDENV=PERL5LIB=$(STDBASE) PERL6LIB=$(STDBASE):$(STDBASE)/lib

COMPILER=Body.pm CgOp.pm CodeGen.pm CompilerDriver.pm Decl.pm Op.pm Sig.pm\
Unit.pm Niecza/Actions.pm Niecza/Grammar.pmc .STD_build_stamp
COMPILER=Body.pm CgOp.pm CodeGen.pm CompilerDriver.pm Decl.pm Op.pm RxOp.pm\
Sig.pm Unit.pm Niecza/Actions.pm Niecza/Grammar.pmc .STD_build_stamp

all: Setting.dll

Expand Down
57 changes: 56 additions & 1 deletion Niecza/Actions.pm
Expand Up @@ -4,10 +4,13 @@ use strict;
use warnings;

use Op;
use RxOp;
use Body;
use Unit;
use Sig;

use Try::Tiny;

our $AUTOLOAD;
my %carped;
sub AUTOLOAD {
Expand Down Expand Up @@ -147,9 +150,61 @@ sub quote__S_Q { my ($cl, $M) = @_;
$M->{_ast} = $M->{quibble}{nibble}{_ast};
}

sub quote__S_Slash_Slash { my ($cl, $M) = @_;
$M->{_ast} = $M->{nibble}{_ast};
}

# :: RxOp
sub atom { my ($cl, $M) = @_;
if ($M->{metachar}) {
$M->{_ast} = $M->{metachar}{_ast};
} else {
$M->{_ast} = RxOp::String->new(text => $M->Str);
}
}

sub quantified_atom { my ($cl, $M) = @_; # :: RxOp
my $atom = $M->{atom}{_ast};
my $ns = $M->{normspace}[0];
my $q = $M->{quantifier}[0] ? $M->{quantifier}[0]{_ast} : undef;

if (!$q) {
$M->{_ast} = $atom;
} elsif ($q->{simple}) {
$M->{_ast} = RxOp::Quantifier->new(type => $q->{simple},
zyg => [$atom]);
} else {
$M->sorry("Unhandled quantifier " . $M->{quantifier}[0]->Str);
}
}

# :: Context hash interpreted by quantified_atom
sub quantifier {}
sub quantifier__S_Star { my ($cl, $M) = @_;
$M->{_ast} = { simple => '*' };
}
sub quantifier__S_Plus { my ($cl, $M) = @_;
$M->{_ast} = { simple => '+' };
}
sub quantifier__S_Question { my ($cl, $M) = @_;
$M->{_ast} = { simple => '?' };
}

sub quantmod { my ($cl, $M) = @_;
if ($M->Str ne '') {
$M->sorry('Quantmods NYI');
return;
}
}

sub quant_atom_list { my ($cl, $M) = @_;
$M->{_ast} = RxOp::Sequence->new(zyg =>
[ map { $_->{_ast} } @{ $M->{quantified_atom} } ]);
}

sub nibbler { my ($cl, $M) = @_;
if ($M->isa('STD::Regex')) {
$M->{_ast} = $M->{EXPR}{_ast};
$M->{_ast} = RxOp::to_ops($M->{EXPR}{_ast});
} elsif ($M->isa('Niecza::Grammar::CgOp')) {
# XXX We don't interpret the code, so we can't tell if it's actually
# using variables, but still, it probably is.
Expand Down
52 changes: 52 additions & 0 deletions RxOp.pm
@@ -0,0 +1,52 @@
use strict;
use warnings;
use 5.010;

use CgOp;

{
package RxOp;
use Moose;

has zyg => (isa => 'ArrayRef[RxOp]', is => 'ro');

__PACKAGE__->meta->make_immutable;
no Moose;
}

{
package RxOp::String;
use Moose;
extends 'RxOp';

has text => (isa => 'Str', is => 'ro', required => 1);

__PACKAGE__->meta->make_immutable;
no Moose;
}

{
package RxOp::Quantifier;
use Moose;
extends 'RxOp';

has type => (isa => 'Str', is => 'ro', required => 1);
# ? + * only
# zyg * 1

__PACKAGE__->meta->make_immutable;
no Moose;
}

{
package RxOp::Sequence;
use Moose;
extends 'RxOp';

# zyg * N

__PACKAGE__->meta->make_immutable;
no Moose;
}

1;

0 comments on commit 663c423

Please sign in to comment.