1Pegex::Receiver(3) User Contributed Perl Documentation Pegex::Receiver(3)
2
3
4
6 Pegex::Receiver - Base Class for All Pegex Receivers
7
9 package MyReceiver;
10 use base 'Pegex::Receiver';
11
12 # Handle data for a specific rule
13 sub got_somerulename {
14 my ($self, $got) = @_;
15 # ... process ...
16 return $result;
17 }
18
19 # Handle data for any other rule
20 sub gotrule {
21 my ($self, $got) = @_;
22 return $result;
23 }
24
25 # Pre-process
26 sub initial { ... }
27
28 # Post-process
29 sub final {
30 ...;
31 return $final_result;
32 }
33
35 In Pegex, a receiver is the class object that a parser passes captured
36 data to when a rule in a grammar matches a part of an input stream. A
37 receiver provides action methods to turn parsed data into what the
38 parser is intended to do.
39
40 This is the base class of all Pegex receiver classes.
41
42 It doesn't do much of anything, which is the correct thing to do. If
43 you use this class as your receiver if won't do any extra work. See
44 Pegex::Tree for a receiver base class that will help organize your
45 matches by default.
46
47 How A Receiver Works
48 A Pegex grammar is made up of named-rules, regexes, and groups. When a
49 regex matches, the parser makes array of its capture strings. When a
50 group matches, the parser makes an array of all the submatch arrays. In
51 this way a parse tree forms.
52
53 When a named-rule matches, an action method is called in the receiver
54 class. The method is passed the current parse tree and returns what
55 parser will consider the new parse tree.
56
57 This makes for a very elegant and understandable API.
58
60 This section documents the methods that you can include in receiver
61 subclass.
62
63 "got_$rulename($got)"
64 An action method for a specific, named rule.
65
66 sub got_rule42 {
67 my ($self, $got) = @_;
68 ...
69 return $result;
70 }
71
72 The $got value that is passed in is the current value of the parse
73 tree. What gets returned is whatever you want to new value to be.
74
75 "gotrule($got)"
76 The action method for a named rule that does not have a specific
77 action method.
78
79 "initial()"
80 Called at the beginning of a parse operation, before the parsing
81 begins.
82
83 "final($got)"
84 Called at the end of a parse operation. Whatever this action
85 returns, will be the result of the parse.
86
87 Methods
88 "parser"
89 An attribute containing the parser object that is currently
90 running. This can be very useful to introspect what is happening,
91 and possibly modify the grammar on the fly. (Experts only!)
92
93 "flatten($array)"
94 A utility method that can turn an array of arrays into a single
95 array. For example:
96
97 $self->flatten([1, [2, [3, 4], 5], 6]);
98 # produces [1, 2, 3, 4, 5, 6]
99
100 Hashes are left unchanged. The array is modified in place, but is
101 also the return value.
102
104 Ingy döt Net <ingy@cpan.org>
105
107 Copyright 2010-2020. Ingy döt Net.
108
109 This program is free software; you can redistribute it and/or modify it
110 under the same terms as Perl itself.
111
112 See <http://www.perl.com/perl/misc/Artistic.html>
113
114
115
116perl v5.34.0 2021-07-22 Pegex::Receiver(3)