1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
3

T'ícqàã@s¨dZddlmZdZddlZddlZddlZddlmZddl	m
Z
ejd5krReZ
Gdd	„d	eƒZGd
d„deƒZGdd
„d
eƒZGdd„deƒZGdd„deƒZGdd„deeƒZGdd„deeƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„deƒZGd d!„d!eƒZGd"d#„d#eƒZGd$d%„d%eƒZGd&d'„d'eƒZGd(d)„d)eƒZ Gd*d+„d+eƒZ!Gd,d-„d-eƒZ"d.d/ej#d0ƒfd1d2„Z$d3d4„Z%dS)6a

A finite state machine specialized for regular-expression-based text filters,
this module defines the following classes:

- `StateMachine`, a state machine
- `State`, a state superclass
- `StateMachineWS`, a whitespace-sensitive version of `StateMachine`
- `StateWS`, a state superclass for use with `StateMachineWS`
- `SearchStateMachine`, uses `re.search()` instead of `re.match()`
- `SearchStateMachineWS`, uses `re.search()` instead of `re.match()`
- `ViewList`, extends standard Python lists.
- `StringList`, string-specific ViewList.

Exception classes:

- `StateMachineError`
- `UnknownStateError`
- `DuplicateStateError`
- `UnknownTransitionError`
- `DuplicateTransitionError`
- `TransitionPatternNotFound`
- `TransitionMethodNotFound`
- `UnexpectedIndentationError`
- `TransitionCorrection`: Raised to switch to another transition.
- `StateCorrection`: Raised to switch to another state & transition.

Functions:

- `string2lines()`: split a multi-line string into a list of one-line strings


How To Use This Module
======================
(See the individual classes, methods, and attributes for details.)

1. Import it: ``import statemachine`` or ``from statemachine import ...``.
   You will also need to ``import re``.

2. Derive a subclass of `State` (or `StateWS`) for each state in your state
   machine::

       class MyState(statemachine.State):

   Within the state's class definition:

   a) Include a pattern for each transition, in `State.patterns`::

          patterns = {'atransition': r'pattern', ...}

   b) Include a list of initial transitions to be set up automatically, in
      `State.initial_transitions`::

          initial_transitions = ['atransition', ...]

   c) Define a method for each transition, with the same name as the
      transition pattern::

          def atransition(self, match, context, next_state):
              # do something
              result = [...]  # a list
              return context, next_state, result
              # context, next_state may be altered

      Transition methods may raise an `EOFError` to cut processing short.

   d) You may wish to override the `State.bof()` and/or `State.eof()` implicit
      transition methods, which handle the beginning- and end-of-file.

   e) In order to handle nested processing, you may wish to override the
      attributes `State.nested_sm` and/or `State.nested_sm_kwargs`.

      If you are using `StateWS` as a base class, in order to handle nested
      indented blocks, you may wish to:

      - override the attributes `StateWS.indent_sm`,
        `StateWS.indent_sm_kwargs`, `StateWS.known_indent_sm`, and/or
        `StateWS.known_indent_sm_kwargs`;
      - override the `StateWS.blank()` method; and/or
      - override or extend the `StateWS.indent()`, `StateWS.known_indent()`,
        and/or `StateWS.firstknown_indent()` methods.

3. Create a state machine object::

       sm = StateMachine(state_classes=[MyState, ...],
                         initial_state='MyState')

4. Obtain the input text, which needs to be converted into a tab-free list of
   one-line strings. For example, to read text from a file called
   'inputfile'::

       input_string = open('inputfile').read()
       input_lines = statemachine.string2lines(input_string)

5. Run the state machine on the input text and collect the results, a list::

       results = sm.run(input_lines)

6. Remove any lingering circular references::

       sm.unlink()
é)Úprint_functionZrestructuredtextN)Úutils)ÚErrorOutputéc@sàeZdZdZd6dd„Zdd„Zd7d	d
„Zd8dd„Zd9dd„Zdd„Z	dd„Z
dd„Zd:dd„Zdd„Z
dd„Zdd„Zdd„Zd;d d!„Zd"d#„Zd<d$d%„Zd=d&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„ZdS)>ÚStateMachinea´
    A finite state machine for text filters using regular expressions.

    The input is provided in the form of a list of one-line strings (no
    newlines). States are subclasses of the `State` class. Transitions consist
    of regular expression patterns and transition methods, and are defined in
    each state.

    The state machine is started with the `run()` method, which returns the
    results of processing in a list.
    FcCsLd|_d|_d|_d|_||_||_||_i|_|j|ƒg|_	t
ƒ|_dS)a+
        Initialize a `StateMachine` object; add state objects.

        Parameters:

        - `state_classes`: a list of `State` (sub)classes.
        - `initial_state`: a string, the class name of the initial state.
        - `debug`: a boolean; produce verbose output if true (nonzero).
        Nrééÿÿÿÿ)Úinput_linesÚinput_offsetÚlineÚline_offsetÚdebugÚ
