Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,8 +1330,6 @@ def test_curly_brace_after_primary_raises_immediately(self):
def test_assign_call(self):
self._check_error("f() = 1", "assign")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_assign_del(self):
self._check_error("del (,)", "invalid syntax")
self._check_error("del 1", "cannot delete literal")
Expand Down
9 changes: 8 additions & 1 deletion ast/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ impl<U> ExprKind<U> {
| Constant::Complex { .. }
| Constant::Bytes(_) => "literal",
Constant::Tuple(_) => "tuple",
Constant::Bool(_) | Constant::None => "keyword",
Constant::Bool(b) => {
if *b {
"True"
} else {
"False"
}
}
Constant::None => "None",
Constant::Ellipsis => "ellipsis",
},
ExprKind::List { .. } => "list",
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,11 +847,14 @@ impl Compiler {
self.compile_expression(slice)?;
self.emit(Instruction::DeleteSubscript);
}
ast::ExprKind::Tuple { elts, .. } => {
ast::ExprKind::Tuple { elts, .. } | ast::ExprKind::List { elts, .. } => {
for element in elts {
self.compile_delete(element)?;
}
}
ast::ExprKind::BinOp { .. } | ast::ExprKind::UnaryOp { .. } => {
return Err(self.error(CompileErrorType::Delete("expression")))
}
_ => return Err(self.error(CompileErrorType::Delete(expression.node.name()))),
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ pub enum CompileErrorType {
impl fmt::Display for CompileErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CompileErrorType::Assign(target) => write!(f, "can't assign to {}", target),
CompileErrorType::Delete(target) => write!(f, "can't delete {}", target),
CompileErrorType::Assign(target) => write!(f, "cannot assign to {}", target),
CompileErrorType::Delete(target) => write!(f, "cannot delete {}", target),
CompileErrorType::SyntaxError(err) => write!(f, "{}", err.as_str()),
CompileErrorType::MultipleStarArgs => {
write!(f, "two starred expressions in assignment")
}
CompileErrorType::InvalidStarExpr => write!(f, "can't use starred expression here"),
CompileErrorType::InvalidStarExpr => write!(f, "cannot use starred expression here"),
CompileErrorType::InvalidBreak => write!(f, "'break' outside loop"),
CompileErrorType::InvalidContinue => write!(f, "'continue' outside loop"),
CompileErrorType::InvalidReturn => write!(f, "'return' outside function"),
Expand Down
39 changes: 31 additions & 8 deletions parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::ast;
use crate::fstring::parse_located_fstring;
use crate::function::{ArgumentList, parse_args, parse_params};
use crate::error::LexicalError;
use crate::error::{LexicalError, LexicalErrorType};
use crate::lexer;
use crate::token::StringKind;

Expand Down Expand Up @@ -999,12 +999,29 @@ Atom: ast::Expr = {
node: ast::ExprKind::ListComp { elt: Box::new(elt), generators }
}
},
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" => {
elements.unwrap_or(ast::Expr {
location,
custom: (),
node: ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
})
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" =>? {
match elements {
Some(elt) => {
match elt.node {
ast::ExprKind::Starred { .. } => {
Err(LexicalError{
error : LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
location,
}.into())
},
_ => {
Ok(elt)
}
}
},
None => {
Ok(ast::Expr {
location,
custom: (),
node: ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
})
}
}
},
"(" <e:YieldExpr> ")" => e,
<location:@L> "(" <elt:Test> <generators:CompFor> ")" => {
Expand All @@ -1014,6 +1031,12 @@ Atom: ast::Expr = {
node: ast::ExprKind::GeneratorExp { elt: Box::new(elt), generators }
}
},
"(" <location:@L> "**" <e:Expression> ")" =>? {
Err(LexicalError{
error : LexicalErrorType::OtherError("cannot use double starred expression here".to_string()),
location,
}.into())
},
<location:@L> "{" <e:DictLiteralValues?> "}" => {
let (keys, values) = e.unwrap_or_default();
ast::Expr {
Expand Down Expand Up @@ -1082,7 +1105,7 @@ ExpressionList: ast::Expr = {
};

ExpressionList2: Vec<ast::Expr> = {
<elements:OneOrMore<Expression>> ","? => elements,
<elements:OneOrMore<ExpressionOrStarExpression>> ","? => elements,
};

// A test list is one of:
Expand Down
Loading