Skip to content

Commit 894fa59

Browse files
Merge remote-tracking branch 'origin/master' into joey/set-dict-payload
Conflicts: vm/src/pyobject.rs
2 parents d9c35f9 + 6ba25ef commit 894fa59

23 files changed

+306
-57
lines changed

Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustpython"
3-
version = "0.0.1"
3+
version = "0.0.1-pre-alpha.1"
44
authors = ["Windel Bouwman", "Shing Lyu <[email protected]>"]
55
edition = "2018"
66

parser/src/ast.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub enum Statement {
122122
// docstring: String,
123123
body: Vec<LocatedStatement>,
124124
decorator_list: Vec<Expression>,
125+
returns: Option<Expression>,
125126
},
126127
}
127128

@@ -217,6 +218,7 @@ pub enum Expression {
217218
True,
218219
False,
219220
None,
221+
Ellipsis,
220222
}
221223

222224
impl Expression {
@@ -259,6 +261,7 @@ impl Expression {
259261
Lambda { .. } => "lambda",
260262
IfExpression { .. } => "conditional expression",
261263
True | False | None => "keyword",
264+
Ellipsis => "ellipsis",
262265
}
263266
}
264267
}
@@ -269,14 +272,20 @@ impl Expression {
269272
*/
270273
#[derive(Debug, PartialEq, Default)]
271274
pub struct Parameters {
272-
pub args: Vec<String>,
273-
pub kwonlyargs: Vec<String>,
274-
pub vararg: Option<Option<String>>, // Optionally we handle optionally named '*args' or '*'
275-
pub kwarg: Option<Option<String>>,
275+
pub args: Vec<Parameter>,
276+
pub kwonlyargs: Vec<Parameter>,
277+
pub vararg: Option<Option<Parameter>>, // Optionally we handle optionally named '*args' or '*'
278+
pub kwarg: Option<Option<Parameter>>,
276279
pub defaults: Vec<Expression>,
277280
pub kw_defaults: Vec<Option<Expression>>,
278281
}
279282

283+
#[derive(Debug, PartialEq, Default)]
284+
pub struct Parameter {
285+
pub arg: String,
286+
pub annotation: Option<Box<Expression>>,
287+
}
288+
280289
#[derive(Debug, PartialEq)]
281290
pub enum ComprehensionKind {
282291
GeneratorExpression { element: Expression },

parser/src/lexer.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,15 @@ where
993993
Some('.') => {
994994
let tok_start = self.get_pos();
995995
self.next_char();
996-
let tok_end = self.get_pos();
997-
return Some(Ok((tok_start, Tok::Dot, tok_end)));
996+
if let (Some('.'), Some('.')) = (&self.chr0, &self.chr1) {
997+
self.next_char();
998+
self.next_char();
999+
let tok_end = self.get_pos();
1000+
return Some(Ok((tok_start, Tok::Ellipsis, tok_end)));
1001+
} else {
1002+
let tok_end = self.get_pos();
1003+
return Some(Ok((tok_start, Tok::Dot, tok_end)));
1004+
}
9981005
}
9991006
Some('\n') => {
10001007
let tok_start = self.get_pos();

parser/src/parser.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,16 @@ mod tests {
244244
node: ast::Statement::Expression {
245245
expression: ast::Expression::Lambda {
246246
args: ast::Parameters {
247-
args: vec![String::from("x"), String::from("y")],
247+
args: vec![
248+
ast::Parameter {
249+
arg: String::from("x"),
250+
annotation: None,
251+
},
252+
ast::Parameter {
253+
arg: String::from("y"),
254+
annotation: None,
255+
}
256+
],
248257
kwonlyargs: vec![],
249258
vararg: None,
250259
kwarg: None,
@@ -330,7 +339,10 @@ mod tests {
330339
node: ast::Statement::FunctionDef {
331340
name: String::from("__init__"),
332341
args: ast::Parameters {
333-
args: vec![String::from("self")],
342+
args: vec![ast::Parameter {
343+
arg: String::from("self"),
344+
annotation: None,
345+
}],
334346
kwonlyargs: vec![],
335347
vararg: None,
336348
kwarg: None,
@@ -342,14 +354,24 @@ mod tests {
342354
node: ast::Statement::Pass,
343355
}],
344356
decorator_list: vec![],
357+
returns: None,
345358
}
346359
},
347360
ast::LocatedStatement {
348361
location: ast::Location::new(4, 2),
349362
node: ast::Statement::FunctionDef {
350363
name: String::from("method_with_default"),
351364
args: ast::Parameters {
352-
args: vec![String::from("self"), String::from("arg"),],
365+
args: vec![
366+
ast::Parameter {
367+
arg: String::from("self"),
368+
annotation: None,
369+
},
370+
ast::Parameter {
371+
arg: String::from("arg"),
372+
annotation: None,
373+
}
374+
],
353375
kwonlyargs: vec![],
354376
vararg: None,
355377
kwarg: None,
@@ -365,6 +387,7 @@ mod tests {
365387
node: ast::Statement::Pass,
366388
}],
367389
decorator_list: vec![],
390+
returns: None,
368391
}
369392
}
370393
],

parser/src/python.lalrpop

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -446,21 +446,22 @@ WithItem: ast::WithItem = {
446446
};
447447

448448
FuncDef: ast::LocatedStatement = {
449-
<d:Decorator*> <loc:@L> "def" <i:Identifier> <a:Parameters> ":" <s:Suite> => {
449+
<d:Decorator*> <loc:@L> "def" <i:Identifier> <a:Parameters> <r:("->" Test)?> ":" <s:Suite> => {
450450
ast::LocatedStatement {
451451
location: loc,
452452
node: ast::Statement::FunctionDef {
453453
name: i,
454454
args: a,
455455
body: s,
456456
decorator_list: d,
457+
returns: r.map(|x| x.1),
457458
}
458459
}
459460
},
460461
};
461462

462463
Parameters: ast::Parameters = {
463-
"(" <a: (TypedArgsList)?> ")" => {
464+
"(" <a: (TypedArgsList<TypedParameter>)?> ")" => {
464465
match a {
465466
Some(a) => a,
466467
None => Default::default(),
@@ -470,8 +471,10 @@ Parameters: ast::Parameters = {
470471

471472
// parameters are (String, None), kwargs are (String, Some(Test)) where Test is
472473
// the default
473-
TypedArgsList: ast::Parameters = {
474-
<param1:TypedParameters> <args2:("," ParameterListStarArgs)?> => {
474+
// Note that this is a macro which is used once for function defs, and
475+
// once for lambda defs.
476+
TypedArgsList<ArgType>: ast::Parameters = {
477+
<param1:TypedParameters<ArgType>> <args2:("," ParameterListStarArgs<ArgType>)?> => {
475478
let (names, default_elements) = param1;
476479

477480
// Now gather rest of parameters:
@@ -489,7 +492,7 @@ TypedArgsList: ast::Parameters = {
489492
kw_defaults: kw_defaults,
490493
}
491494
},
492-
<param1:TypedParameters> <kw:("," KwargParameter)> => {
495+
<param1:TypedParameters<ArgType>> <kw:("," KwargParameter<ArgType>)> => {
493496
let (names, default_elements) = param1;
494497

495498
// Now gather rest of parameters:
@@ -507,7 +510,7 @@ TypedArgsList: ast::Parameters = {
507510
kw_defaults: kw_defaults,
508511
}
509512
},
510-
<params:ParameterListStarArgs> => {
513+
<params:ParameterListStarArgs<ArgType>> => {
511514
let (vararg, kwonlyargs, kw_defaults, kwarg) = params;
512515
ast::Parameters {
513516
args: vec![],
@@ -518,7 +521,7 @@ TypedArgsList: ast::Parameters = {
518521
kw_defaults: kw_defaults,
519522
}
520523
},
521-
<kw:KwargParameter> => {
524+
<kw:KwargParameter<ArgType>> => {
522525
ast::Parameters {
523526
args: vec![],
524527
kwonlyargs: vec![],
@@ -532,8 +535,8 @@ TypedArgsList: ast::Parameters = {
532535

533536
// Use inline here to make sure the "," is not creating an ambiguity.
534537
#[inline]
535-
TypedParameters: (Vec<String>, Vec<ast::Expression>) = {
536-
<param1:TypedParameterDef> <param2:("," TypedParameterDef)*> => {
538+
TypedParameters<ArgType>: (Vec<ast::Parameter>, Vec<ast::Expression>) = {
539+
<param1:TypedParameterDef<ArgType>> <param2:("," TypedParameterDef<ArgType>)*> => {
537540
// Combine first parameters:
538541
let mut args = vec![param1];
539542
args.extend(param2.into_iter().map(|x| x.1));
@@ -542,7 +545,6 @@ TypedParameters: (Vec<String>, Vec<ast::Expression>) = {
542545
let mut default_elements = vec![];
543546

544547
for (name, default) in args.into_iter() {
545-
names.push(name.clone());
546548
if let Some(default) = default {
547549
default_elements.push(default);
548550
} else {
@@ -551,28 +553,35 @@ TypedParameters: (Vec<String>, Vec<ast::Expression>) = {
551553
// have defaults
552554
panic!(
553555
"non-default argument follows default argument: {}",
554-
name
556+
&name.arg
555557
);
556558
}
557559
}
560+
names.push(name);
558561
}
559562

560563
(names, default_elements)
561564
}
562565
};
563566

564-
TypedParameterDef: (String, Option<ast::Expression>) = {
565-
<i:TypedParameter> => (i, None),
566-
<i:TypedParameter> "=" <e:Test> => (i, Some(e)),
567+
TypedParameterDef<ArgType>: (ast::Parameter, Option<ast::Expression>) = {
568+
<i:ArgType> => (i, None),
569+
<i:ArgType> "=" <e:Test> => (i, Some(e)),
567570
};
568571

569-
// TODO: add type annotations here:
570-
TypedParameter: String = {
571-
Identifier,
572+
UntypedParameter: ast::Parameter = {
573+
<i:Identifier> => ast::Parameter { arg: i, annotation: None },
572574
};
573575

574-
ParameterListStarArgs: (Option<Option<String>>, Vec<String>, Vec<Option<ast::Expression>>, Option<Option<String>>) = {
575-
"*" <va:Identifier?> <kw:("," TypedParameterDef)*> <kwarg:("," KwargParameter)?> => {
576+
TypedParameter: ast::Parameter = {
577+
<arg:Identifier> <a:(":" Test)?>=> {
578+
let annotation = a.map(|x| Box::new(x.1));
579+
ast::Parameter { arg, annotation }
580+
},
581+
};
582+
583+
ParameterListStarArgs<ArgType>: (Option<Option<ast::Parameter>>, Vec<ast::Parameter>, Vec<Option<ast::Expression>>, Option<Option<ast::Parameter>>) = {
584+
"*" <va:ArgType?> <kw:("," TypedParameterDef<ArgType>)*> <kwarg:("," KwargParameter<ArgType>)?> => {
576585
// Extract keyword arguments:
577586
let mut kwonlyargs = vec![];
578587
let mut kw_defaults = vec![];
@@ -590,8 +599,8 @@ ParameterListStarArgs: (Option<Option<String>>, Vec<String>, Vec<Option<ast::Exp
590599
}
591600
};
592601

593-
KwargParameter: Option<String> = {
594-
"**" <kwarg:Identifier?> => {
602+
KwargParameter<ArgType>: Option<ast::Parameter> = {
603+
"**" <kwarg:ArgType?> => {
595604
kwarg
596605
}
597606
};
@@ -675,7 +684,7 @@ Test: ast::Expression = {
675684
};
676685

677686
LambdaDef: ast::Expression = {
678-
"lambda" <p:TypedArgsList?> ":" <b:Test> =>
687+
"lambda" <p:TypedArgsList<UntypedParameter>?> ":" <b:Test> =>
679688
ast::Expression::Lambda {
680689
args: p.unwrap_or(Default::default()),
681690
body:Box::new(b)
@@ -839,6 +848,7 @@ Atom: ast::Expression = {
839848
"True" => ast::Expression::True,
840849
"False" => ast::Expression::False,
841850
"None" => ast::Expression::None,
851+
"..." => ast::Expression::Ellipsis,
842852
};
843853

844854
TestListComp: Vec<ast::Expression> = {
@@ -1048,6 +1058,7 @@ extern {
10481058
"~" => lexer::Tok::Tilde,
10491059
":" => lexer::Tok::Colon,
10501060
"." => lexer::Tok::Dot,
1061+
"..." => lexer::Tok::Ellipsis,
10511062
"," => lexer::Tok::Comma,
10521063
"*" => lexer::Tok::Star,
10531064
"**" => lexer::Tok::DoubleStar,
@@ -1086,6 +1097,7 @@ extern {
10861097
"<=" => lexer::Tok::LessEqual,
10871098
">" => lexer::Tok::Greater,
10881099
">=" => lexer::Tok::GreaterEqual,
1100+
"->" => lexer::Tok::Rarrow,
10891101
"and" => lexer::Tok::And,
10901102
"as" => lexer::Tok::As,
10911103
"assert" => lexer::Tok::Assert,

tests/snippets/builtin_dir.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
class A:
3+
def test():
4+
pass
5+
6+
a = A()
7+
8+
assert "test" in dir(a)
9+
10+
import socket
11+
12+
assert "AF_INET" in dir(socket)

tests/snippets/builtin_locals.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
a = 5
3+
b = 6
4+
5+
loc = locals()
6+
7+
assert loc['a'] == 5
8+
assert loc['b'] == 6
9+
10+
def f():
11+
c = 4
12+
a = 7
13+
14+
loc = locals()
15+
16+
assert loc['a'] == 4
17+
assert loc['c'] == 7
18+
assert not 'b' in loc
19+

tests/snippets/ellipsis.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
a = ...
4+
b = ...
5+
c = type(a)() # Test singleton behavior
6+
7+
assert a is b
8+
assert b is c

0 commit comments

Comments
 (0)