initial_stateÚ
current_stateÚstatesÚ
add_statesÚ	observersrÚ_stderr)ÚselfÚ
state_classesrr
©rú9/tmp/pip-build-gk9425m9/docutils/docutils/statemachine.pyÚ__init__†s
zStateMachine.__init__cCs&x|jjƒD]}|jƒqWd|_dS)z9Remove circular references to objects no longer required.N)rÚvaluesÚunlink)rÚstaterrrrµszStateMachine.unlinkrNcCsn|jƒt|tƒr||_nt||d|_||_d
|_|p<|j|_|jrft	d|jdj
|jƒf|jdd}g}|jƒ}yÌ|jrŽt	d|jd|j
|ƒ\}}	|j|	ƒx˜y¬yR|jƒ|jrè|jj|jƒ\}
}t	d|
||jf|jd|j|||ƒ\}}}	WnJtk
rH|jr.t	d	|jj|jd|j|ƒ}	|j|	ƒPYnX|j|	ƒWnØtk
r¶}
zB|jƒ|
jd
f}|jr¤t	d|jj|d
f|jdwªWYdd}
~
Xn~tk
r.}
z\|jƒ|
jd
}t|
jƒdkrðd}n|
jdf}|jrt	d||d
f|jdWYdd}
~
XnXd}|j|ƒ}qªWWn|jr\|jƒ‚YnXg|_|S)aÜ
        Run the state machine on `input_lines`. Return results (a list).

        Reset `self.line_offset` and `self.current_state`. Run the
        beginning-of-file transition. Input one line at a time and check for a
        matching transition. If a match is found, call the transition method
        and possibly change the state. Store the context returned by the
        transition method to be passed on to the next transition matched.
        Accumulate the results returned by the transition methods in a list.
        Run the end-of-file transition. Finally, return the accumulated
        results.

        Parameters:

        - `input_lines`: a list of strings without newlines, or `StringList`.
        - `input_offset`: the line offset of `input_lines` from the beginning
          of the file.
        - `context`: application-specific storage.
        - `input_source`: name or path of source of `input_lines`.
        - `initial_state`: name of initial state.
        )Úsourcerz5
StateMachine.run: input_lines (line_offset=%s):
| %sz
| )ÚfileNz!
StateMachine.run: bof transitionz4
StateMachine.run: line (source=%r, offset=%r):
| %sz$
StateMachine.run: %s.eof transitionrzE
StateMachine.run: TransitionCorrection to state "%s", transition %s.z@
StateMachine.run: StateCorrection to state "%s", transition %s.r)Úruntime_initÚ
isinstanceÚ
StringListr	r
rrrr
ÚprintÚjoinrÚ	get_stateÚbofÚextendÚ	next_lineÚinforÚ
check_lineÚEOFErrorÚ	__class__Ú__name__ÚeofÚTransitionCorrectionÚ
previous_lineÚargsÚStateCorrectionÚlenÚerrorr)rr	r
ÚcontextZinput_sourcerÚtransitionsÚresultsrÚresultrÚoffsetÚ
next_stateÚ	exceptionrrrÚrun»sz




(zStateMachine.runcCsh|r8|jr2||jkr2td|j||jƒf|jd||_y|j|jStk
rbt|jƒ‚YnXdS)zá
        Return current state object; set it first if `next_state` given.

        Parameter `next_state`: a string, the name of the next state.

        Exception: `UnknownStateError` raised if `next_state` unknown.
        zJ
StateMachine.get_state: Changing state from "%s" to "%s" (input line %s).)rN)r
rr!Úabs_line_numberrrÚKeyErrorÚUnknownStateError)rr8rrrr#szStateMachine.get_statercCsVzFy |j|7_|j|j|_Wntk
r@d|_t‚YnX|jS|jƒXdS)z9Load `self.line` with the `n`'th next line and return it.N)rr	rÚ
IndexErrorr)Únotify_observers)rÚnrrrr&+s
zStateMachine.next_linecCs0y|j|jdjƒStk
r*dSXdS)z3Return 1 if the next line is blank or non-existant.rN)r	rÚstripr>)rrrrÚis_next_line_blank8szStateMachine.is_next_line_blankcCs|jt|jƒdkS)z0Return 1 if the input is at or past end-of-file.r)rr1r	)rrrrÚat_eof?szStateMachine.at_eofcCs
|jdkS)z8Return 1 if the input is at or before beginning-of-file.r)r)rrrrÚat_bofCszStateMachine.at_bofcCs<|j|8_|jdkr d|_n|j|j|_|jƒ|jS)z=Load `self.line` with the `n`'th previous line and return it.rN)rrr	r?)rr@rrrr.Gs
zStateMachine.previous_linecCsTzDy||j|_|j|j|_Wntk
r>d|_t‚YnX|jS|jƒXdS)z?Jump to absolute line offset `line_offset`, load and return it.N)r
rr	rr>r)r?)rrrrrÚ	goto_lineQs
zStateMachine.goto_linecCs|jj||jƒS)z<Return source of line at absolute line offset `line_offset`.)r	rr
)rrrrrÚ
get_source^szStateMachine.get_sourcecCs|j|jS)z;Return line offset of current line, from beginning of file.)rr
)rrrrÚabs_line_offsetbszStateMachine.abs_line_offsetcCs|j|jdS)z5Return line number of current line (counting from 1).r)rr
)rrrrr;fszStateMachine.abs_line_numbercCsŽ|dkr|j}n||jd}y|jj|ƒ\}}|d}WnJtk
rj|j||jƒ\}}||dfStk
r„d\}}YnX||fS)a\Return (source, line) tuple for current or given line number.

        Looks up the source and line number in the `self.input_lines`
        StringList instance to count for included source files.

        If the optional argument `lineno` is given, convert it from an
        absolute line number to the corresponding (source, line) pair.
        Nr)NN)rr
r	r'Ú	TypeErrorÚget_source_and_liner>)rÚlinenor7ÚsrcZ	srcoffsetZsrclinerrrrIjs	z StateMachine.get_source_and_linecCs^|jj|jddd|t|ƒd|jj|jddd|dd|jj|jdt||ƒƒdS)NrÚzinternal padding after )rr7zinternal padding before ér)r	Úinsertrr1r )rr	rrrrÚinsert_input…szStateMachine.insert_inputcCsly&|jj|j|ƒ}|jt|ƒdƒ|Stk
rf}z$|jd}|jt|ƒdƒ‚WYdd}~XnXdS)zÜ
        Return a contiguous block of text.

        If `flush_left` is true, raise `UnexpectedIndentationError` if an
        indented line is encountered before the text block ends (with a blank
        line).
        rrN)r	Úget_text_blockrr&r1ÚUnexpectedIndentationErrorr/)rÚ
