only one generated solution

stable
Ivory 2023-06-03 11:53:34 -04:00
parent 069c64a865
commit 0ef99c9095
2 changed files with 109 additions and 25 deletions

View File

@ -1,15 +1,19 @@
<program> ::= <optional_greedy_space> <declaration> <optional_greedy_space>
| <optional_greedy_space> <declaration> <optional_greedy_space> <greedy_newline> <program>
<declaration> ::= <use_clause>
| <handler>
<program> ::= <_n> <program$1> <_n>
<program$1> ::= <unit>
| <unit> <_> <__n> <program$1>
<unit> ::= <use_clause>
| <handler>
<handler> ::= "handle" <greedy_space> <identifier> <optional_greedy_space> <parameters> <optional_greedy_space> <body>
<use_clause> ::= "use" <greedy_space> <namespace>
<use_clause> ::= "use" <__> <namespace>
@ -27,15 +31,28 @@
| <LETTER> <identifier>
| <identifier> <DIGIT>
<greedy_newline> ::= <EOL>
| <EOL> <optional_greedy_space> <greedy_newline>
<optional_greedy_space> ::= <NOTHING> | <greedy_space>
<greedy_space> ::= <SPACE> | <TAB> | <greedy_space> <greedy_space>
<LETTER> ::= "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"
<DIGIT> ::= "0" | "1" | "2" | "3" | "4"
| "5" | "6" | "7" | "8" | "9"
<INTEGER> ::= <DIGIT> | <DIGIT> <INTEGER>
<__n> ::= <EOL> <_n>
<_n> ::= <NOTHING>
| <SPACE> | <TAB> | <EOL>
| <SPACE> <_n>
| <TAB> <_n>
| <EOL> <_n>
<_> ::= <NOTHING> | <__>
<__> ::= <SPACE> | <TAB>
| <SPACE> <__>
| <TAB> <__>
<INTEGER> ::= <DIGIT> | <DIGIT> <INTEGER>
<LETTER> ::= "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"
<DIGIT> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<NOTHING> ::= ""
<SPACE> ::= " "
<EOL> ::= "

View File

@ -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<UseDeclaration<'a>>
}
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<UseDeclaration<'a>>
}
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() == "<program$1>" {
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() == "<program$1>" {
self.digest_1(tree);
}
if tree.lhs.to_string() == "<unit>" {
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;
}
}
}