only one generated solution
parent
069c64a865
commit
0ef99c9095
45
src/lang.bnf
45
src/lang.bnf
|
|
@ -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> ::= "
|
||||
|
|
|
|||
83
src/main.rs
83
src/main.rs
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
use bnf::{Grammar, ParseTree};
|
||||
use bnf::ParseTreeNode::{Nonterminal, Terminal};
|
||||
|
||||
const IGNORE: [&'static str; 3] = [
|
||||
"",
|
||||
|
|
@ -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 {}", );
|
||||
fn should_skip(term: &str) -> bool {
|
||||
if term == "program" {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
fn digest(&self, tree: &ParseTree) {
|
||||
|
||||
println!("Program::digest {}", tree.lhs.to_string());
|
||||
// mut because that recursion could get nasty.
|
||||
|
||||
tree.lhs.to_string() == self.get_term_str()
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,3 +152,6 @@ fn pluralize<'a>(n: usize, singular: &'a str, plural: &'a str) -> &'a str {
|
|||
return plural;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue