mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
30 lines
799 B
Rust
30 lines
799 B
Rust
/// Testing if a string is a keyword.
|
|
pub(crate) use decl::make_module;
|
|
|
|
#[pymodule(name = "keyword")]
|
|
mod decl {
|
|
use rustpython_parser::lexer;
|
|
|
|
use crate::builtins::pystr::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(),
|
|
)
|
|
}
|
|
}
|