flush_leftÚblockÚerrrrrrPs

zStateMachine.get_text_blockc
Cs¸|dkr|j}d}|jr2td|jj|f|jdx€|D]P}|j|\}}}|j|jƒ}	|	r8|jr|td||jjf|jd||	||ƒSq8W|jr¨td|jj|jd|j	||ƒSdS)a¯
        Examine one line of input for a transition match & execute its method.

        Parameters:

        - `context`: application-dependent storage.
        - `state`: a `State` object, the current state.
        - `transitions`: an optional ordered list of transition names to try,
          instead of ``state.transition_order``.

        Return the values returned by the transition method:

        - context: possibly modified from the parameter `context`;
        - next state name (`State` subclass name);
        - the result output of the transition, a list.

        When there is no match, ``state.no_match()`` is called and its return
        value is returned.
        Nz5
StateMachine.check_line: state="%s", transitions=%r.)rz@
StateMachine.check_line: Matched transition "%s" in state "%s".z1
StateMachine.check_line: No match in state "%s".)
Útransition_orderr
r!r*r+rr4ÚmatchrÚno_match)
rr3rr4Zstate_correctionÚnameÚpatternÚmethodr8rVrrrr(¡s$
zStateMachine.check_linecCs.|j}||jkrt|ƒ‚|||jƒ|j|<dS)zª
        Initialize & add a `state_class` (`State` subclass) object.

        Exception: `DuplicateStateError` raised if `state_class` was already
        added.
        N)r+rÚDuplicateStateErrorr
)rÚstate_classZ	statenamerrrÚ	add_stateÊs
zStateMachine.add_statecCsx|D]}|j|ƒqWdS)zE
        Add `state_classes` (a list of `State` subclasses).
        N)r])rrr\rrrrÖs
zStateMachine.add_statescCs x|jjƒD]}|jƒqWdS)z+
        Initialize `self.states`.
        N)rrr)rrrrrrÝszStateMachine.runtime_initcCsXtƒ\}}}}}td||f|jdtd|jƒ|jdtd|||f|jddS)zReport error details.z%s: %s)rz
input line %szmodule %s, line %s, function %sN)Ú_exception_datar!rr;)rÚtypeÚvalueÚmodulerÚfunctionrrrr2äs
zStateMachine.errorcCs|jj|ƒdS)z–
        The `observer` parameter is a function or bound method which takes two
        arguments, the source and offset of the current line.
        N)rÚappend)rÚobserverrrrÚattach_observerìszStateMachine.attach_observercCs|jj|ƒdS)N)rÚremove)rrdrrrÚdetach_observerószStateMachine.detach_observercCsHxB|jD]8}y|jj|jƒ}Wntk
r6d}YnX||ŽqWdS)N)NN)rr	r'rr>)rrdr'rrrr?ös
zStateMachine.notify_observers)F)rNNN)N)r)r)N)F)N)r+Ú
__module__Ú__qualname__Ú__doc__rrr:r#r&rBrCrDr.rErFrGr;rIrOrPr(r]rrr2rergr?rrrrrxs4
/
[









)rc@sŒeZdZdZdZdZdZdZddd„Zdd„Z	dd	„Z
d
d„Zdd
„Zdd„Z
dd„Zddd„Zdd„Zdd„Zdd„Zdd„Zdd„ZdS) ÚStateav
    State superclass. Contains a list of transitions, and transition methods.

    Transition methods all have the same signature. They take 3 parameters:

    - An `re` match object. ``match.string`` contains the matched input line,
      ``match.start()`` gives the start index of the match, and
      ``match.end()`` gives the end index.
    - A context object, whose meaning is application-defined (initial value
      ``None``). It can be used to store any information required by the state
      machine, and the retured context is passed on to the next transition
      method unchanged.
    - The name of the next state, a string, taken from the transitions list;
      normally it is returned unchanged, but it may be altered by the
      transition method if necessary.

    Transition methods all return a 3-tuple:

    - A context object, as (potentially) modified by the transition method.
    - The next state name (a return value of ``None`` means no state change).
    - The processing result, a list, which is accumulated by the state
      machine.

    Transition methods may raise an `EOFError` to cut processing short.

    There are two implicit transitions, and corresponding transition methods
    are defined: `bof()` handles the beginning-of-file, and `eof()` handles
    the end-of-file. These methods have non-standard signatures and return
    values. `bof()` returns the initial context and results, and may be used
    to return a header string, or do any other processing needed. `eof()`
    should handle any remaining context and wrap things up; it returns the
    final processing result.

    Typical applications need only subclass `State` (or a subclass), set the
    `patterns` and `initial_transitions` class attributes, and provide
    corresponding transition methods. The default object initialization will
    take care of constructing the list of transitions.
    NFcCsVg|_i|_|jƒ||_||_|jdkr4|jj|_|jdkrR|jg|jjdœ|_dS)zã
        Initialize a `State` object; make & add initial transitions.

        Parameters:

        - `statemachine`: the controlling `StateMachine` object.
        - `debug`: a boolean; produce verbose output if true.
        N)rr)	rUr4Úadd_initial_transitionsÚ
state_machiner
Ú	nested_smr*Únested_sm_kwargsr+)rrmr
rrrrLs
	


zState.__init__cCsdS)z{
        Initialize this `State` before running the state machine; called from
        `self.state_machine.run()`.
        Nr)rrrrrpszState.runtime_initcCs
