Esempio n. 1
0
File: word.py Progetto: silky/oil
def LooksLikeAssignment(w):
  """Tests whether a word looke like FOO=bar.

  If so, return a (string, CompoundWord) pair.  Otherwise, return False.  
  """
  assert w.tag == word_e.CompoundWord
  if len(w.parts) == 0:
    return False

  part0 = w.parts[0]
  if _LiteralPartId(part0) != Id.Lit_VarLike:
    return False

  assert part0.token.val.endswith('=')
  name = part0.token.val[:-1]

  rhs = ast.CompoundWord()
  if len(w.parts) == 1:
    # NOTE: This is necesssary so that EmptyUnquoted elision isn't
    # applied.  EMPTY= is like EMPTY=''.
    rhs.parts.append(ast.SingleQuotedPart())
  else:
    for p in w.parts[1:]:
      rhs.parts.append(p)

  return name, rhs
Esempio n. 2
0
def LooksLikeAssignment(w):
    """Tests whether a word looks like FOO=bar.

  Returns:
    (string, CompoundWord) if it looks like FOO=bar
    False                  if it doesn't

  s=1
  s+=1
  s[x]=1
  s[x]+=1

  a=()
  a+=()
  a[x]=()
  a[x]+=()  # Not valid because arrays can't be nested.

  NOTE: a[ and s[ might be parsed separately?
  """
    assert w.tag == word_e.CompoundWord
    if len(w.parts) == 0:
        return False

    part0 = w.parts[0]
    if _LiteralPartId(part0) != Id.Lit_VarLike:
        return False

    s = part0.token.val
    assert s.endswith('=')
    if s[-2] == '+':
        op = assign_op.PlusEqual
        name = s[:-2]
    else:
        op = assign_op.Equal
        name = s[:-1]

    rhs = ast.CompoundWord()
    if len(w.parts) == 1:
        # NOTE: This is necesssary so that EmptyUnquoted elision isn't
        # applied.  EMPTY= is like EMPTY=''.
        rhs.parts.append(ast.SingleQuotedPart())
    else:
        for p in w.parts[1:]:
            rhs.parts.append(p)

    return name, op, rhs
Esempio n. 3
0
    def _ReadSingleQuotedPart(self, lex_mode):
        quoted_part = ast.SingleQuotedPart()

        done = False
        while not done:
            self._Next(lex_mode)
            self._Peek()

            if self.token_kind == Kind.Lit:
                quoted_part.tokens.append(self.cur_token)

            elif self.token_kind == Kind.Eof:
                self.AddErrorContext('Unexpected EOF in single-quoted string')
                return False

            elif self.token_kind == Kind.Right:
                done = True  # assume Id.Right_S_QUOTE

            else:
                raise AssertionError(
                    'Unhandled token in single-quoted part %s (%d)' %
                    (self.cur_token, self.token_kind))

        return quoted_part