Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2aff9cb9 authored by Martin Geisler's avatar Martin Geisler
Browse files

PDL: Make `endianness_declaration` mandatory

Since we need to know the endianness to parse anything, we now make
the declaration mandatory. This matches the behavior of
`bluetooth_packetgen`.

Bug: 229953170
Test: atest packages/modules/Bluetooth/tools/pdl
Change-Id: I7a48a72ec0fcf53d304f036e18282602668f2b17
parent 852804a5
Loading
Loading
Loading
Loading
+10 −5
Original line number Original line Diff line number Diff line
@@ -12,7 +12,7 @@ pub type FileId = usize;
/// Stores the source file contents for reference.
/// Stores the source file contents for reference.
pub type SourceDatabase = files::SimpleFiles<String, String>;
pub type SourceDatabase = files::SimpleFiles<String, String>;


#[derive(Debug, Copy, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Default, Copy, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct SourceLocation {
pub struct SourceLocation {
    /// Byte offset into the file (counted from zero).
    /// Byte offset into the file (counted from zero).
    pub offset: usize,
    pub offset: usize,
@@ -22,7 +22,7 @@ pub struct SourceLocation {
    pub column: usize,
    pub column: usize,
}
}


#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct SourceRange {
pub struct SourceRange {
    pub file: FileId,
    pub file: FileId,
    pub start: SourceLocation,
    pub start: SourceLocation,
@@ -36,7 +36,7 @@ pub struct Comment {
    pub text: String,
    pub text: String,
}
}


#[derive(Debug, Serialize)]
#[derive(Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum EndiannessValue {
pub enum EndiannessValue {
    LittleEndian,
    LittleEndian,
@@ -164,7 +164,7 @@ pub struct Grammar {
    pub version: String,
    pub version: String,
    pub file: FileId,
    pub file: FileId,
    pub comments: Vec<Comment>,
    pub comments: Vec<Comment>,
    pub endianness: Option<Endianness>,
    pub endianness: Endianness,
    pub declarations: Vec<Decl>,
    pub declarations: Vec<Decl>,
}
}


@@ -227,7 +227,12 @@ impl Grammar {
        Grammar {
        Grammar {
            version: "1,0".to_owned(),
            version: "1,0".to_owned(),
            comments: vec![],
            comments: vec![],
            endianness: None,
            // The endianness is mandatory, so this default value will
            // be updated while parsing.
            endianness: Endianness {
                loc: SourceRange::default(),
                value: EndiannessValue::LittleEndian,
            },
            declarations: vec![],
            declarations: vec![],
            file,
            file,
        }
        }
+19 −4
Original line number Original line Diff line number Diff line
@@ -130,7 +130,7 @@ declaration = _{


grammar = {
grammar = {
    SOI ~
    SOI ~
    endianness_declaration? ~
    endianness_declaration ~
    declaration* ~
    declaration* ~
    EOI
    EOI
}
}
@@ -439,9 +439,7 @@ fn parse_grammar(root: Node<'_>, context: &Context) -> Result<ast::Grammar, Stri
        let loc = node.as_loc(context);
        let loc = node.as_loc(context);
        let rule = node.as_rule();
        let rule = node.as_rule();
        match rule {
        match rule {
            Rule::endianness_declaration => {
            Rule::endianness_declaration => grammar.endianness = parse_endianness(node, context)?,
                grammar.endianness = Some(parse_endianness(node, context)?)
            }
            Rule::checksum_declaration => {
            Rule::checksum_declaration => {
                let mut children = node.children();
                let mut children = node.children();
                let id = parse_identifier(&mut children)?;
                let id = parse_identifier(&mut children)?;
@@ -539,3 +537,20 @@ pub fn parse_file(
    })?;
    })?;
    parse_inline(sources, name, source)
    parse_inline(sources, name, source)
}
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn endianness_is_set() {
        // The grammar starts out with a placeholder little-endian
        // value. This tests that we update it while parsing.
        let mut db = ast::SourceDatabase::new();
        let grammar =
            parse_inline(&mut db, String::from("stdin"), String::from("  big_endian_packets  "))
                .unwrap();
        assert_eq!(grammar.endianness.value, ast::EndiannessValue::BigEndian);
        assert_ne!(grammar.endianness.loc, ast::SourceRange::default());
    }
}
+1 −1
Original line number Original line Diff line number Diff line
@@ -117,7 +117,7 @@ declaration = _{


grammar = {
grammar = {
    SOI ~
    SOI ~
    endianness_declaration? ~
    endianness_declaration ~
    declaration* ~
    declaration* ~
    EOI
    EOI
}
}