mod actors;
pub mod analysis;
mod blocks;
mod blocks_print;
mod build;
mod convert;
mod error;
mod format;
#[cfg(feature = "dsl-fs")]
pub mod fs;
mod grammar;
mod lex;
mod policy;
mod project;
mod rules;
pub mod testing;
mod time;
mod validate;
use std::sync::Arc;
use build::Builder;
use grammar::Cursor;
pub use convert::{ConvertError, convert};
pub use error::{Diagnostic, Diagnostics, Location, PrintError};
pub use format::{FormatError, format, is_formatted};
pub use policy::{Language, Note, NoteAnchor, NoteOrigin, Origins, Placement, Policy, ids};
pub use project::{Built, MAX_PROJECT_BYTES, MAX_SOURCES, Project};
pub const MAX_SOURCE_BYTES: usize = 4 * 1024 * 1024;
#[doc(hidden)]
pub mod __macro_support {
pub const DSL_SCHEMA: u32 = 5;
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Parsed {
pub language: Language,
pub policy: Policy,
pub origins: Origins,
}
impl Policy {
pub fn validate(&self) -> Result<(), Diagnostics> {
let found = validate::validate(self, None);
if found.is_empty() {
Ok(())
} else {
Err(Diagnostics::new(found))
}
}
}
pub fn parse(file: &str, text: &str) -> Result<Parsed, Diagnostics> {
let mut parsed = parse_unvalidated(file, text)?;
let found = validate::validate(&parsed.policy, Some(&parsed.origins));
if !found.is_empty() {
return Err(Diagnostics::new(found));
}
parsed.policy.notes.shrink_to_fit();
Ok(parsed)
}
pub(crate) fn parse_unvalidated(file: &str, text: &str) -> Result<Parsed, Diagnostics> {
let file: Arc<str> = Arc::from(file);
if text.len() > MAX_SOURCE_BYTES {
let raw = error::Raw::new(
error::Span {
line: 1,
col: 1,
len: 1,
},
format!("the source is larger than {MAX_SOURCE_BYTES} bytes"),
);
return Err(Diagnostics::new(vec![raw.locate(&file)]));
}
let lexed = lex::lex(text);
if !lexed.errors.is_empty() {
let found = lexed.errors.into_iter().map(|raw| raw.locate(&file)).collect();
return Err(Diagnostics::new(error::capped(found, lexed.hidden)));
}
let indents: std::collections::HashMap<u32, u32> = lexed
.tokens
.iter()
.filter(|t| t.bol)
.map(|t| (t.span.line, t.span.col))
.collect();
let mut cursor = Cursor::new(lexed.tokens);
let language = match header(&mut cursor) {
Ok(found) => found,
Err(raw) => return Err(Diagnostics::new(vec![raw.locate(&file)])),
};
let mut builder = Builder::new(file, language.1);
builder.open(policy::ids::LANGUAGE, language.0);
builder.close(cursor.previous_span());
if language.1 != Language::Blocks {
check_indentation(text, &mut builder);
}
match language.1 {
Language::Blocks => blocks::parse(&mut cursor, &mut builder),
Language::Rules => rules::parse(&mut cursor, &mut builder),
Language::Actors => actors::parse(&mut cursor, &mut builder),
}
let (policy, origins, found) = builder.finish(&lexed.comments, &indents);
if !found.is_empty() {
return Err(Diagnostics::new(found));
}
Ok(Parsed {
language: language.1,
policy,
origins,
})
}
fn check_indentation(text: &str, builder: &mut Builder) {
for (at, line) in text.lines().enumerate() {
let lead = line.chars().take_while(|c| c.is_whitespace()).count();
if lead == line.chars().count() {
continue;
}
let Some(bad) = line.chars().take(lead).position(|c| c != ' ') else {
continue;
};
let place = |n: usize| u32::try_from(n).unwrap_or(u32::MAX);
builder.report(
error::Raw::new(
error::Span {
line: place(at.saturating_add(1)),
col: place(bad.saturating_add(1)),
len: 1,
},
"the indentation has a tab or another space character",
)
.hint("indent with spaces only"),
);
}
}
fn header(cursor: &mut Cursor) -> error::PResult<(error::Span, Language)> {
let first = cursor.current();
let at = first.span;
if !cursor.eat_word("language") {
return Err(error::Raw::new(at, "a source starts by declaring its language")
.hint("write on the first line: language blocks"));
}
let word = cursor.word("a language name")?;
let language: Language = word.v.parse().map_err(|()| {
error::Raw::new(word.span, format!("unknown language `{}`", word.v))
.hint("expected one of: blocks, rules, actors")
})?;
Ok((at, language))
}
pub fn print(policy: &Policy, language: Language) -> Result<String, PrintError> {
match language {
Language::Blocks => blocks_print::print(policy),
Language::Rules => rules::print(policy),
Language::Actors => actors::print(policy),
}
}