line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
1
|
|
|
1
|
|
29872
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
42
|
|
8
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
614
|
use Erlang::Parser::Lexer; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
11
|
1
|
|
|
1
|
|
1193
|
use Erlang::Parser::Parser; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub parse { |
14
|
|
|
|
|
|
|
my $class = shift; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $parser = new Erlang::Parser::Parser; |
17
|
|
|
|
|
|
|
my $lexerfn = Erlang::Parser::Lexer->lex(@_); |
18
|
|
|
|
|
|
|
@{$parser->YYParse(yylex => $lexerfn, yyerror => \&error)}; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub error { |
22
|
|
|
|
|
|
|
print STDERR "Parse error!\n"; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
if ($_ && $_[0] && ref($_[0]) eq 'ARRAY') { |
25
|
|
|
|
|
|
|
print STDERR "Failed token was ", $_[0]->YYCurtok; |
26
|
|
|
|
|
|
|
print STDERR ", value ", $_[0]->YYCurval; |
27
|
|
|
|
|
|
|
print STDERR ", expected ", join(',', $_[0]->YYExpect); |
28
|
|
|
|
|
|
|
print STDERR ".\n"; |
29
|
|
|
|
|
|
|
} else { |
30
|
|
|
|
|
|
|
print STDERR "Empty object tree!\n"; |
31
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
1; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# vim: set sw=4 ts=4: |