d|_dS)z9Remove circular references to objects no longer required.N)rm)rrrrrwszState.unlinkcCs&|jr"|j|jƒ\}}|j||ƒdS)z>Make and add transitions listed in `self.initial_transitions`.N)Úinitial_transitionsÚmake_transitionsÚadd_transitions)rÚnamesr4rrrrl{szState.add_initial_transitionscCsNx.|D]&}||jkrt|ƒ‚||krt|ƒ‚qW||jdd…<|jj|ƒdS)a"
        Add a list of transitions to the start of the transition list.

        Parameters:

        - `names`: a list of transition names.
        - `transitions`: a mapping of names to transition tuples.

        Exceptions: `DuplicateTransitionError`, `UnknownTransitionError`.
        Nr)r4ÚDuplicateTransitionErrorÚUnknownTransitionErrorrUÚupdate)rrsr4rXrrrrr‚s

zState.add_transitionscCs0||jkrt|ƒ‚|g|jdd…<||j|<dS)z¹
        Add a transition to the start of the transition list.

        Parameter `transition`: a ready-made transition 3-tuple.

        Exception: `DuplicateTransitionError`.
        Nr)r4rtrU)rrXZ
transitionrrrÚadd_transition•s
zState.add_transitionc	Cs2y|j|=|jj|ƒWnt|ƒ‚YnXdS)z^
        Remove a transition by `name`.

        Exception: `UnknownTransitionError`.
        N)r4rUrfru)rrXrrrÚremove_transition¢s
zState.remove_transitioncCs¨|dkr|jj}y,|j|}t|dƒs:tj|ƒ}|j|<Wn(tk
rdtd|jj|fƒ‚YnXyt||ƒ}Wn(t	k
rœt
d|jj|fƒ‚YnX|||fS)aŽ
        Make & return a transition tuple based on `name`.

        This is a convenience function to simplify transition creation.

        Parameters:

        - `name`: a string, the name of the transition pattern & method. This
          `State` object must have a method called '`name`', and a dictionary
          `self.patterns` containing a key '`name`'.
        - `next_state`: a string, the name of the next `State` object for this
          transition. A value of ``None`` (or absent) implies no state change
          (i.e., continue with the same state).

        Exceptions: `TransitionPatternNotFound`, `TransitionMethodNotFound`.
        NrVz%s.patterns[%r]z%s.%s)r*r+ÚpatternsÚhasattrÚreÚcompiler<ÚTransitionPatternNotFoundÚgetattrÚAttributeErrorÚTransitionMethodNotFound)rrXr8rYrZrrrÚmake_transition®s

zState.make_transitioncCsjtdƒ}g}i}xP|D]H}t||ƒr>|j|ƒ||<|j|ƒq|j|Ž||d<|j|dƒqW||fS)zú
        Return a list of transition names and a transition mapping.

        Parameter `name_list`: a list, where each entry is either a transition
        name string, or a 1- or 2-tuple (transition name, optional next state
        name).
        rLr)r_rrrc)rÚ	name_listZ
stringtypersr4Z	namestaterrrrqÏs

zState.make_transitionscCs
|dgfS)a'
        Called when there is no match from `StateMachine.check_line()`.

        Return the same values returned by transition methods:

        - context: unchanged;
        - next state name: ``None``;
        - empty result list.

        Override in subclasses to catch this event.
        Nr)rr3r4rrrrWãszState.no_matchcCs|gfS)z±
        Handle beginning-of-file. Return unchanged `context`, empty result.

        Override in subclasses.

        Parameter `context`: application-defined storage.
        r)rr3rrrr$ñsz	State.bofcCsgS)z–
        Handle end-of-file. Return empty result.

        Override in subclasses.

        Parameter `context`: application-defined storage.
        r)rr3rrrr,ûsz	State.eofcCs
||gfS)z·
        A "do nothing" transition method.

        Return unchanged `context` & `next_state`, empty result. Useful for
        simple state changes (actionless transitions).
        r)rrVr3r8rrrÚnopsz	State.nop)F)N)r+rhrirjryrprnrorrrrlrrrwrxrrqrWr$r,rƒrrrrrkÿs$'
$

!

rkc@s.eZdZdZddd„Zddd„Zd
dd	„Zd
S)ÚStateMachineWSaq
    `StateMachine` subclass specialized for whitespace recognition.

    There are three methods provided for extracting indented text blocks:

    - `get_indented()`: use when the indent is unknown.
    - `get_known_indented()`: use when the indent is known for all lines.
    - `get_first_known_indented()`: use when only the first line's indent is
      known.
    FTcCsj|jƒ}|jj|j||ƒ\}}}|r6|jt|ƒdƒx&|r\|djƒr\|jƒ|d7}q8W||||fS)a
        Return a block of indented lines of text, and info.

        Extract an indented block where the indent is unknown for all lines.

        :Parameters:
            - `until_blank`: Stop collecting at the first blank line if true.
            - `strip_indent`: Strip common leading indent if true (default).

        :Return:
            - the indented block (a list of lines of text),
            - its indent,
            - its first line offset from BOF, and
            - whether or not it finished with a blank line.
        rr)rGr	Úget_indentedrr&r1rAÚ
