Example #1
0
def left(compiler, cont, length=NONE):
  text = compiler.new_var(il.ConstLocalVar('text'))
  pos = compiler.new_var(il.ConstLocalVar('pos'))
  return il.Begin((
    il.AssignFromList(text, pos, il.parse_state),
    cont(il.GetItem(text, il.If(il.IsNot(length, NONE),
                                il.Slice2(pos, il.add(pos,length)),
                                il.Slice2(pos, NONE))))))
Example #2
0
def integer(compiler, cont, arg):
  '''integer'''
  text = compiler.new_var(il.ConstLocalVar('text'))
  pos = compiler.new_var(il.ConstLocalVar('pos'))
  p = compiler.new_var(il.LocalVar('p'))
  length = compiler.new_var(il.ConstLocalVar('length'))
  if isinstance(arg, Var):
    arg = arg.interlang()
    x = compiler.new_var(il.ConstLocalVar('x'))
    return il.Begin((
      il.AssignFromList(text, pos, il.parse_state),
      il.Assign(length, il.Len(text)),
      il.If(il.Ge(pos, length), 
        il.failcont(il.FALSE),
          il.If(il.Not(il.Cle(il.String('0'), il.GetItem(text, pos), il.String('9'))),
            il.failcont(il.FALSE),
            il.Begin((
              il.Assign(p, il.add(pos, il.Integer(1))),
              il.while_(il.And(il.Lt(p, length), 
                              il.Cle(il.String('0'),il.GetItem(text, p),il.String('9'))), 
                       il.AddAssign(p, il.Integer(1))),
              il.Assign(x, il.Deref(arg)),
              il.If(il.IsLogicVar(x),
                    il.begin(il.SetParseState(il.Tuple(text, p)),
                                   il.SetBinding(x, il.GetItem(text, il.Slice2(pos, p))),
                                   il.append_failcont(compiler, 
                                      il.SetParseState(il.Tuple(text, pos)),
                                      il.DelBinding(x)),
                                   cont(il.GetItem(text, pos))),
                    il.If(il.Isinstance(x, il.String('str')),
                          il.If(il.Eq(x, il.GetItem(text, il.Slice2(pos, p))),
                                il.begin(il.append_failcont(compiler, 
                                  il.SetParseState(il.Tuple(text, pos))),
                                  il.SetParseState(il.Tuple(text, p)),
                                  cont(il.GetItem(text, pos))),
                                il.failcont(il.NONE)),
                          il.RaiseTypeError(x)))))))))
  elif isinstance(arg, il.String):
    return il.Begin((
      il.AssignFromList(text, pos, il.parse_state),
      il.Assign(length, il.Len(text)),
      il.If(il.Ge(pos, length), 
        il.failcont(il.FALSE),
          il.If(il.Not(il.Cle(il.String('0'), il.GetItem(text, pos), il.String('9'))),
            il.failcont(il.FALSE),
            il.Begin((
              il.Assign(p, il.add(pos, il.Integer(1))),
              il.while_(il.And(il.Lt(p, length), 
                              il.Cle(il.String('0'),il.GetItem(text, p),il.String('9'))), 
                       il.AddAssign(p, il.Integer(1))),
              il.If(il.Eq(arg, il.GetItem(text, il.Slice2(pos, p))),
                    il.begin(il.append_failcont(compiler, 
                      il.SetParseState(il.Tuple(text, pos))),
                      il.SetParseState(il.Tuple(text, p)),
                      cont(arg)),
                    il.failcont(il.NONE))))))))
  else:
    raise CompileTypeError
Example #3
0
def string_on_predicate1(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, length), 
      il.failcont(il.FALSE),
      il.If(il.Call(test, il.GetItem(text, pos)),
        il.begin(
          il.Assign(p, il.add(pos, il.Integer(1))),
          il.While(il.And(il.Lt(p, length), il.Call(test, il.GetItem(text, p))),
                 il.AddAssign(p, il.Integer(1))),
          il.SetParseState(il.Tuple(text, p)),
          il.append_failcont(compiler, 
                             il.SetParseState(il.Tuple(text, pos))),
          cont(il.GetItem(text, il.Slice2(pos, p)))),
        il.failcont(il.FALSE)))))
Example #4
0
def subsequence(compiler, cont, start, end): 
  text = compiler.new_var(il.ConstLocalVar('text'))
  pos = compiler.new_var(il.ConstLocalVar('pos'))
  start = il.Integer(start.item)
  end = il.Integer(end.item)
  return il.Begin((
    il.AssignFromList(text, pos, il.parse_state),
     cont(il.GetItem(text, il.Slice2(start, end)))))