text
stringlengths
0
828
Can be Nonterminal or epsilon itself.
:param translate: Dictionary where key is nonterminal and value is rule which is next to generate epsilon.
:return: Nonterminal instance with part of AST generating epsilon.
""""""
# the symbol is epsilon directly, just return Terminal.
if root is EPSILON:
return Terminal(EPSILON)
# create nonterminal
created_nonterm = root() # type: Nonterminal
created_rule = translate[root]() # type: Rule
created_nonterm._set_to_rule(created_rule)
created_rule._from_symbols.append(created_nonterm)
# all symbols from the right are rewritable to epsilon, so we need to restore them as well
for ch in created_rule.right:
p = _restore_tree_for(ch, translate) # type: Nonterminal
p._set_from_rule(created_rule)
created_rule._to_symbols.append(p)
return created_nonterm"
4852,"def epsilon_rules_restore(root):
# type: (Nonterminal) -> Nonterminal
""""""
Transform parsed tree to contain epsilon rules originally removed from the grammar.
:param root: Root of the parsed tree.
:return: Modified tree including epsilon rules.
""""""
items = Traversing.post_order(root)
items = filter(lambda x: isinstance(x, EpsilonRemovedRule), items)
for rule in items:
# create original rule
created_rule = rule.from_rule() # type: Rule
# attach parrents parents
for s in rule.from_symbols: # type: Nonterminal
s._set_to_rule(created_rule)
created_rule._from_symbols.append(s)
# attach children up to replace index (that will contain epsilon)
for i in range(rule.replace_index):
ch = rule.to_symbols[i] # type: Nonterminal
ch._set_from_rule(created_rule)
created_rule._to_symbols.append(ch)
# add symbols originally rewrote to epsilon
symb = _restore_tree_for(created_rule.right[rule.replace_index], rule.backtrack) # type: Nonterminal
created_rule._to_symbols.append(symb)
symb._set_from_rule(created_rule)
# attach rest of children
for i in range(rule.replace_index, len(rule.to_symbols)):
ch = rule.to_symbols[i] # type: Nonterminal
ch._set_from_rule(created_rule)
created_rule._to_symbols.append(ch)
return root"
4853,"def char_on_predicate(compiler, cont, test):
'''return current char and step if @test succeed, where
@test: a python function with one argument, which tests on one char and return True or False
@test must be registered with register_function'''
test = test.interlang()
text = compiler.new_var(il.ConstLocalVar('text'))
pos = compiler.new_var(il.ConstLocalVar('pos'))
if not isinstance(test, il.PyFunction):
raise DaoCompileTypeError(test)
return il.Begin((
il.AssignFromList(text, pos, il.parse_state),
il.If(il.Ge(pos,il.Len(text)),
il.failcont(il.FALSE),
il.If(il.Call(test, il.GetItem(text, pos)),
il.begin(
il.SetParseState(il.Tuple(text, il.add(pos, il.Integer(1)))),
il.append_failcont(compiler,
il.SetParseState(il.Tuple(text, pos))),
cont(il.GetItem(text, pos))),
il.failcont(il.FALSE)))))"
4854,"def char_between(lower, upper, func_name):
'''return current char and step if char is between lower and upper, where
@test: a python function with one argument, which tests on one char and return True or False
@test must be registered with register_function'''
function = register_function(func_name,
lambda char: lower<=char<=upper)
return char_on_predicate(function)"
4855,"def char_in(string, func_name):
'''return current char and step if char is in string, where
@test: a python function with one argument, which tests on one char and return True or False
@test must be registered with register_function'''
function = register_function(func_name,
lambda char: char in string)
return char_on_predicate(function)"
4856,"def string_on_predicate0(compiler, cont, test):
'''return current char and step if @test succeed, where
@test: a python function with one argument, which tests on one char and return True or False
@test must be registered with register_function'''
test = test.interlang()
text = compiler.new_var(il.ConstLocalVar('text'))
pos = compiler.new_var(il.ConstLocalVar('pos'))
length = compiler.new_var(il.ConstLocalVar('length'))
p = compiler.new_var(il.LocalVar('p'))
if not isinstance(test, il.PyFunction):
raise DaoCompileTypeError(test)
return il.Begin((
il.AssignFromList(text, pos, il.parse_state),
il.Assign(length, il.Len(text)),
il.If(il.Ge(pos,il.Len(text)),
cont(il.String('')),
il.begin(