Skip to content

Commit

Permalink
first attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcdostone committed Feb 11, 2024
1 parent e4e5969 commit 709e39b
Show file tree
Hide file tree
Showing 46 changed files with 426 additions and 451 deletions.
279 changes: 91 additions & 188 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ members = [
]

[dependencies]
nom = "7.1.3"
nom = "7.0.0"
nom_locate = "4.1.0"
regex = "1.8.4"
serde = { version = "1.0.163", features = ["derive"], optional = true }
Expand Down
6 changes: 4 additions & 2 deletions examples/read_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ endmenu
"#;

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

let content = kconfig_file.content.to_owned();
let (_remaining, kconfig) =
parse_kconfig(KconfigInput::new_extra(INPUT, kconfig_file)).unwrap();
parse_kconfig(KconfigInput::new_extra(&content, kconfig_file)).unwrap();
println!("File '{}' contains the following entries:", kconfig.file);
kconfig.entries.into_iter().for_each(print_entry);
Ok(())
Expand Down
12 changes: 6 additions & 6 deletions examples/read_kernel_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.strip_prefix(&kernel_directory)
.unwrap_or(current_kconfig);

let kconfig_file = KconfigFile::new(kernel_directory.to_path_buf(), filename.to_path_buf());
let input = 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(), "".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)))),
//}
}

println!("{} Kconfig file have been read", kconfigs.len());
Expand Down
12 changes: 6 additions & 6 deletions src/attribute/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ use super::{parse_expression, parse_if_attribute, Expression};
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub struct DefaultAttribute {
pub expression: Expression,
pub struct DefaultAttribute<'a> {
pub expression: Expression<'a>,
#[cfg_attr(
any(feature = "serialize", feature = "deserialize"),
serde(skip_serializing_if = "Option::is_none")
serde(skip_serializing_if = "Option::is_none", borrow)
)]
pub r#if: Option<Expression>,
pub r#if: Option<Expression<'a>>,
}

#[cfg(feature = "display")]
impl Display for DefaultAttribute {
impl Display for DefaultAttribute<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.r#if {
Some(i) => write!(f, "{} if {}", self.expression, i),
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Display for DefaultAttribute {
/// "",
/// DefaultAttribute {
/// expression: Expression::Term(AndExpression::Term(Term::Atom(
/// Atom::Symbol(Symbol::Constant("0x1".to_string()))
/// Atom::Symbol(Symbol::Constant("0x1"))
/// ))),
/// r#if: None
/// }
Expand Down
8 changes: 4 additions & 4 deletions src/attribute/default_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_parse_default() {
"",
DefaultAttribute {
expression: Expression::Term(AndExpression::Term(Term::Atom(Atom::Symbol(
Symbol::Constant("0x1".to_string())
Symbol::Constant("0x1")
)))),
r#if: None
}
Expand All @@ -31,7 +31,7 @@ fn test_parse_default_constant_symbol_with_numbers() {
"",
DefaultAttribute {
expression: Expression::Term(AndExpression::Term(Term::Atom(Atom::Symbol(
Symbol::Constant("7.10.d".to_string())
Symbol::Constant("7.10.d")
)))),
r#if: None
}
Expand All @@ -51,7 +51,7 @@ fn test_parse_default_ambigus() {
expression: Expression::Term(AndExpression::Term(Term::Atom(Atom::String(r#"$(shell,$(srctree)/scripts/gcc-plugin.sh "$(preferred-plugin-hostcc)" "$(HOSTCXX)" "$(CC)")"#.to_string())
))),
r#if: Some(Expression::Term(AndExpression::Term(Term::Atom(
Atom::Symbol(Symbol::Constant("CC_IS_GCC".to_string()))
Atom::Symbol(Symbol::Constant("CC_IS_GCC"))
))))
}
))
Expand All @@ -73,7 +73,7 @@ fn test_default_attribute_to_string() {
DefaultAttribute {
expression: Expression::Term(AndExpression::Term(Term::Atom(Atom::Number(64)))),
r#if: Some(Expression::Term(AndExpression::Term(Term::Atom(
Atom::Symbol(Symbol::Constant("NET".to_string()))
Atom::Symbol(Symbol::Constant("NET"))
))))
}
.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/attribute/depends_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use super::{expression::parse_expression, Attribute};
/// Ok((
/// "",
/// Attribute::DependsOn(Expression::Term(AndExpression::Term(
/// Term::Atom(Atom::Symbol(Symbol::Constant("PCI".to_string())))
/// Term::Atom(Atom::Symbol(Symbol::Constant("PCI")))
/// )))
/// ))
/// )
Expand Down
14 changes: 7 additions & 7 deletions src/attribute/depends_on_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_parse_depends_on() {
Ok((
"",
Attribute::DependsOn(Expression::Term(AndExpression::Term(Term::Atom(
Atom::Symbol(Symbol::Constant("PCI".to_string()))
Atom::Symbol(Symbol::Constant("PCI"))
))))
))
)
Expand All @@ -28,7 +28,7 @@ fn test_parse_depends_on_weird_tab() {
Ok((
"",
Attribute::DependsOn(OrExpression::Term(AndExpression::Term(Term::Atom(
Atom::Symbol(Symbol::Constant("LIVEPATCH".to_string()))
Atom::Symbol(Symbol::Constant("LIVEPATCH"))
))))
))
)
Expand All @@ -46,19 +46,19 @@ 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".to_string()
"ARCH_LUBBOCK"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"MACH_MAINSTONE".to_string()
"MACH_MAINSTONE"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"PXA_SHARPSL".to_string()
"PXA_SHARPSL"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"MACH_ARMCORE".to_string()
"MACH_ARMCORE"
)))),
AndExpression::Term(Term::Atom(Atom::Symbol(Symbol::Constant(
"ARCH_PXA_PALM".to_string()
"ARCH_PXA_PALM"
)))),
))))
))))
Expand Down
66 changes: 39 additions & 27 deletions src/attribute/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,51 +61,53 @@ impl Display for CompareOperator {
}

