Skip to content

Commit

Permalink
0.3 Parser.yp
Browse files Browse the repository at this point in the history
  • Loading branch information
nelhage committed Dec 3, 2006
1 parent 4a0a9b3 commit 76dd37b
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions lib/Data/SExpression/Parser.yp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#

%{
use Symbol;
use Data::SExpression::Cons;
%}

Expand All @@ -17,8 +16,8 @@ sexpression: expression { $_[0]->YYAccept; return $_[1]; }
;

expression: NUMBER
| SYMBOL { Symbol::qualify_to_ref($_[1], 'main') }
| STRING
| SYMBOL { $_[0]->handler->new_symbol($_[1]) }
| STRING { $_[0]->handler->new_string($_[1]) }
| list
| quoted

Expand All @@ -29,39 +28,42 @@ list: '(' list_interior ')' { $_[2] }


list_interior:
expression '.' expression { Data::SExpression::Cons->new($_[1], $_[3]) }
| expression list_interior { Data::SExpression::Cons->new($_[1], $_[2]) }
| expression { Data::SExpression::Cons->new($_[1], undef) }
expression '.' expression { $_[0]->handler->new_cons($_[1], $_[3]) }
| expression list_interior { $_[0]->handler->new_cons($_[1], $_[2]) }
| expression { $_[0]->handler->new_cons($_[1], undef) }

;

quoted:
QUOTE expression { Data::SExpression::Cons->new(Symbol::qualify_to_ref($_[1], "main"),
Data::SExpression::Cons->new($_[2], undef))}
QUOTE expression { $_[0]->handler->new_cons($_[0]->handler->new_symbol($_[1]),
$_[0]->handler->new_cons($_[2], undef))}
;

%%

sub _extract_string {
my $self = shift;
my $str = shift || '';

$str =~ s/\\"/"/g;

return $str;
}

sub set_input {
my $self = shift;
my $input = shift or die(__PACKAGE__ . "::set_input called with 0 arguments");
$self->YYData->{INPUT} = $input;
}

sub set_handler {
my $self = shift;
my $handler = shift or die(__PACKAGE__ . "::set_handler called with 0 arguments");
$self->YYData->{HANDLER} = $handler;
}

sub handler {
my $self = shift;
return $self->YYData->{HANDLER};
}

sub unparsed_input {
my $self = shift;
return substr($self->YYData->{INPUT}, pos($self->YYData->{INPUT}));
}


my %quotes = (q{'} => 'quote',
q{`} => 'quasiquote',
q{,} => 'unquote');
Expand All @@ -84,8 +86,8 @@ sub lexer {
/\G ($symbol_char ($symbol_char | \d )*)/gcx
and return ('SYMBOL', $1);

/\G " ([^"\\]* (?: \\. [^"\\]*)*) "/gcx
and return ('STRING', $self->_extract_string($1));
/\G " (.*? [^\\]) "/gcx || /\G ""/gcx
and return ('STRING', $1 || "");

/\G ([().])/gcx
and return ($1, $1);
Expand Down

0 comments on commit 76dd37b

Please sign in to comment.