Пример #1
0
def _ValueToPartValue(val, quoted):
    """Helper for VarSub evaluation."""
    assert isinstance(val, runtime.value), val
    if val.tag == value_e.Undef:
        return runtime.UndefPartValue()

    elif val.tag == value_e.Str:
        return runtime.StringPartValue(val.s, not quoted, not quoted)

    elif val.tag == value_e.StrArray:
        return runtime.ArrayPartValue(val.strs)

    else:
        raise AssertionError
Пример #2
0
def _ValueToPartValue(val, quoted):
  """Helper for VarSub evaluation.

  Called by _EvalBracedVarSub and __EvalWordPart for SimpleVarSub.
  """
  assert isinstance(val, runtime.value), val

  if val.tag == value_e.Str:
    return runtime.StringPartValue(val.s, not quoted)

  elif val.tag == value_e.StrArray:
    return runtime.ArrayPartValue(val.strs)

  else:
    # Undef should be caught by _EmptyStrOrError().
    raise AssertionError
Пример #3
0
    def _EvalDoubleQuotedPart(self, part):
        # Example of returning array:
        # $ a=(1 2); b=(3); $ c=(4 5)
        # $ argv "${a[@]}${b[@]}${c[@]}"
        # ['1', '234', '5']
        # Example of multiple parts
        # $ argv "${a[@]}${undef[@]:-${c[@]}}"
        # ['1', '24', '5']

        #log('DQ part %s', part)

        # Special case for "".  The parser outputs (DoubleQuotedPart []), instead
        # of (DoubleQuotedPart [LiteralPart '']).  This is better but it means we
        # have to check for it.
        if not part.parts:
            return runtime.StringPartValue('', False, False)

        frag_arrays = [[]]
        for p in part.parts:
            for part_val in self._EvalWordPart(p, quoted=True):
                assert isinstance(part_val, runtime.part_value), (p, part_val)
                if part_val.tag == part_value_e.StringPartValue:
                    frag_arrays[-1].append(part_val.s)
                else:
                    for i, s in enumerate(part_val.strs):
                        if i == 0:
                            frag_arrays[-1].append(s)
                        else:
                            frag_arrays.append([s])

        #log('frag_arrays %s', frag_arrays)

        strs = []
        for frag_array in frag_arrays:
            # "${empty[@]}" leads to [[]], should eval to [] and not ['']
            if frag_array:
                strs.append(''.join(frag_array))

        # This should be able to evaluate to EMPTY ARRAY!
        #log('strs %s', strs)
        if len(strs) == 1:
            val = runtime.StringPartValue(strs[0], False, False)
        else:
            val = runtime.ArrayPartValue(strs)
        return val