Package lepl :: Package core :: Module config
[hide private]
[frames] | no frames]

Source Code for Module lepl.core.config

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  The main configuration object and various standard configurations. 
 32  ''' 
 33   
 34  # pylint bug? 
 35  # pylint: disable-msg=W0404 
 36   
 37  from collections import namedtuple 
 38   
 39  from lepl.core.parser import make_raw_parser, make_single, make_multiple 
 40  from lepl.stream.factory import DEFAULT_STREAM_FACTORY 
 41   
 42   
 43  Configuration = namedtuple('Configuration',  
 44                             'rewriters monitors stream_factory stream_kargs') 
 45  '''Carrier for configuration.''' 
46 47 48 -class ConfigurationError(Exception):
49 ''' 50 Error raised for problems with configuration. 51 ''' 52 pass
53
54 55 -class ConfigBuilder(object):
56 ''' 57 Accumulate configuration through chained methods. 58 ''' 59
60 - def __init__(self, matcher):
61 # we need to delay startup, to avoid loops 62 self.__started = False 63 # this is set whenever any config is changed. it is cleared when 64 # the configuration is read. so if is is false then the configuration 65 # is the same as previously read 66 self.__changed = True 67 self.__rewriters = set() 68 self.__monitors = [] 69 self.__stream_factory = DEFAULT_STREAM_FACTORY 70 self.__alphabet = None 71 self.__stream_kargs = {} 72 # this is set from the matcher. it gives a memory loop, but not a 73 # very serious one, and allows single line configuration which is 74 # useful for timing. 75 self.matcher = matcher
76
77 - def __start(self):
78 ''' 79 Set default values on demand to avoid dependency loop. 80 ''' 81 if not self.__started: 82 self.__started = True 83 self.default()
84 85 86 # raw access to basic components 87
88 - def add_rewriter(self, rewriter):
89 ''' 90 Add a rewriter that will be applied to the matcher graph when the 91 parser is generated. 92 ''' 93 self.__start() 94 self.clear_cache() 95 # we need to remove before adding to ensure last added is the one 96 # used (exclusive rewriters are equal) 97 if rewriter in self.__rewriters: 98 self.__rewriters.remove(rewriter) 99 self.__rewriters.add(rewriter) 100 return self
101
102 - def remove_rewriter(self, rewriter):
103 ''' 104 Remove a rewriter from the current configuration. 105 ''' 106 self.__start() 107 self.clear_cache() 108 self.__rewriters = set(r for r in self.__rewriters 109 if r is not rewriter) 110 return self
111
112 - def remove_all_rewriters(self, type_=None):
113 ''' 114 Remove all rewriters of a given type from the current configuration. 115 ''' 116 self.__start() 117 self.clear_cache() 118 if type_: 119 self.__rewriters = set(r for r in self.__rewriters 120 if not isinstance(r, type_)) 121 else: 122 self.__rewriters = set() 123 return self
124
125 - def add_monitor(self, monitor):
126 ''' 127 Add a monitor to the current configuration. Monitors are called 128 from within the trampolining process and can be used to track 129 evaluation, control resource use, etc. 130 ''' 131 self.__start() 132 self.clear_cache() 133 self.__monitors.append(monitor) 134 return self
135
136 - def remove_all_monitors(self):
137 ''' 138 Remove all monitors from the current configuration. 139 ''' 140 self.__start() 141 self.clear_cache() 142 self.__monitors = [] 143 return self
144
145 - def stream_factory(self, stream_factory=DEFAULT_STREAM_FACTORY):
146 ''' 147 Specify the stream factory. This is used to generate the input stream 148 for the parser. 149 ''' 150 self.__start() 151 self.clear_cache() 152 self.__stream_factory = stream_factory 153 return self
154
155 - def add_stream_kargs(self, ** kargs):
156 ''' 157 Add a value for passing to the stream factory. 158 ''' 159 for name in kargs: 160 self.__stream_kargs[name] = kargs[name] 161 return self
162
163 - def remove_all_stream_kargs(self):
164 ''' 165 Remove all values passed to the stream factory. 166 ''' 167 self.__stream_kargs = {}
168 169 @property
170 - def configuration(self):
171 ''' 172 The current configuration (rewriters, monitors, stream_factory). 173 ''' 174 self.__start() 175 self.__changed = False 176 rewriters = list(self.__rewriters) 177 rewriters.sort() 178 return Configuration(rewriters, list(self.__monitors), 179 self.__stream_factory, dict(self.__stream_kargs))
180
181 - def __get_alphabet(self):
182 ''' 183 Get the alphabet used. 184 185 Typically this is Unicode, which is the default. It is needed for 186 the generation of regular expressions. 187 ''' 188 from lepl.regexp.unicode import UnicodeAlphabet 189 if not self.__alphabet: 190 self.__alphabet = UnicodeAlphabet.instance() 191 return self.__alphabet
192
193 - def alphabet(self, alphabet):
194 ''' 195 Set the alphabet used. It is needed for the generation of regular 196 expressions, for example (but the default, for Unicode, is usually 197 sufficient). 198 ''' 199 if alphabet: 200 if self.__alphabet: 201 if self.__alphabet != alphabet: 202 raise ConfigurationError( 203 'Alphabet has changed during configuration ' 204 '(perhaps the default was already used?)') 205 else: 206 self.__alphabet = alphabet 207 self.__start() 208 self.clear_cache()
209 210 @property
211 - def changed(self):
212 ''' 213 Has the config been changed by the user since it was last returned 214 via `configuration`? if not, any previously generated parser can be 215 reused. 216 ''' 217 return self.__changed
218
219 - def clear_cache(self):
220 ''' 221 Force calculation of a new parser. 222 ''' 223 self.__changed = True
224 225 # rewriters 226
227 - def set_arguments(self, type_, **kargs):
228 ''' 229 Set the given keyword arguments on all matchers of the given `type_` 230 (ie class) in the grammar. 231 ''' 232 from lepl.core.rewriters import SetArguments 233 return self.add_rewriter(SetArguments(type_, **kargs))
234
235 - def no_set_arguments(self):
236 ''' 237 Remove all rewriters that set arguments. 238 ''' 239 from lepl.core.rewriters import SetArguments 240 return self.remove_all_rewriters(SetArguments)
241
242 - def set_alphabet_arg(self, alphabet=None):
243 ''' 244 Set `alphabet` on various matchers. This is useful when using an 245 unusual alphabet (most often when using line-aware parsing), as 246 it saves having to specify it on each matcher when creating the 247 grammar. 248 ''' 249 from lepl.regexp.matchers import BaseRegexp 250 from lepl.lexer.matchers import BaseToken 251 if alphabet: 252 self.alphabet(alphabet) 253 else: 254 alphabet = self.__get_alphabet() 255 if not alphabet: 256 raise ValueError('An alphabet must be provided or already set') 257 self.set_arguments(BaseRegexp, alphabet=alphabet) 258 self.set_arguments(BaseToken, alphabet=alphabet) 259 return self
260
261 - def full_first_match(self, eos=True):
262 ''' 263 Raise an error if the first match fails. If `eos` is True then this 264 requires that the entire input is matched, otherwise it only requires 265 that the matcher succeed. The exception includes information about 266 the deepest read to the stream (which is a good indication of where 267 any error occurs). 268 269 This is part of the default configuration. It can be removed with 270 `no_full_first_match()`. 271 ''' 272 from lepl.core.rewriters import FullFirstMatch 273 return self.add_rewriter(FullFirstMatch(eos))
274
275 - def no_full_first_match(self):
276 ''' 277 Disable the automatic generation of an error if the first match fails. 278 ''' 279 from lepl.core.rewriters import FullFirstMatch 280 return self.remove_all_rewriters(FullFirstMatch)
281
282 - def flatten(self):
283 ''' 284 Combined nested `And()` and `Or()` matchers. This does not change 285 the parser semantics, but improves efficiency. 286 287 This is part of the default configuration. It can be removed with 288 `no_flatten`. 289 ''' 290 from lepl.core.rewriters import Flatten 291 return self.add_rewriter(Flatten())
292
293 - def no_flatten(self):
294 ''' 295 Disable the combination of nested `And()` and `Or()` matchers. 296 ''' 297 from lepl.core.rewriters import Flatten 298 return self.remove_all_rewriters(Flatten)
299
300 - def compile_to_dfa(self, force=False, alphabet=None):
301 ''' 302 Compile simple matchers to DFA regular expressions. This improves 303 efficiency but may change the parser semantics slightly (DFA regular 304 expressions do not provide backtracking / alternative matches). 305 ''' 306 from lepl.regexp.matchers import DfaRegexp 307 from lepl.regexp.rewriters import CompileRegexp 308 self.alphabet(alphabet) 309 return self.add_rewriter( 310 CompileRegexp(self.__get_alphabet(), force, DfaRegexp))
311
312 - def compile_to_nfa(self, force=False, alphabet=None):
313 ''' 314 Compile simple matchers to NFA regular expressions. This improves 315 efficiency and should not change the parser semantics. 316 317 This is part of the default configuration. It can be removed with 318 `no_compile_regexp`. 319 ''' 320 from lepl.regexp.matchers import NfaRegexp 321 from lepl.regexp.rewriters import CompileRegexp 322 self.alphabet(alphabet) 323 return self.add_rewriter( 324 CompileRegexp(self.__get_alphabet(), force, NfaRegexp))
325
326 - def compile_to_re(self, force=False, alphabet=None):
327 ''' 328 Compile simple matchers to re (C library) regular expressions. 329 This improves efficiency but may change the parser semantics slightly 330 (DFA regular expressions do not provide backtracking / alternative 331 matches). 332 ''' 333 from lepl.matchers.core import Regexp 334 from lepl.regexp.rewriters import CompileRegexp 335 def regexp_wrapper(regexp, _alphabet): 336 ''' 337 Adapt the Regexp matcher to the form needed (forcing Unicode). 338 ''' 339 return Regexp(str(regexp))
340 self.alphabet(alphabet) 341 return self.add_rewriter( 342 CompileRegexp(self.__get_alphabet(), force, regexp_wrapper))
343
344 - def no_compile_to_regexp(self):
345 ''' 346 Disable compilation of simple matchers to regular expressions. 347 ''' 348 from lepl.regexp.rewriters import CompileRegexp 349 return self.remove_all_rewriters(CompileRegexp)
350
351 - def optimize_or(self, conservative=False):
352 ''' 353 Rearrange arguments to `Or()` so that left-recursive matchers are 354 tested last. This improves efficiency, but may alter the parser 355 semantics (the ordering of multiple results with ambiguous grammars 356 may change). 357 358 `conservative` refers to the algorithm used to detect loops; False 359 may classify some left--recursive loops as right--recursive. 360 ''' 361 from lepl.core.rewriters import OptimizeOr 362 return self.add_rewriter(OptimizeOr(conservative))
363
364 - def no_optimize_or(self):
365 ''' 366 Disable the re-ordering of some `Or()` arguments. 367 ''' 368 from lepl.core.rewriters import OptimizeOr 369 return self.remove_all_rewriters(OptimizeOr)
370
371 - def lexer(self, alphabet=None, discard=None, lexer=None):
372 ''' 373 Detect the use of `Token()` and modify the parser to use the lexer. 374 If tokens are not used, this has no effect on parsing. 375 376 This is part of the default configuration. It can be disabled with 377 `no_lexer`. 378 ''' 379 from lepl.lexer.rewriters import AddLexer 380 self.alphabet(alphabet) 381 return self.add_rewriter( 382 AddLexer(alphabet=self.__get_alphabet(), 383 discard=discard, lexer=lexer))
384
385 - def no_lexer(self):
386 ''' 387 Disable support for the lexer. 388 ''' 389 from lepl.lexer.rewriters import AddLexer 390 self.remove_all_rewriters(AddLexer)
391
392 - def direct_eval(self, spec=None):
393 ''' 394 Combine simple matchers so that they are evaluated without 395 trampolining. This improves efficiency (particularly because it 396 reduces the number of matchers that can be memoized). 397 398 This is part of the default configuration. It can be removed with 399 `no_direct_eval`. 400 ''' 401 from lepl.core.rewriters import DirectEvaluation 402 return self.add_rewriter(DirectEvaluation(spec))
403
404 - def no_direct_eval(self):
405 ''' 406 Disable direct evaluation. 407 ''' 408 from lepl.core.rewriters import DirectEvaluation 409 return self.remove_all_rewriters(DirectEvaluation)
410
411 - def compose_transforms(self):
412 ''' 413 Combine transforms (functions applied to results) with matchers. 414 This may improve efficiency. 415 416 This is part of the default configuration. It can be removed with 417 `no_compose_transforms`. 418 ''' 419 from lepl.core.rewriters import ComposeTransforms 420 return self.add_rewriter(ComposeTransforms())
421
422 - def no_compose_transforms(self):
423 ''' 424 Disable the composition of transforms. 425 ''' 426 from lepl.core.rewriters import ComposeTransforms 427 return self.remove_all_rewriters(ComposeTransforms)
428
429 - def auto_memoize(self, conservative=None, full=True, d=0):
430 ''' 431 This configuration attempts to detect which memoizer is most effective 432 for each matcher. As such it is a general "fix" for left-recursive 433 grammars and is suggested in the warning shown when the right-only 434 memoizer detects left recursion. 435 436 Lepl does not guarantee that all left-recursive grammars are handled 437 correctly. The corrections applied may be incomplete and can be 438 inefficient. It is always better to re-write a grammar to avoid 439 left-recursion. One way to improve efficiency, at the cost of less 440 accurate matching, is to specify a non-zero ``d`` parameter - this 441 is the maximum iteration depth that will be used (by default, when 442 ``d`` is zero, it is the length of the remaining input, which can 443 be very large). 444 445 ''' 446 from lepl.core.rewriters import AutoMemoize 447 from lepl.matchers.memo import LMemo, RMemo 448 self.no_memoize() 449 return self.add_rewriter( 450 AutoMemoize(conservative=conservative, left=LMemo, 451 right=RMemo if full else None, d=d))
452
453 - def left_memoize(self, d=0):
454 ''' 455 Add memoization that may detect and stabilise left-recursion. This 456 makes the parser more robust (so it can handle more grammars) but 457 also more complex (and probably slower). 458 459 ``config.auto_memoize()`` will also add memoization, but will select 460 left/right memoization depending on the path through the parser. 461 462 Lepl does not guarantee that all left-recursive grammars are handled 463 correctly. The corrections applied may be incomplete and can be 464 inefficient. It is always better to re-write a grammar to avoid 465 left-recursion. One way to improve efficiency, at the cost of less 466 accurate matching, is to specify a non-zero ``d`` parameter - this 467 is the maximum iteration depth that will be used (by default, when 468 ``d`` is zero, it is the length of the remaining input, which can 469 be very large). 470 ''' 471 from lepl.core.rewriters import LeftMemoize 472 self.no_memoize() 473 return self.add_rewriter(LeftMemoize(d))
474
475 - def right_memoize(self):
476 ''' 477 Add memoization that can make some complex parsers (with a lot of 478 backtracking) more efficient. This also detects left-recursive 479 grammars and displays a suitable warning. 480 481 This is included in the default configuration. For simple grammars 482 it may make things slower; it can be disabled by 483 ``config.no_memoize()``. 484 ''' 485 from lepl.core.rewriters import RightMemoize 486 self.no_memoize() 487 return self.add_rewriter(RightMemoize())
488
489 - def no_memoize(self):
490 ''' 491 Remove memoization. To use the default configuration without 492 memoization (which may be faster in some cases), specify 493 `config.no_memoize()`. 494 ''' 495 from lepl.core.rewriters import AutoMemoize, LeftMemoize, RightMemoize 496 self.remove_all_rewriters(LeftMemoize) 497 self.remove_all_rewriters(RightMemoize) 498 return self.remove_all_rewriters(AutoMemoize)
499
500 - def lines(self, discard=None, tabsize=8, 501 block_policy=None, block_start=None):
502 ''' 503 Configure "offside parsing". This enables lexing and adds extra 504 tokens to mark the start and end of lines. If block_policy is 505 specified then the line start token will also include spaces 506 which can be used by the ``Block()`` and ``BLine()`` matchers 507 to do offside (whitespace-aware) parsing. 508 509 `discard` is the regular expression to use to identify spaces 510 between tokens (by default, spaces and tabs). 511 512 The remaining parameters are used only if at least one of 513 `block_policy` and `block_start` is given. 514 515 `block_policy` decides how indentation if calculated. 516 See `explicit` etc in lepl.lexer.blocks.matchers. 517 518 `block_start` is the initial indentation (by default, zero). If set 519 to lepl.lexer.lines.matchers.NO_BLOCKS indentation will not 520 be checked (useful for tests). 521 522 `tabsize` is used only if `block_policy` is given. It is the number 523 of spaces used to replace a leading tab (no replacement if None). 524 ''' 525 from lepl.lexer.lines.lexer import make_offside_lexer 526 from lepl.lexer.lines.matchers import Block, DEFAULT_POLICY, LineStart 527 from lepl.lexer.lines.monitor import block_monitor 528 blocks = block_policy is not None or block_start is not None 529 if blocks: 530 if block_start is None: 531 block_start = 0 532 if block_policy is None: 533 block_policy = DEFAULT_POLICY 534 self.add_monitor(block_monitor(block_start)) 535 self.set_arguments(Block, policy=block_policy) 536 else: 537 self.set_arguments(LineStart, indent=False) 538 self.lexer(self.__get_alphabet(), discard, 539 make_offside_lexer(tabsize, blocks)) 540 return self
541 542 543 # monitors 544
545 - def trace_stack(self, enabled=False):
546 ''' 547 Add a monitor to trace results using `TraceStack()`. 548 549 This is not used by default as it has a cost at runtime. 550 ''' 551 from lepl.core.trace import TraceStack 552 return self.add_monitor(TraceStack(enabled))
553
554 - def trace_variables(self):
555 ''' 556 Add a monitor to correctly insert the transforms needed when using 557 the `TraceVariables()` context: 558 559 with TraceVariables(): 560 ... 561 562 This is used by default as it has no runtime cost (once the parser 563 is created). 564 ''' 565 from lepl.core.rewriters import TraceVariables 566 return self.add_rewriter(TraceVariables())
567
568 - def low_memory(self, queue_len=100):
569 ''' 570 Reduce memory use (at the expense of backtracking). 571 572 This will: 573 - Add a monitor to manage resources. See `GeneratorManager()`. 574 - Disable direct evaluation (more trampolining gives more scope 575 for removing generators) 576 - Disable the full first match error (which requires a copy of the 577 input for the error message) 578 - Disable memoisation (which would keep input in memory) 579 580 This reduces memory usage, but makes the parser less reliable. 581 Usually a value like 100 (the default) for the queue length will make 582 memory use insignificant and still give a useful first parse. 583 584 Note that, although the parser will use less memory, it may run 585 more slowly (as extra work needs to be done to "clean out" the 586 stored values). 587 ''' 588 from lepl.core.manager import GeneratorManager 589 self.add_monitor(GeneratorManager(queue_len)) 590 self.no_direct_eval() 591 self.no_memoize() 592 self.no_full_first_match() 593 self.cache_level(-9) 594 return self
595
596 - def cache_level(self, level=1):
597 ''' 598 Control when the stream can be cached internally (this is used for 599 debugging and error messages) - streams are cached for debugging when 600 the value is greater than zero. The value is incremented each time 601 a new stream is constructed (eg when constructing tokens). 602 603 A value of 1 implies that a stream would be always cached. A value of 604 0 might be used when iterating over a file with the lexer - the 605 iteration is not cached, but individual tokens will be. 606 ''' 607 self.add_stream_kargs(cache_level=level)
608
609 - def record_deepest(self, n_before=6, n_results_after=2, n_done_after=2):
610 ''' 611 Add a monitor to record deepest match. See `RecordDeepest()`. 612 ''' 613 from lepl.core.trace import RecordDeepest 614 return self.add_monitor( 615 RecordDeepest(n_before, n_results_after, n_done_after))
616 617 # packages 618
619 - def clear(self):
620 ''' 621 Delete any earlier configuration and disable the default (so no 622 rewriters or monitors are used). 623 ''' 624 self.__started = True 625 self.clear_cache() 626 self.__rewriters = set() 627 self.__monitors = [] 628 self.__stream_factory = DEFAULT_STREAM_FACTORY 629 self.__alphabet = None 630 return self
631
632 - def default(self):
633 ''' 634 Provide the default configuration (deleting what may have been 635 configured previously). This is equivalent to the initial 636 configuration. It provides a moderately efficient, stable parser. 637 ''' 638 self.clear() 639 self.flatten() 640 self.trace_variables() 641 self.compose_transforms() 642 self.lexer() 643 self.right_memoize() 644 self.direct_eval() 645 self.compile_to_nfa() 646 self.full_first_match() 647 return self
648
649 650 -class ParserMixin(object):
651 ''' 652 Methods to configure and generate a parser or matcher. 653 ''' 654
655 - def __init__(self, *args, **kargs):
656 super(ParserMixin, self).__init__(*args, **kargs) 657 self.config = ConfigBuilder(self) 658 self.__raw_parser_cache = None 659 self.__from = None # needed to check cache is valid
660
661 - def _raw_parser(self, from_=None):
662 ''' 663 Provide the parser. This underlies the "fancy" methods below. 664 ''' 665 if self.config.changed or self.__raw_parser_cache is None \ 666 or self.__from != from_: 667 config = self.config.configuration 668 self.__from = from_ 669 if from_: 670 stream_factory = \ 671 getattr(config.stream_factory, 'from_' + from_) 672 else: 673 stream_factory = config.stream_factory # __call__ 674 self.__raw_parser_cache = \ 675 make_raw_parser(self, stream_factory, config) 676 return self.__raw_parser_cache
677 678
679 - def get_match_file(self):
680 ''' 681 Get a function that will parse the contents of a file, returning a 682 sequence of (results, stream) pairs. The data will be read as 683 required (using an iterator), so the file must remain open during 684 parsing. To avoid this, read all data into a string and parse that. 685 ''' 686 return self._raw_parser('file')
687
688 - def get_match_iterable(self):
689 ''' 690 Get a function that will parse the contents of an iterable 691 (eg. a generator), returning a sequence of (results, stream) pairs. 692 The data will be read as required. 693 ''' 694 return self._raw_parser('iterable')
695
696 - def get_match_list(self):
697 ''' 698 Get a function that will parse the contents of a list returning a 699 sequence of (results, stream) pairs. 700 ''' 701 return self._raw_parser('list')
702
703 - def get_match_string(self,):
704 ''' 705 Get a function that will parse the contents of a string returning a 706 sequence of (results, stream) pairs. 707 ''' 708 return self._raw_parser('string')
709
710 - def get_match_sequence(self):
711 ''' 712 Get a function that will parse the contents of a generic sequence 713 (with [] and len()) returning a sequence of (results, stream) pairs. 714 ''' 715 return self._raw_parser('sequence')
716
717 - def get_match(self):
718 ''' 719 Get a function that will parse input, returning a sequence of 720 (results, stream) pairs. 721 The type of stream is inferred from the input to the parser. 722 ''' 723 return self._raw_parser()
724 725
726 - def match_file(self, file_, **kargs):
727 ''' 728 Parse the contents of a file, returning a sequence of 729 (results, stream) pairs. The data will be read as required 730 (using an iterator), so the file must remain open during parsing. 731 To avoid this, read all data into a string and parse that. 732 ''' 733 return self.get_match_file()(file_, **kargs)
734
735 - def match_iterable(self, iterable, **kargs):
736 ''' 737 Parse the contents of an iterable (eg. a generator), returning 738 a sequence of (results, stream) pairs. The data will be read as 739 required. 740 ''' 741 return self.get_match_iterable()(iterable, **kargs)
742
743 - def match_list(self, list_, **kargs):
744 ''' 745 Parse the contents of a list returning a sequence of (results, stream) 746 pairs. 747 ''' 748 return self.get_match_list()(list_, **kargs)
749
750 - def match_string(self, string, **kargs):
751 ''' 752 Parse the contents of a string, returning a sequence of 753 (results, stream) pairs. 754 ''' 755 return self.get_match_string()(string, **kargs)
756
757 - def match_sequence(self, sequence, **kargs):
758 ''' 759 Parse the contents of a generic sequence (with [] and len()) 760 returning a sequence of (results, stream) pairs. 761 ''' 762 return self.get_match_sequence()(sequence, **kargs)
763
764 - def match(self, input_, **kargs):
765 ''' 766 Parse input, returning a sequence of (results, stream) pairs. 767 The type of stream is inferred from the input. 768 ''' 769 return self.get_match()(input_, **kargs)
770 771
772 - def get_parse_file(self):
773 ''' 774 Get a function that will parse the contents of a file, returning a 775 single match. The data will be read as required (using an iterator), 776 so the file must remain open during parsing. To avoid this, read 777 all data into a string and parse that. 778 ''' 779 return make_single(self.get_match_file())
780
781 - def get_parse_iterable(self):
782 ''' 783 Get a function that will parse the contents of an iterable 784 (eg. a generator), returning a single match. The data will be read 785 as required. 786 ''' 787 return make_single(self.get_match_iterable())
788
789 - def get_parse_list(self):
790 ''' 791 Get a function that will parse the contents of a list returning a 792 single match. 793 ''' 794 return make_single(self.get_match_list())
795
796 - def get_parse_string(self):
797 ''' 798 Get a function that will parse the contents of a string returning a 799 single match. 800 ''' 801 return make_single(self.get_match_string())
802
803 - def get_parse_sequence(self):
804 ''' 805 Get a function that will parse the contents of a generic sequence 806 (with [] and len()) returning a single match. 807 ''' 808 return make_single(self.get_match_sequence())
809
810 - def get_parse(self):
811 ''' 812 Get a function that will parse input, returning a single match. 813 The type of stream is inferred from the input to the parser. 814 ''' 815 return make_single(self.get_match())
816 817
818 - def parse_file(self, file_, **kargs):
819 ''' 820 Parse the contents of a file, returning a single match. The data 821 will be read as required (using an iterator), so the file must 822 remain open during parsing. To avoid this, read all data into a 823 string and parse that. 824 ''' 825 return self.get_parse_file()(file_, **kargs)
826
827 - def parse_iterable(self, iterable, **kargs):
828 ''' 829 Parse the contents of an iterable (eg. a generator), returning 830 a single match. The data will be read as required. 831 ''' 832 return self.get_parse_iterable()(iterable, **kargs)
833
834 - def parse_list(self, list_, **kargs):
835 ''' 836 Parse the contents of a list returning a single match. 837 ''' 838 return self.get_parse_list()(list_, **kargs)
839
840 - def parse_string(self, string, **kargs):
841 ''' 842 Parse the contents of a string, returning a single match. 843 ''' 844 return self.get_parse_string()(string, **kargs)
845
846 - def parse_sequence(self, sequence, **kargs):
847 ''' 848 Pparse the contents of a generic sequence (with [] and len()) 849 returning a single match. 850 ''' 851 return self.get_parse_sequence()(sequence, **kargs)
852
853 - def parse(self, input_, **kargs):
854 ''' 855 Parse the input, returning a single match. The type of stream is 856 inferred from the input. 857 ''' 858 return self.get_parse()(input_, **kargs)
859 860
861 - def get_parse_file_all(self):
862 ''' 863 Get a function that will parse the contents of a file, returning a 864 sequence of matches. The data will be read as required (using an 865 iterator), so the file must remain open during parsing. To avoid 866 this, read all data into a string and parse that. 867 ''' 868 return make_multiple(self.get_match_file())
869
870 - def get_parse_iterable_all(self):
871 ''' 872 Get a function that will parse the contents of an iterable 873 (eg. a generator), returning a sequence of matches. The data will 874 be read as required. 875 ''' 876 return make_multiple(self.get_match_iterable())
877
878 - def get_parse_list_all(self):
879 ''' 880 Get a function that will parse the contents of a list returning a 881 sequence of matches. 882 ''' 883 return make_multiple(self.get_match_list())
884
885 - def get_parse_string_all(self):
886 ''' 887 Get a function that will parse a string, returning a sequence of 888 matches. 889 ''' 890 return make_multiple(self.get_match_string())
891
892 - def get_parse_sequence_all(self):
893 ''' 894 Get a function that will parse the contents of a generic sequence 895 (with [] and len()) returning a sequence of matches. 896 ''' 897 return make_multiple(self.get_match_sequence())
898
899 - def get_parse_all(self):
900 ''' 901 Get a function that will parse input, returning a sequence of 902 matches. The type of stream is inferred from the input to the 903 parser. 904 ''' 905 return make_multiple(self.get_match())
906 907
908 - def parse_file_all(self, file_, **kargs):
909 ''' 910 Parse the contents of a file, returning a sequence of matches. 911 The data will be read as required (using an iterator), so the file 912 must remain open during parsing. To avoid this, read all data 913 into a string and parse that. 914 ''' 915 return self.get_parse_file_all()(file_, **kargs)
916
917 - def parse_iterable_all(self, iterable, **kargs):
918 ''' 919 Parse the contents of an iterable (eg. a generator), returning 920 a sequence of matches. The data will be read as required. 921 ''' 922 return self.get_parse_iterable_all()(iterable, **kargs)
923
924 - def parse_list_all(self, list_, **kargs):
925 ''' 926 Parse the contents of a list returning a sequence of matches. 927 ''' 928 return self.get_parse_list_all()(list_, **kargs)
929
930 - def parse_string_all(self, string, **kargs):
931 ''' 932 Parse the contents of a string, returning a sequence of matches. 933 ''' 934 return self.get_parse_string_all()(string, **kargs)
935
936 - def parse_sequence_all(self, sequence, **kargs):
937 ''' 938 Parse the contents of a generic sequence (with [] and len()) 939 returning a sequence of matches. 940 ''' 941 return self.get_parse_sequence_all()(sequence, **kargs)
942
943 - def parse_all(self, input_, **kargs):
944 ''' 945 Parse input, returning a sequence of 946 matches. The type of stream is inferred from the input to the 947 parser. 948 ''' 949 return self.get_parse_all()(input_, **kargs)
950