@@ -70,107 +70,12 @@ into effect for the Regex:
70
70
71
71
= SUBTITLE Group of named regexes that form a formal grammar
72
72
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
79
77
conjunction with the grammar.
80
78
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
-
174
79
= head1 Action Objects
175
80
176
81
A successful grammar match gives you a parse tree of L < Match|/type/Match >
0 commit comments