forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyword.rs
More file actions
27 lines (24 loc) · 664 Bytes
/
keyword.rs
File metadata and controls
27 lines (24 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// Testing if a string is a keyword.
pub(crate) use keyword::make_module;
#[pymodule]
mod keyword {
use crate::vm::{builtins::PyStr, PyObjectRef, VirtualMachine};
use itertools::Itertools;
use rustpython_parser::lexer;
#[pyfunction]
fn iskeyword(s: PyObjectRef) -> bool {
if let Some(s) = s.payload::<PyStr>() {
lexer::KEYWORDS.contains_key(s.as_str())
} else {
false
}
}
#[pyattr]
fn kwlist(vm: &VirtualMachine) -> Vec<PyObjectRef> {
lexer::KEYWORDS
.keys()
.sorted()
.map(|&k| vm.ctx.new_str(k).into())
.collect()
}
}