本文整理匯總了Python中quex.engine.state_machine.core.StateMachine.add_transition方法的典型用法代碼示例。如果您正苦於以下問題:Python StateMachine.add_transition方法的具體用法?Python StateMachine.add_transition怎麽用?Python StateMachine.add_transition使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類quex.engine.state_machine.core.StateMachine
的用法示例。
在下文中一共展示了StateMachine.add_transition方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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: get_setup
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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]
示例3: test
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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)
示例4: snap_non_control_character
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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)
示例5: get_any
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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
示例6: create_ALL_BUT_NEWLINE_state_machine
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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
示例7: create_ALL_BUT_NEWLINE_state_machine
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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
示例8: do
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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)
示例9: do
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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
示例10: test_on_UCS_range
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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
示例11: snap_character_set_expression
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
def snap_character_set_expression(stream, PatternDict):
# GRAMMAR:
#
# set_expression:
# [: set_term :]
# traditional character set
# \P '{' propperty string '}'
# '{' identifier '}'
#
# set_term:
# "alnum"
# "alpha"
# "blank"
# "cntrl"
# "digit"
# "graph"
# "lower"
# "print"
# "punct"
# "space"
# "upper"
# "xdigit"
# "union" '(' set_term [ ',' set_term ]+ ')'
# "intersection" '(' set_term [ ',' set_term ]+ ')'
# "difference" '(' set_term [ ',' set_term ]+ ')'
# "inverse" '(' set_term ')'
# set_expression
#
trigger_set = snap_set_expression(stream, PatternDict)
if trigger_set is None:
error_msg("Regular Expression: snap_character_set_expression called for something\n" + \
"that does not start with '[:', '[' or '\\P'", stream)
elif trigger_set.is_empty():
error_msg("Regular Expression: Character set expression results in empty set.", stream, DontExitF=True)
# 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: seal
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
def seal(self):
if len(self.space_db) == 0 and len(self.grid_db) == 0:
default_space = ord(' ')
default_tab = ord('\t')
bad = self.bad_character_set
if bad.get().contains(default_space) == False:
self.specify_space("[ ]", NumberSet(default_space), 1, self.fh)
if bad.get().contains(default_tab) == False:
self.specify_grid("[\\t]", NumberSet(default_tab), 4, self.fh)
if len(self.space_db) == 0 and len(self.grid_db) == 0:
error_msg("No space or grid defined for indentation counting. Default\n"
"values ' ' and '\\t' could not be used since they are specified as 'bad'.",
bad.file_name, bad.line_n)
if self.newline_state_machine.get() is None:
sm = StateMachine()
end_idx = sm.add_transition(sm.init_state_index, NumberSet(ord('\n')), AcceptanceF=True)
mid_idx = sm.add_transition(sm.init_state_index, NumberSet(ord('\r')), AcceptanceF=False)
sm.add_transition(mid_idx, NumberSet(ord('\n')), end_idx, AcceptanceF=False)
self.specify_newline("(\\r\\n)|(\\n)", sm, self.fh)
示例13: __init__
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
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, TransformFunc):
"""This function throws an exception as soon as one single value
is not matched according to the expectation.
"""
print "## [%i] Name = %s" % (self.id, self.name),
interval_list = self.charset.get_intervals(PromiseToTreatWellF=True)
interval_count = len(interval_list)
for interval in interval_list:
for i in range(interval.begin, interval.end):
lexatom_seq = TransformFunc(i)
# Apply sequence to state machine
state = SM.apply_sequence(lexatom_seq)
if state is None:
error(self.sm, SM, lexatom_seq)
# All acceptance flags must belong to the original state machine
acceptance_id_list = [
cmd.acceptance_id()
for cmd in state.single_entry.get_iterable(SeAccept)
]
if acceptance_id_list and self.id not in acceptance_id_list:
print eval("u'\U%08X'" % i)
print "#Seq: ", ["%02X" % x for x in lexatom_seq]
print "#acceptance-ids:", acceptance_id_list
error(self.sm, SM, lexatom_seq)
print " (OK=%i)" % interval_count
示例14: StateMachine_Newline
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
def StateMachine_Newline():
"""Creates a state machine matching newline according to what has been
specified in the setup (Setup.dos_carriage_return_newline_f).
That is, if is DOS newline then the state machine represents '\r\n' and
if it is unix only, then it represents '\n'. If both is required they
are implemented in parallel.
RETURNS: StateMachine
"""
UnixF = True
DosF = Setup.dos_carriage_return_newline_f
NL = ord('\n') # (pure) newline, i.e. line feed
CR = ord('\r') # carriage return
sm = StateMachine()
if UnixF:
sm.add_transition(sm.init_state_index, NL, AcceptanceF=True)
if DosF:
idx = sm.add_transition(sm.init_state_index, CR, AcceptanceF=False)
sm.add_transition(idx, NL, AcceptanceF=True)
return beautifier.do(sm)
示例15: __parse_option
# 需要導入模塊: from quex.engine.state_machine.core import StateMachine [as 別名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_transition [as 別名]
def __parse_option(fh, new_mode):
def get_pattern_object(SM):
if not SM.is_DFA_compliant(): result = nfa_to_dfa.do(SM)
else: result = SM
result = hopcroft.do(result, CreateNewStateMachineF=False)
return Pattern(result, AllowStateMachineTrafoF=True)
identifier = read_option_start(fh)
if identifier is None: return False
verify_word_in_list(identifier, mode_option_info_db.keys(),
"mode option", fh.name, get_current_line_info_number(fh))
if identifier == "skip":
# A skipper 'eats' characters at the beginning of a pattern that belong
# to a specified set of characters. A useful application is most probably
# the whitespace skipper '[ \t\n]'. The skipper definition allows quex to
# implement a very effective way to skip these regions.
pattern_str, trigger_set = regular_expression.parse_character_set(fh, PatternStringF=True)
skip_whitespace(fh)
if fh.read(1) != ">":
error_msg("missing closing '>' for mode option '%s'." % identifier, fh)
if trigger_set.is_empty():
error_msg("Empty trigger set for skipper." % identifier, fh)
# TriggerSet skipping is implemented the following way: As soon as one element of the
# trigger set appears, the state machine enters the 'trigger set skipper section'.
# Enter the skipper as if the opener pattern was a normal pattern and the 'skipper' is the action.
# NOTE: The correspondent CodeFragment for skipping is created in 'implement_skippers(...)'
pattern_sm = StateMachine()
pattern_sm.add_transition(pattern_sm.init_state_index, trigger_set, AcceptanceF=True)
# Skipper code is to be generated later
action = GeneratedCode(skip_character_set.do,
FileName = fh.name,
LineN = get_current_line_info_number(fh))
action.data["character_set"] = trigger_set
new_mode.add_match(pattern_str, action, get_pattern_object(pattern_sm),
Comment=E_SpecialPatterns.SKIP)
return True
elif identifier in ["skip_range", "skip_nested_range"]:
# A non-nesting skipper can contain a full fledged regular expression as opener,
# since it only effects the trigger. Not so the nested range skipper-see below.
# -- opener
skip_whitespace(fh)
if identifier == "skip_nested_range":
# Nested range state machines only accept 'strings' not state machines
opener_str, opener_sequence = __parse_string(fh, "Opener pattern for 'skip_nested_range'")
opener_sm = StateMachine.from_sequence(opener_sequence)
else:
opener_str, opener_pattern = regular_expression.parse(fh)
opener_sm = opener_pattern.sm
# For 'range skipping' the opener sequence is not needed, only the opener state
# machine is webbed into the pattern matching state machine.
opener_sequence = None
skip_whitespace(fh)
# -- closer
closer_str, closer_sequence = __parse_string(fh, "Closing pattern for 'skip_range' or 'skip_nested_range'")
skip_whitespace(fh)
if fh.read(1) != ">":
error_msg("missing closing '>' for mode option '%s'" % identifier, fh)
# Skipper code is to be generated later
generator_function, comment = {
"skip_range": (skip_range.do, E_SpecialPatterns.SKIP_RANGE),
"skip_nested_range": (skip_nested_range.do, E_SpecialPatterns.SKIP_NESTED_RANGE),
}[identifier]
action = GeneratedCode(generator_function,
FileName = fh.name,
LineN = get_current_line_info_number(fh))
action.data["opener_sequence"] = opener_sequence
action.data["closer_sequence"] = closer_sequence
action.data["mode_name"] = new_mode.name
new_mode.add_match(opener_str, action, get_pattern_object(opener_sm), Comment=comment)
return True
elif identifier == "indentation":
value = indentation_setup.do(fh)
# Enter 'Newline' and 'Suppressed Newline' as matches into the engine.
# Similar to skippers, the indentation count is then triggered by the newline.
# -- Suppressed Newline = Suppressor followed by Newline,
# then newline does not trigger indentation counting.
suppressed_newline_pattern_str = ""
if value.newline_suppressor_state_machine.get() is not None:
suppressed_newline_pattern_str = \
"(" + value.newline_suppressor_state_machine.pattern_string() + ")" \
+ "(" + value.newline_state_machine.pattern_string() + ")"
#.........這裏部分代碼省略.........