mod declarations;
mod nodes;
mod scan;
use crate::common::diagnostic::{Diagnostic, Diagnostics};
use crate::common::error::listed;
use crate::syntax::ast::{FlowAst, DECLARATIONS, LATEST_EDITION};
use crate::syntax::lexer::{lex, Lexed, Mode};
use declarations::LetPlace;
use temple_dsl::Span;
pub(crate) fn parse(src: &str) -> (FlowAst, Vec<Diagnostic>) {
let mut parser = Parser::new(src);
let ast = parser.file();
(ast, parser.diagnostics)
}
pub(crate) fn parse_guard(src: &str) -> Result<(), Diagnostics> {
let mut parser = Parser::refusing_comments(src);
let parsed = parser.declaration(Parser::expression).is_some();
parser.finish(parsed, "guard")
}
pub(crate) fn parse_outcome(src: &str) -> Result<(), Diagnostics> {
let mut parser = Parser::refusing_comments(src);
let parsed = parser.declaration(Parser::outcome).is_some();
parser.finish(parsed, "outcome")
}
pub(crate) fn parse_node(src: &str) -> Result<(), Diagnostics> {
let mut parser = Parser::new(src);
parser.skip_trivia();
let parsed = parser
.declaration(|parser| parser.node(Vec::new()))
.is_some();
parser.finish(parsed, "node")
}
struct Fail;
type ParseResult<T> = Result<T, Fail>;
struct Parser<'a> {
whole: &'a str,
src: &'a str,
bytes: &'a [u8],
at: usize,
diagnostics: Vec<Diagnostic>,
declaration_lines: Vec<usize>,
comment: Option<usize>,
refuse_comments: bool,
token_end: usize,
trivia_end: usize,
}
impl<'a> Parser<'a> {
fn new(src: &'a str) -> Self {
let mut parser = Parser {
whole: src,
src,
bytes: src.as_bytes(),
at: 0,
diagnostics: Vec::new(),
declaration_lines: Vec::new(),
comment: None,
refuse_comments: false,
token_end: 0,
trivia_end: 0,
};
let line_starts = std::iter::once(0).chain(
src.bytes()
.enumerate()
.filter(|&(_, byte)| byte == b'\n')
.map(|(at, _)| at + 1),
);
parser.declaration_lines = line_starts
.filter(|&start| parser.starts_declaration(start))
.collect();
parser
}
fn refusing_comments(src: &'a str) -> Self {
Parser {
refuse_comments: true,
..Parser::new(src)
}
}
fn declaration<T>(&mut self, parse: impl FnOnce(&mut Self) -> ParseResult<T>) -> Option<T> {
let first_diagnostic = self.diagnostics.len();
let end = self.declaration_end(self.at);
self.src = &self.whole[..end];
self.bytes = self.src.as_bytes();
let result = parse(self);
self.src = self.whole;
self.bytes = self.whole.as_bytes();
let text_end = self.token_end;
for diagnostic in &mut self.diagnostics[first_diagnostic..] {
if diagnostic.span.start as usize >= end {
diagnostic.span = Span::new(text_end, text_end);
}
}
if result.is_err() {
self.at = end;
}
result.ok()
}
fn lex_unit(&mut self, span: Span, mode: Mode) -> Lexed<'a> {
let lexed = lex(self.src, span, mode);
if let Some(at) = lexed.comment.filter(|_| self.refuse_comments) {
self.comment = Some(self.comment.map_or(at, |first| first.min(at)));
}
lexed
}
fn finish(mut self, parsed: bool, what: &str) -> Result<(), Diagnostics> {
if parsed {
self.skip_trivia();
if self.at < self.bytes.len() {
let end = self.line_end(self.at);
self.diagnostics.push(Diagnostic::error(
Span::new(self.at, end),
format!("unexpected text after the {what}"),
));
}
}
if let Some(at) = self.comment.filter(|_| self.refuse_comments) {
let refused = Diagnostic::error(
Span::new(at, self.line_end(at)),
format!("an edit's {what} can't hold a `#` comment"),
)
.with_help("it would hide the rest of the line it lands on");
self.diagnostics.insert(0, refused);
}
let diagnostics = Diagnostics::new(self.diagnostics);
if diagnostics.has_errors() {
Err(diagnostics)
} else {
Ok(())
}
}
fn file(&mut self) -> FlowAst {
let mut ast = FlowAst::default();
let mut pending = Vec::new();
let mut first = true;
loop {
self.skip_trivia();
if self.at >= self.bytes.len() {
break;
}
let start = self.at;
if self.peek() == Some(b'@') {
if let Some(annotation) = self.declaration(Self::annotation) {
pending.push(annotation);
}
continue;
}
let word = self.peek_word();
let takes_annotations = matches!(word, Some("flow" | "param" | "node"));
if !pending.is_empty() && !takes_annotations {
self.diagnostics.push(
Diagnostic::error(
pending[0].span,
"annotations can only be placed on a flow, a param or a node",
)
.with_help("move it directly above `flow`, `param` or `node`"),
);
pending.clear();
}
self.declaration(|parser| match word {
Some("edition") => parser.edition(&mut ast, first),
Some("flow") => parser.flow_declaration(&mut ast, std::mem::take(&mut pending)),
Some("entry") => parser
.name_list("entry", "a node name", Self::word)
.map(|list| ast.entries.push(list)),
Some("events") => parser
.name_list("events", "an event name", Self::dotted_name)
.map(|list| ast.events.push(list)),
Some("param") => parser
.param(std::mem::take(&mut pending))
.map(|param| ast.params.push(param)),
Some("let") => parser
.let_declaration(LetPlace::TopLevel)
.map(|binding| ast.lets.push(binding)),
Some("node") => parser
.node(std::mem::take(&mut pending))
.map(|node| ast.nodes.push(node)),
_ => {
let end = parser.line_end(start);
parser.diagnostics.push(
Diagnostic::error(
Span::new(start, end),
format!("expected a declaration: {}", listed(DECLARATIONS)),
)
.with_help("each declaration starts at the beginning of a line"),
);
Err(Fail)
}
});
if first && word != Some("edition") {
self.diagnostics.push(
Diagnostic::error(
Span::new(start, start),
format!("a flow file must start with `edition {LATEST_EDITION}`"),
)
.with_help(format!("add `edition {LATEST_EDITION}` as the first line")),
);
}
first = false;
}
if let Some(annotation) = pending.first() {
self.diagnostics.push(Diagnostic::error(
annotation.span,
"this annotation is not attached to a flow, param or node",
));
}
ast
}
}