本文整理匯總了Python中quex.engine.state_machine.core.StateMachine類的典型用法代碼示例。如果您正苦於以下問題:Python StateMachine類的具體用法?Python StateMachine怎麽用?Python StateMachine使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了StateMachine類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
class X:
def __init__(self, Name):
sh = StringIO("[:\\P{Script=%s}:]" % Name)
self.name = Name
self.charset = regex.snap_set_expression(sh, {})
self.sm = StateMachine()
self.sm.add_transition(self.sm.init_state_index, self.charset, AcceptanceF=True)
self.id = self.sm.get_id()
def check(self, SM):
"""This function throws an exception as soon as one single value
is not matched according to the expectation.
"""
print "Name = " + self.name,
for interval in self.charset.get_intervals(PromiseToTreatWellF=True):
for i in range(interval.begin, interval.end):
utf8_seq = unicode_to_utf8(i)
# Apply sequence to state machine
s_idx = result.init_state_index
for byte in utf8_seq:
s_idx = result.states[s_idx].target_map.get_resulting_target_state_index(byte)
# All acceptance flags must belong to the original state machine
for cmd in result.states[s_idx].single_entry:
if cmd.__class__ != SeAccept: continue
# HERE: As soon as something is wrong --> fire an exception
assert cmd.acceptance_id() == self.id
print " (OK=%i)" % self.id
示例2: test_on_UCS_range
def test_on_UCS_range(Trafo, Source, Drain, CharacterBackwardTrafo):
sm = StateMachine()
acc_db = {}
for x in range(Source.begin, Source.end):
ti = sm.add_transition(sm.init_state_index, x, AcceptanceF=True)
acc_id = len(acc_db)
sm.states[ti].mark_acceptance_id(acc_id)
acc_db[x] = acc_id
if Setup.bad_lexatom_detection_f:
acc_db[None] = E_IncidenceIDs.BAD_LEXATOM
else:
acc_db[None] = None
state_n_before, result = transform(Trafo, sm)
# assert state_n_before == len(result.states)
init_state = result.get_init_state()
count = 0
for y in range(Drain.begin, Drain.end):
# Translate character into
x = CharacterBackwardTrafo(y)
# Transit on the translated charater
ti = init_state.target_map.get_resulting_target_state_index(y)
# Compare resulting state with the expected state's acceptance
assert_only_acceptance_id(sm.states, ti, acc_db, x, y)
count += 1
print "<terminated: %i transitions ok>" % count
示例3: do
def do(sh):
"""Converts a uni-code string into a state machine that parses
its letters sequentially. Each state in the sequence correponds
to the sucessful triggering of a letter. Only the last state, though,
is an acceptance state. Any bailing out before is 'not accepted'.
Example:
"hey" is translated into the state machine:
(0)-- 'h' -->(1)-- 'e' -->(2)-- 'y' --> ACCEPTANCE
| | |
FAIL FAIL FAIL
Note: The state indices are globally unique. But, they are not necessarily
0, 1, 2, ...
"""
assert sh.__class__.__name__ == "StringIO" \
or sh.__class__.__name__ == "file"
# resulting state machine
result = StateMachine()
state_idx = result.init_state_index
# Only \" is a special character '"', any other backslashed character
# remains as the sequence 'backslash' + character
for char_code in get_character_code_sequence(sh):
state_idx = result.add_transition(state_idx, char_code)
# when the last state has trigger it is supposed to end up in 'acceptance'
result.states[state_idx].set_acceptance()
return result
示例4: test
def test(TestString):
print "expression = \"" + TestString + "\""
sm = StateMachine()
try:
trigger_set = character_set.do(StringIO(TestString + "]"))
sm.add_transition(sm.init_state_index, trigger_set, AcceptanceF=True)
print "state machine\n", sm
except RegularExpressionException, x:
print repr(x)
示例5: snap_non_control_character
def snap_non_control_character(stream, PatternDict):
__debug_entry("non-control characters", stream)
# (*) read first character
char_code = utf8.__read_one_utf8_code_from_stream(stream)
if char_code is None:
error_msg("Character could not be interpreted as UTF8 code or End of File reached prematurely.",
stream)
result = StateMachine()
result.add_transition(result.init_state_index, char_code, AcceptanceF=True)
return __debug_exit(result, stream)
示例6: get_any
def get_any():
"""RETURNS:
A state machine that 'eats' any character, but only one.
(0)--- \Any --->(( 0 ))
"""
result = StateMachine()
result.add_transition(result.init_state_index, NumberSet(Interval(-sys.maxint, sys.maxint)), AcceptanceF=True)
return result
示例7: create_ALL_BUT_NEWLINE_state_machine
def create_ALL_BUT_NEWLINE_state_machine():
global Setup
result = StateMachine()
# NOTE: Buffer control characters are supposed to be filtered out by the code
# generator.
trigger_set = NumberSet(Interval(ord("\n")).inverse())
if Setup.get_character_value_limit() != sys.maxint:
trigger_set.intersect_with(Interval(0, Setup.get_character_value_limit()))
result.add_transition(result.init_state_index, trigger_set, AcceptanceF=True)
return result
示例8: create_ALL_BUT_NEWLINE_state_machine
def create_ALL_BUT_NEWLINE_state_machine(stream):
global Setup
result = StateMachine()
# NOTE: Buffer control characters are supposed to be filtered out by the code
# generator.
trigger_set = NumberSet(Interval(ord("\n"))).get_complement(Setup.buffer_codec.source_set)
if trigger_set.is_empty():
error_msg("The set of admissible characters contains only newline.\n"
"The '.' for 'all but newline' is an empty set.",
SourceRef.from_FileHandle(stream))
result.add_transition(result.init_state_index, trigger_set, AcceptanceF=True)
return result
示例9: test
def test(ByteSequenceDB):
L = len(ByteSequenceDB[0])
for seq in ByteSequenceDB:
assert len(seq) == L
for x in seq:
assert isinstance(x, Interval)
first_different_byte_index = -1
for i in range(L):
x0 = ByteSequenceDB[0][i]
for seq in ByteSequenceDB[1:]:
if not seq[i].is_equal(x0):
first_different_byte_index = i
break
if first_different_byte_index != -1:
break
if first_different_byte_index == -1:
first_different_byte_index = 0
print "# Best To be Displayed by:"
print "#"
print "# > " + sys.argv[0] + " " + sys.argv[1] + " | dot -Tsvg -o tmp.svg"
print "#"
print "# -------------------------"
print "# Byte Sequences: "
i = -1
for seq in ByteSequenceDB:
i += 1
print "# (%i) " % i,
for x in seq:
print " " + x.get_string(Option="hex"),
print
print "# L = %i" % L
print "# DIdx = %i" % first_different_byte_index
sm = StateMachine()
end_index = state_machine.index.get()
sm.states[end_index] = State()
trafo.plug_state_sequence_for_trigger_set_sequence(sm, sm.init_state_index, end_index,
ByteSequenceDB, L, first_different_byte_index)
if len(sm.get_orphaned_state_index_list()) != 0:
print "Error: Orphaned States Detected!"
print sm.get_graphviz_string(Option="hex")
示例10: get_setup
def get_setup(L0, L1, FSM0, FSM1, FSM2):
# SPECIALITIES: -- sm0 and sm1 have an intersection between their second
# transition.
# -- sm1 transits further upon acceptance.
# -- sm2 has only one transition.
ci_list = [
CountInfo(dial_db.new_incidence_id(), NumberSet.from_range(L0, L1),
CountAction(E_CharacterCountType.COLUMN, 0)),
]
# Generate State Machine that does not have any intersection with
# the loop transitions.
sm0 = StateMachine()
si = sm0.add_transition(sm0.init_state_index, FSM0)
si = sm0.add_transition(si, NS_A, AcceptanceF=True)
sm0.states[si].mark_acceptance_id(dial_db.new_incidence_id())
sm1 = StateMachine()
si0 = sm1.add_transition(sm1.init_state_index, FSM1)
si = sm1.add_transition(si0, NS_A, AcceptanceF=True)
iid1 = dial_db.new_incidence_id()
sm1.states[si].mark_acceptance_id(iid1)
si = sm1.add_transition(si, NS_B, si0)
sm1.states[si].mark_acceptance_id(iid1)
sm2 = StateMachine()
si = sm2.add_transition(sm2.init_state_index, FSM2, AcceptanceF=True)
sm2.states[si].mark_acceptance_id(dial_db.new_incidence_id())
return ci_list, [sm0, sm1, sm2]
示例11: do
def do(stream, PatternDict):
trigger_set = snap_set_expression(stream, PatternDict)
if trigger_set is None:
raise RegularExpressionException("Regular Expression: character_set_expression called for something\n" + \
"that does not start with '[:', '[' or '\\P'")
if trigger_set.is_empty():
raise RegularExpressionException("Regular Expression: Character set expression results in empty set.")
# Create state machine that triggers with the trigger set to SUCCESS
# NOTE: The default for the ELSE transition is FAIL.
sm = StateMachine()
sm.add_transition(sm.init_state_index, trigger_set, AcceptanceF=True)
return __debug_exit(sm, stream)
示例12: create_range_skipper_code
def create_range_skipper_code(Language, TestStr, CloserSequence, QuexBufferSize=1024,
CommentTestStrF=False, ShowPositionF=False):
assert QuexBufferSize >= len(CloserSequence) + 2
end_str = __prepare(Language)
door_id_on_skip_range_open = dial_db.new_door_id()
data = {
"closer_sequence": CloserSequence,
"closer_pattern": Pattern(StateMachine.from_sequence(CloserSequence),
PatternString="<skip range closer>"),
"mode_name": "MrUnitTest",
"on_skip_range_open": CodeFragment([end_str]),
"door_id_after": DoorID.continue_without_on_after_match(),
}
skipper_code = range_skipper.do(data, Analyzer)
__require_variables()
return create_customized_analyzer_function(Language, TestStr, skipper_code,
QuexBufferSize, CommentTestStrF, ShowPositionF, end_str,
MarkerCharList = [],
LocalVariableDB = deepcopy(variable_db.get()),
DoorIdOnSkipRangeOpen=door_id_on_skip_range_open)
示例13: __init__
def __init__(self, Name):
sh = StringIO("[:\\P{Script=%s}:]" % Name)
self.name = Name
self.charset = regex.snap_set_expression(sh, {})
self.sm = StateMachine()
self.sm.add_transition(self.sm.init_state_index, self.charset, AcceptanceF=True)
self.id = self.sm.get_id()
示例14: __sm_newline_default
def __sm_newline_default(self):
"""Default newline: '(\n)|(\r\n)'
"""
global cc_type_name_db
newline_set = NumberSet(ord('\n'))
retour_set = NumberSet(ord('\r'))
before = self.specifier_count_op_map.find_occupier(newline_set, set())
if before is not None:
error.warning("Trying to implement default newline: '\\n' or '\\r\\n'.\n"
"The '\\n' option is not possible, since it has been occupied by '%s'.\n" \
"No newline can be defined by default."
% cc_type_name_db[before.cc_type], before.sr,
SuppressCode=NotificationDB.warning_default_newline_0A_impossible)
# In this case, no newline can be defined!
return
sm = StateMachine.from_character_set(newline_set)
if Setup.dos_carriage_return_newline_f:
before = self.specifier_count_op_map.find_occupier(retour_set, set())
if before is not None:
error.warning("Trying to implement default newline: '\\n' or '\\r\\n'.\n"
"The '\\r\\n' option is not possible, since '\\r' has been occupied by '%s'." \
% cc_type_name_db[before.cc_type],
before.sr,
SuppressCode=NotificationDB.warning_default_newline_0D_impossible)
else:
sm.add_transition_sequence(sm.init_state_index, [retour_set, newline_set])
return sm
示例15: create_state_machine
def create_state_machine(SM, Result, Class_StateMachine, Class_State):
# If all states are of size one, this means, that there were no states that
# could have been combined. In this case a simple copy of the original
# state machine will do.
if len(filter(lambda state_set: len(state_set) != 1, Result.state_set_list)) == 0:
return SM.clone()
# Define a mapping from the state set to a new target state index
#
# map: state_set_index ---> index of the state that represents it
#
map_new_state_index = dict([(i, state_machine_index.get()) for i in xrange(len(Result.state_set_list))])
# The state set that contains the initial state becomes the initial state of
# the new state machine.
state_set_containing_initial_state_i = Result.map[SM.init_state_index]
new_init_state_index = map_new_state_index[state_set_containing_initial_state_i]
result = StateMachine(new_init_state_index)
# Ensure that each target state index has a state inside the state machine
# Build up the state machine out of the state sets
for state_set_idx, state_set in enumerate(Result.state_set_list):
new_state_index = map_new_state_index[state_set_idx]
# Merge all core information of the states inside the state set.
# If one state set contains an acceptance state, then the result is 'acceptance'.
# (Note: The initial split separates acceptance states from those that are not
# acceptance states. There can be no state set containing acceptance and
# non-acceptance states)
# (Note, that the prototype's info has not been included yet, consider whole set)
result.states[new_state_index] = Class_State.new_merged_core_state(SM.states[i] for i in state_set)
for state_set_idx, state_set in enumerate(Result.state_set_list):
# The prototype: States in one set behave all equivalent with respect to target state sets
# thus only one state from the start set has to be considered.
prototype = SM.states[state_set[0]]
representative = result.states[map_new_state_index[state_set_idx]]
# The representative must have all transitions that the prototype has
for target_state_index, trigger_set in prototype.target_map.get_map().iteritems():
target_state_set_index = Result.map[target_state_index]
target_index = map_new_state_index[target_state_set_index]
representative.add_transition(trigger_set, target_index)
return result