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

Source Code for Module lepl.matchers._test.core

   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  Tests for the lepl.matchers.core module. 
  32  ''' 
  33   
  34  #from logging import basicConfig, DEBUG 
  35  from unittest import TestCase 
  36   
  37  from lepl._test.base import BaseTest, assert_str 
  38  from lepl.matchers.combine import And, Or 
  39  from lepl.matchers.core import Any, Literal, Eof, Regexp, Delayed, Lookahead, \ 
  40      Consumer 
  41  from lepl.matchers.complex import Columns, PostMatch 
  42  from lepl.matchers.derived import Word, Digit, Integer, Drop, Space, AnyBut, \ 
  43      Newline 
  44  from lepl.support.node import Node 
45 46 47 # pylint: disable-msg=C0103, C0111, C0301, W0702, C0324, C0102, C0321, W0141, R0201, R0904, R0903 48 # (dude this is just a test) 49 50 51 -class AnyTest(TestCase):
52
53 - def test_any(self):
54 matcher = Any() 55 result = matcher.parse('a') 56 assert result, result
57
58 59 -class DelayedTest(TestCase):
60
61 - def test_delayed(self):
62 matcher = Delayed() 63 matcher += Literal('abc') 64 result = matcher.parse('abc') 65 assert result == ['abc'], result
66
67 - def test_missing(self):
68 matcher = Delayed() 69 try: 70 matcher.parse('abc') 71 assert False, 'expected error' 72 except ValueError as e: 73 assert 'unbound' in str(e)
74
75 76 -class AndTest(BaseTest):
77
78 - def test_simple(self):
79 #basicConfig(level=DEBUG) 80 self.assert_join([1], Any(), [[[1]]]) 81 self.assert_join([1,2], And(Any(), Any()), [[[1],[2]]]) 82 self.assert_join([1,2,3], And(Any(), Any()), [[[1],[2]]]) 83 self.assert_join([1], And(Any(), Any()), [])
84
85 - def test_and(self):
86 #basicConfig(level=DEBUG) 87 self.assert_join([1,2], Any() & Any(), [[[1],[2]]]) 88 self.assert_join([1,2,3], Any() & Any(), [[[1],[2]]]) 89 self.assert_join([1,2,3], Any() & Any() & Any(), [[[1],[2],[3]]]) 90 self.assert_join([1], Any() & Any(), [])
91
92 - def assert_join(self, stream, match, target):
93 match.config.no_full_first_match() 94 result = list(match.parse_list_all(stream)) 95 assert target == result, result
96
97 - def test_add(self):
98 #basicConfig(level=DEBUG) 99 self.assert_list([1,2], Any() + Any(), [[[1,2]]]) 100 self.assert_list([1,2,3], Any() + Any(), [[[1,2]]]) 101 self.assert_list([1,2,3], Any() + Any() + Any(), [[[1,2,3]]]) 102 self.assert_list([1], Any() + Any(), [])
103
104 105 -class CoercionTest(BaseTest):
106
107 - def test_right(self):
108 #basicConfig(level=DEBUG) 109 self.assert_direct('12', Any() + '2', [['12']])
110
111 - def test_left(self):
112 #basicConfig(level=DEBUG) 113 self.assert_direct('12', '1' + Any(), [['12']])
114
115 116 -class OrTest(BaseTest):
117
118 - def test_simple(self):
119 self.assert_direct('a', Or(Any('x'), Any('a'), Any()), [['a'],['a']])
120
121 - def test_bar(self):
122 self.assert_direct('a', Any('x') | Any('a') | Any(), [['a'],['a']])
123
124 125 -class FirstTest(BaseTest):
126
127 - def test_first(self):
128 s = Space() 129 aline = '#define' & ~s[1:] & Word() & ~s[1:] & Word() > list 130 bline = AnyBut(s[0:] & Newline())[1:] 131 line = aline % ~bline 132 parser = line[0:,~(s[0:] & Newline())] 133 parser.config.no_full_first_match() 134 parser.config.clear() 135 n = len(list(parser.match('#define A 1\ncrap n stuff\n#define B 22\n'))) 136 assert n == 16, n 137 r = parser.parse('#define A 1\ncrap n stuff\n#define B 22\n') 138 assert r == [['#define', 'A', '1'], ['#define', 'B', '22']], r
139
140 141 -class LookaheadTest(BaseTest):
142
143 - def test_simple(self):
144 self.assert_direct('ab', Any() + Lookahead('c') + Any(), []) 145 self.assert_direct('ab', Any() + Lookahead('b') + Any(), [['ab']])
146
147 - def test_bang(self):
148 self.assert_direct('ab', Any() + ~Lookahead('c') + Any(), [['ab']]) 149 self.assert_direct('ab', Any() + ~Lookahead('b') + Any(), [])
150
151 152 -class RegexpTest(BaseTest):
153
154 - def test_group(self):
155 self.assert_direct(' 123x', Regexp(r'\s*\d+') & Any(), [[' 123', 'x']])
156
157 - def test_groups(self):
158 self.assert_direct(' 123x', Regexp(r'\s*(\d)(\d+)') & Any(), [['1','23','x']])
159
160 161 -class WordTest(BaseTest):
162
163 - def test_phone(self):
164 #basicConfig(level=DEBUG) 165 self.assert_direct('andrew, 3333253', Word() / ',' / Integer() / Eof(), 166 [['andrew', ',', ' ', '3333253']])
167
168 169 -class EofTest(BaseTest):
170
171 - def test_eof(self):
172 self.assert_direct('foo ', 'foo' / Eof(), [['foo', ' ']])
173
174 175 -class LiteralTest(BaseTest):
176
177 - def test_literal(self):
178 self.assert_direct('foo ', Literal('foo'), [['foo']])
179
180 181 -class TransformTest(BaseTest):
182 183 @staticmethod
184 - def mkappend(x):
185 return lambda a: a + x
186
187 - def test_apply(self):
188 # note extra list 189 self.assert_direct('foo ', Literal('foo') > self.mkappend(['b']), [[['foo', 'b']]])
190
191 - def test_kapply(self):
192 # note extra list 193 self.assert_direct('foo ', Literal('foo') >> self.mkappend('b'), [['foob']])
194
195 - def test_nested(self):
196 # note extra list 197 self.assert_direct('foo ', 198 (Literal('foo') >> self.mkappend('b')) > self.mkappend(['c']), 199 [[['foob', 'c']]])
200
201 202 -class StrTest(TestCase):
203
204 - def assert_same(self, text1, text2):
205 assert_str(text1, text2)
206 # assert self.__clean(text1) == self.__clean(text2), text1 207
208 - def __clean(self, text):
209 depth = 0 210 result = '' 211 for c in text: 212 if c == '<': 213 depth += 1 214 elif c == '>': 215 depth -= 1 216 elif depth == 0: 217 result += c 218 return result
219
220 - def test_str(self):
221 class Term(Node): pass 222 class Factor(Node): pass 223 class Expression(Node): pass 224 225 expression = Delayed() 226 number = Digit()[1:,...] > 'number' 227 term = (number | '(' / expression / ')') > Term 228 muldiv = Any('*/') > 'operator' 229 factor = (term / (muldiv / term)[0::]) > Factor 230 addsub = Any('+-') > 'operator' 231 expression += (factor / (addsub / factor)[0::]) > Expression 232 233 description = repr(expression) 234 self.assert_same(description, r'''Delayed(matcher=Transform:<apply>( 235 And( 236 Transform:<apply>( 237 And( 238 Transform:<apply>( 239 Or( 240 Transform:<apply>( 241 Transform:<add>( 242 DepthFirst( 243 generator_manager_queue_len=None, 244 start=1, 245 stop=None, 246 rest=FunctionWrapper<Any:<>>('0123456789'), 247 first=FunctionWrapper<Any:<>>('0123456789')), 248 TransformationWrapper(<add>)), 249 TransformationWrapper(<apply>)), 250 And( 251 And( 252 FunctionWrapper<Literal:<>>('('), 253 Transform:<add>( 254 DepthFirst( 255 generator_manager_queue_len=None, 256 start=0, 257 stop=None, 258 rest=FunctionWrapper<Any:<>>(' \t'), 259 first=FunctionWrapper<Any:<>>(' \t')), 260 TransformationWrapper(<add>)), 261 [Delayed]), 262 Transform:<add>( 263 DepthFirst( 264 generator_manager_queue_len=None, 265 start=0, 266 stop=None, 267 rest=FunctionWrapper<Any:<>>(' \t'), 268 first=FunctionWrapper<Any:<>>(' \t')), 269 TransformationWrapper(<add>)), 270 FunctionWrapper<Literal:<>>(')'))), 271 TransformationWrapper(<apply>)), 272 Transform:<add>( 273 DepthFirst( 274 generator_manager_queue_len=None, 275 start=0, 276 stop=None, 277 rest=FunctionWrapper<Any:<>>(' \t'), 278 first=FunctionWrapper<Any:<>>(' \t')), 279 TransformationWrapper(<add>)), 280 DepthFirst( 281 generator_manager_queue_len=None, 282 start=0, 283 stop=None, 284 rest=And( 285 Transform:<apply>( 286 FunctionWrapper<Any:<>>('*/'), 287 TransformationWrapper(<apply>)), 288 Transform:<add>( 289 DepthFirst( 290 generator_manager_queue_len=None, 291 start=0, 292 stop=None, 293 rest=FunctionWrapper<Any:<>>(' \t'), 294 first=FunctionWrapper<Any:<>>(' \t')), 295 TransformationWrapper(<add>)), 296 Transform:<apply>( 297 Or( 298 Transform:<apply>( 299 Transform:<add>( 300 DepthFirst( 301 generator_manager_queue_len=None, 302 start=1, 303 stop=None, 304 rest=FunctionWrapper<Any:<>>('0123456789'), 305 first=FunctionWrapper<Any:<>>('0123456789')), 306 TransformationWrapper(<add>)), 307 TransformationWrapper(<apply>)), 308 And( 309 And( 310 FunctionWrapper<Literal:<>>('('), 311 Transform:<add>( 312 DepthFirst( 313 generator_manager_queue_len=None, 314 start=0, 315 stop=None, 316 rest=FunctionWrapper<Any:<>>(' \t'), 317 first=FunctionWrapper<Any:<>>(' \t')), 318 TransformationWrapper(<add>)), 319 [Delayed]), 320 Transform:<add>( 321 DepthFirst( 322 generator_manager_queue_len=None, 323 start=0, 324 stop=None, 325 rest=FunctionWrapper<Any:<>>(' \t'), 326 first=FunctionWrapper<Any:<>>(' \t')), 327 TransformationWrapper(<add>)), 328 FunctionWrapper<Literal:<>>(')'))), 329 TransformationWrapper(<apply>))), 330 first=And( 331 Transform:<apply>( 332 FunctionWrapper<Any:<>>('*/'), 333 TransformationWrapper(<apply>)), 334 Transform:<add>( 335 DepthFirst( 336 generator_manager_queue_len=None, 337 start=0, 338 stop=None, 339 rest=FunctionWrapper<Any:<>>(' \t'), 340 first=FunctionWrapper<Any:<>>(' \t')), 341 TransformationWrapper(<add>)), 342 Transform:<apply>( 343 Or( 344 Transform:<apply>( 345 Transform:<add>( 346 DepthFirst( 347 generator_manager_queue_len=None, 348 start=1, 349 stop=None, 350 rest=FunctionWrapper<Any:<>>('0123456789'), 351 first=FunctionWrapper<Any:<>>('0123456789')), 352 TransformationWrapper(<add>)), 353 TransformationWrapper(<apply>)), 354 And( 355 And( 356 FunctionWrapper<Literal:<>>('('), 357 Transform:<add>( 358 DepthFirst( 359 generator_manager_queue_len=None, 360 start=0, 361 stop=None, 362 rest=FunctionWrapper<Any:<>>(' \t'), 363 first=FunctionWrapper<Any:<>>(' \t')), 364 TransformationWrapper(<add>)), 365 [Delayed]), 366 Transform:<add>( 367 DepthFirst( 368 generator_manager_queue_len=None, 369 start=0, 370 stop=None, 371 rest=FunctionWrapper<Any:<>>(' \t'), 372 first=FunctionWrapper<Any:<>>(' \t')), 373 TransformationWrapper(<add>)), 374 FunctionWrapper<Literal:<>>(')'))), 375 TransformationWrapper(<apply>))))), 376 TransformationWrapper(<apply>)), 377 Transform:<add>( 378 DepthFirst( 379 generator_manager_queue_len=None, 380 start=0, 381 stop=None, 382 rest=FunctionWrapper<Any:<>>(' \t'), 383 first=FunctionWrapper<Any:<>>(' \t')), 384 TransformationWrapper(<add>)), 385 DepthFirst( 386 generator_manager_queue_len=None, 387 start=0, 388 stop=None, 389 rest=And( 390 Transform:<apply>( 391 FunctionWrapper<Any:<>>('+-'), 392 TransformationWrapper(<apply>)), 393 Transform:<add>( 394 DepthFirst( 395 generator_manager_queue_len=None, 396 start=0, 397 stop=None, 398 rest=FunctionWrapper<Any:<>>(' \t'), 399 first=FunctionWrapper<Any:<>>(' \t')), 400 TransformationWrapper(<add>)), 401 Transform:<apply>( 402 And( 403 Transform:<apply>( 404 Or( 405 Transform:<apply>( 406 Transform:<add>( 407 DepthFirst( 408 generator_manager_queue_len=None, 409 start=1, 410 stop=None, 411 rest=FunctionWrapper<Any:<>>('0123456789'), 412 first=FunctionWrapper<Any:<>>('0123456789')), 413 TransformationWrapper(<add>)), 414 TransformationWrapper(<apply>)), 415 And( 416 And( 417 FunctionWrapper<Literal:<>>('('), 418 Transform:<add>( 419 DepthFirst( 420 generator_manager_queue_len=None, 421 start=0, 422 stop=None, 423 rest=FunctionWrapper<Any:<>>(' \t'), 424 first=FunctionWrapper<Any:<>>(' \t')), 425 TransformationWrapper(<add>)), 426 [Delayed]), 427 Transform:<add>( 428 DepthFirst( 429 generator_manager_queue_len=None, 430 start=0, 431 stop=None, 432 rest=FunctionWrapper<Any:<>>(' \t'), 433 first=FunctionWrapper<Any:<>>(' \t')), 434 TransformationWrapper(<add>)), 435 FunctionWrapper<Literal:<>>(')'))), 436 TransformationWrapper(<apply>)), 437 Transform:<add>( 438 DepthFirst( 439 generator_manager_queue_len=None, 440 start=0, 441 stop=None, 442 rest=FunctionWrapper<Any:<>>(' \t'), 443 first=FunctionWrapper<Any:<>>(' \t')), 444 TransformationWrapper(<add>)), 445 DepthFirst( 446 generator_manager_queue_len=None, 447 start=0, 448 stop=None, 449 rest=And( 450 Transform:<apply>( 451 FunctionWrapper<Any:<>>('*/'), 452 TransformationWrapper(<apply>)), 453 Transform:<add>( 454 DepthFirst( 455 generator_manager_queue_len=None, 456 start=0, 457 stop=None, 458 rest=FunctionWrapper<Any:<>>(' \t'), 459 first=FunctionWrapper<Any:<>>(' \t')), 460 TransformationWrapper(<add>)), 461 Transform:<apply>( 462 Or( 463 Transform:<apply>( 464 Transform:<add>( 465 DepthFirst( 466 generator_manager_queue_len=None, 467 start=1, 468 stop=None, 469 rest=FunctionWrapper<Any:<>>('0123456789'), 470 first=FunctionWrapper<Any:<>>('0123456789')), 471 TransformationWrapper(<add>)), 472 TransformationWrapper(<apply>)), 473 And( 474 And( 475 FunctionWrapper<Literal:<>>('('), 476 Transform:<add>( 477 DepthFirst( 478 generator_manager_queue_len=None, 479 start=0, 480 stop=None, 481 rest=FunctionWrapper<Any:<>>(' \t'), 482 first=FunctionWrapper<Any:<>>(' \t')), 483 TransformationWrapper(<add>)), 484 [Delayed]), 485 Transform:<add>( 486 DepthFirst( 487 generator_manager_queue_len=None, 488 start=0, 489 stop=None, 490 rest=FunctionWrapper<Any:<>>(' \t'), 491 first=FunctionWrapper<Any:<>>(' \t')), 492 TransformationWrapper(<add>)), 493 FunctionWrapper<Literal:<>>(')'))), 494 TransformationWrapper(<apply>))), 495 first=And( 496 Transform:<apply>( 497 FunctionWrapper<Any:<>>('*/'), 498 TransformationWrapper(<apply>)), 499 Transform:<add>( 500 DepthFirst( 501 generator_manager_queue_len=None, 502 start=0, 503 stop=None, 504 rest=FunctionWrapper<Any:<>>(' \t'), 505 first=FunctionWrapper<Any:<>>(' \t')), 506 TransformationWrapper(<add>)), 507 Transform:<apply>( 508 Or( 509 Transform:<apply>( 510 Transform:<add>( 511 DepthFirst( 512 generator_manager_queue_len=None, 513 start=1, 514 stop=None, 515 rest=FunctionWrapper<Any:<>>('0123456789'), 516 first=FunctionWrapper<Any:<>>('0123456789')), 517 TransformationWrapper(<add>)), 518 TransformationWrapper(<apply>)), 519 And( 520 And( 521 FunctionWrapper<Literal:<>>('('), 522 Transform:<add>( 523 DepthFirst( 524 generator_manager_queue_len=None, 525 start=0, 526 stop=None, 527 rest=FunctionWrapper<Any:<>>(' \t'), 528 first=FunctionWrapper<Any:<>>(' \t')), 529 TransformationWrapper(<add>)), 530 [Delayed]), 531 Transform:<add>( 532 DepthFirst( 533 generator_manager_queue_len=None, 534 start=0, 535 stop=None, 536 rest=FunctionWrapper<Any:<>>(' \t'), 537 first=FunctionWrapper<Any:<>>(' \t')), 538 TransformationWrapper(<add>)), 539 FunctionWrapper<Literal:<>>(')'))), 540 TransformationWrapper(<apply>))))), 541 TransformationWrapper(<apply>))), 542 first=And( 543 Transform:<apply>( 544 FunctionWrapper<Any:<>>('+-'), 545 TransformationWrapper(<apply>)), 546 Transform:<add>( 547 DepthFirst( 548 generator_manager_queue_len=None, 549 start=0, 550 stop=None, 551 rest=FunctionWrapper<Any:<>>(' \t'), 552 first=FunctionWrapper<Any:<>>(' \t')), 553 TransformationWrapper(<add>)), 554 Transform:<apply>( 555 And( 556 Transform:<apply>( 557 Or( 558 Transform:<apply>( 559 Transform:<add>( 560 DepthFirst( 561 generator_manager_queue_len=None, 562 start=1, 563 stop=None, 564 rest=FunctionWrapper<Any:<>>('0123456789'), 565 first=FunctionWrapper<Any:<>>('0123456789')), 566 TransformationWrapper(<add>)), 567 TransformationWrapper(<apply>)), 568 And( 569 And( 570 FunctionWrapper<Literal:<>>('('), 571 Transform:<add>( 572 DepthFirst( 573 generator_manager_queue_len=None, 574 start=0, 575 stop=None, 576 rest=FunctionWrapper<Any:<>>(' \t'), 577 first=FunctionWrapper<Any:<>>(' \t')), 578 TransformationWrapper(<add>)), 579 [Delayed]), 580 Transform:<add>( 581 DepthFirst( 582 generator_manager_queue_len=None, 583 start=0, 584 stop=None, 585 rest=FunctionWrapper<Any:<>>(' \t'), 586 first=FunctionWrapper<Any:<>>(' \t')), 587 TransformationWrapper(<add>)), 588 FunctionWrapper<Literal:<>>(')'))), 589 TransformationWrapper(<apply>)), 590 Transform:<add>( 591 DepthFirst( 592 generator_manager_queue_len=None, 593 start=0, 594 stop=None, 595 rest=FunctionWrapper<Any:<>>(' \t'), 596 first=FunctionWrapper<Any:<>>(' \t')), 597 TransformationWrapper(<add>)), 598 DepthFirst( 599 generator_manager_queue_len=None, 600 start=0, 601 stop=None, 602 rest=And( 603 Transform:<apply>( 604 FunctionWrapper<Any:<>>('*/'), 605 TransformationWrapper(<apply>)), 606 Transform:<add>( 607 DepthFirst( 608 generator_manager_queue_len=None, 609 start=0, 610 stop=None, 611 rest=FunctionWrapper<Any:<>>(' \t'), 612 first=FunctionWrapper<Any:<>>(' \t')), 613 TransformationWrapper(<add>)), 614 Transform:<apply>( 615 Or( 616 Transform:<apply>( 617 Transform:<add>( 618 DepthFirst( 619 generator_manager_queue_len=None, 620 start=1, 621 stop=None, 622 rest=FunctionWrapper<Any:<>>('0123456789'), 623 first=FunctionWrapper<Any:<>>('0123456789')), 624 TransformationWrapper(<add>)), 625 TransformationWrapper(<apply>)), 626 And( 627 And( 628 FunctionWrapper<Literal:<>>('('), 629 Transform:<add>( 630 DepthFirst( 631 generator_manager_queue_len=None, 632 start=0, 633 stop=None, 634 rest=FunctionWrapper<Any:<>>(' \t'), 635 first=FunctionWrapper<Any:<>>(' \t')), 636 TransformationWrapper(<add>)), 637 [Delayed]), 638 Transform:<add>( 639 DepthFirst( 640 generator_manager_queue_len=None, 641 start=0, 642 stop=None, 643 rest=FunctionWrapper<Any:<>>(' \t'), 644 first=FunctionWrapper<Any:<>>(' \t')), 645 TransformationWrapper(<add>)), 646 FunctionWrapper<Literal:<>>(')'))), 647 TransformationWrapper(<apply>))), 648 first=And( 649 Transform:<apply>( 650 FunctionWrapper<Any:<>>('*/'), 651 TransformationWrapper(<apply>)), 652 Transform:<add>( 653 DepthFirst( 654 generator_manager_queue_len=None, 655 start=0, 656 stop=None, 657 rest=FunctionWrapper<Any:<>>(' \t'), 658 first=FunctionWrapper<Any:<>>(' \t')), 659 TransformationWrapper(<add>)), 660 Transform:<apply>( 661 Or( 662 Transform:<apply>( 663 Transform:<add>( 664 DepthFirst( 665 generator_manager_queue_len=None, 666 start=1, 667 stop=None, 668 rest=FunctionWrapper<Any:<>>('0123456789'), 669 first=FunctionWrapper<Any:<>>('0123456789')), 670 TransformationWrapper(<add>)), 671 TransformationWrapper(<apply>)), 672 And( 673 And( 674 FunctionWrapper<Literal:<>>('('), 675 Transform:<add>( 676 DepthFirst( 677 generator_manager_queue_len=None, 678 start=0, 679 stop=None, 680 rest=FunctionWrapper<Any:<>>(' \t'), 681 first=FunctionWrapper<Any:<>>(' \t')), 682 TransformationWrapper(<add>)), 683 [Delayed]), 684 Transform:<add>( 685 DepthFirst( 686 generator_manager_queue_len=None, 687 start=0, 688 stop=None, 689 rest=FunctionWrapper<Any:<>>(' \t'), 690 first=FunctionWrapper<Any:<>>(' \t')), 691 TransformationWrapper(<add>)), 692 FunctionWrapper<Literal:<>>(')'))), 693 TransformationWrapper(<apply>))))), 694 TransformationWrapper(<apply>))))), 695 TransformationWrapper(<apply>)))''') 696 parser = expression.get_parse() 697 description = parser.matcher.tree() 698 print(description) 699 self.assert_same(description, r"""TrampolineWrapper<FullFirstMatch> 700 +- Delayed 701 | `- matcher Transform:<apply> 702 | +- _RMemo 703 | | `- TrampolineWrapper<And> 704 | | +- Transform:<apply> 705 | | | +- _RMemo 706 | | | | `- TrampolineWrapper<And> 707 | | | | +- Transform:<apply> 708 | | | | | +- _RMemo 709 | | | | | | `- TrampolineWrapper<Or> 710 | | | | | | +- _RMemo 711 | | | | | | | `- NfaRegexp:<empty_adapter,apply> 712 | | | | | | | +- Sequence(...) 713 | | | | | | | `- alphabet <Unicode> 714 | | | | | | `- _RMemo 715 | | | | | | `- TrampolineWrapper<And> 716 | | | | | | +- _RMemo 717 | | | | | | | `- FunctionWrapper<Literal:<>> 718 | | | | | | | `- '(' 719 | | | | | | +- _RMemo 720 | | | | | | | `- NfaRegexp:<empty_adapter> 721 | | | | | | | +- Sequence(...) 722 | | | | | | | `- alphabet <Unicode> 723 | | | | | | +- Delayed 724 | | | | | | | `- matcher <loop> 725 | | | | | | +- _RMemo 726 | | | | | | | `- NfaRegexp:<empty_adapter> 727 | | | | | | | +- Sequence(...) 728 | | | | | | | `- alphabet <Unicode> 729 | | | | | | `- _RMemo 730 | | | | | | `- FunctionWrapper<Literal:<>> 731 | | | | | | `- ')' 732 | | | | | `- TransformationWrapper(<apply>) 733 | | | | +- _RMemo 734 | | | | | `- NfaRegexp:<empty_adapter> 735 | | | | | +- Sequence(...) 736 | | | | | `- alphabet <Unicode> 737 | | | | `- _RMemo 738 | | | | `- TrampolineWrapper<DepthFirst> 739 | | | | +- generator_manager_queue_len None 740 | | | | +- start 0 741 | | | | +- stop None 742 | | | | +- rest _RMemo 743 | | | | | `- TrampolineWrapper<And> 744 | | | | | +- _RMemo 745 | | | | | | `- FunctionWrapper<Any:<apply>> 746 | | | | | | `- '*/' 747 | | | | | +- _RMemo 748 | | | | | | `- NfaRegexp:<empty_adapter> 749 | | | | | | +- Sequence(...) 750 | | | | | | `- alphabet <Unicode> 751 | | | | | `- Transform:<apply> 752 | | | | | +- _RMemo 753 | | | | | | `- TrampolineWrapper<Or> 754 | | | | | | +- _RMemo 755 | | | | | | | `- NfaRegexp:<empty_adapter,apply> 756 | | | | | | | +- Sequence(...) 757 | | | | | | | `- alphabet <Unicode> 758 | | | | | | `- _RMemo 759 | | | | | | `- TrampolineWrapper<And> 760 | | | | | | +- _RMemo 761 | | | | | | | `- FunctionWrapper<Literal:<>> 762 | | | | | | | `- '(' 763 | | | | | | +- _RMemo 764 | | | | | | | `- NfaRegexp:<empty_adapter> 765 | | | | | | | +- Sequence(...) 766 | | | | | | | `- alphabet <Unicode> 767 | | | | | | +- Delayed 768 | | | | | | | `- matcher <loop> 769 | | | | | | +- _RMemo 770 | | | | | | | `- NfaRegexp:<empty_adapter> 771 | | | | | | | +- Sequence(...) 772 | | | | | | | `- alphabet <Unicode> 773 | | | | | | `- _RMemo 774 | | | | | | `- FunctionWrapper<Literal:<>> 775 | | | | | | `- ')' 776 | | | | | `- TransformationWrapper(<apply>) 777 | | | | `- first _RMemo 778 | | | | `- TrampolineWrapper<And> 779 | | | | +- _RMemo 780 | | | | | `- FunctionWrapper<Any:<apply>> 781 | | | | | `- '*/' 782 | | | | +- _RMemo 783 | | | | | `- NfaRegexp:<empty_adapter> 784 | | | | | +- Sequence(...) 785 | | | | | `- alphabet <Unicode> 786 | | | | `- Transform:<apply> 787 | | | | +- _RMemo 788 | | | | | `- TrampolineWrapper<Or> 789 | | | | | +- _RMemo 790 | | | | | | `- NfaRegexp:<empty_adapter,apply> 791 | | | | | | +- Sequence(...) 792 | | | | | | `- alphabet <Unicode> 793 | | | | | `- _RMemo 794 | | | | | `- TrampolineWrapper<And> 795 | | | | | +- _RMemo 796 | | | | | | `- FunctionWrapper<Literal:<>> 797 | | | | | | `- '(' 798 | | | | | +- _RMemo 799 | | | | | | `- NfaRegexp:<empty_adapter> 800 | | | | | | +- Sequence(...) 801 | | | | | | `- alphabet <Unicode> 802 | | | | | +- Delayed 803 | | | | | | `- matcher <loop> 804 | | | | | +- _RMemo 805 | | | | | | `- NfaRegexp:<empty_adapter> 806 | | | | | | +- Sequence(...) 807 | | | | | | `- alphabet <Unicode> 808 | | | | | `- _RMemo 809 | | | | | `- FunctionWrapper<Literal:<>> 810 | | | | | `- ')' 811 | | | | `- TransformationWrapper(<apply>) 812 | | | `- TransformationWrapper(<apply>) 813 | | +- _RMemo 814 | | | `- NfaRegexp:<empty_adapter> 815 | | | +- Sequence(...) 816 | | | `- alphabet <Unicode> 817 | | `- _RMemo 818 | | `- TrampolineWrapper<DepthFirst> 819 | | +- generator_manager_queue_len None 820 | | +- start 0 821 | | +- stop None 822 | | +- rest _RMemo 823 | | | `- TrampolineWrapper<And> 824 | | | +- _RMemo 825 | | | | `- FunctionWrapper<Any:<apply>> 826 | | | | `- '+-' 827 | | | +- _RMemo 828 | | | | `- NfaRegexp:<empty_adapter> 829 | | | | +- Sequence(...) 830 | | | | `- alphabet <Unicode> 831 | | | `- Transform:<apply> 832 | | | +- _RMemo 833 | | | | `- TrampolineWrapper<And> 834 | | | | +- Transform:<apply> 835 | | | | | +- _RMemo 836 | | | | | | `- TrampolineWrapper<Or> 837 | | | | | | +- _RMemo 838 | | | | | | | `- NfaRegexp:<empty_adapter,apply> 839 | | | | | | | +- Sequence(...) 840 | | | | | | | `- alphabet <Unicode> 841 | | | | | | `- _RMemo 842 | | | | | | `- TrampolineWrapper<And> 843 | | | | | | +- _RMemo 844 | | | | | | | `- FunctionWrapper<Literal:<>> 845 | | | | | | | `- '(' 846 | | | | | | +- _RMemo 847 | | | | | | | `- NfaRegexp:<empty_adapter> 848 | | | | | | | +- Sequence(...) 849 | | | | | | | `- alphabet <Unicode> 850 | | | | | | +- Delayed 851 | | | | | | | `- matcher <loop> 852 | | | | | | +- _RMemo 853 | | | | | | | `- NfaRegexp:<empty_adapter> 854 | | | | | | | +- Sequence(...) 855 | | | | | | | `- alphabet <Unicode> 856 | | | | | | `- _RMemo 857 | | | | | | `- FunctionWrapper<Literal:<>> 858 | | | | | | `- ')' 859 | | | | | `- TransformationWrapper(<apply>) 860 | | | | +- _RMemo 861 | | | | | `- NfaRegexp:<empty_adapter> 862 | | | | | +- Sequence(...) 863 | | | | | `- alphabet <Unicode> 864 | | | | `- _RMemo 865 | | | | `- TrampolineWrapper<DepthFirst> 866 | | | | +- generator_manager_queue_len None 867 | | | | +- start 0 868 | | | | +- stop None 869 | | | | +- rest _RMemo 870 | | | | | `- TrampolineWrapper<And> 871 | | | | | +- _RMemo 872 | | | | | | `- FunctionWrapper<Any:<apply>> 873 | | | | | | `- '*/' 874 | | | | | +- _RMemo 875 | | | | | | `- NfaRegexp:<empty_adapter> 876 | | | | | | +- Sequence(...) 877 | | | | | | `- alphabet <Unicode> 878 | | | | | `- Transform:<apply> 879 | | | | | +- _RMemo 880 | | | | | | `- TrampolineWrapper<Or> 881 | | | | | | +- _RMemo 882 | | | | | | | `- NfaRegexp:<empty_adapter,apply> 883 | | | | | | | +- Sequence(...) 884 | | | | | | | `- alphabet <Unicode> 885 | | | | | | `- _RMemo 886 | | | | | | `- TrampolineWrapper<And> 887 | | | | | | +- _RMemo 888 | | | | | | | `- FunctionWrapper<Literal:<>> 889 | | | | | | | `- '(' 890 | | | | | | +- _RMemo 891 | | | | | | | `- NfaRegexp:<empty_adapter> 892 | | | | | | | +- Sequence(...) 893 | | | | | | | `- alphabet <Unicode> 894 | | | | | | +- Delayed 895 | | | | | | | `- matcher <loop> 896 | | | | | | +- _RMemo 897 | | | | | | | `- NfaRegexp:<empty_adapter> 898 | | | | | | | +- Sequence(...) 899 | | | | | | | `- alphabet <Unicode> 900 | | | | | | `- _RMemo 901 | | | | | | `- FunctionWrapper<Literal:<>> 902 | | | | | | `- ')' 903 | | | | | `- TransformationWrapper(<apply>) 904 | | | | `- first _RMemo 905 | | | | `- TrampolineWrapper<And> 906 | | | | +- _RMemo 907 | | | | | `- FunctionWrapper<Any:<apply>> 908 | | | | | `- '*/' 909 | | | | +- _RMemo 910 | | | | | `- NfaRegexp:<empty_adapter> 911 | | | | | +- Sequence(...) 912 | | | | | `- alphabet <Unicode> 913 | | | | `- Transform:<apply> 914 | | | | +- _RMemo 915 | | | | | `- TrampolineWrapper<Or> 916 | | | | | +- _RMemo 917 | | | | | | `- NfaRegexp:<empty_adapter,apply> 918 | | | | | | +- Sequence(...) 919 | | | | | | `- alphabet <Unicode> 920 | | | | | `- _RMemo 921 | | | | | `- TrampolineWrapper<And> 922 | | | | | +- _RMemo 923 | | | | | | `- FunctionWrapper<Literal:<>> 924 | | | | | | `- '(' 925 | | | | | +- _RMemo 926 | | | | | | `- NfaRegexp:<empty_adapter> 927 | | | | | | +- Sequence(...) 928 | | | | | | `- alphabet <Unicode> 929 | | | | | +- Delayed 930 | | | | | | `- matcher <loop> 931 | | | | | +- _RMemo 932 | | | | | | `- NfaRegexp:<empty_adapter> 933 | | | | | | +- Sequence(...) 934 | | | | | | `- alphabet <Unicode> 935 | | | | | `- _RMemo 936 | | | | | `- FunctionWrapper<Literal:<>> 937 | | | | | `- ')' 938 | | | | `- TransformationWrapper(<apply>) 939 | | | `- TransformationWrapper(<apply>) 940 | | `- first _RMemo 941 | | `- TrampolineWrapper<And> 942 | | +- _RMemo 943 | | | `- FunctionWrapper<Any:<apply>> 944 | | | `- '+-' 945 | | +- _RMemo 946 | | | `- NfaRegexp:<empty_adapter> 947 | | | +- Sequence(...) 948 | | | `- alphabet <Unicode> 949 | | `- Transform:<apply> 950 | | +- _RMemo 951 | | | `- TrampolineWrapper<And> 952 | | | +- Transform:<apply> 953 | | | | +- _RMemo 954 | | | | | `- TrampolineWrapper<Or> 955 | | | | | +- _RMemo 956 | | | | | | `- NfaRegexp:<empty_adapter,apply> 957 | | | | | | +- Sequence(...) 958 | | | | | | `- alphabet <Unicode> 959 | | | | | `- _RMemo 960 | | | | | `- TrampolineWrapper<And> 961 | | | | | +- _RMemo 962 | | | | | | `- FunctionWrapper<Literal:<>> 963 | | | | | | `- '(' 964 | | | | | +- _RMemo 965 | | | | | | `- NfaRegexp:<empty_adapter> 966 | | | | | | +- Sequence(...) 967 | | | | | | `- alphabet <Unicode> 968 | | | | | +- Delayed 969 | | | | | | `- matcher <loop> 970 | | | | | +- _RMemo 971 | | | | | | `- NfaRegexp:<empty_adapter> 972 | | | | | | +- Sequence(...) 973 | | | | | | `- alphabet <Unicode> 974 | | | | | `- _RMemo 975 | | | | | `- FunctionWrapper<Literal:<>> 976 | | | | | `- ')' 977 | | | | `- TransformationWrapper(<apply>) 978 | | | +- _RMemo 979 | | | | `- NfaRegexp:<empty_adapter> 980 | | | | +- Sequence(...) 981 | | | | `- alphabet <Unicode> 982 | | | `- _RMemo 983 | | | `- TrampolineWrapper<DepthFirst> 984 | | | +- generator_manager_queue_len None 985 | | | +- start 0 986 | | | +- stop None 987 | | | +- rest _RMemo 988 | | | | `- TrampolineWrapper<And> 989 | | | | +- _RMemo 990 | | | | | `- FunctionWrapper<Any:<apply>> 991 | | | | | `- '*/' 992 | | | | +- _RMemo 993 | | | | | `- NfaRegexp:<empty_adapter> 994 | | | | | +- Sequence(...) 995 | | | | | `- alphabet <Unicode> 996 | | | | `- Transform:<apply> 997 | | | | +- _RMemo 998 | | | | | `- TrampolineWrapper<Or> 999 | | | | | +- _RMemo 1000 | | | | | | `- NfaRegexp:<empty_adapter,apply> 1001 | | | | | | +- Sequence(...) 1002 | | | | | | `- alphabet <Unicode> 1003 | | | | | `- _RMemo 1004 | | | | | `- TrampolineWrapper<And> 1005 | | | | | +- _RMemo 1006 | | | | | | `- FunctionWrapper<Literal:<>> 1007 | | | | | | `- '(' 1008 | | | | | +- _RMemo 1009 | | | | | | `- NfaRegexp:<empty_adapter> 1010 | | | | | | +- Sequence(...) 1011 | | | | | | `- alphabet <Unicode> 1012 | | | | | +- Delayed 1013 | | | | | | `- matcher <loop> 1014 | | | | | +- _RMemo 1015 | | | | | | `- NfaRegexp:<empty_adapter> 1016 | | | | | | +- Sequence(...) 1017 | | | | | | `- alphabet <Unicode> 1018 | | | | | `- _RMemo 1019 | | | | | `- FunctionWrapper<Literal:<>> 1020 | | | | | `- ')' 1021 | | | | `- TransformationWrapper(<apply>) 1022 | | | `- first _RMemo 1023 | | | `- TrampolineWrapper<And> 1024 | | | +- _RMemo 1025 | | | | `- FunctionWrapper<Any:<apply>> 1026 | | | | `- '*/' 1027 | | | +- _RMemo 1028 | | | | `- NfaRegexp:<empty_adapter> 1029 | | | | +- Sequence(...) 1030 | | | | `- alphabet <Unicode> 1031 | | | `- Transform:<apply> 1032 | | | +- _RMemo 1033 | | | | `- TrampolineWrapper<Or> 1034 | | | | +- _RMemo 1035 | | | | | `- NfaRegexp:<empty_adapter,apply> 1036 | | | | | +- Sequence(...) 1037 | | | | | `- alphabet <Unicode> 1038 | | | | `- _RMemo 1039 | | | | `- TrampolineWrapper<And> 1040 | | | | +- _RMemo 1041 | | | | | `- FunctionWrapper<Literal:<>> 1042 | | | | | `- '(' 1043 | | | | +- _RMemo 1044 | | | | | `- NfaRegexp:<empty_adapter> 1045 | | | | | +- Sequence(...) 1046 | | | | | `- alphabet <Unicode> 1047 | | | | +- Delayed 1048 | | | | | `- matcher <loop> 1049 | | | | +- _RMemo 1050 | | | | | `- NfaRegexp:<empty_adapter> 1051 | | | | | +- Sequence(...) 1052 | | | | | `- alphabet <Unicode> 1053 | | | | `- _RMemo 1054 | | | | `- FunctionWrapper<Literal:<>> 1055 | | | | `- ')' 1056 | | | `- TransformationWrapper(<apply>) 1057 | | `- TransformationWrapper(<apply>) 1058 | `- TransformationWrapper(<apply>) 1059 `- True""")
1060
1061 -class ColumnsTest(BaseTest):
1062
1063 - def test_columns(self):
1064 self.assert_direct('0123456789', 1065 Columns(((0,3), Any()[3,...]), 1066 ((0,4), Any()[4:,...]), 1067 ((5,8), Any()[3:,...])), 1068 [['012', '0123', '567']])
1069
1070 - def test_table(self):
1071 #basicConfig(level=DEBUG) 1072 self.assert_direct( 1073 '''0123456789 1074 abcdefghij 1075 ''', 1076 Columns(((0,3), Any()[3:,...]), 1077 ((0,4), Any()[4:,...]), 1078 ((5,8), Any()[3:,...]))[2], 1079 [['012', '0123', '567', 1080 'abc', 'abcd', 'fgh']])
1081
1082 1083 -class ConsumerTest(BaseTest):
1084
1085 - def test_simple(self):
1086 parser = Consumer(Any()).get_parse() 1087 result = parser('a') 1088 assert ['a'] == result, result
1089
1090 - def test_fail(self):
1091 matcher = Consumer(Any('b')) 1092 matcher.config.no_full_first_match() 1093 parser = matcher.get_parse() 1094 result = parser('a') 1095 assert None == result, result
1096
1097 - def test_complex(self):
1098 ''' 1099 This test requires evaluation of sub-matchers via trampolining; if 1100 it fails then there may be an issue with generator_matcher. 1101 ''' 1102 parser = Consumer(Any() & Any('b')).get_parse() 1103 result = parser('ab') 1104 assert ['a', 'b'] == result, result
1105
1106 1107 -class PostMatchTest(BaseTest):
1108
1109 - def test_normal(self):
1110 matcher = PostMatch(Drop(Any()[:]) & Any(), r'[0-9]') 1111 matcher.config.no_full_first_match() 1112 results = list(matcher.parse_all('12a')) 1113 assert results == [['2'], ['1']], results
1114
1115 - def test_not(self):
1116 matcher = PostMatch(Drop(Any()[:]) & Any(), r'[0-9]', not_=True) 1117 matcher.config.no_full_first_match() 1118 results = list(matcher.parse_all('12a')) 1119 assert results == [['a']], results
1120