class StateMachine(object): (source)
Known subclasses: docutils.statemachine.SearchStateMachine
, docutils.statemachine.StateMachineWS
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.
Method | __init__ |
Initialize a StateMachine object; add state objects. |
Method | abs_line_number |
Return line number of current line (counting from 1). |
Method | abs_line_offset |
Return line offset of current line, from beginning of file. |
Method | add_state |
Initialize & add a state_class (State subclass) object. |
Method | add_states |
Add state_classes (a list of State subclasses). |
Method | at_bof |
Return 1 if the input is at or before beginning-of-file. |
Method | at_eof |
Return 1 if the input is at or past end-of-file. |
Method | attach_observer |
The observer parameter is a function or bound method which takes two arguments, the source and offset of the current line. |
Method | check_line |
Examine one line of input for a transition match & execute its method. |
Method | detach_observer |
Undocumented |
Method | error |
Report error details. |
Method | get_source |
Return source of line at absolute line offset line_offset . |
Method | get_source_and_line |
Return (source, line) tuple for current or given line number. |
Method | get_state |
Return current state object; set it first if next_state given. |
Method | get_text_block |
Return a contiguous block of text. |
Method | goto_line |
Jump to absolute line offset line_offset , load and return it. |
Method | insert_input |
Undocumented |
Method | is_next_line_blank |
Return 1 if the next line is blank or non-existant. |
Method | next_line |
Load self.line with the n 'th next line and return it. |
Method | notify_observers |
Undocumented |
Method | previous_line |
Load self.line with the n 'th previous line and return it. |
Method | run |
Run the state machine on input_lines . Return results (a list). |
Method | runtime_init |
Initialize self.states . |
Method | unlink |
Remove circular references to objects no longer required. |
Instance Variable | current_state |
The name of the current state (key to self.states ). |
Instance Variable | debug |
Debugging mode on/off. |
Instance Variable | initial_state |
The name of the initial state (key to self.states ). |
Instance Variable | input_lines |
StringList of input lines (without newlines). Filled by self.run() . |
Instance Variable | input_offset |
Offset of self.input_lines from the beginning of the file. |
Instance Variable | line |
Current input line. |
Instance Variable | line_offset |
Current input line offset from beginning of self.input_lines . |
Instance Variable | observers |
List of bound methods or functions to call whenever the current line changes. Observers are called with one argument, self. Cleared at the end of run() . |
Instance Variable | states |
Mapping of {state_name: State_object}. |
Instance Variable | _stderr |
Wrapper around sys.stderr catching en-/decoding errors |
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).Initialize & add a state_class
(State
subclass) object.
Exception: DuplicateStateError
raised if state_class
was already
added.
observer
parameter is a function or bound method which takes two
arguments, the source and offset of the current line.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
;State
subclass name);When there is no match, state.no_match() is called and its return value is returned.
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.
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.
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).
docutils.parsers.rst.states.NestedStateMachine
, docutils.parsers.rst.states.RSTStateMachine
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.run()
.