trim_start)rÚuntil_blankÚstrip_indentr7ÚindentedÚindentÚblank_finishrrrr…szStateMachineWS.get_indentedcCsh|jƒ}|jj|j|||d\}}}|jt|ƒdƒx&|r\|djƒr\|jƒ|d7}q8W|||fS)aß
        Return an indented block and info.

        Extract an indented block where the indent is known for all lines.
        Starting with the current line, extract the entire text block with at
        least `indent` indentation (which must be whitespace, except for the
        first line).

        :Parameters:
            - `indent`: The number of indent columns/characters.
            - `until_blank`: Stop collecting at the first blank line if true.
            - `strip_indent`: Strip `indent` characters of indentation if true
              (default).

        :Return:
            - the indented block,
            - its first line offset from BOF, and
            - whether or not it finished with a blank line.
        )Úblock_indentrr)rGr	r…rr&r1rAr†)rrŠr‡rˆr7r‰r‹rrrÚget_known_indented6sz!StateMachineWS.get_known_indentedcCsn|jƒ}|jj|j|||d\}}}|jt|ƒdƒ|rbx&|r`|djƒr`|jƒ|d7}q<W||||fS)aã
        Return an indented block and info.

        Extract an indented block where the indent is known for the first line
        and unknown for all other lines.

        :Parameters:
            - `indent`: The first line's indent (# of columns/characters).
            - `until_blank`: Stop collecting at the first blank line if true
              (1).
            - `strip_indent`: Strip `indent` characters of indentation if true
              (1, default).
            - `strip_top`: Strip blank lines from the beginning of the block.

        :Return:
            - the indented block,
            - its indent,
            - its first line offset from BOF, and
            - whether or not it finished with a blank line.
        )Úfirst_indentrr)rGr	r…rr&r1rAr†)rrŠr‡rˆZ	strip_topr7r‰r‹rrrÚget_first_known_indentedTsz'StateMachineWS.get_first_known_indentedN)FT)FT)FTT)r+rhrirjr…rrrrrrr„s


r„c@sleZdZdZdZdZdZdZej	dƒej	dƒdœZ
dZdd	d
„Zdd„Z
d
d„Zdd„Zdd„Zdd„ZdS)ÚStateWSa´
    State superclass specialized for whitespace (blank lines & indents).

    Use this class with `StateMachineWS`.  The transitions 'blank' (for blank
    lines) and 'indent' (for indented text blocks) are added automatically,
    before any other transitions.  The transition method `blank()` handles
    blank lines and `indent()` handles nested indented blocks.  Indented
    blocks trigger a new state machine to be created by `indent()` and run.
    The class of the state machine to be created is in `indent_sm`, and the
    constructor keyword arguments are in the dictionary `indent_sm_kwargs`.

    The methods `known_indent()` and `firstknown_indent()` are provided for
    indented blocks where the indent (all lines' and first line's only,
    respectively) is known to the transition method, along with the attributes
    `known_indent_sm` and `known_indent_sm_kwargs`.  Neither transition method
    is triggered automatically.
    Nz *$z +)ÚblankrŠr‘rŠFcCsZtj|||ƒ|jdkr |j|_|jdkr2|j|_|jdkrD|j|_|jdkrV|j|_dS)z™
        Initialize a `StateSM` object; extends `State.__init__()`.

        Check for indent state machine attributes, set defaults if not set.
        N)rkrÚ	indent_smrnÚindent_sm_kwargsroÚknown_indent_smÚknown_indent_sm_kwargs)rrmr
rrrr³s



zStateWS.__init__cCsHtj|ƒ|jdkri|_|jj|jƒ|j|jƒ\}}|j||ƒdS)z‹
        Add whitespace-specific transitions before those defined in subclass.

        Extends `State.add_initial_transitions()`.
        N)rkrlryrvÚws_patternsrqÚws_initial_transitionsrr)rrsr4rrrrlÃs

zStateWS.add_initial_transitionscCs|j|||ƒS)z9Handle blank lines. Does nothing. Override in subclasses.)rƒ)rrVr3r8rrrr‘Ñsz
StateWS.blankc
CsB|jjƒ\}}}}|jfd|ji|j—Ž}|j||d}	|||	fS)z¸
        Handle an indented text block. Extend or override in subclasses.

        Recursively run the registered state machine for indented blocks
        (`self.indent_sm`).
        r
)r
)rmr…r’r
r“r:)
rrVr3r8r‰rŠrr‹Úsmr5rrrrŠÕszStateWS.indentc	CsF|jj|jƒƒ\}}}|jfd|ji|j—Ž}|j||d}|||fS)a
        Handle a known-indent text block. Extend or override in subclasses.

        Recursively run the registered state machine for known-indent indented
        blocks (`self.known_indent_sm`). The indent is the length of the
        match, ``match.end()``.
        r
)r
)rmrÚendr”r
r•r:)	rrVr3r8r‰rr‹r˜r5rrrÚknown_indentâs
	
zStateWS.known_indentc	CsF|jj|jƒƒ\}}}|jfd|ji|j—Ž}|j||d}|||fS)a0
        Handle an indented text block (first line's indent known).

        Extend or override in subclasses.

        Recursively run the registered state machine for known-indent indented
        blocks (`self.known_indent_sm`). The indent is the length of the
        match, ``match.end()``.
        r
)r
)rmrr™r”r
r•r:)	rrVr3r8r‰rr‹r˜r5rrrÚfirst_known_indentñs

zStateWS.first_known_indent)r‘rŠ)F)r+rhrirjr’r“r”r•r{r|r–r—rrlr‘rŠršr›rrrrrvs

rc@seZdZdZdd„ZdS)Ú_SearchOverridea£
    Mix-in class to override `StateMachine` regular expression behavior.

    Changes regular expression matching, from the default `re.match()`
    (succeeds only if the pattern matches at the start of `self.line`) to
    `re.search()` (succeeds if the pattern matches anywhere in `self.line`).
    When subclassing a `StateMachine`, list this class **first** in the
    inheritance list of the class definition.
    cCs|j|jƒS)z®
        Return the result of a regular expression search.

        Overrides `StateMachine.match()`.

        Parameter `pattern`: `re` compiled regular expression.
        )Úsearchr)rrYrrrrVsz_SearchOverride.matchN)r+rhrirjrVrrrrrœs
