nom_kconfig/entry/
config.rs1use nom::{
2 branch::alt,
3 bytes::complete::tag,
4 character::complete::{alphanumeric1, one_of},
5 combinator::{map, recognize},
6 multi::{many0, many1},
7 sequence::{pair, preceded},
8 IResult, Parser,
9};
10#[cfg(feature = "deserialize")]
11use serde::Deserialize;
12#[cfg(feature = "serialize")]
13use serde::Serialize;
14
15use crate::{
16 attribute::{parse_attribute, r#type::parse_type, Attribute},
17 util::ws,
18 KconfigInput,
19};
20
21#[derive(Debug, Clone, PartialEq)]
23#[cfg_attr(feature = "hash", derive(Hash))]
24#[cfg_attr(feature = "serialize", derive(Serialize))]
25#[cfg_attr(feature = "deserialize", derive(Deserialize))]
26pub struct Config {
27 pub symbol: String,
28 pub attributes: Vec<Attribute>,
29}
30
31#[macro_export]
32macro_rules! generic_config_parser {
33 ($t:ident, $tag:expr, $fn:expr) => {{
34 use nom::branch::alt;
35 use nom::multi::many0;
36 use $crate::attribute::parse_attribute;
37
38 map(
39 pair(
40 map(pair(ws(tag($tag)), ws(parse_config_symbol)), |(_, id)| id),
41 many0(ws(alt(($fn, parse_attribute)))),
42 ),
43 |(symbol, attributes)| $t {
44 symbol: symbol.to_string(),
45 attributes,
46 },
47 )
48 }};
49}
50
51pub fn parse_config_symbol(input: KconfigInput<'_>) -> IResult<KconfigInput<'_>, &str> {
52 map(
53 recognize(ws(many1(alt((alphanumeric1, recognize(one_of("_"))))))),
54 |d: KconfigInput| d.fragment().to_owned(),
55 )
56 .parse(input)
57}
58
59pub fn parse_config(input: KconfigInput) -> IResult<KconfigInput, Config> {
60 map(
61 pair(
62 preceded(ws(tag("config")), ws(parse_config_symbol)),
63 many0(ws(alt((parse_type, parse_attribute)))),
64 ),
65 |(symbol, attributes)| Config {
66 symbol: symbol.to_string(),
67 attributes,
68 },
69 )
70 .parse(input)
71}