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

Commit be49cb67 authored by Martin Geisler's avatar Martin Geisler Committed by Automerger Merge Worker
Browse files

Merge "PDL: Make `endianness_declaration` mandatory" am: f3cf5cc1

parents 9c652abd f3cf5cc1
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ pub type FileId = usize;
/// Stores the source file contents for reference.
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 {
    /// Byte offset into the file (counted from zero).
    pub offset: usize,
@@ -22,7 +22,7 @@ pub struct SourceLocation {
    pub column: usize,
}

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

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

@@ -227,7 +227,12 @@ impl Grammar {
        Grammar {
            version: "1,0".to_owned(),
            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![],
            file,
        }
+19 −4
Original line number Diff line number Diff line
@@ -130,7 +130,7 @@ declaration = _{

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

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