rœc@seZdZdZdS)ÚSearchStateMachinez@`StateMachine` which uses `re.search()` instead of `re.match()`.N)r+rhrirjrrrrržsržc@seZdZdZdS)ÚSearchStateMachineWSzB`StateMachineWS` which uses `re.search()` instead of `re.match()`.N)r+rhrirjrrrrrŸsrŸc@sPeZdZdZdQdd„Zdd„Zdd„Zd	d
„Zdd„Zd
d„Z	dd„Z
dd„Zdd„Zdd„Z
dd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„ZeZd+d,„Zd-d.„ZdRd0d1„ZdSd2d3„ZdUd5d6„ZdVd7d8„ZdWd9d:„Zd;d<„Z d=d>„Z!d?d@„Z"dAdB„Z#dCdD„Z$dEdF„Z%dGdH„Z&dIdJ„Z'dKdL„Z(dMdN„Z)dOdP„Z*dS)XÚViewLista>
    List with extended functionality: slices of ViewList objects are child
    lists, linked to their parents. Changes made to a child list also affect
    the parent list.  A child list is effectively a "view" (in the SQL sense)
    of the parent list.  Changes to parent lists, however, do *not* affect
    active child lists.  If a parent list is changed, any active child lists
    should be recreated.

    The start and end of the slice can be trimmed using the `trim_start()` and
    `trim_end()` methods, without affecting the parent list.  The link between
    child and parent lists can be broken by calling `disconnect()` on the
    child list.

    Also, ViewList objects keep track of the source & offset of each item.
    This information is accessible via the `source()`, `offset()`, and
    `info()` methods.
    Ncsžg|_g|_||_||_t|tƒrD|jdd…|_|jdd…|_n:|dk	r~t|ƒ|_|rb||_n‡fdd„tt|ƒƒDƒ|_t|jƒt|jƒksšt	dƒ‚dS)Ncsg|]}ˆ|f‘qSrr)Ú.0Úi)rrrú
<listcomp>Psz%ViewList.__init__.<locals>.<listcomp>z
data mismatch)
ÚdataÚitemsÚparentÚ
parent_offsetrr ÚlistÚranger1ÚAssertionError)rÚinitlistrr¥r¦r§r)rrr8s

zViewList.__init__cCs
t|jƒS)N)Ústrr¤)rrrrÚ__str__SszViewList.__str__cCsd|jj|j|jfS)Nz%s(%s, items=%s))r*r+r¤r¥)rrrrÚ__repr__VszViewList.__repr__cCs|j|j|ƒkS)N)r¤Ú_ViewList__cast)rÚotherrrrÚ__lt__ZszViewList.__lt__cCs|j|j|ƒkS)N)r¤r¯)rr°rrrÚ__le__[szViewList.__le__cCs|j|j|ƒkS)N)r¤r¯)rr°rrrÚ__eq__\szViewList.__eq__cCs|j|j|ƒkS)N)r¤r¯)rr°rrrÚ__ne__]szViewList.__ne__cCs|j|j|ƒkS)N)r¤r¯)rr°rrrÚ__gt__^szViewList.__gt__cCs|j|j|ƒkS)N)r¤r¯)rr°rrrÚ__ge___szViewList.__ge__cCs |j}|j|ƒ}||k||kS)N)r¤r¯)rr°ZmineZyoursrrrÚ__cmp__as
zViewList.__cmp__cCst|tƒr|jS|SdS)N)rr r¤)rr°rrrÚ__castgs
zViewList.__castcCs
||jkS)N)r¤)rÚitemrrrÚ__contains__mszViewList.__contains__cCs
t|jƒS)N)r1r¤)rrrrÚ__len__nszViewList.__len__cCs^t|tƒrP|jdkstdƒ‚|j|j|j|j…|j|j|j…||jpJddS|j|SdS)Nrzcannot handle slice with strider)r¥r¦r§)Nr)	rÚsliceÚsteprªr*r¤ÚstartÚstopr¥)rr¢rrrÚ__getitem__ts
zViewList.__getitem__cCsÈt|tƒr¤|jdkstdƒ‚t|tƒs.tdƒ‚|j|j|j|j…<|j	|j	|j|j…<t
|jƒt
|j	ƒksrtdƒ‚|jrÄ||j|jp†d|j|jp˜t
|ƒ|j…<n ||j|<|jrÄ||j||j<dS)Nrzcannot handle slice with stridez(assigning non-ViewList to ViewList slicez
data mismatchr)Nr)
rr¼r½rªr rHr¤r¾r¿r¥r1r¦r§)rr¢r¹rrrÚ__setitem__}s

