示例#1
0
def assertStyle(test, expected_style, word_str):
    w = _assertReadWord(test, word_str)

    new_word = word.TildeDetect(w)
    if new_word is not None:
        w = new_word

    actual = osh2oil._GetRhsStyle(w)
    test.assertEqual(expected_style, actual)
示例#2
0
  def ParseRedirect(self):
    """
    Problem: You don't know which kind of redir_node to instantiate before
    this?  You could stuff them all in one node, and then have a switch() on
    the type.

    You need different types.
    """
    self._Peek()
    assert self.c_kind == Kind.Redir, self.cur_word

    op = self.cur_word.token

    # For now only supporting single digit descriptor
    first_char = self.cur_word.token.val[0]
    if first_char.isdigit():
      fd = int(first_char)
    else:
      fd = const.NO_INTEGER

    if op.id in (Id.Redir_DLess, Id.Redir_DLessDash):  # here doc
      node = redir.HereDoc()
      node.op = op
      node.fd = fd
      self._Next()

      self._Peek()
      node.here_begin = self.cur_word
      self._Next()

      self.pending_here_docs.append(node)  # will be filled on next newline.

    else:
      node = redir.Redir()
      node.op = op
      node.fd = fd
      self._Next()

      self._Peek()
      if self.c_kind != Kind.Word:
        p_die('Invalid token after redirect operator', word=self.cur_word)

      new_word = word.TildeDetect(self.cur_word)
      node.arg_word = new_word or self.cur_word
      self._Next()

    return node
示例#3
0
def _MakeAssignPair(parse_ctx, preparsed):
  """Create an assign_pair from a 4-tuples from DetectAssignment."""

  left_token, close_token, part_offset, w = preparsed

  if left_token.id == Id.Lit_VarLike:  # s=1
    if left_token.val[-2] == '+':
      var_name = left_token.val[:-2]
      op = assign_op_e.PlusEqual
    else:
      var_name = left_token.val[:-1]
      op = assign_op_e.Equal

    lhs = lhs_expr.LhsName(var_name)
    lhs.spids.append(left_token.span_id)

  elif left_token.id == Id.Lit_ArrayLhsOpen:  # a[x++]=1
    var_name = left_token.val[:-1]
    if close_token.val[-2] == '+':
      op = assign_op_e.PlusEqual
    else:
      op = assign_op_e.Equal

    # Adapted from tools/osh2oil.py Cursor.PrintUntil
    # TODO: Make a method like arena.AppendPieces(start, end, []), and share
    # with alias.
    pieces = []
    for span_id in xrange(left_token.span_id + 1, close_token.span_id):
      span = parse_ctx.arena.GetLineSpan(span_id)
      line = parse_ctx.arena.GetLine(span.line_id)
      piece = line[span.col : span.col + span.length]
      pieces.append(piece)

    # Now reparse everything between here
    code_str = ''.join(pieces)

    # NOTE: It's possible that an alias expansion underlies this, not a real file!
    # We have to use a SideArena since this will happen during translation.
    line_num = 99
    source_name = 'TODO'
    arena = alloc.SideArena('<LHS array index at line %d of %s>' %
                            (line_num, source_name))

    a_parser = parse_ctx.MakeArithParser(code_str, arena)
    expr = a_parser.Parse()  # raises util.ParseError
                             # TODO: It reports from the wrong arena!
    lhs = lhs_expr.LhsIndexedName(var_name, expr)
    lhs.spids.append(left_token.span_id)

  else:
    raise AssertionError

  # TODO: Should we also create a rhs_exp.ArrayLiteral here?
  n = len(w.parts)
  if part_offset == n:
    val = osh_word.EmptyWord()
  else:
    val = osh_word.CompoundWord(w.parts[part_offset:])
    val = word.TildeDetect(val) or val

  pair = syntax_asdl.assign_pair(lhs, op, val)
  pair.spids.append(left_token.span_id)  # Do we need this?
  return pair