use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use crate::engine::{Language, ParseResult};
use crate::error::Recovery;
use crate::latexlike::check_latexlike_tree_invariants;
use crate::scopes::{IntoSpecsProvider, Package};
use crate::spec::StdCallableSpec;
use crate::state::ParsingState;
use super::{CallableType, GroupType, Latexlike, LatexlikeDriver, LatexlikeLang, Mode};
use crate::node::NodeKind;
use crate::source::SourceSpan;
use crate::state::Lang;
pub(super) fn strict() -> Language<Latexlike> {
Language::new(
LatexlikeDriver::new(Recovery::Strict),
ParsingState::lang_initial().expect("seed state"),
)
}
pub(super) fn tolerant() -> Language<Latexlike> {
Language::new(
LatexlikeDriver::new(Recovery::Tolerant),
ParsingState::lang_initial().expect("seed state"),
)
}
pub(super) fn with_package(recovery: Recovery, package: impl IntoSpecsProvider<Latexlike>) -> Language<Latexlike> {
Language::new(
LatexlikeDriver::new(recovery),
ParsingState::lang_initial_with_packages([package]).expect("seed state"),
)
}
pub(super) fn with_packages(
recovery: Recovery,
packages: impl IntoIterator<Item: IntoSpecsProvider<Latexlike>>,
) -> Language<Latexlike> {
Language::new(
LatexlikeDriver::new(recovery),
ParsingState::lang_initial_with_packages(packages).expect("seed state"),
)
}
pub(super) fn root_shapes(result: &ParseResult<Latexlike>) -> Vec<String> {
result.tree.root().children().iter().map(|node| node.summary()).collect()
}
pub(super) fn parse_shapes(input: &str) -> Vec<String> {
let result = strict().parse(input).unwrap();
check_latexlike_tree_invariants(&result.tree);
assert!(result.diagnostics.is_empty(), "unexpected diagnostics: {:?}", result.diagnostics);
root_shapes(&result)
}
pub(super) fn macro_package(
pkg_name: &str,
macro_name: &str,
visible_modes: Option<Vec<Mode>>,
) -> Arc<Package<Latexlike>> {
Package::new_shared(pkg_name, |package| {
let mut spec = StdCallableSpec::<Latexlike>::default();
if let Some(provenance) = package.provenance_for(CallableType::Macro, macro_name) {
spec = spec.with_provenance(provenance);
}
package.insert(CallableType::Macro, macro_name, Arc::new(spec));
if visible_modes.is_some() {
package.set_visible_modes(visible_modes);
}
})
}
#[derive(Debug, Clone, Copy)]
pub(super) struct RelaxedLatexlike;
impl Lang for RelaxedLatexlike {
const OBEYS_SPAN_TILING: bool = false;
type Features = crate::state::AllLangFeatures;
type GroupTypeId = GroupType;
type CallableTypeId = CallableType;
type ModeId = Mode;
type StateExt = ();
type Event = crate::latexlike::Event;
type SessionExt = ();
type SourceOrigin = Option<String>;
type Tokenization = crate::token::StdTokenization;
type NodeExts = crate::latexlike::LatexlikeNodeExts;
type InvocationSyntax =
crate::latexlike::InvocationSyntaxData<crate::latexlike::StdEnvironmentSyntax<Self>>;
type Driver = LatexlikeDriver<Self>;
fn initial_state_data() -> Result<crate::state::StateData<Self>, crate::state::FinalizeError>
{
let mut scopes = crate::scopes::ScopeStack::new();
scopes.push(crate::latexlike::builtin_package());
Ok(crate::state::StateData {
rules: crate::latexlike::default_token_rules(),
scopes,
mode: Mode::Text,
ext: (),
})
}
fn scan_specials(
state: &ParsingState<Self>,
content: &str,
pos: usize,
) -> Result<Option<crate::token::SpecialsMatch<Self>>, crate::token::SpecialsScanError>
{
state.scopes().scan_specials(state, content, pos)
}
fn specials_trigger_chars(
data: &crate::state::StateData<Self>,
) -> crate::token::TriggerChars {
data.scopes.specials_trigger_chars()
}
fn make_node_ext(
_kind: &NodeKind<Self>,
_span: &SourceSpan<Self::SourceOrigin>,
_state: &Arc<ParsingState<Self>>,
_children: crate::node::StagedChildren<'_, Self>,
) -> Result<(), crate::node::NodeBuildError> {
Ok(())
}
}
impl LatexlikeLang for RelaxedLatexlike {}
#[derive(Debug, Clone, Copy)]
pub(super) struct RelaxedScriptedLatexlike;
impl Lang for RelaxedScriptedLatexlike {
const OBEYS_SPAN_TILING: bool = false;
type Features = crate::state::AllLangFeatures;
type GroupTypeId = GroupType;
type CallableTypeId = CallableType;
type ModeId = Mode;
type StateExt = ();
type Event = crate::latexlike::Event;
type SessionExt = ();
type SourceOrigin = Option<String>;
type Tokenization = crate::token::ScriptedTokenization;
type NodeExts = crate::latexlike::LatexlikeNodeExts;
type InvocationSyntax =
crate::latexlike::InvocationSyntaxData<crate::latexlike::StdEnvironmentSyntax<Self>>;
type Driver = LatexlikeDriver<Self>;
fn initial_state_data() -> Result<crate::state::StateData<Self>, crate::state::FinalizeError>
{
let mut scopes = crate::scopes::ScopeStack::new();
scopes.push(crate::latexlike::builtin_package());
Ok(crate::state::StateData {
rules: crate::latexlike::default_token_rules(),
scopes,
mode: Mode::Text,
ext: (),
})
}
fn scan_specials(
state: &ParsingState<Self>,
content: &str,
pos: usize,
) -> Result<Option<crate::token::SpecialsMatch<Self>>, crate::token::SpecialsScanError>
{
state.scopes().scan_specials(state, content, pos)
}
fn specials_trigger_chars(
data: &crate::state::StateData<Self>,
) -> crate::token::TriggerChars {
data.scopes.specials_trigger_chars()
}
fn make_node_ext(
_kind: &NodeKind<Self>,
_span: &SourceSpan<Self::SourceOrigin>,
_state: &Arc<ParsingState<Self>>,
_children: crate::node::StagedChildren<'_, Self>,
) -> Result<(), crate::node::NodeBuildError> {
Ok(())
}
}
impl LatexlikeLang for RelaxedScriptedLatexlike {}
const _: () = assert!(!RelaxedScriptedLatexlike::OBEYS_SPAN_TILING);