| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# Copyright 2011-2016 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
|
3
|
|
|
3
|
|
13757
|
use strict; |
|
|
3
|
|
|
|
|
3
|
|
|
|
3
|
|
|
|
|
79
|
|
|
8
|
3
|
|
|
3
|
|
11
|
use warnings; |
|
|
3
|
|
|
|
|
3
|
|
|
|
3
|
|
|
|
|
69
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
1091
|
use Erlang::Parser::Lexer; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
120
|
|
|
11
|
3
|
|
|
3
|
|
2699
|
use Erlang::Parser::Parser; |
|
|
3
|
|
|
|
|
9
|
|
|
|
3
|
|
|
|
|
576
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub parse { |
|
14
|
2
|
|
|
2
|
1
|
1136
|
my $class = shift; |
|
15
|
|
|
|
|
|
|
|
|
16
|
2
|
|
|
|
|
15
|
my $parser = new Erlang::Parser::Parser; |
|
17
|
2
|
|
|
|
|
14
|
my $lexerfn = Erlang::Parser::Lexer->lex(@_); |
|
18
|
2
|
|
|
|
|
4
|
@{$parser->YYParse(yylex => $lexerfn, yyerror => \&error)}; |
|
|
2
|
|
|
|
|
15
|
|
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub error { |
|
22
|
0
|
|
|
0
|
1
|
|
print STDERR "Parse error!\n"; |
|
23
|
|
|
|
|
|
|
|
|
24
|
0
|
0
|
0
|
|
|
|
if ($_ && $_[0] && ref($_[0]) eq 'ARRAY') { |
|
|
|
|
0
|
|
|
|
|
|
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.6 of Erlang::Parser released 2016-06-20. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
our $VERSION = '0.6'; |
|
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/kivikakk/Erlang--Parser> |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item * GitHub: open an issue |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
L<http://github.com/kivikakk/Erlang--Parser/issues> |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item * Twitter: the author |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
L<http://twitter.com/kivikakk> |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=back |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Copyright (c) 2011-2016, Yuki Izumi C<< <ANNELI@CPAN.org> >>. All rights |
|
125
|
|
|
|
|
|
|
reserved. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or |
|
128
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. See L<perlartistic>. |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# vim: set sw=4 ts=4: |