Example #1
0
    def ReadWord(self, unused_lex_mode):
        # type: (lex_mode_t) -> word__String
        """Interface for bool_parse.py.

    TODO: This should probably be word_t
    """
        if self.i == self.n:
            # Does it make sense to define Eof_Argv or something?
            # TODO: Add a way to show this location.  Show 1 char past the right-most
            # spid of the last word?  But we only have the left-most spid.
            w = word.String(Id.Eof_Real, '', runtime.NO_SPID)
            return w

        #log('ARGV %s i %d', self.argv, self.i)
        s = self.cmd_val.argv[self.i]
        left_spid = self.cmd_val.arg_spids[self.i]
        self.i += 1

        # default is an operand word
        id_ = match.BracketUnary(s)
        if id_ == Id.Undefined_Tok:
            id_ = match.BracketBinary(s)
        if id_ == Id.Undefined_Tok:
            id_ = match.BracketOther(s)
        if id_ == Id.Undefined_Tok:
            id_ = Id.Word_Compound

        # NOTE: We only have the left spid now.  It might be useful to add the
        # right one.
        w = word.String(id_, s, left_spid)
        return w
Example #2
0
def _ThreeArgs(w_parser):
    # type: (_StringWordEmitter) -> bool_expr_t
    """Returns an expression tree to be evaluated."""
    w0 = w_parser.Read()
    w1 = w_parser.Read()
    w2 = w_parser.Read()

    # NOTE: Order is important here.

    binary_id = match.BracketBinary(w1.s)
    if binary_id != Id.Undefined_Tok:
        return bool_expr.Binary(binary_id, w0, w2)

    if w1.s == '-a':
        return bool_expr.LogicalAnd(bool_expr.WordTest(w0),
                                    bool_expr.WordTest(w2))

    if w1.s == '-o':
        return bool_expr.LogicalOr(bool_expr.WordTest(w0),
                                   bool_expr.WordTest(w2))

    if w0.s == '!':
        w_parser.Rewind(2)
        child = _TwoArgs(w_parser)
        return bool_expr.LogicalNot(child)

    if w0.s == '(' and w2.s == ')':
        return bool_expr.WordTest(w1)

    p_die('Expected binary operator, got %r (3 args)', w1.s, word=w1)