| 1 | # Copyright 2011-2012 Yuki Izumi. ( anneli AT cpan DOT org ) | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # This is free software; you can redistribute it and/or modify it under the | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | package Erlang::Parser; | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Erlang::Parser::Lexer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Erlang::Parser::Parser; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | sub parse { | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | my $parser = new Erlang::Parser::Parser; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | my $lexerfn = Erlang::Parser::Lexer->lex(@_); | |||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | @{$parser->YYParse(yylex => $lexerfn, yyerror => \&error)}; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | sub error { | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | print STDERR "Parse error!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ($_ && $_[0] && ref($_[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$_ and $_[0] and ref $_[0] eq "ARRAY"
$_ and $_[0]
MC/DC0%0/3
$_ and $_[0] and ref $_[0] eq "ARRAY"
$_
$_[0]
ref $_[0] eq "ARRAY"
Truth table0%0/4
A and B and C
A= $_B= $_[0]C= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | print STDERR "Failed token was ", $_[0]->YYCurtok; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | print STDERR ", value ", $_[0]->YYCurval; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | print STDERR ", expected ", join(',', $_[0]->YYExpect); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | print STDERR ".\n"; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | print STDERR "Empty object tree!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | die("Can not continue w/o object tree!\n"); | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | Erlang::Parser - Erlang source code parser | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | This document describes version 0.5.2 of Erlang::Parser released 2014-01-27. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | our $VERSION = '0.5.2'; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | use Erlang::Parser; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | # Parse the code found in DATA; return all root-level nodes. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | my @nodes = Erlang::Parser->parse(\*DATA); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # Each object in @nodes implements the Erlang::Parser::Node role, which | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # is the function 'print'. It takes one argument, the filehandle to | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # pretty-print to. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | $_->print(*STDOUT) for @nodes; | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # Use the accessors of each node type to get at the innards: | ||||||||||||||||||||||||||||||||||||||||||
| 60 | my ($directive, $def) = Erlang::Parser->parse(<<ERL); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | -export([my_fun/2]). | ||||||||||||||||||||||||||||||||||||||||||
| 62 | my_fun(X, Y) -> X + Y. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | ERL | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # Have fun! | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | L<Erlang::Parser> is an Erlang source code parser. You can feed C<parse()> any | ||||||||||||||||||||||||||||||||||||||||||
| 70 | fragment of code which would be acceptable at the top-level of a C<.erl> file, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | including a full file. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head2 Methods | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =item C<parse> | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Parses an top-level Erlang declarations from a string, list of lines of code, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | or filehandle. Returns a list of top-level nodes. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | my @nodes = Erlang::Parser->parse( | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 'myfun(X) -> X + X.', | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 'myfun(X, Y) -> X + Y.', | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =item C<error> | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | Called when an error occurs. Reports based on the parser given as the first | ||||||||||||||||||||||||||||||||||||||||||
| 90 | argument. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | Yuki Izumi (anneli@cpan.org) | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | You can find documentation for L<Erlang::Parser> with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | perldoc Erlang::Parser | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | Other places of interest: | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =item * GitHub: source code repository | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | L<http://github.com/unnali/Erlang--Parser> | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =item * GitHub: open an issue | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | L<http://github.com/unnali/Erlang--Parser/issues> | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | =item * Mailing list | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | L<http://groups.google.com/group/erlang--parser-devel>, <erlang--parser-devel@googlegroups.com> | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | =item * Twitter: the author | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | L<http://twitter.com/unnali> | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | Copyright (c) 2011-2012, Yuki Izumi C<< <ANNELI@CPAN.org> >>. All rights | ||||||||||||||||||||||||||||||||||||||||||
| 129 | reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | This module is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||||||||||
| 132 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # vim: set sw=4 ts=4: | ||||||||||||||||||||||||||||||||||||||||||