kproc_parser/rust/
kimpl.rs

1//! API to parse a rust `impl`
2use crate::kparser::{self, KParserError, KParserTracer};
3use crate::kproc_macros::KTokenStream;
4use crate::rust::ast_nodes::ImplToken;
5use crate::rust::core::check_and_parse_bounds;
6use crate::rust::kattr::check_and_parse_cond_attribute;
7use crate::rust::kfunc::parse_fn;
8use crate::{check, trace};
9
10/// helper function that allow to parse an impl block
11pub fn parse_impl(
12    toks: &mut KTokenStream,
13    tracer: &dyn KParserTracer,
14) -> kparser::Result<ImplToken> {
15    let attr = check_and_parse_cond_attribute(toks, tracer);
16    let impl_tok = toks.advance();
17    check!("impl", impl_tok)?;
18    let generics = check_and_parse_bounds(toks, tracer)?;
19    let name = toks.advance();
20    let _for_ty = if toks.match_tok("for") {
21        // FIXME: parsing the generic and lifetime usage
22        let for_tok = toks.advance();
23        check!("for", for_tok)?;
24        Some(toks.advance())
25    } else {
26        None
27    };
28
29    // FIXME: parse the where clause!
30
31    // store the raw content of the block because there
32    // if the user want parse it,
33    // it has all the necessary tools for parse it.
34    let raw_impl_block = toks.unwrap_group_as_stream();
35    let mut impl_block = toks.to_ktoken_stream();
36
37    let mut funs = Vec::new();
38    while !impl_block.is_end() {
39        // FIXME we Suppose to have all the function and no
40        // extra stuff
41        let fn_tok = parse_fn(&mut impl_block, tracer)?;
42        funs.push(fn_tok);
43    }
44
45    let impl_tok = ImplToken {
46        attributes: attr,
47        generics,
48        name,
49        // FIXME: make an abstraction for this kind of type
50        for_ty: None,
51        raw_block: raw_impl_block,
52        functions: funs,
53    };
54
55    trace!(tracer, "impl tok parserd: {:#?}", impl_tok);
56    Ok(impl_tok)
57}