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 stop=None, 245 reduce=([], <built-in function __add__>), 246 rest=FunctionWrapper<Any:<>>('0123456789'), 247 start=1, 248 first=FunctionWrapper<Any:<>>('0123456789')), 249 TransformationWrapper(<add>)), 250 TransformationWrapper(<apply>)), 251 And( 252 And( 253 FunctionWrapper<Literal:<>>('('), 254 Transform:<add>( 255 DepthFirst( 256 generator_manager_queue_len=None, 257 stop=None, 258 reduce=([], <built-in function __add__>), 259 rest=FunctionWrapper<Any:<>>(' \t'), 260 start=0, 261 first=FunctionWrapper<Any:<>>(' \t')), 262 TransformationWrapper(<add>)), 263 [Delayed]), 264 Transform:<add>( 265 DepthFirst( 266 generator_manager_queue_len=None, 267 stop=None, 268 reduce=([], <built-in function __add__>), 269 rest=FunctionWrapper<Any:<>>(' \t'), 270 start=0, 271 first=FunctionWrapper<Any:<>>(' \t')), 272 TransformationWrapper(<add>)), 273 FunctionWrapper<Literal:<>>(')'))), 274 TransformationWrapper(<apply>)), 275 Transform:<add>( 276 DepthFirst( 277 generator_manager_queue_len=None, 278 stop=None, 279 reduce=([], <built-in function __add__>), 280 rest=FunctionWrapper<Any:<>>(' \t'), 281 start=0, 282 first=FunctionWrapper<Any:<>>(' \t')), 283 TransformationWrapper(<add>)), 284 DepthFirst( 285 generator_manager_queue_len=None, 286 stop=None, 287 reduce=([], <built-in function __add__>), 288 rest=And( 289 Transform:<apply>( 290 FunctionWrapper<Any:<>>('*/'), 291 TransformationWrapper(<apply>)), 292 Transform:<add>( 293 DepthFirst( 294 generator_manager_queue_len=None, 295 stop=None, 296 reduce=([], <built-in function __add__>), 297 rest=FunctionWrapper<Any:<>>(' \t'), 298 start=0, 299 first=FunctionWrapper<Any:<>>(' \t')), 300 TransformationWrapper(<add>)), 301 Transform:<apply>( 302 Or( 303 Transform:<apply>( 304 Transform:<add>( 305 DepthFirst( 306 generator_manager_queue_len=None, 307 stop=None, 308 reduce=([], <built-in function __add__>), 309 rest=FunctionWrapper<Any:<>>('0123456789'), 310 start=1, 311 first=FunctionWrapper<Any:<>>('0123456789')), 312 TransformationWrapper(<add>)), 313 TransformationWrapper(<apply>)), 314 And( 315 And( 316 FunctionWrapper<Literal:<>>('('), 317 Transform:<add>( 318 DepthFirst( 319 generator_manager_queue_len=None, 320 stop=None, 321 reduce=([], <built-in function __add__>), 322 rest=FunctionWrapper<Any:<>>(' \t'), 323 start=0, 324 first=FunctionWrapper<Any:<>>(' \t')), 325 TransformationWrapper(<add>)), 326 [Delayed]), 327 Transform:<add>( 328 DepthFirst( 329 generator_manager_queue_len=None, 330 stop=None, 331 reduce=([], <built-in function __add__>), 332 rest=FunctionWrapper<Any:<>>(' \t'), 333 start=0, 334 first=FunctionWrapper<Any:<>>(' \t')), 335 TransformationWrapper(<add>)), 336 FunctionWrapper<Literal:<>>(')'))), 337 TransformationWrapper(<apply>))), 338 start=0, 339 first=And( 340 Transform:<apply>( 341 FunctionWrapper<Any:<>>('*/'), 342 TransformationWrapper(<apply>)), 343 Transform:<add>( 344 DepthFirst( 345 generator_manager_queue_len=None, 346 stop=None, 347 reduce=([], <built-in function __add__>), 348 rest=FunctionWrapper<Any:<>>(' \t'), 349 start=0, 350 first=FunctionWrapper<Any:<>>(' \t')), 351 TransformationWrapper(<add>)), 352 Transform:<apply>( 353 Or( 354 Transform:<apply>( 355 Transform:<add>( 356 DepthFirst( 357 generator_manager_queue_len=None, 358 stop=None, 359 reduce=([], <built-in function __add__>), 360 rest=FunctionWrapper<Any:<>>('0123456789'), 361 start=1, 362 first=FunctionWrapper<Any:<>>('0123456789')), 363 TransformationWrapper(<add>)), 364 TransformationWrapper(<apply>)), 365 And( 366 And( 367 FunctionWrapper<Literal:<>>('('), 368 Transform:<add>( 369 DepthFirst( 370 generator_manager_queue_len=None, 371 stop=None, 372 reduce=([], <built-in function __add__>), 373 rest=FunctionWrapper<Any:<>>(' \t'), 374 start=0, 375 first=FunctionWrapper<Any:<>>(' \t')), 376 TransformationWrapper(<add>)), 377 [Delayed]), 378 Transform:<add>( 379 DepthFirst( 380 generator_manager_queue_len=None, 381 stop=None, 382 reduce=([], <built-in function __add__>), 383 rest=FunctionWrapper<Any:<>>(' \t'), 384 start=0, 385 first=FunctionWrapper<Any:<>>(' \t')), 386 TransformationWrapper(<add>)), 387 FunctionWrapper<Literal:<>>(')'))), 388 TransformationWrapper(<apply>))))), 389 TransformationWrapper(<apply>)), 390 Transform:<add>( 391 DepthFirst( 392 generator_manager_queue_len=None, 393 stop=None, 394 reduce=([], <built-in function __add__>), 395 rest=FunctionWrapper<Any:<>>(' \t'), 396 start=0, 397 first=FunctionWrapper<Any:<>>(' \t')), 398 TransformationWrapper(<add>)), 399 DepthFirst( 400 generator_manager_queue_len=None, 401 stop=None, 402 reduce=([], <built-in function __add__>), 403 rest=And( 404 Transform:<apply>( 405 FunctionWrapper<Any:<>>('+-'), 406 TransformationWrapper(<apply>)), 407 Transform:<add>( 408 DepthFirst( 409 generator_manager_queue_len=None, 410 stop=None, 411 reduce=([], <built-in function __add__>), 412 rest=FunctionWrapper<Any:<>>(' \t'), 413 start=0, 414 first=FunctionWrapper<Any:<>>(' \t')), 415 TransformationWrapper(<add>)), 416 Transform:<apply>( 417 And( 418 Transform:<apply>( 419 Or( 420 Transform:<apply>( 421 Transform:<add>( 422 DepthFirst( 423 generator_manager_queue_len=None, 424 stop=None, 425 reduce=([], <built-in function __add__>), 426 rest=FunctionWrapper<Any:<>>('0123456789'), 427 start=1, 428 first=FunctionWrapper<Any:<>>('0123456789')), 429 TransformationWrapper(<add>)), 430 TransformationWrapper(<apply>)), 431 And( 432 And( 433 FunctionWrapper<Literal:<>>('('), 434 Transform:<add>( 435 DepthFirst( 436 generator_manager_queue_len=None, 437 stop=None, 438 reduce=([], <built-in function __add__>), 439 rest=FunctionWrapper<Any:<>>(' \t'), 440 start=0, 441 first=FunctionWrapper<Any:<>>(' \t')), 442 TransformationWrapper(<add>)), 443 [Delayed]), 444 Transform:<add>( 445 DepthFirst( 446 generator_manager_queue_len=None, 447 stop=None, 448 reduce=([], <built-in function __add__>), 449 rest=FunctionWrapper<Any:<>>(' \t'), 450 start=0, 451 first=FunctionWrapper<Any:<>>(' \t')), 452 TransformationWrapper(<add>)), 453 FunctionWrapper<Literal:<>>(')'))), 454 TransformationWrapper(<apply>)), 455 Transform:<add>( 456 DepthFirst( 457 generator_manager_queue_len=None, 458 stop=None, 459 reduce=([], <built-in function __add__>), 460 rest=FunctionWrapper<Any:<>>(' \t'), 461 start=0, 462 first=FunctionWrapper<Any:<>>(' \t')), 463 TransformationWrapper(<add>)), 464 DepthFirst( 465 generator_manager_queue_len=None, 466 stop=None, 467 reduce=([], <built-in function __add__>), 468 rest=And( 469 Transform:<apply>( 470 FunctionWrapper<Any:<>>('*/'), 471 TransformationWrapper(<apply>)), 472 Transform:<add>( 473 DepthFirst( 474 generator_manager_queue_len=None, 475 stop=None, 476 reduce=([], <built-in function __add__>), 477 rest=FunctionWrapper<Any:<>>(' \t'), 478 start=0, 479 first=FunctionWrapper<Any:<>>(' \t')), 480 TransformationWrapper(<add>)), 481 Transform:<apply>( 482 Or( 483 Transform:<apply>( 484 Transform:<add>( 485 DepthFirst( 486 generator_manager_queue_len=None, 487 stop=None, 488 reduce=([], <built-in function __add__>), 489 rest=FunctionWrapper<Any:<>>('0123456789'), 490 start=1, 491 first=FunctionWrapper<Any:<>>('0123456789')), 492 TransformationWrapper(<add>)), 493 TransformationWrapper(<apply>)), 494 And( 495 And( 496 FunctionWrapper<Literal:<>>('('), 497 Transform:<add>( 498 DepthFirst( 499 generator_manager_queue_len=None, 500 stop=None, 501 reduce=([], <built-in function __add__>), 502 rest=FunctionWrapper<Any:<>>(' \t'), 503 start=0, 504 first=FunctionWrapper<Any:<>>(' \t')), 505 TransformationWrapper(<add>)), 506 [Delayed]), 507 Transform:<add>( 508 DepthFirst( 509 generator_manager_queue_len=None, 510 stop=None, 511 reduce=([], <built-in function __add__>), 512 rest=FunctionWrapper<Any:<>>(' \t'), 513 start=0, 514 first=FunctionWrapper<Any:<>>(' \t')), 515 TransformationWrapper(<add>)), 516 FunctionWrapper<Literal:<>>(')'))), 517 TransformationWrapper(<apply>))), 518 start=0, 519 first=And( 520 Transform:<apply>( 521 FunctionWrapper<Any:<>>('*/'), 522 TransformationWrapper(<apply>)), 523 Transform:<add>( 524 DepthFirst( 525 generator_manager_queue_len=None, 526 stop=None, 527 reduce=([], <built-in function __add__>), 528 rest=FunctionWrapper<Any:<>>(' \t'), 529 start=0, 530 first=FunctionWrapper<Any:<>>(' \t')), 531 TransformationWrapper(<add>)), 532 Transform:<apply>( 533 Or( 534 Transform:<apply>( 535 Transform:<add>( 536 DepthFirst( 537 generator_manager_queue_len=None, 538 stop=None, 539 reduce=([], <built-in function __add__>), 540 rest=FunctionWrapper<Any:<>>('0123456789'), 541 start=1, 542 first=FunctionWrapper<Any:<>>('0123456789')), 543 TransformationWrapper(<add>)), 544 TransformationWrapper(<apply>)), 545 And( 546 And( 547 FunctionWrapper<Literal:<>>('('), 548 Transform:<add>( 549 DepthFirst( 550 generator_manager_queue_len=None, 551 stop=None, 552 reduce=([], <built-in function __add__>), 553 rest=FunctionWrapper<Any:<>>(' \t'), 554 start=0, 555 first=FunctionWrapper<Any:<>>(' \t')), 556 TransformationWrapper(<add>)), 557 [Delayed]), 558 Transform:<add>( 559 DepthFirst( 560 generator_manager_queue_len=None, 561 stop=None, 562 reduce=([], <built-in function __add__>), 563 rest=FunctionWrapper<Any:<>>(' \t'), 564 start=0, 565 first=FunctionWrapper<Any:<>>(' \t')), 566 TransformationWrapper(<add>)), 567 FunctionWrapper<Literal:<>>(')'))), 568 TransformationWrapper(<apply>))))), 569 TransformationWrapper(<apply>))), 570 start=0, 571 first=And( 572 Transform:<apply>( 573 FunctionWrapper<Any:<>>('+-'), 574 TransformationWrapper(<apply>)), 575 Transform:<add>( 576 DepthFirst( 577 generator_manager_queue_len=None, 578 stop=None, 579 reduce=([], <built-in function __add__>), 580 rest=FunctionWrapper<Any:<>>(' \t'), 581 start=0, 582 first=FunctionWrapper<Any:<>>(' \t')), 583 TransformationWrapper(<add>)), 584 Transform:<apply>( 585 And( 586 Transform:<apply>( 587 Or( 588 Transform:<apply>( 589 Transform:<add>( 590 DepthFirst( 591 generator_manager_queue_len=None, 592 stop=None, 593 reduce=([], <built-in function __add__>), 594 rest=FunctionWrapper<Any:<>>('0123456789'), 595 start=1, 596 first=FunctionWrapper<Any:<>>('0123456789')), 597 TransformationWrapper(<add>)), 598 TransformationWrapper(<apply>)), 599 And( 600 And( 601 FunctionWrapper<Literal:<>>('('), 602 Transform:<add>( 603 DepthFirst( 604 generator_manager_queue_len=None, 605 stop=None, 606 reduce=([], <built-in function __add__>), 607 rest=FunctionWrapper<Any:<>>(' \t'), 608 start=0, 609 first=FunctionWrapper<Any:<>>(' \t')), 610 TransformationWrapper(<add>)), 611 [Delayed]), 612 Transform:<add>( 613 DepthFirst( 614 generator_manager_queue_len=None, 615 stop=None, 616 reduce=([], <built-in function __add__>), 617 rest=FunctionWrapper<Any:<>>(' \t'), 618 start=0, 619 first=FunctionWrapper<Any:<>>(' \t')), 620 TransformationWrapper(<add>)), 621 FunctionWrapper<Literal:<>>(')'))), 622 TransformationWrapper(<apply>)), 623 Transform:<add>( 624 DepthFirst( 625 generator_manager_queue_len=None, 626 stop=None, 627 reduce=([], <built-in function __add__>), 628 rest=FunctionWrapper<Any:<>>(' \t'), 629 start=0, 630 first=FunctionWrapper<Any:<>>(' \t')), 631 TransformationWrapper(<add>)), 632 DepthFirst( 633 generator_manager_queue_len=None, 634 stop=None, 635 reduce=([], <built-in function __add__>), 636 rest=And( 637 Transform:<apply>( 638 FunctionWrapper<Any:<>>('*/'), 639 TransformationWrapper(<apply>)), 640 Transform:<add>( 641 DepthFirst( 642 generator_manager_queue_len=None, 643 stop=None, 644 reduce=([], <built-in function __add__>), 645 rest=FunctionWrapper<Any:<>>(' \t'), 646 start=0, 647 first=FunctionWrapper<Any:<>>(' \t')), 648 TransformationWrapper(<add>)), 649 Transform:<apply>( 650 Or( 651 Transform:<apply>( 652 Transform:<add>( 653 DepthFirst( 654 generator_manager_queue_len=None, 655 stop=None, 656 reduce=([], <built-in function __add__>), 657 rest=FunctionWrapper<Any:<>>('0123456789'), 658 start=1, 659 first=FunctionWrapper<Any:<>>('0123456789')), 660 TransformationWrapper(<add>)), 661 TransformationWrapper(<apply>)), 662 And( 663 And( 664 FunctionWrapper<Literal:<>>('('), 665 Transform:<add>( 666 DepthFirst( 667 generator_manager_queue_len=None, 668 stop=None, 669 reduce=([], <built-in function __add__>), 670 rest=FunctionWrapper<Any:<>>(' \t'), 671 start=0, 672 first=FunctionWrapper<Any:<>>(' \t')), 673 TransformationWrapper(<add>)), 674 [Delayed]), 675 Transform:<add>( 676 DepthFirst( 677 generator_manager_queue_len=None, 678 stop=None, 679 reduce=([], <built-in function __add__>), 680 rest=FunctionWrapper<Any:<>>(' \t'), 681 start=0, 682 first=FunctionWrapper<Any:<>>(' \t')), 683 TransformationWrapper(<add>)), 684 FunctionWrapper<Literal:<>>(')'))), 685 TransformationWrapper(<apply>))), 686 start=0, 687 first=And( 688 Transform:<apply>( 689 FunctionWrapper<Any:<>>('*/'), 690 TransformationWrapper(<apply>)), 691 Transform:<add>( 692 DepthFirst( 693 generator_manager_queue_len=None, 694 stop=None, 695 reduce=([], <built-in function __add__>), 696 rest=FunctionWrapper<Any:<>>(' \t'), 697 start=0, 698 first=FunctionWrapper<Any:<>>(' \t')), 699 TransformationWrapper(<add>)), 700 Transform:<apply>( 701 Or( 702 Transform:<apply>( 703 Transform:<add>( 704 DepthFirst( 705 generator_manager_queue_len=None, 706 stop=None, 707 reduce=([], <built-in function __add__>), 708 rest=FunctionWrapper<Any:<>>('0123456789'), 709 start=1, 710 first=FunctionWrapper<Any:<>>('0123456789')), 711 TransformationWrapper(<add>)), 712 TransformationWrapper(<apply>)), 713 And( 714 And( 715 FunctionWrapper<Literal:<>>('('), 716 Transform:<add>( 717 DepthFirst( 718 generator_manager_queue_len=None, 719 stop=None, 720 reduce=([], <built-in function __add__>), 721 rest=FunctionWrapper<Any:<>>(' \t'), 722 start=0, 723 first=FunctionWrapper<Any:<>>(' \t')), 724 TransformationWrapper(<add>)), 725 [Delayed]), 726 Transform:<add>( 727 DepthFirst( 728 generator_manager_queue_len=None, 729 stop=None, 730 reduce=([], <built-in function __add__>), 731 rest=FunctionWrapper<Any:<>>(' \t'), 732 start=0, 733 first=FunctionWrapper<Any:<>>(' \t')), 734 TransformationWrapper(<add>)), 735 FunctionWrapper<Literal:<>>(')'))), 736 TransformationWrapper(<apply>))))), 737 TransformationWrapper(<apply>))))), 738 TransformationWrapper(<apply>)))''') 739 parser = expression.get_parse() 740 description = parser.matcher.tree() 741 self.assert_same(description, r"""TrampolineWrapper<FullFirstMatch> 742 +- Delayed 743 | `- matcher Transform:<apply> 744 | +- _RMemo 745 | | `- TrampolineWrapper<And> 746 | | +- Transform:<apply> 747 | | | +- _RMemo 748 | | | | `- TrampolineWrapper<And> 749 | | | | +- Transform:<apply> 750 | | | | | +- _RMemo 751 | | | | | | `- TrampolineWrapper<Or> 752 | | | | | | +- _RMemo 753 | | | | | | | `- NfaRegexp:<empty_adapter,apply> 754 | | | | | | | +- Sequence(...) 755 | | | | | | | `- alphabet <Unicode> 756 | | | | | | `- _RMemo 757 | | | | | | `- TrampolineWrapper<And> 758 | | | | | | +- _RMemo 759 | | | | | | | `- FunctionWrapper<Literal:<>> 760 | | | | | | | `- '(' 761 | | | | | | +- _RMemo 762 | | | | | | | `- NfaRegexp:<empty_adapter> 763 | | | | | | | +- Sequence(...) 764 | | | | | | | `- alphabet <Unicode> 765 | | | | | | +- Delayed 766 | | | | | | | `- matcher <loop> 767 | | | | | | +- _RMemo 768 | | | | | | | `- NfaRegexp:<empty_adapter> 769 | | | | | | | +- Sequence(...) 770 | | | | | | | `- alphabet <Unicode> 771 | | | | | | `- _RMemo 772 | | | | | | `- FunctionWrapper<Literal:<>> 773 | | | | | | `- ')' 774 | | | | | `- TransformationWrapper(<apply>) 775 | | | | +- _RMemo 776 | | | | | `- NfaRegexp:<empty_adapter> 777 | | | | | +- Sequence(...) 778 | | | | | `- alphabet <Unicode> 779 | | | | `- _RMemo 780 | | | | `- TrampolineWrapper<DepthFirst> 781 | | | | +- generator_manager_queue_len None 782 | | | | +- stop None 783 | | | | +- reduce ([], <built-in function __add__>) 784 | | | | +- rest _RMemo 785 | | | | | `- TrampolineWrapper<And> 786 | | | | | +- _RMemo 787 | | | | | | `- FunctionWrapper<Any:<apply>> 788 | | | | | | `- '*/' 789 | | | | | +- _RMemo 790 | | | | | | `- NfaRegexp:<empty_adapter> 791 | | | | | | +- Sequence(...) 792 | | | | | | `- alphabet <Unicode> 793 | | | | | `- Transform:<apply> 794 | | | | | +- _RMemo 795 | | | | | | `- TrampolineWrapper<Or> 796 | | | | | | +- _RMemo 797 | | | | | | | `- NfaRegexp:<empty_adapter,apply> 798 | | | | | | | +- Sequence(...) 799 | | | | | | | `- alphabet <Unicode> 800 | | | | | | `- _RMemo 801 | | | | | | `- TrampolineWrapper<And> 802 | | | | | | +- _RMemo 803 | | | | | | | `- FunctionWrapper<Literal:<>> 804 | | | | | | | `- '(' 805 | | | | | | +- _RMemo 806 | | | | | | | `- NfaRegexp:<empty_adapter> 807 | | | | | | | +- Sequence(...) 808 | | | | | | | `- alphabet <Unicode> 809 | | | | | | +- Delayed 810 | | | | | | | `- matcher <loop> 811 | | | | | | +- _RMemo 812 | | | | | | | `- NfaRegexp:<empty_adapter> 813 | | | | | | | +- Sequence(...) 814 | | | | | | | `- alphabet <Unicode> 815 | | | | | | `- _RMemo 816 | | | | | | `- FunctionWrapper<Literal:<>> 817 | | | | | | `- ')' 818 | | | | | `- TransformationWrapper(<apply>) 819 | | | | +- start 0 820 | | | | `- first _RMemo 821 | | | | `- TrampolineWrapper<And> 822 | | | | +- _RMemo 823 | | | | | `- FunctionWrapper<Any:<apply>> 824 | | | | | `- '*/' 825 | | | | +- _RMemo 826 | | | | | `- NfaRegexp:<empty_adapter> 827 | | | | | +- Sequence(...) 828 | | | | | `- alphabet <Unicode> 829 | | | | `- Transform:<apply> 830 | | | | +- _RMemo 831 | | | | | `- TrampolineWrapper<Or> 832 | | | | | +- _RMemo 833 | | | | | | `- NfaRegexp:<empty_adapter,apply> 834 | | | | | | +- Sequence(...) 835 | | | | | | `- alphabet <Unicode> 836 | | | | | `- _RMemo 837 | | | | | `- TrampolineWrapper<And> 838 | | | | | +- _RMemo 839 | | | | | | `- FunctionWrapper<Literal:<>> 840 | | | | | | `- '(' 841 | | | | | +- _RMemo 842 | | | | | | `- NfaRegexp:<empty_adapter> 843 | | | | | | +- Sequence(...) 844 | | | | | | `- alphabet <Unicode> 845 | | | | | +- Delayed 846 | | | | | | `- matcher <loop> 847 | | | | | +- _RMemo 848 | | | | | | `- NfaRegexp:<empty_adapter> 849 | | | | | | +- Sequence(...) 850 | | | | | | `- alphabet <Unicode> 851 | | | | | `- _RMemo 852 | | | | | `- FunctionWrapper<Literal:<>> 853 | | | | | `- ')' 854 | | | | `- TransformationWrapper(<apply>) 855 | | | `- TransformationWrapper(<apply>) 856 | | +- _RMemo 857 | | | `- NfaRegexp:<empty_adapter> 858 | | | +- Sequence(...) 859 | | | `- alphabet <Unicode> 860 | | `- _RMemo 861 | | `- TrampolineWrapper<DepthFirst> 862 | | +- generator_manager_queue_len None 863 | | +- stop None 864 | | +- reduce ([], <built-in function __add__>) 865 | | +- rest _RMemo 866 | | | `- TrampolineWrapper<And> 867 | | | +- _RMemo 868 | | | | `- FunctionWrapper<Any:<apply>> 869 | | | | `- '+-' 870 | | | +- _RMemo 871 | | | | `- NfaRegexp:<empty_adapter> 872 | | | | +- Sequence(...) 873 | | | | `- alphabet <Unicode> 874 | | | `- Transform:<apply> 875 | | | +- _RMemo 876 | | | | `- TrampolineWrapper<And> 877 | | | | +- Transform:<apply> 878 | | | | | +- _RMemo 879 | | | | | | `- TrampolineWrapper<Or> 880 | | | | | | +- _RMemo 881 | | | | | | | `- NfaRegexp:<empty_adapter,apply> 882 | | | | | | | +- Sequence(...) 883 | | | | | | | `- alphabet <Unicode> 884 | | | | | | `- _RMemo 885 | | | | | | `- TrampolineWrapper<And> 886 | | | | | | +- _RMemo 887 | | | | | | | `- FunctionWrapper<Literal:<>> 888 | | | | | | | `- '(' 889 | | | | | | +- _RMemo 890 | | | | | | | `- NfaRegexp:<empty_adapter> 891 | | | | | | | +- Sequence(...) 892 | | | | | | | `- alphabet <Unicode> 893 | | | | | | +- Delayed 894 | | | | | | | `- matcher <loop> 895 | | | | | | +- _RMemo 896 | | | | | | | `- NfaRegexp:<empty_adapter> 897 | | | | | | | +- Sequence(...) 898 | | | | | | | `- alphabet <Unicode> 899 | | | | | | `- _RMemo 900 | | | | | | `- FunctionWrapper<Literal:<>> 901 | | | | | | `- ')' 902 | | | | | `- TransformationWrapper(<apply>) 903 | | | | +- _RMemo 904 | | | | | `- NfaRegexp:<empty_adapter> 905 | | | | | +- Sequence(...) 906 | | | | | `- alphabet <Unicode> 907 | | | | `- _RMemo 908 | | | | `- TrampolineWrapper<DepthFirst> 909 | | | | +- generator_manager_queue_len None 910 | | | | +- stop None 911 | | | | +- reduce ([], <built-in function __add__>) 912 | | | | +- rest _RMemo 913 | | | | | `- TrampolineWrapper<And> 914 | | | | | +- _RMemo 915 | | | | | | `- FunctionWrapper<Any:<apply>> 916 | | | | | | `- '*/' 917 | | | | | +- _RMemo 918 | | | | | | `- NfaRegexp:<empty_adapter> 919 | | | | | | +- Sequence(...) 920 | | | | | | `- alphabet <Unicode> 921 | | | | | `- Transform:<apply> 922 | | | | | +- _RMemo 923 | | | | | | `- TrampolineWrapper<Or> 924 | | | | | | +- _RMemo 925 | | | | | | | `- NfaRegexp:<empty_adapter,apply> 926 | | | | | | | +- Sequence(...) 927 | | | | | | | `- alphabet <Unicode> 928 | | | | | | `- _RMemo 929 | | | | | | `- TrampolineWrapper<And> 930 | | | | | | +- _RMemo 931 | | | | | | | `- FunctionWrapper<Literal:<>> 932 | | | | | | | `- '(' 933 | | | | | | +- _RMemo 934 | | | | | | | `- NfaRegexp:<empty_adapter> 935 | | | | | | | +- Sequence(...) 936 | | | | | | | `- alphabet <Unicode> 937 | | | | | | +- Delayed 938 | | | | | | | `- matcher <loop> 939 | | | | | | +- _RMemo 940 | | | | | | | `- NfaRegexp:<empty_adapter> 941 | | | | | | | +- Sequence(...) 942 | | | | | | | `- alphabet <Unicode> 943 | | | | | | `- _RMemo 944 | | | | | | `- FunctionWrapper<Literal:<>> 945 | | | | | | `- ')' 946 | | | | | `- TransformationWrapper(<apply>) 947 | | | | +- start 0 948 | | | | `- first _RMemo 949 | | | | `- TrampolineWrapper<And> 950 | | | | +- _RMemo 951 | | | | | `- FunctionWrapper<Any:<apply>> 952 | | | | | `- '*/' 953 | | | | +- _RMemo 954 | | | | | `- NfaRegexp:<empty_adapter> 955 | | | | | +- Sequence(...) 956 | | | | | `- alphabet <Unicode> 957 | | | | `- Transform:<apply> 958 | | | | +- _RMemo 959 | | | | | `- TrampolineWrapper<Or> 960 | | | | | +- _RMemo 961 | | | | | | `- NfaRegexp:<empty_adapter,apply> 962 | | | | | | +- Sequence(...) 963 | | | | | | `- alphabet <Unicode> 964 | | | | | `- _RMemo 965 | | | | | `- TrampolineWrapper<And> 966 | | | | | +- _RMemo 967 | | | | | | `- FunctionWrapper<Literal:<>> 968 | | | | | | `- '(' 969 | | | | | +- _RMemo 970 | | | | | | `- NfaRegexp:<empty_adapter> 971 | | | | | | +- Sequence(...) 972 | | | | | | `- alphabet <Unicode> 973 | | | | | +- Delayed 974 | | | | | | `- matcher <loop> 975 | | | | | +- _RMemo 976 | | | | | | `- NfaRegexp:<empty_adapter> 977 | | | | | | +- Sequence(...) 978 | | | | | | `- alphabet <Unicode> 979 | | | | | `- _RMemo 980 | | | | | `- FunctionWrapper<Literal:<>> 981 | | | | | `- ')' 982 | | | | `- TransformationWrapper(<apply>) 983 | | | `- TransformationWrapper(<apply>) 984 | | +- start 0 985 | | `- first _RMemo 986 | | `- TrampolineWrapper<And> 987 | | +- _RMemo 988 | | | `- FunctionWrapper<Any:<apply>> 989 | | | `- '+-' 990 | | +- _RMemo 991 | | | `- NfaRegexp:<empty_adapter> 992 | | | +- Sequence(...) 993 | | | `- alphabet <Unicode> 994 | | `- Transform:<apply> 995 | | +- _RMemo 996 | | | `- TrampolineWrapper<And> 997 | | | +- Transform:<apply> 998 | | | | +- _RMemo 999 | | | | | `- TrampolineWrapper<Or> 1000 | | | | | +- _RMemo 1001 | | | | | | `- NfaRegexp:<empty_adapter,apply> 1002 | | | | | | +- Sequence(...) 1003 | | | | | | `- alphabet <Unicode> 1004 | | | | | `- _RMemo 1005 | | | | | `- TrampolineWrapper<And> 1006 | | | | | +- _RMemo 1007 | | | | | | `- FunctionWrapper<Literal:<>> 1008 | | | | | | `- '(' 1009 | | | | | +- _RMemo 1010 | | | | | | `- NfaRegexp:<empty_adapter> 1011 | | | | | | +- Sequence(...) 1012 | | | | | | `- alphabet <Unicode> 1013 | | | | | +- Delayed 1014 | | | | | | `- matcher <loop> 1015 | | | | | +- _RMemo 1016 | | | | | | `- NfaRegexp:<empty_adapter> 1017 | | | | | | +- Sequence(...) 1018 | | | | | | `- alphabet <Unicode> 1019 | | | | | `- _RMemo 1020 | | | | | `- FunctionWrapper<Literal:<>> 1021 | | | | | `- ')' 1022 | | | | `- TransformationWrapper(<apply>) 1023 | | | +- _RMemo 1024 | | | | `- NfaRegexp:<empty_adapter> 1025 | | | | +- Sequence(...) 1026 | | | | `- alphabet <Unicode> 1027 | | | `- _RMemo 1028 | | | `- TrampolineWrapper<DepthFirst> 1029 | | | +- generator_manager_queue_len None 1030 | | | +- stop None 1031 | | | +- reduce ([], <built-in function __add__>) 1032 | | | +- rest _RMemo 1033 | | | | `- TrampolineWrapper<And> 1034 | | | | +- _RMemo 1035 | | | | | `- FunctionWrapper<Any:<apply>> 1036 | | | | | `- '*/' 1037 | | | | +- _RMemo 1038 | | | | | `- NfaRegexp:<empty_adapter> 1039 | | | | | +- Sequence(...) 1040 | | | | | `- alphabet <Unicode> 1041 | | | | `- Transform:<apply> 1042 | | | | +- _RMemo 1043 | | | | | `- TrampolineWrapper<Or> 1044 | | | | | +- _RMemo 1045 | | | | | | `- NfaRegexp:<empty_adapter,apply> 1046 | | | | | | +- Sequence(...) 1047 | | | | | | `- alphabet <Unicode> 1048 | | | | | `- _RMemo 1049 | | | | | `- TrampolineWrapper<And> 1050 | | | | | +- _RMemo 1051 | | | | | | `- FunctionWrapper<Literal:<>> 1052 | | | | | | `- '(' 1053 | | | | | +- _RMemo 1054 | | | | | | `- NfaRegexp:<empty_adapter> 1055 | | | | | | +- Sequence(...) 1056 | | | | | | `- alphabet <Unicode> 1057 | | | | | +- Delayed 1058 | | | | | | `- matcher <loop> 1059 | | | | | +- _RMemo 1060 | | | | | | `- NfaRegexp:<empty_adapter> 1061 | | | | | | +- Sequence(...) 1062 | | | | | | `- alphabet <Unicode> 1063 | | | | | `- _RMemo 1064 | | | | | `- FunctionWrapper<Literal:<>> 1065 | | | | | `- ')' 1066 | | | | `- TransformationWrapper(<apply>) 1067 | | | +- start 0 1068 | | | `- first _RMemo 1069 | | | `- TrampolineWrapper<And> 1070 | | | +- _RMemo 1071 | | | | `- FunctionWrapper<Any:<apply>> 1072 | | | | `- '*/' 1073 | | | +- _RMemo 1074 | | | | `- NfaRegexp:<empty_adapter> 1075 | | | | +- Sequence(...) 1076 | | | | `- alphabet <Unicode> 1077 | | | `- Transform:<apply> 1078 | | | +- _RMemo 1079 | | | | `- TrampolineWrapper<Or> 1080 | | | | +- _RMemo 1081 | | | | | `- NfaRegexp:<empty_adapter,apply> 1082 | | | | | +- Sequence(...) 1083 | | | | | `- alphabet <Unicode> 1084 | | | | `- _RMemo 1085 | | | | `- TrampolineWrapper<And> 1086 | | | | +- _RMemo 1087 | | | | | `- FunctionWrapper<Literal:<>> 1088 | | | | | `- '(' 1089 | | | | +- _RMemo 1090 | | | | | `- NfaRegexp:<empty_adapter> 1091 | | | | | +- Sequence(...) 1092 | | | | | `- alphabet <Unicode> 1093 | | | | +- Delayed 1094 | | | | | `- matcher <loop> 1095 | | | | +- _RMemo 1096 | | | | | `- NfaRegexp:<empty_adapter> 1097 | | | | | +- Sequence(...) 1098 | | | | | `- alphabet <Unicode> 1099 | | | | `- _RMemo 1100 | | | | `- FunctionWrapper<Literal:<>> 1101 | | | | `- ')' 1102 | | | `- TransformationWrapper(<apply>) 1103 | | `- TransformationWrapper(<apply>) 1104 | `- TransformationWrapper(<apply>) 1105 `- True""")
1106
1107 -class ColumnsTest(BaseTest):
1108
1109 - def test_columns(self):
1110 self.assert_direct('0123456789', 1111 Columns(((0,3), Any()[3,...]), 1112 ((0,4), Any()[4:,...]), 1113 ((5,8), Any()[3:,...])), 1114 [['012', '0123', '567']])
1115
1116 - def test_table(self):
1117 #basicConfig(level=DEBUG) 1118 self.assert_direct( 1119 '''0123456789 1120 abcdefghij 1121 ''', 1122 Columns(((0,3), Any()[3:,...]), 1123 ((0,4), Any()[4:,...]), 1124 ((5,8), Any()[3:,...]))[2], 1125 [['012', '0123', '567', 1126 'abc', 'abcd', 'fgh']])
1127
1128 1129 -class ConsumerTest(BaseTest):
1130
1131 - def test_simple(self):
1132 parser = Consumer(Any()).get_parse() 1133 result = parser('a') 1134 assert ['a'] == result, result
1135
1136 - def test_fail(self):
1137 matcher = Consumer(Any('b')) 1138 matcher.config.no_full_first_match() 1139 parser = matcher.get_parse() 1140 result = parser('a') 1141 assert None == result, result
1142
1143 - def test_complex(self):
1144 ''' 1145 This test requires evaluation of sub-matchers via trampolining; if 1146 it fails then there may be an issue with generator_matcher. 1147 ''' 1148 parser = Consumer(Any() & Any('b')).get_parse() 1149 result = parser('ab') 1150 assert ['a', 'b'] == result, result
1151
1152 1153 -class PostMatchTest(BaseTest):
1154
1155 - def test_normal(self):
1156 matcher = PostMatch(Drop(Any()[:]) & Any(), r'[0-9]') 1157 matcher.config.no_full_first_match() 1158 results = list(matcher.parse_all('12a')) 1159 assert results == [['2'], ['1']], results
1160
1161 - def test_not(self):
1162 matcher = PostMatch(Drop(Any()[:]) & Any(), r'[0-9]', not_=True) 1163 matcher.config.no_full_first_match() 1164 results = list(matcher.parse_all('12a')) 1165 assert results == [['a']], results
1166