Skip to content

Commit

Permalink
Perlito5 - misc/Perl5_parser regex
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Jul 9, 2024
1 parent 9c74c94 commit 12bf854
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions misc/perl5_parser/perl5_parser.pl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
map { $_ => OPERATOR() }
qw(
== != <= >= < > <=>
=~ !~
+ * ** % ++ -- && || // ! ^ ~ ~~ & |
>> <<
**= += -= *= /= x= |= &= .= <<= >>= %= ||= &&= ^= //=
Expand Down Expand Up @@ -268,19 +269,21 @@ sub tokenize {
'%' => 16,
'x' => 16, # String repetition

'!' => 17, # Unary negation
'\\' => 17, # create reference
'=~' => 17,
'!~' => 17,

'**' => 18,
'!' => 18, # Unary negation
'\\' => 18, # create reference

'++' => 19,
'--' => 19,
'**' => 19,

'->' => 21,
'++' => 20,
'--' => 20,

'(' => 21, # function call
'{' => 21, # hash element
'[' => 21, # array elemnt
'->' => 21,
'(' => 21, # function call
'{' => 21, # hash element
'[' => 21, # array elemnt

'$' => 22,
'$#' => 22,
Expand Down Expand Up @@ -825,12 +828,12 @@ sub parse_statement_block {
sub parse_term {
my ( $tokens, $index ) = @_;
my $type = $tokens->[$index][0];
my $pos = $index;
my $ast;
if ( $type == NUMBER() || $type == DOT() ) {
$ast = parse_number( $tokens, $index );
}
elsif ( $type == IDENTIFIER() ) {
my $pos = $index;
my $stmt = $tokens->[$pos][1];
$pos = parse_optional_whitespace( $tokens, $pos + 1 )->{next};
if ( $tokens->[$pos][0] == FAT_ARROW() ) { # bareword
Expand Down Expand Up @@ -868,11 +871,15 @@ sub parse_term {
if ( $ast->{FAIL} ) {
return parse_fail( $tokens, $index );
}

# TODO parse regex modifiers
return { type => 'REGEX', index => $index, value => { args => $ast }, next => $ast->{next} };
$pos = $ast->{next};
my $modifier = "";
if ( $tokens->[$pos][0] == IDENTIFIER() ) { # regex modifiers
$modifier = $tokens->[$pos][1];
$pos++;
}
return { type => 'REGEX', index => $index, value => { args => $ast, modifier => $modifier }, next => $pos, };
}
elsif ( $stmt eq 'qw' ) { # qw/.../
elsif ( $stmt eq 'qw' ) { # qw/.../
$ast = parse_single_quote_string( $tokens, $index, $pos );
if ( $ast->{FAIL} ) {
return parse_fail( $tokens, $index );
Expand Down Expand Up @@ -909,9 +916,13 @@ sub parse_term {
if ( $ast->{FAIL} ) {
return parse_fail( $tokens, $index );
}

# TODO parse regex modifiers
return { type => 'REGEX', index => $index, value => { args => $ast }, next => $ast->{next} };
$pos = $ast->{next};
my $modifier = "";
if ( $tokens->[$pos][0] == IDENTIFIER() ) { # regex modifiers
$modifier = $tokens->[$pos][1];
$pos++;
}
return { type => 'REGEX', index => $index, value => { args => $ast, modifier => $modifier }, next => $pos, };
}
else {
return parse_fail( $tokens, $index );
Expand Down Expand Up @@ -1098,7 +1109,8 @@ sub main {
\$a
=2
;
print => 123
print => 123;
$a =~ /123/i;
__END__
123

0 comments on commit 12bf854

Please sign in to comment.