| 1 | use 5.010; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package MarpaX::Repa; | ||
| 6 | our $VERSION = '0.11'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | MarpaX::Repa - helps start with Marpa | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | Shipped with distribution - F<examples/synopsis.pl>: | ||
| 15 | |||
| 16 | use 5.010; | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use lib 'lib/'; | ||
| 20 | |||
| 21 | use Marpa::R2; | ||
| 22 | use MarpaX::Repa::Lexer; | ||
| 23 | use MarpaX::Repa::Actions; | ||
| 24 | |||
| 25 | my $grammar = Marpa::R2::Grammar->new( { | ||
| 26 | action_object => 'MarpaX::Repa::Actions', | ||
| 27 | default_action => 'do_scalar_or_list', | ||
| 28 | start => 'query', | ||
| 29 | rules => [ | ||
| 30 | { | ||
| 31 | lhs => 'query', rhs => [qw(condition)], | ||
| 32 | min => 1, separator => 'OP', proper => 1, keep => 1, | ||
| 33 | }, | ||
| 34 | [ condition => [qw(word)] ], | ||
| 35 | [ condition => [qw(quoted)] ], | ||
| 36 | [ condition => [qw(OPEN-PAREN SPACE? query SPACE? CLOSE-PAREN)] ], | ||
| 37 | [ condition => [qw(NOT condition)] ], | ||
| 38 | |||
| 39 | [ 'SPACE?' => [] ], | ||
| 40 | { lhs => 'SPACE?', rhs => [qw(SPACE)], action => 'do_ignore', }, | ||
| 41 | ], | ||
| 42 | }); | ||
| 43 | $grammar->precompute; | ||
| 44 | |||
| 45 | use Regexp::Common qw /delimited/; | ||
| 46 | my $lexer = MyLexer->new( | ||
| 47 | tokens => { | ||
| 48 | word => { match => qr{\b\w+\b}, store => 'scalar' }, | ||
| 49 | 'quoted' => { | ||
| 50 | match => qr[$RE{delimited}{-delim=>qq{\"}}], | ||
| 51 | store => sub { | ||
| 52 | ${$_[1]} =~ s/^"//; | ||
| 53 | ${$_[1]} =~ s/"$//; | ||
| 54 | ${$_[1]} =~ s/\\([\\"])/$1/g; | ||
| 55 | return $_[1]; | ||
| 56 | }, | ||
| 57 | }, | ||
| 58 | OP => { match => qr{\s+OR\s+|\s+}, store => sub { ${$_[1]} =~ /\S/? \'|' : \'&' } }, | ||
| 59 | NOT => { match => '!', store => sub {\'!'} }, | ||
| 60 | 'OPEN-PAREN' => { match => '(', store => 'undef' }, | ||
| 61 | 'CLOSE-PAREN' => { match => ')', store => 'undef' }, | ||
| 62 | 'SPACE' => { match => qr{\s+}, store => 'undef' }, | ||
| 63 | }, | ||
| 64 | debug => 1, | ||
| 65 | ); | ||
| 66 | |||
| 67 | my $recognizer = $lexer->recognize( Marpa::R2::Recognizer->new( { grammar => $grammar } ), \*DATA ); | ||
| 68 | |||
| 69 | use Data::Dumper; | ||
| 70 | print Dumper $recognizer->value; | ||
| 71 | |||
| 72 | package MyLexer; | ||
| 73 | use base 'MarpaX::Repa::Lexer'; | ||
| 74 | |||
| 75 | sub grow_buffer { | ||
| 76 | my $self = shift; | ||
| 77 | my $rv = $self->SUPER::grow_buffer( @_ ); | ||
| 78 | ${ $self->buffer } =~ s/[\r\n]+//g; | ||
| 79 | return $rv; | ||
| 80 | } | ||
| 81 | |||
| 82 | package main; | ||
| 83 | __DATA__ | ||
| 84 | hello !world OR "he hehe hee" ( foo OR !boo ) | ||
| 85 | |||
| 86 | =head1 WARNING | ||
| 87 | |||
| 88 | This is experimental module in beta stage. Some API still may change, | ||
| 89 | but it's already very close to stability. | ||
| 90 | |||
| 91 | =head1 DESCRIPTION | ||
| 92 | |||
| 93 | This module helps you start with L<Marpa::R2> parser and simplifies lexing. | ||
| 94 | |||
| 95 | =head1 TUTORIAL | ||
| 96 | |||
| 97 | =head2 Where to start | ||
| 98 | |||
| 99 | Here is template you can start a new parser from | ||
| 100 | (shipped with distribution - F<examples/template.pl>): | ||
| 101 | |||
| 102 | use strict; use warnings; | ||
| 103 | |||
| 104 | use Marpa::R2; | ||
| 105 | use MarpaX::Repa::Lexer; | ||
| 106 | use MarpaX::Repa::Actions; | ||
| 107 | |||
| 108 | my $grammar = Marpa::R2::Grammar->new( { | ||
| 109 | action_object => 'MarpaX::Repa::Actions', | ||
| 110 | start => 'query', | ||
| 111 | rules => [ | ||
| 112 | [ query => [qw(something)] ], | ||
| 113 | ], | ||
| 114 | }); | ||
| 115 | $grammar->precompute; | ||
| 116 | my $lexer = MarpaX::Repa::Lexer->new( | ||
| 117 | tokens => {}, | ||
| 118 | debug => 1, | ||
| 119 | ); | ||
| 120 | |||
| 121 | my $recognizer = $lexer->recognize( Marpa::R2::Recognizer->new( { grammar => $grammar } ), \*DATA); | ||
| 122 | |||
| 123 | __DATA__ | ||
| 124 | hello !world "he hehe hee" ( foo OR boo ) | ||
| 125 | |||
| 126 | It's a working program that prints the following output: | ||
| 127 | |||
| 128 | Expect token(s): 'something' | ||
| 129 | Buffer start: hello !world "he heh... | ||
| 130 | Unknown token: 'something' | ||
| 131 | Failed to parse: Problem in ... | ||
| 132 | |||
| 133 | First line says that at this moment parser expects 'something'. | ||
| 134 | It's going to look for it in the buffer (second line). | ||
| 135 | Third line says that lexer doesn't know anything about token 'something'. | ||
| 136 | It's not a surprise that parsing fails. | ||
| 137 | |||
| 138 | What can we do with 'something'? We either put it into grammar or | ||
| 139 | lexer. In above example it's pretty obvious that it's gonna be in | ||
| 140 | the grammar. | ||
| 141 | |||
| 142 | =head2 Put some grammar | ||
| 143 | |||
| 144 | rules => [ | ||
| 145 | # query is a sequence of conditions separated with OPs | ||
| 146 | { | ||
| 147 | lhs => 'query', rhs => [qw(condition)], | ||
| 148 | min => 1, separator => 'OP', proper => 1, keep => 1, | ||
| 149 | }, | ||
| 150 | # each condition can be one of the following | ||
| 151 | [ condition => [qw(word)] ], | ||
| 152 | [ condition => [qw(quoted)] ], | ||
| 153 | [ condition => [qw(OPEN-PAREN SPACE? query SPACE? CLOSE-PAREN)] ], | ||
| 154 | [ condition => [qw(NOT condition)] ], | ||
| 155 | ], | ||
| 156 | |||
| 157 | Our program works and gives us helpful results: | ||
| 158 | |||
| 159 | Expect token(s): 'word', 'quoted', 'OPEN-PAREN', 'NOT' | ||
| 160 | Buffer start: hello !world OR "he ... | ||
| 161 | Unknown token: 'word' | ||
| 162 | ... | ||
| 163 | |||
| 164 | =head2 First token | ||
| 165 | |||
| 166 | tokens => { | ||
| 167 | word => qr{\w+}, | ||
| 168 | }, | ||
| 169 | |||
| 170 | Ouput: | ||
| 171 | |||
| 172 | Expect token(s): 'word', 'quoted', 'OPEN-PAREN', 'NOT' | ||
| 173 | Buffer start: hello !world OR "he ... | ||
| 174 | Token 'word' matched hello | ||
| 175 | Unknown token: 'quoted' | ||
| 176 | Unknown token: 'OPEN-PAREN' | ||
| 177 | Unknown token: 'NOT' | ||
| 178 | Expect token(s): 'OP' | ||
| 179 | |||
| 180 | Congrats! First token matched. More tokens: | ||
| 181 | |||
| 182 | use Regexp::Common qw /delimited/; | ||
| 183 | |||
| 184 | my $lexer = MarpaX::Repa::Lexer->new( | ||
| 185 | tokens => { | ||
| 186 | word => qr{\b\w+\b}, | ||
| 187 | OP => qr{\s+|\s+OR\s+}, | ||
| 188 | NOT => '!', | ||
| 189 | 'OPEN-PAREN' => '(', | ||
| 190 | 'CLOSE-PAREN' => ')', | ||
| 191 | 'quoted' => qr[$RE{delimited}{-delim=>qq{\"}}], | ||
| 192 | }, | ||
| 193 | debug => 1, | ||
| 194 | ); | ||
| 195 | |||
| 196 | =head2 Tokens matching empty string | ||
| 197 | |||
| 198 | You can not have such. In our example grammar we have 'SPACE?' that | ||
| 199 | is optional. You could try to use C<qr{\s*}>, but lexer would die | ||
| 200 | with an error. Instead use the following rules: | ||
| 201 | |||
| 202 | rules => [ | ||
| 203 | ... | ||
| 204 | [ 'SPACE?' => [] ], | ||
| 205 | [ 'SPACE?' => [qw(SPACE)] ], | ||
| 206 | ], | ||
| 207 | ... | ||
| 208 | tokens => { | ||
| 209 | ... | ||
| 210 | 'SPACE' => qr{\s+}, | ||
| 211 | }, | ||
| 212 | |||
| 213 | =head2 Lexer's ambiguity | ||
| 214 | |||
| 215 | This module uses marpa's alternative input model what allows you to | ||
| 216 | describe an ambiguous lexer, e.g. several tokens start at the same | ||
| 217 | position. This does not always give you multiple parse trees, but | ||
| 218 | allows you to start faster and keep improving tokens and grammar | ||
| 219 | to avoid unnecessary ambiguity cases. | ||
| 220 | |||
| 221 | =head2 Longest token match | ||
| 222 | |||
| 223 | Let's look at string "x OR y". It should match "word OP word", | ||
| 224 | but it matches "word OP word OP word" and it's not correct. | ||
| 225 | It happens because of how we defined OP token - C<qr{\s+|\s+OR\s+}>. | ||
| 226 | If we change it to C<qr{\s+OR\s+|\s+}> then we get correct result. | ||
| 227 | |||
| 228 | =head2 Input buffer | ||
| 229 | |||
| 230 | By default lexer reads data from the input stream in chunks into | ||
| 231 | a buffer and grow the buffer only when it's shorter than | ||
| 232 | C<min_buffer> bytes. By default it's 4kb. This is good for memory | ||
| 233 | consuption, but it can result in troubles when a terminal may be | ||
| 234 | larger than a buffer. For example consider a document with embedded | ||
| 235 | base64 encoded binary files. You can use several solutions to | ||
| 236 | workaround this problem. | ||
| 237 | |||
| 238 | Read everything into memory. Simplest way out. It's not default | ||
| 239 | value to avoid encouragement: | ||
| 240 | |||
| 241 | my $lexer = MarpaX::Repa::Lexer->new( | ||
| 242 | min_buffer => 0, | ||
| 243 | ... | ||
| 244 | ); | ||
| 245 | |||
| 246 | Use larger buffer: | ||
| 247 | |||
| 248 | my $lexer = MarpaX::Repa::Lexer->new( | ||
| 249 | min_buffer => 10*1024*1024, # 10MB | ||
| 250 | ... | ||
| 251 | ); | ||
| 252 | |||
| 253 | Use built in protection from such cases. When a token based on a | ||
| 254 | regular expression matches whole buffer and buffer still can grow | ||
| 255 | then lexer grows buffer and retries. This allows you to write a regular | ||
| 256 | expression that matches till end of token or end of input (C<$>). | ||
| 257 | Note that this may result in token incomplete match if input ends | ||
| 258 | right in the middle of it. | ||
| 259 | |||
| 260 | tokens => { | ||
| 261 | ... | ||
| 262 | 'text-paragraph' => qr{\w[\w\s]+?(?:\n\n|$)}, | ||
| 263 | }, | ||
| 264 | |||
| 265 | Adjust grammar. In most cases you can split a long terminal into | ||
| 266 | multiple terminals with limitted length. For example: | ||
| 267 | |||
| 268 | rules => [ | ||
| 269 | ... | ||
| 270 | { lhs => 'text', rhs => 'text-chunk', min => 1 }, | ||
| 271 | ], | ||
| 272 | |||
| 273 | =head2 Filtering input | ||
| 274 | |||
| 275 | Input can be filtered with subclassing grow_buffer method: | ||
| 276 | |||
| 277 | package MyLexer; | ||
| 278 | use base 'MarpaX::Repa::Lexer'; | ||
| 279 | |||
| 280 | sub grow_buffer { | ||
| 281 | my $self = shift; | ||
| 282 | my $rv = $self->SUPER::grow_buffer( @_ ); | ||
| 283 | ${ $self->buffer } =~ s/[\r\n]+//g; | ||
| 284 | return $rv; | ||
| 285 | } | ||
| 286 | |||
| 287 | =head2 Actions | ||
| 288 | |||
| 289 | Repa comes with set of actions to help you start by concentrating | ||
| 290 | on grammar. Start from <MarpaX::Repa::Actions/do_what_I_mean>: | ||
| 291 | |||
| 292 | my $grammar = Marpa::R2::Grammar->new( { | ||
| 293 | action_object => 'MarpaX::Repa::Actions', | ||
| 294 | default_action => 'do_what_I_mean', | ||
| 295 | ... | ||
| 296 | ); | ||
| 297 | |||
| 298 | =head2 Token's values | ||
| 299 | |||
| 300 | Values of tokens are set to a hash by default: | ||
| 301 | |||
| 302 | { token => 'a token name', value => 'matched value' } | ||
| 303 | |||
| 304 | You can change format per token or for all tokens using | ||
| 305 | 'store' option, see L</SYNOPSIS> for examples and | ||
| 306 | L<MarpaX::Repa::Lexer> for full list. | ||
| 307 | |||
| 308 | =head2 What's next | ||
| 309 | |||
| 310 | Add more actions. Experiment. Enjoy. | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | =head1 AUTHOR | ||
| 315 | |||
| 316 | Ruslan Zakirov E<lt>Ruslan.Zakirov@gmail.comE<gt> | ||
| 317 | |||
| 318 | =head1 LICENSE | ||
| 319 | |||
| 320 | Under the same terms as perl itself. | ||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | 1; |