Skip to content
Closed
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
12 changes: 10 additions & 2 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ def parse(source, filename='<unknown>', mode='exec', *,

def literal_eval(node_or_string):
"""
Safely evaluate an expression node or a string containing a Python
Evaluate an expression node or a string containing only a Python
expression. The string or node provided may only consist of the following
Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
sets, booleans, and None.

Caution: A complex expression can overflow the C stack and cause a crash.
"""
if isinstance(node_or_string, str):
node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval')
Expand Down Expand Up @@ -234,6 +236,12 @@ def increment_lineno(node, n=1):
location in a file.
"""
for child in walk(node):
# TypeIgnore is a special case where lineno is not an attribute
# but rather a field of the node itself.
if isinstance(child, TypeIgnore):
child.lineno = getattr(child, 'lineno', 0) + n
continue

if 'lineno' in child._attributes:
child.lineno = getattr(child, 'lineno', 0) + n
if (
Expand Down Expand Up @@ -849,7 +857,7 @@ def visit_Import(self, node):

def visit_ImportFrom(self, node):
self.fill("from ")
self.write("." * node.level)
self.write("." * (node.level or 0))
if node.module:
self.write(node.module)
self.write(" import ")
Expand Down
9 changes: 4 additions & 5 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,11 @@ def wrapper(self):

def _create_fn(name, args, body, *, globals=None, locals=None,
return_type=MISSING):
# Note that we mutate locals when exec() is called. Caller
# beware! The only callers are internal to this module, so no
# Note that we may mutate locals. Callers beware!
# The only callers are internal to this module, so no
# worries about external callers.
if locals is None:
locals = {}
if 'BUILTINS' not in locals:
locals['BUILTINS'] = builtins
return_annotation = ''
if return_type is not MISSING:
locals['_return_type'] = return_type
Expand All @@ -443,7 +441,7 @@ def _field_assign(frozen, name, value, self_name):
# self_name is what "self" is called in this function: don't
# hard-code "self", since that might be a field name.
if frozen:
return f'BUILTINS.object.__setattr__({self_name},{name!r},{value})'
return f'__dataclass_builtins_object__.__setattr__({self_name},{name!r},{value})'
return f'{self_name}.{name}={value}'


Expand Down Expand Up @@ -550,6 +548,7 @@ def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,
locals.update({
'MISSING': MISSING,
'_HAS_DEFAULT_FACTORY': _HAS_DEFAULT_FACTORY,
'__dataclass_builtins_object__': object,
})

body_lines = []
Expand Down
1 change: 1 addition & 0 deletions compiler/parser/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fn gen_phf() {
.entry("in", "Tok::In")
.entry("is", "Tok::Is")
.entry("lambda", "Tok::Lambda")
.entry("match", "Tok::Match")
.entry("nonlocal", "Tok::Nonlocal")
.entry("not", "Tok::Not")
.entry("or", "Tok::Or")
Expand Down
164 changes: 164 additions & 0 deletions compiler/parser/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ AssertStatement: ast::Stmt = {
};

CompoundStatement: ast::Stmt = {
MatchStatement,
IfStatement,
WhileStatement,
ForStatement,
Expand All @@ -356,6 +357,167 @@ CompoundStatement: ast::Stmt = {
ClassDef,
};

// TODO: https://github.com/RustPython/RustPython/pull/2399
// TODO: https://github.com/python/cpython/pulls?page=2&q=is%3Apr+pattern+matching+is%3Aclosed
// TODO: https://github.com/python/cpython/pull/22917
// TODO: https://github.com/python/cpython/pull/28043
// TODO: https://github.com/lalrpop/lalrpop
MatchStatement: ast::Stmt = {
<location:@L> "match" <subject:NamedExpressionTest> ":" "\n" Indent <cases:OneOrMore<MatchCase>> <end_location:@R> Dedent => {
ast::Stmt {
custom: (),
location,
end_location: Some(end_location),
node: ast::StmtKind::Match {
subject: Box::new(subject),
cases: cases
}
}
}
}

MatchCase: ast::MatchCase = {
"case" <pattern:Pattern> <guard:(Guard)?> ":" <body:Suite> => {
ast::MatchCase {
pattern: Box::new(pattern),
guard: guard.map(Box::new),
body
}
}
}

Guard: ast::Expr = {
"if" <guard:NamedExpressionTest> => {
guard
}
}

Pattern: ast::Pattern = {
// <pattern:AsPattern> => pattern,
<pattern:OrPattern> => pattern,
}

// AsPattern: ast::Pattern = {
// <location:@L> <pattern:OrPattern> "as" <target:PatternCaptureTarget> => ast::Pattern {

// }
// }

// FIXME
OrPattern: ast::Pattern = {
<pattern:ClosedPattern> => pattern,
}

ClosedPattern: ast::Pattern = {
<location:@L> <node:LiteralPattern> <end_location:@R> => ast::Pattern {
location,
end_location: Some(end_location),
custom: (),
node,
},
// <pattern:CapturePattern> => pattern,
// <location:@L> "_" <end_location:@R> => ast::Pattern {
// location,
// end_location: Some(end_location),
// custom: (),
// node: ast::PatternKind::MatchAs(),
// },
// <pattern:ValuePattern> => pattern,
// <pattern:GroupPattern> => pattern,
// <pattern:SequencePattern> => pattern,
// <pattern:MappingPattern> => pattern,
// <pattern:ClassPattern> => pattern,
}

LiteralPattern: ast::PatternKind = {
<location:@L> <value:Constant> <end_location:@R> => ast::PatternKind::MatchValue {
value: Box::new(ast::Expr {
location,
end_location: Some(end_location),
custom: (),
node: ast::ExprKind::Constant { value, kind: None }
})
},
"None" => ast::PatternKind::MatchSingleton {
value: ast::Constant::None
},
"True" => ast::PatternKind::MatchSingleton {
value: true.into()
},
"False" => ast::PatternKind::MatchSingleton {
value: false.into()
},
}

// ValuePattern: ast::Pattern = {
// // TODO:
// <value:MatchValue> => value,
// }

// MatchValue: ast::Pattern = {
// <location:@L> "not" <e:NotTest> <end_location:@R> => ast::Pattern {
// location,
// end_location: Some(end_location),
// custom: (),
// node: ast::PatternKind::MatchValue { }
// },
// };

// MatchSequence: ast::Pattern = {
// <location: @L> <end_location:@R> => ast::Pattern {
// location,
// end_location: Some(end_location),
// custom: (),
// node: ast::PatternKind::MatchSequence { }
// }
// }

// MatchMapping: ast::Pattern = {
// <location: @L> <end_location:@R> => ast::Pattern {
// location,
// end_location: Some(end_location),
// custom: (),
// node: ast::PatternKind::MatchMapping { }
// }
// }

// MatchClass: ast::Pattern = {
// <location: @L> <end_location:@R> => ast::Pattern {
// location,
// end_location: Some(end_location),
// custom: (),
// node: ast::PatternKind::MatchClass { }
// }
// }

// MatchStar: ast::Pattern = {
// <location: @L> <end_location:@R> => ast::Pattern {
// location,
// end_location: Some(end_location),
// custom: (),
// node: ast::PatternKind::MatchStar { }
// }
// }

// MatchAs: ast::Pattern = {
// <location: @L> <end_location:@R> => ast::Pattern {
// location,
// end_location: Some(end_location),
// custom: (),
// node: ast::PatternKind::MatchAs { }
// }
// }

// MatchOr: ast::Pattern = {
// <location: @L> <end_location:@R> => ast::Pattern {
// location,
// end_location: Some(end_location),
// custom: (),
// node: ast::PatternKind::MatchOr { }
// }
// }


IfStatement: ast::Stmt = {
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite @R)*> <s3:("else" ":" Suite)?> <end_location:@R> => {
// Determine last else:
Expand Down Expand Up @@ -1401,6 +1563,8 @@ extern {
"return" => lexer::Tok::Return,
"try" => lexer::Tok::Try,
"while" => lexer::Tok::While,
"match" => lexer::Tok::Match,
"case" => lexer::Tok::Case,
"with" => lexer::Tok::With,
"yield" => lexer::Tok::Yield,
"True" => lexer::Tok::True,
Expand Down
4 changes: 4 additions & 0 deletions compiler/parser/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ pub enum Tok {
Return,
Try,
While,
Match,
Case,
With,
Yield,
}
Expand Down Expand Up @@ -230,6 +232,8 @@ impl fmt::Display for Tok {
Return => f.write_str("'return'"),
Try => f.write_str("'try'"),
While => f.write_str("'while'"),
Match => f.write_str("'match'"),
Case => f.write_str("'case'"),
With => f.write_str("'with'"),
Yield => f.write_str("'yield'"),
ColonEqual => f.write_str("':='"),
Expand Down