Browse Source

Start parsing term objects, and our first: DefScope

Isaac Woods 5 years ago
parent
commit
dd1a132d5e
2 changed files with 54 additions and 1 deletions
  1. 8 1
      aml_parser/src/lib.rs
  2. 46 0
      aml_parser/src/term_object.rs

+ 8 - 1
aml_parser/src/lib.rs

@@ -13,12 +13,15 @@ pub(crate) mod name_object;
 pub(crate) mod opcode;
 pub(crate) mod opcode;
 pub(crate) mod parser;
 pub(crate) mod parser;
 pub(crate) mod pkg_length;
 pub(crate) mod pkg_length;
+pub(crate) mod term_object;
 pub mod value;
 pub mod value;
 
 
 pub use crate::value::AmlValue;
 pub use crate::value::AmlValue;
 
 
 use alloc::{collections::BTreeMap, string::String};
 use alloc::{collections::BTreeMap, string::String};
 use log::{error, trace};
 use log::{error, trace};
+use parser::Parser;
+use pkg_length::PkgLength;
 
 
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
 pub enum AmlError {
 pub enum AmlError {
@@ -41,7 +44,11 @@ impl AmlNamespace {
             return Err(AmlError::UnexpectedEndOfStream);
             return Err(AmlError::UnexpectedEndOfStream);
         }
         }
 
 
-        match term_object::parse_term_list(stream, self) {
+        match term_object::term_list(
+            PkgLength::from_raw_length(stream, stream.len() as u32) as PkgLength
+        )
+        .parse(stream)
+        {
             Ok(_) => Ok(()),
             Ok(_) => Ok(()),
             Err((remaining, err)) => {
             Err((remaining, err)) => {
                 error!("Failed to parse AML stream. Err = {:?}", err);
                 error!("Failed to parse AML stream. Err = {:?}", err);

+ 46 - 0
aml_parser/src/term_object.rs

@@ -0,0 +1,46 @@
+use crate::{
+    name_object::name_string,
+    opcode::{opcode, SCOPE_OP},
+    parser::{choice, comment, comment_scope, take_n, ParseResult, Parser},
+    pkg_length::{pkg_length, PkgLength},
+    AmlNamespace,
+};
+use log::{debug, trace};
+
+/// `TermList`s are usually found within explicit-length objects (so they have a `PkgLength`
+/// elsewhere in the structure), so this takes a number of bytes to parse.
+pub fn term_list<'a>(list_length: PkgLength) -> impl Parser<'a, ()> {
+    /*
+     * TermList := Nothing | <TermObj TermList>
+     */
+    move |input: &'a [u8]| -> ParseResult<'a, ()> {
+        while list_length.still_parsing(input) {
+            let (input, ()) = term_object().parse(input)?;
+        }
+        Ok((input, ()))
+    }
+}
+
+// TODO: maybe return `AmlValue` on success
+pub fn term_object<'a>() -> impl Parser<'a, ()> {
+    /*
+     * TermObj := NamespaceModifierObj | NamedObj | Type1Opcode | Type2Opcode
+     * NamespaceModifierObj := DefAlias | DefName | DefScope
+     */
+    comment_scope("TermObj", choice!(def_scope()))
+}
+
+pub fn def_scope<'a>() -> impl Parser<'a, ()> {
+    /*
+     * DefScope := 0x10 PkgLength NameString TermList
+     */
+    opcode(SCOPE_OP)
+        .then(comment_scope(
+            "DefScope",
+            pkg_length().then(name_string()).feed(move |(pkg_length, name)| {
+                debug!("Scope with name: {}, length: {:?}", name, pkg_length);
+                term_list(pkg_length)
+            }),
+        ))
+        .discard_result()
+}