Skip to content

Commit e25742f

Browse files
committed
Add ellipsis syntax.
1 parent b50f0be commit e25742f

File tree

7 files changed

+36
-2
lines changed

7 files changed

+36
-2
lines changed

parser/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ pub enum Expression {
217217
True,
218218
False,
219219
None,
220+
Ellipsis,
220221
}
221222

222223
impl Expression {
@@ -259,6 +260,7 @@ impl Expression {
259260
Lambda { .. } => "lambda",
260261
IfExpression { .. } => "conditional expression",
261262
True | False | None => "keyword",
263+
Ellipsis => "ellipsis"
262264
}
263265
}
264266
}

parser/src/lexer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,8 +1011,14 @@ where
10111011
Some('.') => {
10121012
let tok_start = self.get_pos();
10131013
self.next_char();
1014-
let tok_end = self.get_pos();
1015-
return Some(Ok((tok_start, Tok::Dot, tok_end)));
1014+
if let (Some('.'), Some('.')) = (&self.chr0, &self.chr1) {
1015+
self.next_char();self.next_char();
1016+
let tok_end = self.get_pos();
1017+
return Some(Ok((tok_start, Tok::Ellipsis, tok_end)));
1018+
} else {
1019+
let tok_end = self.get_pos();
1020+
return Some(Ok((tok_start, Tok::Dot, tok_end)));
1021+
}
10161022
}
10171023
Some('\n') => {
10181024
let tok_start = self.get_pos();

parser/src/python.lalrpop

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ Atom: ast::Expression = {
839839
"True" => ast::Expression::True,
840840
"False" => ast::Expression::False,
841841
"None" => ast::Expression::None,
842+
"..." => ast::Expression::Ellipsis,
842843
};
843844

844845
TestListComp: Vec<ast::Expression> = {
@@ -1048,6 +1049,7 @@ extern {
10481049
"~" => lexer::Tok::Tilde,
10491050
":" => lexer::Tok::Colon,
10501051
"." => lexer::Tok::Dot,
1052+
"..." => lexer::Tok::Ellipsis,
10511053
"," => lexer::Tok::Comma,
10521054
"*" => lexer::Tok::Star,
10531055
"**" => lexer::Tok::DoubleStar,

vm/src/bytecode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ pub enum Constant {
193193
Code { code: Box<CodeObject> },
194194
Tuple { elements: Vec<Constant> },
195195
None,
196+
Ellipsis,
196197
}
197198

198199
#[derive(Debug, Clone, PartialEq)]
@@ -385,6 +386,7 @@ impl fmt::Display for Constant {
385386
.join(", ")
386387
),
387388
Constant::None => write!(f, "None"),
389+
Constant::Ellipsis => write!(f, "Ellipsis"),
388390
}
389391
}
390392
}

vm/src/compile.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,11 @@ impl Compiler {
10561056
value: bytecode::Constant::None,
10571057
});
10581058
}
1059+
ast::Expression::Ellipsis => {
1060+
self.emit(Instruction::LoadConst {
1061+
value: bytecode::Constant::Ellipsis,
1062+
});
1063+
}
10591064
ast::Expression::String { value } => {
10601065
self.compile_string(value)?;
10611066
}

vm/src/pyobject.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub struct PyContext {
130130
pub map_type: PyObjectRef,
131131
pub memoryview_type: PyObjectRef,
132132
pub none: PyObjectRef,
133+
pub ellipsis: PyObjectRef,
133134
pub not_implemented: PyObjectRef,
134135
pub tuple_type: PyObjectRef,
135136
pub set_type: PyObjectRef,
@@ -223,6 +224,12 @@ impl PyContext {
223224
create_type("NoneType", &type_type, &object_type, &dict_type),
224225
);
225226

227+
// TODO: implement proper ellipsis class?
228+
let ellipsis = PyObject::new(
229+
PyObjectPayload::None,
230+
create_type("EllipsisType", &type_type, &object_type, &dict_type),
231+
);
232+
226233
let not_implemented = PyObject::new(
227234
PyObjectPayload::NotImplemented,
228235
create_type("NotImplementedType", &type_type, &object_type, &dict_type),
@@ -263,6 +270,7 @@ impl PyContext {
263270
zip_type,
264271
dict_type,
265272
none,
273+
ellipsis,
266274
not_implemented,
267275
str_type,
268276
range_type,
@@ -441,6 +449,11 @@ impl PyContext {
441449
pub fn none(&self) -> PyObjectRef {
442450
self.none.clone()
443451
}
452+
453+
pub fn ellipsis(&self) -> PyObjectRef {
454+
self.ellipsis.clone()
455+
}
456+
444457
pub fn not_implemented(&self) -> PyObjectRef {
445458
self.not_implemented.clone()
446459
}
@@ -699,6 +712,7 @@ impl PyContext {
699712
self.new_tuple(elements)
700713
}
701714
bytecode::Constant::None => self.none(),
715+
bytecode::Constant::Ellipsis => self.ellipsis(),
702716
}
703717
}
704718
}

vm/src/stdlib/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ fn expression_to_ast(ctx: &PyContext, expression: &ast::Expression) -> PyObjectR
395395

396396
node
397397
}
398+
ast::Expression::Ellipsis => {
399+
create_node(ctx, "Ellipsis")
400+
}
398401
ast::Expression::List { elements } => {
399402
let node = create_node(ctx, "List");
400403

0 commit comments

Comments
 (0)