,
zViewList.__setitem__cCs¢y(|j|=|j|=|jr&|j||j=Wnttk
rœ|jdksJtdƒ‚|j|j|j…=|j|j|j…=|jr˜|j|jp|d|j|jpŽt	|ƒ|j…=YnXdS)Nzcannot handle slice with strider)
r¤r¥r¦r§rHr½rªr¾r¿r1)rr¢rrrÚ__delitem__szViewList.__delitem__cCs4t|tƒr(|j|j|j|j|jdStdƒ‚dS)N)r¥z!adding non-ViewList to a ViewList)rr r*r¤r¥rH)rr°rrrÚ__add__›s
zViewList.__add__cCs4t|tƒr(|j|j|j|j|jdStdƒ‚dS)N)r¥z!adding ViewList to a non-ViewList)rr r*r¤r¥rH)rr°rrrÚ__radd__¢s
zViewList.__radd__cCs(t|tƒr|j|j7_ntdƒ‚|S)Nz!argument to += must be a ViewList)rr r¤rH)rr°rrrÚ__iadd__©s
zViewList.__iadd__cCs|j|j||j|dS)N)r¥)r*r¤r¥)rr@rrrÚ__mul__°szViewList.__mul__cCs |j|9_|j|9_|S)N)r¤r¥)rr@rrrÚ__imul__µszViewList.__imul__cCsRt|tƒstdƒ‚|jr2|jjt|jƒ|j|ƒ|jj|jƒ|j	j|j	ƒdS)Nz(extending a ViewList with a non-ViewList)
rr rHr¦rNr1r¤r§r%r¥)rr°rrrr%ºs
zViewList.extendrcCsX|dkr|j|ƒn@|jr8|jjt|jƒ|j|||ƒ|jj|ƒ|jj||fƒdS)N)r%r¦rNr1r¤r§rcr¥)rr¹rr7rrrrcÂszViewList.appendcCsÈ|dkrnt|tƒstdƒ‚|j|j||…<|j|j||…<|jrÄt|jƒ|t|jƒ}|jj||j|ƒnV|jj||ƒ|jj|||fƒ|jrÄt|jƒ|t|jƒ}|jj||j|||ƒdS)Nz+inserting non-ViewList with no source given)	rr rHr¤r¥r¦r1rNr§)rr¢r¹rr7ÚindexrrrrNÌs
zViewList.insertrcCsH|jr0t|jƒ|t|jƒ}|jj||jƒ|jj|ƒ|jj|ƒS)N)r¦r1r¤Úpopr§r¥)rr¢rÈrrrrÉÝs
zViewList.popcCsf|t|jƒkr&td|t|jƒfƒ‚n|dkr6tdƒ‚|jd|…=|jd|…=|jrb|j|7_dS)zW
        Remove items from the start of the list, without touching the parent.
        zCSize of trim too large; can't trim %s items from a list of size %s.rzTrim size must be >= 0.N)r1r¤r>r¥r¦r§)rr@rrrr†äszViewList.trim_startcCsV|t|jƒkr&td|t|jƒfƒ‚n|dkr6tdƒ‚|j|d…=|j|d…=dS)zU
        Remove items from the end of the list, without touching the parent.
        zCSize of trim too large; can't trim %s items from a list of size %s.rzTrim size must be >= 0.N)r1r¤r>r¥)rr@rrrÚtrim_endòszViewList.trim_endcCs|j|ƒ}||=dS)N)rÈ)rr¹rÈrrrrfþs
zViewList.removecCs|jj|ƒS)N)r¤Úcount)rr¹rrrrËszViewList.countcCs|jj|ƒS)N)r¤rÈ)rr¹rrrrÈszViewList.indexcCs|jjƒ|jjƒd|_dS)N)r¤Úreverser¥r¦)rrrrrÌs

zViewList.reversecGsBtt|j|jƒf|žŽ}dd„|Dƒ|_dd„|Dƒ|_d|_dS)NcSsg|]}|d‘qS)rr)r¡Úentryrrrr£sz!ViewList.sort.<locals>.<listcomp>cSsg|]}|d‘qS)rr)r¡rÍrrrr£
s)ÚsortedÚzipr¤r¥r¦)rr/ÚtmprrrÚsort
sz
ViewList.sortcCsJy
|j|Stk
rD|t|jƒkr>|j|dddfS‚YnXdS)z%Return source & offset for index `i`.rrN)r¥r>r1r¤)rr¢rrrr's
z
ViewList.infocCs|j|ƒdS)zReturn source for index `i`.r)r')rr¢rrrrszViewList.sourcecCs|j|ƒdS)zReturn offset for index `i`.r)r')rr¢rrrr7szViewList.offsetcCs
d|_dS)z-Break link between this list and parent list.N)r¦)rrrrÚ
disconnect"szViewList.disconnectccs0x*t|j|jƒD]\}\}}|||fVqWdS)z8Return iterator yielding (source, offset, value) tuples.N)rÏr¤r¥)rr`rr7rrrÚxitems&szViewList.xitemscCs"x|jƒD]}td|ƒq
WdS)z=Print the list in `grep` format (`source:offset:value` lines)z%s:%d:%sN)rÓr!)rrrrrÚpprint+szViewList.pprint)NNNNN)Nr)Nrr)r)r)r)+r+rhrirjrr­r®r±r²r³r´rµr¶r·r¯rºr»rÀrÁrÂrÃrÄrÅrÆÚ__rmul__rÇr%rcrNrÉr†rÊrfrËrÈrÌrÑr'rr7rÒrÓrÔrrrrr $sR
	






r c@sNeZdZdZdejfdd„Zddd„Zdd
d„Zddd
„Z	dd„Z
dd„Zd	S)r z*A `ViewList` with string-specific methods.rcs*‡fdd„|j||…Dƒ|j||…<dS)zÕ
        Trim `length` characters off the beginning of each item, in-place,
        from index `start` to `end`.  No whitespace-checking is done on the
        trimmed text.  Does not affect slice parent.
        csg|]}|ˆd…‘qS)Nr)r¡r)Úlengthrrr£;sz(StringList.trim_left.<locals>.<listcomp>N)r¤)rrÖr¾r™r)rÖrÚ	trim_left5s
zStringList.trim_leftFcCsz|}t|jƒ}x^||krl|j|}|jƒs,P|rb|ddkrb|j|ƒ\}}t|||…||dƒ‚|d7}qW|||…S)zÜ
        Return a contiguous block of text.

        If `flush_left` is true, raise `UnexpectedIndentationError` if an
        indented line is encountered before the text block ends (with a blank
        line).
        rú r)r1r¤rAr'rQ)rr¾rRr™Úlastrrr7rrrrP>s



