Skip to content

Commit

Permalink
update name to lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
imlyzh committed May 1, 2021
1 parent 1550765 commit 4f1bd90
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
7 changes: 3 additions & 4 deletions src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashSet;

use crate::ast::*;

pub trait Gen {
Expand All @@ -9,7 +7,7 @@ pub trait Gen {
impl Gen for Term {
fn gen(&self) -> String {
match self {
Term::Symbol(x) => x.clone(),
Term::Symbol(x) => x.to_lowercase(),
Term::Group(expr) => format!("({})", expr.gen()),
Term::Option(expr) => format!("({})?", expr.gen()),
Term::Repetition(expr) => format!("({})*", expr.gen()),
Expand Down Expand Up @@ -44,12 +42,13 @@ impl Gen for Expr {

impl Gen for Bind {
fn gen(&self) -> String {
let name = self.0.to_lowercase();
let expr = if let Some(expr) = &self.1 {
expr.gen()
} else {
"UNDEFINED".to_string()
};
format!("{} = {{ {} }}", self.0, expr)
format!("{} = {{ {} }}", name, expr)
}
}

Expand Down
17 changes: 5 additions & 12 deletions src/grammar.pest
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
unit = { (!EOI ~ production)* ~ EOI }

production = {
production_name ~ "=" ~ expression ~ "."
}

expression = {
alternative ~ ("|" ~ alternative)*
}

alternative = {
//term ~ term*
term+
}
production = { production_name ~ "=" ~ expression ~ "." }

expression = { alternative ~ ("|" ~ alternative)* }

alternative = { term+ }

term =
{ production_name
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::parser::*;
use crate::gen::*;

fn main() {
let mut r = File::open("./test/1.bnf").unwrap();
let mut r = File::open("./test/bnf.bnf").unwrap();
let mut buf = String::new();
r.read_to_string(&mut buf).unwrap();
let res = parse(&buf);
Expand Down
7 changes: 7 additions & 0 deletions test/bnf.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Production = production_name "=" [ Expression ] "." .
Expression = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term = production_name | token [ "…" token ] | Group | Option | Repetition .
Group = "(" Expression ")" .
Option = "[" Expression "]" .
Repetition = "{" Expression "}" .
Empty file added test/golang.bnf
Empty file.

0 comments on commit 4f1bd90

Please sign in to comment.