mod compile;
mod converter;
mod numeric;
mod parser_abnf;
mod tree;
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme_examples {}
use std::fmt;
use tabnas::{Plugin, PluginError, Tabnas, Value};
pub use compile::abnf_compile;
pub use converter::{parse_abnf, AbnfParseError};
pub use parser_abnf::abnf_rules;
pub use tree::{make, parse_abnf_tree};
pub use tabnas_bnf::{
attach_action_slots, attach_actions, eliminate_left_recursion, mark_listing, to_jsonic,
to_pure_spec, to_recognition_spec, transduce_alc, ActionError as AbnfActionError, ActionFn,
ActionsMap, AltSpec, CompileError as AbnfCompileError, ConvertOptions as AbnfConvertOptions,
Element, EmitError, Grammar, GrammarSpec, JsonicOptions, Kind, NodeKind, Production, RefAction,
RuleSpec, Sequence, SrcSpan, TransducePart, ValueAnnotation,
};
pub const VERSION: &str = "0.5.1";
pub const TAG: &str = "abnf";
#[derive(Debug, Clone, PartialEq)]
pub enum AbnfError {
Parse(AbnfParseError),
Emit(EmitError),
Compile(AbnfCompileError),
Action(AbnfActionError),
Install(String),
}
impl fmt::Display for AbnfError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse(error) => error.fmt(formatter),
Self::Emit(error) => error.fmt(formatter),
Self::Compile(error) => error.fmt(formatter),
Self::Action(error) => error.fmt(formatter),
Self::Install(message) => formatter.write_str(message),
}
}
}
impl std::error::Error for AbnfError {}
impl From<AbnfParseError> for AbnfError {
fn from(error: AbnfParseError) -> Self {
Self::Parse(error)
}
}
impl From<EmitError> for AbnfError {
fn from(error: EmitError) -> Self {
Self::Emit(error)
}
}
impl From<AbnfCompileError> for AbnfError {
fn from(error: AbnfCompileError) -> Self {
Self::Compile(error)
}
}
impl From<AbnfActionError> for AbnfError {
fn from(error: AbnfActionError) -> Self {
Self::Action(error)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AbnfCompileOptions {
pub start: Option<String>,
pub tag: Option<String>,
pub recognition: bool,
pub strict: bool,
pub indent: Option<usize>,
}
impl Default for AbnfCompileOptions {
fn default() -> Self {
Self {
start: None,
tag: None,
recognition: true,
strict: false,
indent: None,
}
}
}
#[derive(Clone, Default)]
pub struct AbnfOptions {
pub convert: AbnfConvertOptions,
pub actions: Option<ActionsMap>,
}
impl AbnfOptions {
pub fn new(convert: AbnfConvertOptions) -> Self {
Self {
convert,
actions: None,
}
}
pub fn with_actions(mut self, actions: ActionsMap) -> Self {
self.actions = Some(actions);
self
}
}
impl fmt::Debug for AbnfOptions {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("AbnfOptions")
.field("convert", &self.convert)
.field(
"actions",
&self.actions.as_ref().map(|actions| actions.len()),
)
.finish()
}
}
pub fn emit_grammar_spec(
grammar: &Grammar,
opts: Option<&AbnfConvertOptions>,
) -> Result<GrammarSpec, EmitError> {
let mut options = opts.cloned().unwrap_or_default();
if options.tag.is_none() {
options.tag = Some(TAG.to_string());
}
tabnas_bnf::emit_grammar_spec(grammar, &options)
}
pub fn abnf_convert(
src: &str,
opts: Option<&AbnfConvertOptions>,
) -> Result<GrammarSpec, AbnfError> {
let grammar = parse_abnf(src)?;
Ok(emit_grammar_spec(&grammar, opts)?)
}
pub fn abnf_transduce(
src: &str,
name: &str,
opts: Option<&AbnfConvertOptions>,
) -> Result<TransducePart, AbnfError> {
Ok(transduce_alc(&abnf_convert(src, opts)?, name))
}
pub fn abnf(
parser: &mut Tabnas,
src: &str,
opts: Option<&AbnfOptions>,
) -> Result<GrammarSpec, AbnfError> {
let mut convert = opts.map(|opts| opts.convert.clone()).unwrap_or_default();
let actions = opts.and_then(|opts| opts.actions.clone());
if actions.is_some() {
convert.builtins = false;
convert.marks = true;
}
let mut spec = abnf_convert(src, Some(&convert))?;
if let Some(actions) = actions {
attach_actions(&mut spec, actions)?;
}
spec.install(parser)
.map_err(|error| AbnfError::Install(error.to_string()))?;
Ok(spec)
}
pub fn plugin() -> Plugin {
Plugin::new("Abnf", |parser, options| {
let source = match options {
Value::Object(entries) => match entries.get("src") {
Some(Value::String(src)) => Some(src.clone()),
_ => None,
},
_ => None,
};
let Some(source) = source else {
return Ok(());
};
abnf(parser, &source, None)
.map(|_| ())
.map_err(|error| PluginError(error.to_string()))
})
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TranslationPart {
pub entry: &'static str,
pub source: Option<&'static str>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TranslationParts {
pub manifest: &'static str,
pub lift: Option<TranslationPart>,
pub embed: Option<TranslationPart>,
pub render: Option<TranslationPart>,
pub transduce: Option<TranslationPart>,
pub spec: Option<TranslationPart>,
}
const TRANSLATION: TranslationParts = TranslationParts {
manifest: include_str!("../translate/manifest.json"),
lift: None,
embed: None,
render: Some(TranslationPart {
entry: "abnf-render",
source: Some(include_str!("../translate/render.alc")),
}),
transduce: Some(TranslationPart {
entry: "abnf-transduce",
source: Some(include_str!("../translate/transduce.alc")),
}),
spec: Some(TranslationPart {
entry: "abnf-render-spec",
source: Some(include_str!("../translate/render-spec.alc")),
}),
};
#[must_use]
pub const fn translate() -> Option<TranslationParts> {
Some(TRANSLATION)
}
pub fn manifest_text() -> &'static str {
TRANSLATION.manifest
}
fn part_text(part: Option<TranslationPart>) -> &'static str {
match part {
Some(part) => part.source.unwrap_or_default(),
None => "",
}
}
pub fn render_text() -> &'static str {
part_text(TRANSLATION.render)
}
pub fn transduce_text() -> &'static str {
part_text(TRANSLATION.transduce)
}
pub fn render_spec_text() -> &'static str {
part_text(TRANSLATION.spec)
}