rust-parser-combinator/src/representation.rs

130 lines
3.5 KiB
Rust

use std::fmt;
use crate::util::intersperse_option;
#[derive(Debug, Clone)]
pub struct Representation {
production_output: EBNF,
}
impl Representation {
pub fn show(&self) -> String {
self.production_output.to_string()
}
pub fn production(&self) -> EBNF {
self.production_output.clone()
}
pub fn new() -> Self {
Self {
production_output: EBNF::None,
}
}
pub fn with_production(self, production_output: EBNF) -> Self {
Self {
production_output,
..self
}
}
}
#[derive(Debug, Clone)]
pub enum EBNF {
None,
Nonterminal(String),
CharTerminal(char),
StringTerminal(String),
LabeledTerminal(String),
Alternation(Vec<EBNF>),
Sequence(Vec<EBNF>),
Repeated {
inner: Box<EBNF>,
more_than_once: bool,
},
}
impl EBNF {
fn needs_wrapping(&self) -> bool {
match self {
EBNF::None => false,
EBNF::Nonterminal(_) => false,
EBNF::CharTerminal(_) => false,
EBNF::StringTerminal(_) => false,
EBNF::LabeledTerminal(_) => false,
EBNF::Sequence(items) => items.len() > 1,
EBNF::Alternation(_) => true,
EBNF::Repeated { .. } => false,
}
}
}
impl fmt::Display for EBNF {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
//TODO should try to show the name if possible
EBNF::None => write!(f, "<no-representation>"),
EBNF::CharTerminal(ch) => write!(f, "'{ch}'"),
EBNF::Alternation(items) => {
for item in intersperse_option(items.iter()) {
match item {
None => write!(f, " | ")?,
Some(item) => write!(f, "{item}")?,
}
}
write!(f, "")
}
EBNF::Nonterminal(name) => write!(f, "{name}"),
EBNF::StringTerminal(term) => write!(f, r#""{term}""#),
EBNF::LabeledTerminal(s) => write!(f, "<{s}>"),
EBNF::Repeated {
inner,
more_than_once,
} => {
let sigil = if *more_than_once { '+' } else { '*' };
if inner.needs_wrapping() {
write!(f, "[{inner}]{sigil}")
} else {
write!(f, "{inner}{sigil}")
}
}
EBNF::Sequence(items) => {
for item in intersperse_option(items.iter()) {
if let Some(item) = item {
write!(f, "{item}")?;
} else {
write!(f, " ")?;
}
}
write!(f, "")
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ebnf_print() {
let example = EBNF::Alternation(vec![
EBNF::CharTerminal('f'),
EBNF::CharTerminal('a'),
EBNF::CharTerminal('k'),
EBNF::CharTerminal('e'),
]);
assert_eq!(example.to_string(), "'f' | 'a' | 'k' | 'e'");
let example = EBNF::Alternation(vec![
EBNF::Nonterminal("other-rule".into()),
EBNF::CharTerminal('q'),
EBNF::CharTerminal('m'),
EBNF::StringTerminal("focus".into()),
]);
assert_eq!(example.to_string(), "other-rule | 'q' | 'm' | \"focus\"");
}
}