| 1 | package Math::Expression::Evaluator::Lexer; | ||||||||||
| 2 | 18 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 18 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 18 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Math::Expression::Evaluator::Lexer - Simple Lexer | ||||||||||
| 10 | |||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||
| 12 | |||||||||||
| 13 | use Math::Expression::Evaluator::Lexer qw(lex); | ||||||||||
| 14 | # suppose you want to parse simple math expressions | ||||||||||
| 15 | my @input_tokens = ( | ||||||||||
| 16 | ['Int', qr/[+-]?\d+/ ], | ||||||||||
| 17 | ['Op', qr{[+-/*]} ], | ||||||||||
| 18 | ['Brace_Open', qr/\(/ ], | ||||||||||
| 19 | ['Brace_Close', qr/\)/ ], | ||||||||||
| 20 | ['Whitespace', qr/\s+/, sub { return; }], | ||||||||||
| 21 | ); | ||||||||||
| 22 | my $text = "-12 * (3+4)"; | ||||||||||
| 23 | my $out_tokens = lex($text, \@input_tokens); | ||||||||||
| 24 | for (@$out_tokens){ | ||||||||||
| 25 | my ($name, $text, $pos) = @$_; | ||||||||||
| 26 | print "Found Token $name: $text (string pos: $pos)\n"; | ||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||
| 30 | |||||||||||
| 31 | Math::Expression::Evaluator::Lexer is a simple lexer that breaks up a text | ||||||||||
| 32 | into tokens, depending on the input tokens you provide | ||||||||||
| 33 | |||||||||||
| 34 | =head1 METHODS | ||||||||||
| 35 | |||||||||||
| 36 | =over 2 | ||||||||||
| 37 | |||||||||||
| 38 | =item lex | ||||||||||
| 39 | |||||||||||
| 40 | The only exported routine is lex, which expects input text as its | ||||||||||
| 41 | first argument and a array ref to list of input tokens. | ||||||||||
| 42 | |||||||||||
| 43 | Each input token consists of a token name (which you can choose freely), | ||||||||||
| 44 | a regex which matches the desired token, and optionally a reference to | ||||||||||
| 45 | a functions that takes the matched token text as its argument. The | ||||||||||
| 46 | token text is replaced by the return value of that function. If the | ||||||||||
| 47 | function returns undef, that token will not be included in the list | ||||||||||
| 48 | of output tokens. The regex should either fail or match at least one | ||||||||||
| 49 | character; zero-width matches utterly confuse the lexer, and are disallowed. | ||||||||||
| 50 | |||||||||||
| 51 | lex() returns an array ref to a list of output tokens, each output | ||||||||||
| 52 | token is a reference to a list which contains the token name, the matched | ||||||||||
| 53 | text, the string position (in characters, counted from the start of | ||||||||||
| 54 | the input string, zero based) and the line number. | ||||||||||
| 55 | |||||||||||
| 56 | Note that C<lex()> puts parentheses around the entire regex, so if you | ||||||||||
| 57 | want to use backreferences, the numbering of the capturing group is changed. | ||||||||||
| 58 | |||||||||||
| 59 | =back | ||||||||||
| 60 | |||||||||||
| 61 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 62 | |||||||||||
| 63 | Copyright (C) 2007 by Moritz Lenz, L<http://moritz.faui2k3.org>, | ||||||||||
| 64 | L<moritz@faui2k3.org>. | ||||||||||
| 65 | |||||||||||
| 66 | This Program and its Documentation is free software. You may distribute it | ||||||||||
| 67 | under the same terms as perl itself. | ||||||||||
| 68 | |||||||||||
| 69 | However all code examples are to be public domain, so you can use it in any | ||||||||||
| 70 | way you want to. | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | 18 | require Exporter; | |||||||||
| 75 | 18 | our @ISA = qw(Exporter); | |||||||||
| 76 | 18 | our @EXPORT_OK = qw(lex); | |||||||||
| 77 | |||||||||||
| 78 | 18 | our %EXPORT_TAGS = (":all" => \@EXPORT_OK); | |||||||||
| 79 | |||||||||||
| 80 | 297 | ▶ | sub lex { | ||||||||
|
Subroutine
lex: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||
| 81 | 297 | my ($text, $tokens) = @_; | |||||||||
| 82 | 297 | ▶ | confess("passed undefined value to lex()") unless defined $text; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 296 | my $l = length $text; | |||||||||
| 84 | 296 | ▶ | return [] unless $l; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | |||||||||||
| 86 | 295 | my ($last_line_number, $last_pos) = (0, 0); | |||||||||
| 87 | 1348 | ▶ | my $pos_and_line_number = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 88 | 1348 | my $pos = shift; | |||||||||
| 89 | 1348 | $last_line_number += | |||||||||
| 90 | (substr($text, $last_pos, $pos - $last_pos) =~ tr/\n//); | ||||||||||
| 91 | 1348 | $last_pos = $pos; | |||||||||
| 92 | 1348 | return ($pos, $last_line_number + 1); | |||||||||
| 93 | 295 | }; | |||||||||
| 94 | |||||||||||
| 95 | 295 | my @res; | |||||||||
| 96 | |||||||||||
| 97 | # avoid 'Use of uninitialized value in numeric lt (<)' warnings: | ||||||||||
| 98 | 295 | pos($text) = 0; | |||||||||
| 99 | |||||||||||
| 100 | 295 | ▶ | while (pos($text) < $l){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 963 | my $matched = 0; | |||||||||
| 102 | REGEXES: | ||||||||||
| 103 | 963 | for (@$tokens){ | |||||||||
| 104 | 11513 | my $re = $_->[1]; | |||||||||
| 105 | # failed regex matches reset pos() unless the /c modifier | ||||||||||
| 106 | # is present | ||||||||||
| 107 | 11513 | ▶ | if ($text =~ m/\G($re)/gc){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 1582 | $matched = 1; | |||||||||
| 109 | 1582 | my $match = $1; | |||||||||
| 110 | 1582 | ▶ | if (length $match == 0){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1 | confess("Each token has to require at least one " | |||||||||
| 112 | . "character; Rule $_->[0] matched Zero!\n"); | ||||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | # safe information before callbacks can modify $match | ||||||||||
| 116 | # and thus length($match) | ||||||||||
| 117 | 1581 | my $pos = pos($text) - length($match); | |||||||||
| 118 | |||||||||||
| 119 | 1581 | ▶ | if ($_->[2]){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 234 | $match = $_->[2]->($match); | |||||||||
| 121 | } | ||||||||||
| 122 | 1581 | ▶ | if (defined $match){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 1348 | push @res, [ | |||||||||
| 124 | $_->[0], | ||||||||||
| 125 | $match, | ||||||||||
| 126 | $pos_and_line_number->($pos), | ||||||||||
| 127 | ]; | ||||||||||
| 128 | } | ||||||||||
| 129 | 1581 | next REGEXES; | |||||||||
| 130 | } | ||||||||||
| 131 | } | ||||||||||
| 132 | 962 | ▶ | if ($matched == 0){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 1 | confess("No token matched input text <$text> at position " . pos($text)); | |||||||||
| 134 | } | ||||||||||
| 135 | } | ||||||||||
| 136 | 293 | return \@res; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 18 | 1; | |||||||||
| 140 | |||||||||||
| 141 | # vim: sw=4 ts=4 expandtab | ||||||||||