def _ParseVarExpr(self, arg_lex_mode): """ Start parsing at the op -- we already skipped past the name. """ part = self._ParseVarOf() if not part: return None self._Peek() if self.token_type == Id.Right_VarSub: return part # no ops # Or maybe this is a VarOpKind op_kind = self.token_kind if op_kind == Kind.VTest: op_id = self.token_type arg_word = self._ReadVarOpArg(arg_lex_mode) if self.token_type != Id.Right_VarSub: self._BadToken('Unexpected token after test arg: %s', self.cur_token) return None part.suffix_op = ast.StringUnary(op_id, arg_word) elif op_kind == Kind.VOp1: op_id = self.token_type arg_word = self._ReadVarOpArg(arg_lex_mode) if self.token_type != Id.Right_VarSub: self._BadToken('Unexpected token after unary op: %s', self.cur_token) return None op = ast.StringUnary(op_id, arg_word) part.suffix_op = op elif op_kind == Kind.VOp2: if self.token_type == Id.VOp2_Slash: op = self._ReadPatSubVarOp(arg_lex_mode) if not op: return None # Checked by the method above assert self.token_type == Id.Right_VarSub, self.cur_token elif self.token_type == Id.VOp2_Colon: op = self._ReadSliceVarOp() if not op: return None if self.token_type != Id.Arith_RBrace: self._BadToken('Unexpected token after slice: %s', self.cur_token) return None else: raise AssertionError("Invalid op token %s" % self.cur_token) part.suffix_op = op # Now look for ops return part
def testVarOps(self): ev = InitEvaluator() # initializes x=xxx and y=yyy unset_sub = ast.BracedVarSub(ast.token(Id.VSub_Name, 'unset')) part_vals = [] ev._EvalWordPart(unset_sub, part_vals) print(part_vals) set_sub = ast.BracedVarSub(ast.token(Id.VSub_Name, 'x')) part_vals = [] ev._EvalWordPart(set_sub, part_vals) print(part_vals) # Now add some ops part = ast.LiteralPart(ast.token(Id.Lit_Chars, 'default')) arg_word = ast.CompoundWord([part]) test_op = ast.StringUnary(Id.VTest_ColonHyphen, arg_word) unset_sub.suffix_op = test_op set_sub.suffix_op = test_op part_vals = [] ev._EvalWordPart(unset_sub, part_vals) print(part_vals) part_vals = [] ev._EvalWordPart(set_sub, part_vals) print(part_vals)
def testVarOps(self): ev = InitEvaluator() # initializes x=xxx and y=yyy unset_sub = ast.BracedVarSub(ast.token(Id.Lit_Chars, 'unset')) print(ev.EvalVarSub(unset_sub)) set_sub = ast.BracedVarSub(ast.token(Id.Lit_Chars, 'x')) print(ev.EvalVarSub(set_sub)) part = ast.LiteralPart(ast.token(Id.Lit_Chars, 'default')) arg_word = ast.CompoundWord([part]) test_op = ast.StringUnary(Id.VTest_ColonHyphen, arg_word) unset_sub.suffix_op = test_op set_sub.suffix_op = test_op print(ev.EvalVarSub(unset_sub)) print(ev.EvalVarSub(set_sub))
def _ParseVarExpr(self, arg_lex_mode): """ Start parsing at the op -- we already skipped past the name. """ part = self._ParseVarOf() if not part: return None self._Peek() if self.token_type == Id.Right_VarSub: return part # no ops # Or maybe this is a VarOpKind op_kind = self.token_kind if op_kind == Kind.VTest: op_id = self.token_type arg_word = self._ReadVarOpArg(arg_lex_mode) if self.token_type != Id.Right_VarSub: self._BadToken('Unexpected token after test arg: %s', self.cur_token) return None part.suffix_op = ast.StringUnary(op_id, arg_word) elif op_kind == Kind.VOp1: op_id = self.token_type arg_word = self._ReadVarOpArg(arg_lex_mode) if self.token_type != Id.Right_VarSub: self._BadToken('Unexpected token after unary op: %s', self.cur_token) return None op = ast.StringUnary(op_id, arg_word) part.suffix_op = op elif op_kind == Kind.VOp2: if self.token_type == Id.VOp2_Slash: op = self._ReadPatSubVarOp(arg_lex_mode) if not op: return None # Checked by the method above assert self.token_type == Id.Right_VarSub, self.cur_token elif self.token_type == Id.VOp2_Colon: op = self._ReadSliceVarOp() if not op: return None if self.token_type != Id.Arith_RBrace: self._BadToken('Unexpected token after slice: %s', self.cur_token) return None else: p_die('Unexpected token %s', self.cur_token, token=self.cur_token) part.suffix_op = op # NOTE: Arith_RBrace is for slicing, because it reads } in arithmetic # mode. It's redundantly checked above. if self.token_type not in (Id.Right_VarSub, Id.Arith_RBrace): self._BadToken('Unexpected token after var sub: %s', self.cur_token) return None # Now look for ops return part