Skip to content

Commit

Permalink
Allow precedence expressions to omit operator classes
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyBlakey committed Feb 24, 2023
1 parent 93061e8 commit 9776de1
Show file tree
Hide file tree
Showing 196 changed files with 4,859 additions and 7,783 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions crates/codegen/ebnf/src/ebnf_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub enum TokenKind {
comment,
constant,
keyword,
operator,
string,
}

pub trait EBNFWriter {
fn write_token(&mut self, kind: TokenKind, value: &str);
fn write_line_break(&mut self);

fn write_production_definition(&mut self, production_name: &str);
fn write_production_reference(&mut self, production_name: &str);

fn write_prelude(&mut self) {}
fn write_postlude(&mut self) {}
fn write_line_start(&mut self) {}

fn write_comment(&mut self, value: &str) {
self.write_token(TokenKind::comment, value)
}
fn write_constant(&mut self, value: &str) {
self.write_token(TokenKind::constant, value)
}
fn write_keyword(&mut self, value: &str) {
self.write_token(TokenKind::keyword, value)
}
fn write_operator(&mut self, value: &str) {
self.write_token(TokenKind::operator, value)
}
fn write_string(&mut self, value: &str) {
self.write_token(TokenKind::string, &Self::format_string_literal(value))
}

fn format_string_literal(value: &str) -> String {
let delimiter = if value.len() == 1 {
if value.contains("'") && !value.contains('"') {
'"'
} else {
'\''
}
} else {
if value.contains('"') && !value.contains("'") {
'\''
} else {
'"'
}
};

let formatted: String = value
.chars()
.map(|c| {
if c == '\'' || c == '\\' {
format!("\\{}", c)
} else if c.is_ascii_graphic() || c == '¬' || c == '…' || c == '«' || c == '»'
{
c.to_string()
} else {
c.escape_unicode().to_string()
}
})
.collect();

return format!("{}{}{}", delimiter, formatted, delimiter);
}
}

pub trait EBNFWritable<W: EBNFWriter> {
fn write_ebnf(&self, name: &str, writer: &mut W);
}
192 changes: 0 additions & 192 deletions crates/codegen/ebnf/src/expression.rs

This file was deleted.

43 changes: 0 additions & 43 deletions crates/codegen/ebnf/src/grammar.rs

This file was deleted.

8 changes: 4 additions & 4 deletions crates/codegen/ebnf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod expression;
mod grammar;
mod parser;
mod precedence_parser;
mod production;
mod scanner;

pub use grammar::GrammarEBNFGeneratorExtensions;
pub use production::ProductionEBNFGeneratorExtensions;
pub mod ebnf_writer;
Loading

0 comments on commit 9776de1

Please sign in to comment.