pub mod ast;
pub mod core;
pub mod json;
pub mod lexer;
pub mod syntax;
pub mod validate;
pub mod xml;
pub mod yaml;
use crate::errors::GelError;
use crate::exec::{execute, serialize_execution, RuntimeFormat};
use crate::parser::json::JsonGenerator;
use crate::parser::syntax::parse_gel_document;
use crate::parser::xml::XmlGenerator;
use crate::parser::yaml::YamlGenerator;
#[derive(Debug, Clone, Copy)]
pub enum OutputFormat {
Json,
Xml,
Yaml,
}
pub struct Parser {
format: OutputFormat,
}
impl Parser {
pub fn new(format: OutputFormat) -> Self {
Self { format }
}
pub fn parse_str(&self, input: &str) -> Result<String, GelError> {
let tokens = lexer::lex(input)?;
let ast = parse_gel_document(&tokens)?;
match self.format {
OutputFormat::Json => Ok(JsonGenerator::generate_from_ast(&ast)),
OutputFormat::Xml => Ok(XmlGenerator::generate_from_ast(&ast)),
OutputFormat::Yaml => Ok(YamlGenerator::generate_from_ast(&ast)),
}
}
pub fn parse_and_run(&self, gel_source: &str, grammar: &str, runtime_input: &str) -> Result<String, GelError> {
let tokens = lexer::lex(gel_source)?;
let mut doc = parse_gel_document(&tokens)?;
let diags = validate::validate(&doc);
for d in &diags {
if d.severity == crate::errors::Severity::Error {
return Err(GelError::validation(d.message.clone(), d.span.unwrap_or_default()));
}
}
let mut exec_result = execute(&mut doc, grammar, runtime_input)?;
for d in diags {
exec_result.diagnostics.push(d);
}
Ok(serialize_execution(&exec_result, RuntimeFormat::Json))
}
}