-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.pm.skel
222 lines (144 loc) · 4.39 KB
/
Parser.pm.skel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package <<$package>>;
# ABSTRACT: SPF v1 parser implementation
####################################################################
#
# This file was generated using Parse::Yapp version <<$version>>.
#
# Don't edit this file, use source file instead.
#
# ANY CHANGE MADE HERE WILL BE LOST !
#
####################################################################
use strict;
use warnings;
# VERSION
# AUTHORITY
use vars qw ( @ISA );
@ISA = qw( Parse::Yapp::Driver );
<<$driver>>
<<$head>>
=head1 SYNOPSIS
use <<$package>>;
$parser = <<$package>>->new;
$ast = $parser->parse( 'v=spf1 a include:_spf.example.com ~all' );
unless ( $ast ) {
# fail
print "Error: " . $parser->error->{code} . ": " . $parser->error->{text} . "\n";
}
else {
# ok
...
}
=method new
Creates an instance of SPF parser.
my $parser = <<$package>>->new;
=cut
sub new {
my( $class ) = shift;
ref( $class ) and $class = ref( $class );
my $self =
$class->SUPER::new(
yyversion => '<<$version>>',
yystates => <<$states>>,
yyrules => <<$rules>>,
@_
);
bless $self, $class;
}
=method parse
Builds an abstract syntax tree (AST) for given text representation of SPF.
my $ast = $parser->parse( 'v=spf1 ~all' );
Returns an C<undef> if error occured. See L</error> for details.
=method raise_error
Raises a parser error.
$parser->raise_error( $error_code, $context, @extra );
$parser->raise_error( 'E_FOO', 'context line', qw( bar baz ) );
Arguments are:
=over 4
=item B<$error_code>
Error code. If code does not exist in error table it will be replaced with L</E_DEFAULT>.
=item B<$context>
Context line.
=item B<@extra>
Extra parameters for error text.
=back
=method error
Returns last error occured as HashRef.
$parser->error;
Here is an example
{
code => "E_DEFAULT",
text => "Just error",
context => "",
}
=for Pod::Coverage _error _lexer _build_error _ver_generic _mod_generic
=for Pod::Coverage _mech_generic _mech_domain _mech_domain_bitmask _mech_ipaddr_bitmask
=head1 ERROR HANDLING
The following errors might be returned.
=head2 E_SYNTAX
Syntax error. The marker pointed to errored token in context line. E.g.:
{
code => "E_SYNTAX",
context => "v=spf1 <*>exclude:foo.example.com mx ~all",
text => "Syntax error near token 'exclude'",
}
=head2 E_INVALID_VERSION
Returned in cases of version token does not equal C<spf1>.
{
code => "E_INVALID_VERSION",
text => "Invalid SPF version",
context => "v=spf2",
}
=head2 E_IPADDR_EXPECTED
Returned in cases of C<ip4> or C<ip6> token has been used without ip or network address.
{
code => "E_IPADDR_EXPECTED",
text => "Expected ip or network address",
context => "ip4",
}
=head2 E_DOMAIN_EXPECTED
Returned in cases of C<exists> or C<include> token has been used without domain name.
{
code => "E_DOMAIN_EXPECTED",
text => "Expected domain name",
context => "exists",
}
=head2 E_UNEXPECTED_BITMASK
Returned in cases of C<ptr> or C<all> token has been used with bitmask.
{
code => "E_UNEXPECTED_BITMASK",
text => "Unexpected bitmask",
context => "?ptr:foo.net/18",
}
=head2 E_UNEXPECTED_IPADDR
Returned in cases of C<ptr> or C<all> token has been used with ip or network address.
{
code => "E_UNEXPECTED_IPADDR",
text => "Unexpected ip address",
context => "-ptr:127.0.0.1",
}
=head2 E_UNEXPECTED_DOMAIN
Returned in cases of C<all> token has been used with domain name.
{
code => "E_UNEXPECTED_DOMAIN",
text => "Unexpected domain name",
context => "-all:quux.com",
}
=head2 E_DEFAULT
Default (last resort) error.
{
code => "E_DEFAULT",
text => "Just error",
context => "",
}
=head1 BUILD PARSER
In cases of C<Parser.yp> was modified you should re-build this module. Ensure you have L<Parse::Yapp>
distribution installed.
In root directory:
$ yapp -s -m Validate::SPF::Parser -o lib/Validate/SPF/Parser.pm -t Parser.pm.skel Parser.yp
Ensure the C<lib/Validate/SPF/Parser.pm> saved without tab symbols and has unix line endings.
=head1 SEE ALSO
L<Parse::Yapp>
=cut
<<$tail>>
1;