From ef5da6c28a5466e9346d8146edcb90d552a141ac Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Wed, 3 May 2023 19:13:26 -0500 Subject: [PATCH] parser parses file --- src/main.rs | 1 - src/metafile.rs | 3 +++ src/parser/parse.rs | 31 +++++++++++++++++-------------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index b648bd2..83f53c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ extern crate pest; -#[macro_use] extern crate pest_derive; fn main() { diff --git a/src/metafile.rs b/src/metafile.rs index f1f3114..a82d809 100644 --- a/src/metafile.rs +++ b/src/metafile.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +#[derive(Debug, Default, Clone)] pub struct MetaFile<'a> { pub variables: HashMap<&'a str, &'a str>, pub arrays: HashMap<&'a str, Vec<&'a str>>, @@ -18,11 +19,13 @@ impl<'a> MetaFile<'a> { } } +#[derive(Debug, Clone)] pub enum Source<'a> { Str(&'a str), Sub(Substitution<'a>), } +#[derive(Debug, Clone)] pub enum Substitution<'a> { Variable(&'a str), Array(&'a str), diff --git a/src/parser/parse.rs b/src/parser/parse.rs index f87e9df..82ff15a 100644 --- a/src/parser/parse.rs +++ b/src/parser/parse.rs @@ -20,21 +20,24 @@ pub fn parse_pair(pair: Pair) -> MetaFile { let mut meta_file = MetaFile::new(); if Rule::file == pair.as_rule() { - match pair.as_rule() { - Rule::source => meta_file.source = parse_source(pair.into_inner()), - Rule::var_def => meta_file.variables = parse_defs(pair.into_inner()), - Rule::arr_def => meta_file.arrays = parse_array_defs(pair.into_inner()), - Rule::pat_def => meta_file.patterns = parse_defs(pair.into_inner()), - // anything else is either hidden or children of previous nodes and will be dealt with - // in respective parse functions - _ => unreachable!(), + for pair in pair.into_inner() { + match pair.as_rule() { + Rule::source => meta_file.source = parse_source(pair.into_inner()), + Rule::var_def => meta_file.variables = parse_defs(pair.into_inner()), + Rule::arr_def => meta_file.arrays = parse_array_defs(pair.into_inner()), + Rule::pat_def => meta_file.patterns = parse_defs(pair.into_inner()), + Rule::EOI => (), + // anything else is either hidden or children of previous nodes and will be dealt with + // in respective parse functions + _ => unreachable!(), + } } } meta_file } -fn parse_defs<'a>(pairs: Pairs<'a, Rule>) -> HashMap<&'a str, &'a str> { +fn parse_defs(pairs: Pairs) -> HashMap<&'_ str, &'_ str> { let mut map = HashMap::new(); for pair in pairs { if Rule::assign == pair.as_rule() { @@ -45,7 +48,7 @@ fn parse_defs<'a>(pairs: Pairs<'a, Rule>) -> HashMap<&'a str, &'a str> { map } -fn parse_array_defs<'a>(pairs: Pairs<'a, Rule>) -> HashMap<&'a str, Vec<&'a str>> { +fn parse_array_defs(pairs: Pairs) -> HashMap<&'_ str, Vec<&'_ str>> { let mut map = HashMap::new(); for pair in pairs { if Rule::assign == pair.as_rule() { @@ -56,7 +59,7 @@ fn parse_array_defs<'a>(pairs: Pairs<'a, Rule>) -> HashMap<&'a str, Vec<&'a str> map } -fn parse_source<'a>(pairs: Pairs) -> Vec { +fn parse_source(pairs: Pairs) -> Vec { let mut vec: Vec = Vec::new(); for pair in pairs { @@ -73,7 +76,7 @@ fn parse_source<'a>(pairs: Pairs) -> Vec { vec } -fn parse_assign<'a>(pair: Pair<'a, Rule>) -> (&'a str, &'a str) { +fn parse_assign(pair: Pair) -> (&'_ str, &'_ str) { let mut key = ""; let mut val = ""; for pair in pair.into_inner() { @@ -88,7 +91,7 @@ fn parse_assign<'a>(pair: Pair<'a, Rule>) -> (&'a str, &'a str) { (key, val) } -fn parse_assign_array<'a>(pair: Pair<'a, Rule>) -> (&'a str, Vec<&'a str>) { +fn parse_assign_array(pair: Pair) -> (&'_ str, Vec<&'_ str>) { let mut key = ""; let mut val: Vec<&str> = Vec::default(); for pair in pair.into_inner() { @@ -103,7 +106,7 @@ fn parse_assign_array<'a>(pair: Pair<'a, Rule>) -> (&'a str, Vec<&'a str>) { (key, val) } -fn parse_array<'a>(pairs: Pairs<'a, Rule>) -> Vec<&'a str> { +fn parse_array(pairs: Pairs) -> Vec<&'_ str> { let mut vec: Vec<&str> = Vec::default(); for pair in pairs { -- 2.45.2