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
29 lines (25 loc) · 795 Bytes
/
keyword.rs
File metadata and controls
29 lines (25 loc) · 795 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
28
29
/// Testing if a string is a keyword.
pub(crate) use decl::make_module;
#[pymodule(name = "keyword")]
mod decl {
use rustpython_parser::lexer;
use crate::obj::objstr::PyStrRef;
use crate::pyobject::{BorrowValue, PyObjectRef, PyResult};
use crate::vm::VirtualMachine;
#[pyfunction]
fn iskeyword(s: PyStrRef, vm: &VirtualMachine) -> PyResult {
let keywords = lexer::get_keywords();
let value = keywords.contains_key(s.borrow_value());
let value = vm.ctx.new_bool(value);
Ok(value)
}
#[pyattr]
fn kwlist(vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_list(
lexer::get_keywords()
.keys()
.map(|k| vm.ctx.new_str(k.to_owned()))
.collect(),
)
}
}