diff --git a/src/lang.bnf b/src/lang.bnf index f78b86e..3b571d1 100644 --- a/src/lang.bnf +++ b/src/lang.bnf @@ -1,15 +1,19 @@ - ::= - | - ::= - | + + ::= <_n> <_n> + + ::= + | <_> <__n> + + + + ::= + | - - ::= "handle" - ::= "use" + ::= "use" <__> @@ -27,15 +31,28 @@ | | - ::= - | - ::= | - ::= | | + ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" + | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" + | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" + | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" + | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" + | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" + ::= "0" | "1" | "2" | "3" | "4" + | "5" | "6" | "7" | "8" | "9" + ::= | + +<__n> ::= <_n> +<_n> ::= + | | | + | <_n> + | <_n> + | <_n> +<_> ::= | <__> +<__> ::= | + | <__> + | <__> - ::= | - ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" - ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ::= "" ::= " " ::= " diff --git a/src/main.rs b/src/main.rs index 546e539..cb2c94d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use bnf::{Grammar, ParseTree}; +use bnf::ParseTreeNode::{Nonterminal, Terminal}; const IGNORE: [&'static str; 3] = [ "", @@ -8,7 +9,7 @@ const IGNORE: [&'static str; 3] = [ ]; fn main() { - + let input = include_str!("lang.bnf"); let grammar: Grammar = input.parse().unwrap(); @@ -38,11 +39,6 @@ fn main() { println!("---"); } -#[derive(Debug)] -struct Program<'a> { - uses: Vec> -} - trait TermDigestor { fn get_term_str(&self) -> &'static str; } @@ -53,6 +49,11 @@ impl TermDigestor for Program<'_> { } } +#[derive(Debug)] +struct Program<'a> { + pub uses: Vec> +} + impl Program<'_> { pub fn new(tree: &ParseTree) -> Self { let program = Program { @@ -62,11 +63,74 @@ impl Program<'_> { return program } - fn digest(&self, mut tree: &ParseTree) { - // println!("digest {}", ); - // mut because that recursion could get nasty. + fn should_skip(term: &str) -> bool { + if term == "program" { + return true; + } else { + return false; + } + } + + fn digest(&self, tree: &ParseTree) { - tree.lhs.to_string() == self.get_term_str() + println!("Program::digest {}", tree.lhs.to_string()); + // mut because that recursion could get nasty. + + for term in tree.rhs_iter() { + match term { + Terminal(str) => { + println!("{:?}", str); + }, + Nonterminal(tree) => { + if tree.lhs.to_string().starts_with("<_") { + continue; + } + if tree.lhs.to_string() == "" { + self.digest_1(tree); + } + } + } + } + } + + fn digest_1(&self, tree: &ParseTree) { + println!("Program::digest$1 {}", tree.lhs.to_string()); + + for term in tree.rhs_iter() { + match term { + Terminal(str) => { + println!("{:?}", str); + }, + Nonterminal(tree) => { + if tree.lhs.to_string().starts_with("<_") { + continue; + } + if tree.lhs.to_string() == "" { + self.digest_1(tree); + } + if tree.lhs.to_string() == "" { + self.digest_unit(tree); + } + } + } + } + } + + fn digest_unit(&self, tree: &ParseTree) { + println!("Program::digest_unit {}", tree.lhs.to_string()); + for term in tree.rhs_iter() { + match term { + Terminal(str) => { + println!("{:?}", str); + }, + Nonterminal(tree) => { + if tree.lhs.to_string().starts_with("<_") { + continue; + } + println!("{:?}", tree.lhs.to_string()) + } + } + } } } @@ -87,4 +151,7 @@ fn pluralize<'a>(n: usize, singular: &'a str, plural: &'a str) -> &'a str { else { return plural; } -} \ No newline at end of file +} + + +