// https://stackoverflow.com/questions/9509048/antlr-parser-for-and-or-logic-how-to-get-expressions-between-logic-operators
pub type Expression = OrExpression;
pub type Expression<'a> = OrExpression<'a>;
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq, Clone)]
pub enum AndExpression {
#[cfg_attr(feature = "serialize", serde(rename = "AndTerm"))]
Term(Term),
pub enum AndExpression<'a> {
#[cfg_attr(feature = "serialize", serde(rename = "AndTerm", borrow))]
Term(Term<'a>),
#[cfg_attr(feature = "serialize", serde(rename = "And"))]
Expression(Vec<Term>),
Expression(Vec<Term<'a>>),
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub enum OrExpression {
#[cfg_attr(feature = "serialize", serde(rename = "OrTerm"))]
Term(AndExpression),
#[cfg_attr(feature = "serialize", serde(rename = "Or"))]
Expression(Vec<AndExpression>),
pub enum OrExpression<'a> {
#[cfg_attr(feature = "serialize", serde(rename = "OrTerm", borrow))]
Term(AndExpression<'a>),
#[cfg_attr(feature = "serialize", serde(rename = "Or", borrow))]
Expression(Vec<AndExpression<'a>>),
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub enum Term {
Not(Atom),
Atom(Atom),
pub enum Term<'a> {
#[serde(borrow)]
Not(Atom<'a>),
Atom(Atom<'a>),
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[cfg_attr(feature = "serialize", serde(rename = "Compare"))]
pub struct CompareExpression {
pub left: Symbol,
pub struct CompareExpression<'a> {
pub left: Symbol<'a>,
pub operator: CompareOperator,
pub right: Symbol,
#[serde(borrow)]
pub right: Symbol<'a>,
}

#[cfg(feature = "display")]
impl Display for CompareExpression {
impl Display for CompareExpression<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{} {} {}", self.left, self.operator, self.right)
}
Expand All @@ -115,17 +117,18 @@ impl Display for CompareExpression {
#[cfg_attr(feature = "hash", derive(Hash))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
pub enum Atom {
Symbol(Symbol),
pub enum Atom<'a> {
#[serde(borrow)]
Symbol(Symbol<'a>),
Number(i64),
Compare(CompareExpression),
Compare(CompareExpression<'a>),
Function(FunctionCall),
Parenthesis(Box<Expression>),
Parenthesis(Box<Expression<'a>>),
String(String),
}

#[cfg(feature = "display")]
impl Display for AndExpression {
impl Display for AndExpression<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Term(t) => write!(f, "{}", t),
Expand All @@ -142,7 +145,7 @@ impl Display for AndExpression {
}

#[cfg(feature = "display")]
impl Display for OrExpression {
impl Display for OrExpression<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Term(t) => write!(f, "{}", t),
Expand All @@ -159,7 +162,7 @@ impl Display for OrExpression {
}

#[cfg(feature = "display")]
impl Display for Term {
impl Display for Term<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Term::Not(atom) => write!(f, "!{}", atom),
Expand All @@ -169,7 +172,7 @@ impl Display for Term {
}

#[cfg(feature = "display")]
impl Display for Atom {
impl Display for Atom<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Atom::Symbol(s) => write!(f, "{}", s),
Expand Down Expand Up @@ -315,7 +318,7 @@ pub fn parse_compare(input: KconfigInput) -> IResult<KconfigInput, Atom> {
pub fn parse_number_or_symbol(input: KconfigInput) -> IResult<KconfigInput, Atom> {
let (input, sym) = parse_symbol(input)?;
match sym.clone() {
Symbol::Constant(s) => match string_to_number(s.as_str()) {
Symbol::Constant(s) => match string_to_number(s) {
Ok((_, i)) => Ok((input, Atom::Number(i))),
Err(_) => Ok((input, Atom::Symbol(sym))),
},
Expand All @@ -337,9 +340,18 @@ 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(input: KconfigInput) -> IResult<KconfigInput, i64> {
pub fn parse_number<'a>(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> {
map(
recognize(pair(opt(char('-')), digit1)),
|lol: KconfigInput| *lol.fragment(),
)(input)
}
Loading

0 comments on commit 709e39b

Please sign in to comment.