Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcdostone committed Mar 24, 2024
1 parent 797a21b commit 5888b2f
Show file tree
Hide file tree
Showing 31 changed files with 156 additions and 160 deletions.
10 changes: 10 additions & 0 deletions benches/my_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ use walkdir::WalkDir;
fn parse_files(kernel_directory: &str, files: Vec<&str>) -> Result<(), Box<dyn std::error::Error>> {
let mut kconfigs: Vec<Kconfig> = vec![];
let root_directory = Path::new(kernel_directory);
let mut inputs = vec!();

for current_kconfig in files.iter() {
}


for current_kconfig in files.iter() {
let kconfig_file = KconfigFile::new(
root_directory.to_path_buf(),
root_directory.join(current_kconfig),
);
let input = kconfig_file.read_to_string()?;
inputs.push(input);


let input = inputs.get(inputs.len() -1).unwrap();
match parse_kconfig(KconfigInput::new_extra(&input, kconfig_file)) {
Ok((_, kconfig)) => kconfigs.push(kconfig),
Err(e) => return Err(Box::new(e.map_input(|f| (f.to_string().clone(), f.extra)))),
Expand Down
8 changes: 5 additions & 3 deletions examples/read_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ endmenu
"#;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let kconfig_file = KconfigFile::new(PathBuf::from("/"), PathBuf::from("Kconfig"), INPUT.to_string());
let kconfig_file = KconfigFile::new(
PathBuf::from("/"),
PathBuf::from("Kconfig"),
);

let content = kconfig_file.content.to_owned();
let (_remaining, kconfig) =
parse_kconfig(KconfigInput::new_extra(&content, kconfig_file)).unwrap();
parse_kconfig(KconfigInput::new_extra(INPUT, kconfig_file)).unwrap();
println!("File '{}' contains the following entries:", kconfig.file);
kconfig.entries.into_iter().for_each(print_entry);
Ok(())
Expand Down
30 changes: 22 additions & 8 deletions examples/read_kernel_directory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use nom_kconfig::{kconfig::parse_kconfig, Kconfig, KconfigFile, KconfigInput};
use std::path::{Path, PathBuf};
use nom_kconfig::{parse_kconfig, Kconfig, KconfigFile, KconfigInput};
use std::{collections::HashMap, path::{Path, PathBuf}};
use walkdir::WalkDir;

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -8,18 +8,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.join("linux-6.4.10");

let entries: Vec<PathBuf> = list_kconfig_files(&kernel_directory);

let mut kconfigs: Vec<Kconfig> = vec![];
let mut inputs = HashMap::new();


for current_kconfig in entries.iter() {

let filename = current_kconfig
.strip_prefix(&kernel_directory)
.unwrap_or(current_kconfig);

let kconfig_file = KconfigFile::new(kernel_directory.to_path_buf(), filename.to_path_buf(), "".to_string());
let input: String = kconfig_file.read_to_string()?;
//match parse_kconfig(KconfigInput::new_extra(&input, kconfig_file)) {
// Ok((_, kconfig)) => kconfigs.push(kconfig),
// Err(e) => return Err(Box::new(e.map_input(|f| (f.to_string().clone(), f.extra)))),
//}
let kconfig_file = KconfigFile::new(
kernel_directory.to_path_buf(),
filename.to_path_buf(),
);

let l = kconfig_file.read_to_string()?;
inputs.insert(kconfig_file, l);
}

for (k, v) in inputs.iter() {
let input = KconfigInput::new_extra(v.as_str(), k.clone());
match parse_kconfig(input) {
Ok((_, kconfig)) => kconfigs.push(kconfig),
Err(e) => return Err(Box::new(e.map_input(|f| (f.to_string().clone(), f.extra)))),
}
}

println!("{} Kconfig file have been read", kconfigs.len());
Expand Down
12 changes: 3 additions & 9 deletions src/attribute/depends_on_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,12 @@ fn test_parse_depends_on_backslash() {
"",
Attribute::DependsOn(Expression::Term(AndExpression::Term(Term::Atom(
Atom::Parenthesis(Box::new(Expression::Expression(vec!(
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"ARCH_LUBBOCK"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant("ARCH_LUBBOCK")))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"MACH_MAINSTONE"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"PXA_SHARPSL"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"MACH_ARMCORE"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant("PXA_SHARPSL")))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant("MACH_ARMCORE")))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"ARCH_PXA_PALM"
)))),
Expand Down
6 changes: 2 additions & 4 deletions src/attribute/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,14 @@ pub fn parse_if_expression(input: KconfigInput) -> IResult<KconfigInput, Express
map(pair(wsi(tag("if")), wsi(parse_expression)), |(_, e)| e)(input)
}

pub fn parse_number<'a>(input: KconfigInput) -> IResult<KconfigInput, i64> {
pub fn parse_number(input: KconfigInput) -> IResult<KconfigInput, i64> {
map_res(
recognize(pair(opt(char('-')), digit1)),
|d: KconfigInput| FromStr::from_str(d.fragment()),
)(input)
}



pub fn parse_number_as_str<'a>(input: KconfigInput) -> IResult<KconfigInput, &str> {
pub fn parse_number_as_str(input: KconfigInput) -> IResult<KconfigInput, &str> {
map(
recognize(pair(opt(char('-')), digit1)),
|lol: KconfigInput| *lol.fragment(),
Expand Down
12 changes: 3 additions & 9 deletions src/attribute/expression_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ fn test_parse_depends_on_ambigus() {
Ok((
"",
Expression::Expression(vec!(
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"ALPHA_MIATA"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant("ALPHA_MIATA")))),
AndExpression::Expression(vec!(
Term::Atom(Atom::Symbol(Symbol::Constant("ALPHA_LX164"))),
Term::Atom(Atom::Symbol(Symbol::Constant("ALPHA_SX164"))),
Expand Down Expand Up @@ -317,12 +315,8 @@ fn test_expression_to_string() {
assert_eq!(
"KVM || NET",
Expression::Expression(vec!(
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"KVM"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"NET"
))))
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant("KVM")))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant("NET"))))
))
.to_string()
);
Expand Down
12 changes: 6 additions & 6 deletions src/attribute/mod_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn test_parse_attribute() {
Ok((
"",
Attribute::Select(Select {
symbol: "MTK_INFRACFG".to_string(),
symbol: "MTK_INFRACFG",
r#if: None
})
))
Expand Down Expand Up @@ -73,7 +73,7 @@ fn test_parse_attribute() {
Ok((
"",
Attribute::Prompt(Prompt {
prompt: "hello world".to_string(),
prompt: "hello world",
r#if: None
})
))
Expand Down Expand Up @@ -110,7 +110,7 @@ fn test_parse_attribute() {
Ok((
"",
Attribute::Select(Select {
symbol: "KVM".to_string(),
symbol: "KVM",
r#if: None
})
))
Expand All @@ -135,7 +135,7 @@ fn test_parse_attributes() {
"",
vec!(
Attribute::Select(Select {
symbol: "KVM".to_string(),
symbol: "KVM",
r#if: None
}),
Attribute::Modules,
Expand All @@ -159,7 +159,7 @@ fn test_attributes_to_string() {
);
assert_eq!(
Attribute::Prompt(Prompt {
prompt: "a prompt".to_string(),
prompt: "a prompt",
r#if: None
})
.to_string(),
Expand All @@ -174,7 +174,7 @@ fn test_attributes_to_string() {
);
assert_eq!(
Attribute::Select(Select {
symbol: "NET".to_string(),
symbol: "NET",
r#if: None
})
.to_string(),
Expand Down
8 changes: 4 additions & 4 deletions src/attribute/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use serde::Serialize;
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub struct Prompt<'a> {
pub prompt: String,
pub prompt: &'a str,
#[cfg_attr(
any(feature = "serialize", feature = "deserialize"),
serde(skip_serializing_if = "Option::is_none", borrow)
Expand All @@ -46,7 +46,7 @@ pub fn parse_prompt(input: KconfigInput) -> IResult<KconfigInput, Prompt> {
map(
tuple((ws(tag("prompt")), parse_prompt_value, parse_if_attribute)),
|(_, p, i)| Prompt {
prompt: p.to_string(),
prompt: p,
r#if: i,
},
)(input)
Expand All @@ -59,7 +59,7 @@ pub fn parse_prompt(input: KconfigInput) -> IResult<KconfigInput, Prompt> {
///
/// assert_parsing_eq!(parse_prompt_value, "scripts/Kconfig.include", Ok(("", "scripts/Kconfig.include".to_string())))
/// ```
pub fn parse_prompt_value(input: KconfigInput) -> IResult<KconfigInput, String> {
pub fn parse_prompt_value(input: KconfigInput) -> IResult<KconfigInput, &str> {
map(
alt((
delimited(
Expand All @@ -81,6 +81,6 @@ pub fn parse_prompt_value(input: KconfigInput) -> IResult<KconfigInput, String>
|d: &KconfigInput| !d.trim().is_empty(),
),
)),
|d: KconfigInput| d.fragment().to_owned().trim().to_string(),
|d: KconfigInput| d.fragment().to_owned().trim(),
)(input)
}
10 changes: 5 additions & 5 deletions src/attribute/prompt_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_parse_prompt() {
assert_parsing_eq!(
parse_prompt_value,
input,
Ok(("", "scripts/Kconfig.include".to_string()))
Ok(("", "scripts/Kconfig.include"))
)
}

Expand All @@ -20,22 +20,22 @@ fn test_parse_prompt_1() {
assert_parsing_eq!(
parse_prompt_value,
input,
Ok(("", "Support in-kernel module decompression".to_string()))
Ok(("", "Support in-kernel module decompression"))
)
}

// 3.0.18/arch/arm/plat-tcc/Kconfig
#[test]
fn test_parse_prompt_no_quote() {
let input = " TCC8000";
assert_parsing_eq!(parse_prompt_value, input, Ok(("", "TCC8000".to_string())))
assert_parsing_eq!(parse_prompt_value, input, Ok(("", "TCC8000")))
}

#[test]
fn test_prompt_to_string() {
assert_eq!(
Prompt {
prompt: "Support of KVM".to_string(),
prompt: "Support of KVM",
r#if: None
}
.to_string(),
Expand All @@ -44,7 +44,7 @@ fn test_prompt_to_string() {

assert_eq!(
Prompt {
prompt: "Support of KVM".to_string(),
prompt: "Support of KVM",
r#if: Some(Expression::Term(AndExpression::Term(Term::Atom(
Atom::Symbol(Symbol::Constant("KVM"))
))))
Expand Down
10 changes: 4 additions & 6 deletions src/attribute/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ impl Display for Range<'_> {

fn parse_bounds(input: KconfigInput) -> IResult<KconfigInput, (Symbol, Symbol)> {
alt((
map(tuple((ws(parse_number_as_str), ws(parse_number_as_str))), |(l, r)| {
(
Symbol::Constant(l),
Symbol::Constant(r),
)
}),
map(
tuple((ws(parse_number_as_str), ws(parse_number_as_str))),
|(l, r)| (Symbol::Constant(l), Symbol::Constant(r)),
),
tuple((ws(parse_symbol), ws(parse_symbol))),
))(input)
}
Expand Down
4 changes: 2 additions & 2 deletions src/attribute/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::expression::{parse_if_attribute, Expression};
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub struct Select<'a> {
pub symbol: String,
pub symbol: &'a str,
#[cfg_attr(
any(feature = "serialize", feature = "deserialize"),
serde(skip_serializing_if = "Option::is_none", borrow)
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn parse_select(input: KconfigInput) -> IResult<KconfigInput, Select> {
parse_if_attribute,
)),
|(_, s, i)| Select {
symbol: s.to_string(),
symbol: s,
r#if: i,
},
)(input)
Expand Down
8 changes: 4 additions & 4 deletions src/attribute/select_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_parse_select() {
"",
Select {
r#if: None,
symbol: "MTK_INFRACFG".to_string()
symbol: "MTK_INFRACFG"
}
))
)
Expand All @@ -24,7 +24,7 @@ fn test_select_to_string() {
assert_eq!(
Select {
r#if: None,
symbol: "MTK_INFRACFG".to_string()
symbol: "MTK_INFRACFG"
}
.to_string(),
"MTK_INFRACFG"
Expand All @@ -35,7 +35,7 @@ fn test_select_to_string() {
r#if: Some(Expression::Term(AndExpression::Term(Term::Not(
Atom::Symbol(Symbol::Constant("KVM"))
)))),
symbol: "MTK_INFRACFG".to_string()
symbol: "MTK_INFRACFG"
}
.to_string(),
"MTK_INFRACFG if !KVM"
Expand All @@ -52,7 +52,7 @@ fn test_parse_enable() {
"",
Select {
r#if: None,
symbol: "MTK_INFRACFG".to_string()
symbol: "MTK_INFRACFG"
}
))
)
Expand Down
17 changes: 7 additions & 10 deletions src/attribute/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ pub fn parse_type(input: KconfigInput) -> IResult<KconfigInput, Attribute> {
pair(
ws(alt((
map(
preceded(tag("boolean"), opt(parse_prompt_value)),
preceded(tag("boolean"), opt(map(parse_prompt_value, |d| d.to_string()))),
Type::Bool,
),
map(preceded(tag("bool"), opt(parse_prompt_value)), Type::Bool),
map(preceded(tag("hex"), opt(parse_prompt_value)), Type::Hex),
map(preceded(tag("int"), opt(parse_prompt_value)), Type::Int),
map(preceded(tag("bool"), opt(map(parse_prompt_value, |d| d.to_string()))), Type::Bool),
map(preceded(tag("hex"), opt(map(parse_prompt_value, |d| d.to_string()))), Type::Hex),
map(preceded(tag("int"), opt(map(parse_prompt_value, |d| d.to_string()))), Type::Int),
map(
preceded(tag("string"), opt(parse_prompt_value)),
preceded(tag("string"), opt(map(parse_prompt_value, |d| d.to_string()))),
Type::String,
),
map(
preceded(tag("tristate"), opt(parse_prompt_value)),
preceded(tag("tristate"), opt(map(parse_prompt_value, |d| d.to_string()))),
Type::Tristate,
),
map(preceded(tag("def_bool"), ws(parse_expression)), |e| {
Expand All @@ -58,10 +58,7 @@ pub fn parse_type(input: KconfigInput) -> IResult<KconfigInput, Attribute> {
serde(rename_all = "lowercase")
)]
pub enum Type<'a> {
#[cfg_attr(
any(feature = "serialize", feature = "deserialize"),
serde(borrow)
)]
#[cfg_attr(any(feature = "serialize", feature = "deserialize"), serde(borrow))]
DefBool(Expression<'a>),
DefTristate(Expression<'a>),
Bool(Option<String>),
Expand Down
Loading

0 comments on commit 5888b2f

Please sign in to comment.