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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Module defining the different Kconfig entries.
//! Most entries define a config option; all other entries help to organize them. [https://www.kernel.org/doc/html/next/kbuild/kconfig-language.html#menu-entries](https://www.kernel.org/doc/html/next/kbuild/kconfig-language.html#menu-entries)

use nom::{branch::alt, combinator::map, multi::many0, sequence::delimited, IResult};
#[cfg(feature = "deserialize")]
use serde::Deserialize;
#[cfg(feature = "serialize")]
use serde::Serialize;

use crate::{
    attribute::function::{parse_function_call, FunctionCall},
    util::{ws, ws_comment},
    KconfigInput,
};

pub use self::{
    choice::{parse_choice, Choice},
    comment::{parse_comment, Comment},
    config::{parse_config, Config},
    function::{parse_function, Function},
    main_menu::{parse_main_menu, MainMenu},
    menu::{parse_menu, Menu},
    menuconfig::{parse_menu_config, MenuConfig},
    r#if::{parse_if, If},
    source::{parse_source, Source},
    variable::{parse_variable_assignment, Value, VariableAssignment, VariableIdentifier},
};

pub mod choice;
pub mod comment;
pub mod config;
pub mod function;
pub mod r#if;
pub mod main_menu;
pub mod menu;
pub mod menuconfig;
pub mod source;
pub mod variable;

/// Official documentation about the different entries: [https://www.kernel.org/doc/html/next/kbuild/kconfig-language.html#menu-entries](https://www.kernel.org/doc/html/next/kbuild/kconfig-language.html#menu-entries)
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub enum Entry {
    Config(Config),
    Choice(Choice),
    MenuConfig(MenuConfig),
    Menu(Menu),
    Comment(Comment),
    Source(Source),
    VariableAssignment(VariableAssignment),
    FunctionCall(FunctionCall),
    Function(Function),
    If(If),
    MainMenu(MainMenu),
}

pub fn parse_entry(input: KconfigInput) -> IResult<KconfigInput, Entry> {
    alt((
        map(ws(parse_config), Entry::Config),
        map(ws(parse_choice), Entry::Choice),
        map(ws(parse_menu_config), Entry::MenuConfig),
        map(ws(parse_function), Entry::Function),
        map(ws(parse_main_menu), Entry::MainMenu),
        map(ws(parse_if), Entry::If),
        map(ws(parse_menu), Entry::Menu),
        map(ws(parse_comment), Entry::Comment),
        map(ws(parse_source), Entry::Source),
        map(ws(parse_variable_assignment), Entry::VariableAssignment),
        map(ws(parse_function_call), Entry::FunctionCall),
    ))(input)
}

pub fn parse_entries(input: KconfigInput) -> IResult<KconfigInput, Vec<Entry>> {
    delimited(ws_comment, many0(parse_entry), ws_comment)(input)
}

#[cfg(test)]
pub mod mod_test;

#[cfg(test)]
pub mod choice_test;
#[cfg(test)]
mod comment_test;
#[cfg(test)]
mod config_test;
#[cfg(test)]
mod function_test;
#[cfg(test)]
pub mod if_test;
#[cfg(test)]
mod main_menu_test;
#[cfg(test)]
mod menu_test;
#[cfg(test)]
mod menuconfig_test;
#[cfg(test)]
mod source_test;
#[cfg(test)]
mod variable_test;