App-Mowyw

 view release on metacpan or  search on metacpan

lib/App/Mowyw/Lexer.pm  view on Meta::CPAN

                if (defined $match){
                    push @res, [$_->[0],
                                $match,
                                $pos_and_line_number->($token_pos),
                            ];
                }
                last;
            }
        }
        unless ($matched){
            my $next_token;
            my $next_token_match;
            my $match;

            my $min = length($text);
            my $pos = pos($text);

            # find the token that matches first
            my $token_pos;
            foreach(@$tokens){
                my $re = $_->[1];
                if ($text =~ m#\G((?s:.)*?)($re)#gc){
                    if ($+[1] < $min){
                        $min              = $+[1];
                        $next_token       = $_;
                        $next_token_match = $2;
                        $match            = $1;
                        $token_pos        = pos($text) - length($match);
                    }
                }
                pos($text) = $pos;
            }
            if (defined $match){
                push @res, ['UNMATCHED',
                            $match,
                            $pos_and_line_number->($token_pos - length($match)),
                           ];
                die "Each token has to require at least one character; Rule $_->[0] matched Zero!\n"
                    unless (length($next_token_match) > 0);
                if (my $fun = $next_token->[2]){
                    $match = $fun->($match);
                }
                push @res, [$next_token->[0],
                            $next_token_match,
                            $pos_and_line_number->($min),
                           ] if defined $match;
                pos($text) = $min + length($next_token_match);
            } else {
                push @res, ['UNMATCHED',
                            substr($text, $pos),
                            $pos_and_line_number->($pos)
                           ];
                pos($text) = length($text);
            }
        }
    }
    return @res;
}
-1;

# vim: sw=4 ts=4 expandtab



( run in 3.288 seconds using v1.01-cache-2.11-cpan-b9db842bd85 )