zStringList.get_text_blockTNcCsJ|}|}|dk	r|dkr|}|dk	r,|d7}t|jƒ}x°||krâ|j|}	|	r|	ddksr|dk	r|	d|…jƒr||koŒ|j|djƒ}
P|	jƒ}|s¨|rØd}
Pn0|dkrØt|	ƒt|ƒ}|dkrÎ|}n
t||ƒ}|d7}q8Wd}
|||…}
|dk	r|
r|
jd|d…|
jd<|r:|r:|
j||dk	d|
|pDd|
fS)a¯
        Extract and return a StringList of indented lines of text.

        Collect all lines with indentation, determine the minimum indentation,
        remove the minimum indentation from all indented lines (unless
        `strip_indent` is false), and return them. All lines up to but not
        including the first unindented line will be returned.

        :Parameters:
          - `start`: The index of the first line to examine.
          - `until_blank`: Stop collecting at the first blank line if true.
          - `strip_indent`: Strip common leading indent if true (default).
          - `block_indent`: The indent of the entire block, if known.
          - `first_indent`: The indent of the first line, if known.

        :Return:
          - a StringList of indented lines with mininum indent removed;
          - the amount of the indent;
          - a boolean: did the indented block finish with a blank line or EOF?
        NrrrØ)r¾)r1r¤rAÚlstripÚminr×)rr¾r‡rˆrŒrŽrŠr™rÙrr‹ÚstrippedZline_indentrSrrrr…SsB



zStringList.get_indentedc
s*|||…}|‰xÜtt|jƒƒD]Ê}tj|j|ƒ}y||}Wn.tk
rn|t|j|ƒt|ƒ7}YnXy||}Wn.tk
rª|t|j|ƒt|ƒ7}YnX|j|||…jƒ|j|<}	|	r tˆt|	ƒt|	jƒƒƒ‰q W|r&dˆko|knr&‡fdd„|jDƒ|_|S)Nrcsg|]}|ˆd…‘qS)Nr)r¡r)rŠrrr£¡sz+StringList.get_2D_block.<locals>.<listcomp>)	r©r1r¤rZcolumn_indicesr>ÚrstriprÛrÚ)
rÚtopÚleftZbottomÚrightrˆrSr¢Zcirr)rŠrÚget_2D_blocks$   "zStringList.get_2D_blockcCsxtj}xltt|jƒƒD]Z}|j|}t|tƒrg}x,|D]$}|j|ƒ||ƒdkr8|j|ƒq8Wdj|ƒ|j|<qWdS)zƒ
        Pad all double-width characters in self by appending `pad_char` to each.
        For East Asian language support.
        ZWFrLN)	ÚunicodedataÚeast_asian_widthr©r1r¤rÚunicodercr")rZpad_charrãr¢rÚnewÚcharrrrÚpad_double_width¤s



zStringList.pad_double_widthcCs4x.tt|jƒƒD]}|j|j||ƒ|j|<qWdS)z6Replace all occurrences of substring `old` with `new`.N)r©r1r¤Úreplace)rÚoldrår¢rrrrè´szStringList.replace)F)rFTNN)T)r+rhrirjÚsysÚmaxsizer×rPr…rárçrèrrrrr 1s	

;
r c@seZdZdS)ÚStateMachineErrorN)r+rhrirrrrrìºsrìc@seZdZdS)r=N)r+rhrirrrrr=»sr=c@seZdZdS)r[N)r+rhrirrrrr[¼sr[c@seZdZdS)ruN)r+rhrirrrrru½sruc@seZdZdS)rtN)r+rhrirrrrrt¾srtc@seZdZdS)r}N)r+rhrirrrrr}¿sr}c@seZdZdS)r€N)r+rhrirrrrr€Àsr€c@seZdZdS)rQN)r+rhrirrrrrQÁsrQc@seZdZdZdS)r-z‡
    Raise from within a transition method to switch to another transition.

    Raise with one argument, the new transition name.
    N)r+rhrirjrrrrr-Äsr-c@seZdZdZdS)r0zª
    Raise from within a transition method to switch to another state.

    Raise with one or two arguments: new state name, and an optional new
    transition name.
    N)r+rhrirjrrrrr0Ísr0éFz[]cs*|r|jd|ƒ}‡fdd„|jƒDƒ}|S)a6
    Return a list of one-line strings with tabs expanded, no newlines, and
    trailing whitespace stripped.

    Each tab is expanded with between 1 and `tab_width` spaces, so that the
    next character's index becomes a multiple of `tab_width` (8 by default).

    Parameters:

    - `astring`: a multi-line string.
    - `tab_width`: the number of columns between tab stops.
    - `convert_whitespace`: convert form feeds and vertical tabs to spaces?
    - `whitespace`: pattern object with the to-be-converted
      whitespace characters (default [\v\f]).
    rØcsg|]}|jˆƒjƒ‘qSr)Ú
expandtabsrÝ)r¡Ús)Ú	tab_widthrrr£ész string2lines.<locals>.<listcomp>)ÚsubÚ
splitlines)ZastringrðZconvert_whitespaceÚ
whitespaceÚlinesr)rðrÚstring2linesÖsrõcCs>tjƒ\}}}x|jr|j}qW|jj}|j||j|j|jfS)zü
    Return exception information:

    - the exception's class name;
    - the exception object;
    - the name of the file containing the offending code;
    - the line number of the offending code;
    - the function name of the offending code.
    )	rêÚexc_infoÚtb_nextÚtb_frameÚf_coder+Úco_filenameÚ	tb_linenoÚco_name)r_r`Ú	tracebackÚcoderrrr^ìs

r^)rr)&rjÚ
__future__rZ
__docformat__rêr{râZdocutilsrZdocutils.utils.error_reportingrÚversion_infor¬räÚobjectrrkr„rrœržrŸr r Ú	Exceptionrìr=r[rurtr}r€rQr-r0r|rõr^rrrrÚ<module>isP

g