Skip to content

Commit 95fd85d

Browse files
committed
Unify Type/Grammar.pod and Language/grammars.pod on methods. See issue #58.
1 parent 6a83d1f commit 95fd85d

File tree

2 files changed

+56
-102
lines changed

2 files changed

+56
-102
lines changed

lib/Language/grammars.pod

Lines changed: 4 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -70,107 +70,12 @@ into effect for the Regex:
7070
7171
=SUBTITLE Group of named regexes that form a formal grammar
7272
73-
class Grammar is Cursor { }
74-
75-
C<Grammar> is the superclass that classes automatically get when they are
76-
declared with the C<grammar> keyword instead of C<class>. Grammars should
77-
only be used to parse text; if you wish to extract complex data, an L<action
78-
object|/language/grammars#Action_Objects> is recommended to be used in
73+
L<Grammar|/type/Grammar> is the superclass that classes automatically get when
74+
they are declared with the C<grammar> keyword instead of C<class>. Grammars
75+
should only be used to parse text; if you wish to extract complex data, an
76+
L<action object|/language/grammars#Action_Objects> is recommended to be used in
7977
conjunction with the grammar.
8078
81-
=begin code :allow<B L>
82-
B<grammar> CSV {
83-
token TOP { [ <line> \n? ]+ }
84-
token line {
85-
^^ # Beginning of a line
86-
<value>* % \, # Any number of <value>s with commas in between them
87-
$$ # End of a line
88-
}
89-
token value {
90-
[
91-
| <-[",\n]> # Anything not a double quote, comma or newline
92-
| <quoted-text> # Or some quoted text
93-
]* # Any number of times
94-
}
95-
token quoted-text {
96-
\"
97-
[
98-
| <-["\\]> # Anything not a " or \
99-
| '\"' # Or \", an escaped quotation mark
100-
]* # Any number of times
101-
\"
102-
}
103-
}
104-
105-
say "Valid CSV file!" if CSV.L<parse>( q:to/EOCSV/ );
106-
Year,Make,Model,Length
107-
1997,Ford,E350,2.34
108-
2000,Mercury,Cougar,2.38
109-
EOCSV
110-
=end code
111-
112-
=head2 Methods
113-
114-
=head3 method parse
115-
116-
method parse($str, :$rule = 'TOP', :$actions) returns Match:D
117-
118-
Matches the grammar against C<$str>, using C<$rule> as the starting rule,
119-
optionally applying C<$actions> as its actions object.
120-
121-
This will fail if the grammar does not parse the I<entire> string. If a
122-
parse of only a part of the string is desired, use L<subparse>.
123-
124-
The method returns the resulting L<Match> object and also sets the caller's
125-
C<$/> variable to the Match object.
126-
127-
=begin code :allow<B>
128-
say CSVB<.parse>( q:to/EOCSV/ );
129-
Year,Make,Model,Length
130-
1997,Ford,E350,2.34
131-
2000,Mercury,Cougar,2.38
132-
EOCSV
133-
=end code
134-
135-
This outputs:
136-
137-
「Year,Make,Model,Length
138-
1997,Ford,E350,2.34
139-
2000,Mercury,Cougar,2.38
140-
141-
line => 「Year,Make,Model,Length」
142-
value => 「Year」
143-
value => 「Make」
144-
value => 「Model」
145-
value => 「Length」
146-
line => 「1997,Ford,E350,2.34」
147-
value => 「1997」
148-
value => 「Ford」
149-
value => 「E350」
150-
value => 「2.34」
151-
line => 「2000,Mercury,Cougar,2.38 」
152-
value => 「2000」
153-
value => 「Mercury」
154-
value => 「Cougar」
155-
value => 「2.38 」
156-
157-
=head3 method subparse
158-
159-
method subparse($str, :$rule = 'TOP', :$actions) returns Match:D
160-
161-
Matches the grammar against C<$str>, using C<$rule> as the starting rule,
162-
optionally applying C<$actions> as its actions object.
163-
164-
Unlike L<parse>, C<subparse> will allow the grammar to match only part of
165-
the supplied string.
166-
167-
=head3 method parsefile
168-
169-
method parsefile(Str(Cool) $filename, *%opts) returns Match:D
170-
171-
Parses the contents of the file C<$filename> with the L<parse> method,
172-
passing any named options in C<%opts>.
173-
17479
=head1 Action Objects
17580
17681
A successful grammar match gives you a parse tree of L<Match|/type/Match>

lib/Type/Grammar.pod

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ More L<documentation on grammars|/language/grammars> is available.
2727
2828
=head2 method parse
2929
30-
method parse($target, :$rule = 'TOP', Parcel :$args = (), Mu :$actions, *%opts)
30+
method parse($target, :$rule = 'TOP', Parcel :$args = (), Mu :$actions = Mu, *%opt) {
3131
3232
Parses the C<$target> (which will be coerced to L<Str|/type/Str> if it isn't
3333
one), using C<$rule> as the starting rule. Additional C<$args> will be given
@@ -49,9 +49,58 @@ if you want to be able to stop in the middle.
4949
Returns a L<Match object|/type/Match> on success, and L<Nil|/type/Nil> on
5050
failure.
5151
52+
grammar CSV {
53+
token TOP { [ <line> \n? ]+ }
54+
token line {
55+
^^ # Beginning of a line
56+
<value>* % \, # Any number of <value>s with commas in between them
57+
$$ # End of a line
58+
}
59+
token value {
60+
[
61+
| <-[",\n]> # Anything not a double quote, comma or newline
62+
| <quoted-text> # Or some quoted text
63+
]* # Any number of times
64+
}
65+
token quoted-text {
66+
\"
67+
[
68+
| <-["\\]> # Anything not a " or \
69+
| '\"' # Or \", an escaped quotation mark
70+
]* # Any number of times
71+
\"
72+
}
73+
}.parse( q:to/EOCSV/ ).say;
74+
Year,Make,Model,Length
75+
1997,Ford,E350,2.34
76+
2000,Mercury,Cougar,2.38
77+
EOCSV
78+
79+
This outputs:
80+
81+
「Year,Make,Model,Length
82+
1997,Ford,E350,2.34
83+
2000,Mercury,Cougar,2.38
84+
85+
line => 「Year,Make,Model,Length」
86+
value => 「Year」
87+
value => 「Make」
88+
value => 「Model」
89+
value => 「Length」
90+
line => 「1997,Ford,E350,2.34」
91+
value => 「1997」
92+
value => 「Ford」
93+
value => 「E350」
94+
value => 「2.34」
95+
line => 「2000,Mercury,Cougar,2.38 」
96+
value => 「2000」
97+
value => 「Mercury」
98+
value => 「Cougar」
99+
value => 「2.38 」
100+
52101
=head2 method subparse
53102
54-
method subparse($target, :$rule = 'TOP', Parcel :$args = (), Mu :$actions, *%opts)
103+
method subparse($target, :$rule = 'TOP', Parcel :$args = (), Mu :$actions = Mu, *%opt) {
55104
56105
Does exactly the same as L<method parse|/routine/parse>, except that cursor
57106
doesn't have to reach the end of the string to succeed. (That is, it doesn't
@@ -66,7 +115,7 @@ have to match the whole string).
66115
67116
=head2 method parsefile
68117
69-
method parsefile(Cool $filename, *%opts);
118+
method parsefile(Str(Cool) $filename, :$enc, *%opts) {
70119
71120
Reads file C<$filename>, and parses it. All named arguments are passsed on to
72121
L<method parse|/routine/parse>.

0 commit comments

Comments
 (0)