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
32 lines (28 loc) · 929 Bytes
/
keyword.rs
File metadata and controls
32 lines (28 loc) · 929 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
30
31
32
/*
* Testing if a string is a keyword.
*/
extern crate rustpython_parser;
use self::rustpython_parser::lexer;
use crate::obj::objstr;
use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use crate::VirtualMachine;
fn keyword_iskeyword(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
let s = objstr::get_value(s);
let keywords = lexer::get_keywords();
let value = keywords.contains_key(&s);
let value = vm.ctx.new_bool(value);
Ok(value)
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let keyword_kwlist = ctx.new_list(
lexer::get_keywords()
.keys()
.map(|k| ctx.new_str(k.to_string()))
.collect(),
);
py_module!(ctx, "keyword", {
"iskeyword" => ctx.new_rustfunc(keyword_iskeyword),
"kwlist" => keyword_kwlist
})
}