feat: render top-level Endor scripts through embedded Aster
Verify Tessera GUI / verify (push) Failing after 6m52s
Verify Tessera GUI / verify (push) Failing after 6m52s
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
//! Feed a source snapshot to Aster's existing module or interactive entry points.
|
||||
//! Statement syntax, tokens and ASTs all remain Aster-owned. The host only batches
|
||||
//! complete interactive inputs, just as a REPL feeds consecutive inputs to a VM.
|
||||
use anyhow::Result;
|
||||
use frontend::{
|
||||
ParseMode, SourceUnit, Span, TokenKind,
|
||||
ast::{AstRoot, Decl, DeclKind, Expr, ExprKind, InteractiveInput, Stmt, StmtKind},
|
||||
};
|
||||
|
||||
pub enum SourcePlan {
|
||||
Module(AstRoot),
|
||||
Script(Vec<AstRoot>),
|
||||
}
|
||||
|
||||
impl SourcePlan {
|
||||
pub fn roots(&self) -> &[AstRoot] {
|
||||
match self {
|
||||
Self::Module(root) => std::slice::from_ref(root),
|
||||
Self::Script(roots) => roots,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(source: &SourceUnit) -> Result<SourcePlan> {
|
||||
if let Ok(module) = frontend::parse_source_ast(source, ParseMode::Module) {
|
||||
return Ok(SourcePlan::Module(module));
|
||||
}
|
||||
let tokens = frontend::lex_source(source)?;
|
||||
let mut roots = Vec::new();
|
||||
let mut start = 0;
|
||||
let mut depth = 0_i64;
|
||||
for (index, token) in tokens.iter().enumerate() {
|
||||
match token.kind {
|
||||
TokenKind::LeftParen | TokenKind::LeftBrace => depth += 1,
|
||||
TokenKind::RightParen | TokenKind::RightBrace => depth -= 1,
|
||||
_ => {}
|
||||
}
|
||||
let end = token.span.end;
|
||||
let boundary = tokens.get(index + 1).is_none_or(|next| {
|
||||
depth == 0
|
||||
&& source.text()[end..next.span.start].contains(['\r', '\n'])
|
||||
&& !matches!(
|
||||
next.kind,
|
||||
TokenKind::Dot
|
||||
| TokenKind::LeftParen
|
||||
| TokenKind::Plus
|
||||
| TokenKind::Minus
|
||||
| TokenKind::Star
|
||||
| TokenKind::Slash
|
||||
)
|
||||
});
|
||||
if !boundary {
|
||||
continue;
|
||||
}
|
||||
match frontend::parse_ast(&source.text()[start..end], ParseMode::Interactive) {
|
||||
Ok(mut root) => {
|
||||
let AstRoot::Interactive(input) = &mut root else {
|
||||
unreachable!("interactive mode");
|
||||
};
|
||||
shift_input(input, start);
|
||||
roots.push(root);
|
||||
start = end;
|
||||
}
|
||||
// An incomplete interactive input can continue on the next line.
|
||||
// Only Aster decides whether its combined syntax is valid.
|
||||
Err(diagnostic) if index + 1 < tokens.len() && diagnostic.span.start == end - start => {
|
||||
}
|
||||
Err(mut diagnostic) => {
|
||||
shift_span(&mut diagnostic.span, start);
|
||||
return Err(source.annotate(diagnostic).into());
|
||||
}
|
||||
}
|
||||
}
|
||||
if roots.is_empty() {
|
||||
// Produce Aster's original empty-input diagnostic and provenance.
|
||||
frontend::parse_source_ast(source, ParseMode::Interactive)?;
|
||||
}
|
||||
Ok(SourcePlan::Script(roots))
|
||||
}
|
||||
|
||||
fn shift_span(span: &mut Span, offset: usize) {
|
||||
span.start += offset;
|
||||
span.end += offset;
|
||||
}
|
||||
|
||||
fn shift_input(input: &mut InteractiveInput, offset: usize) {
|
||||
shift_span(&mut input.span, offset);
|
||||
for statement in &mut input.statements {
|
||||
shift_statement(statement, offset);
|
||||
}
|
||||
}
|
||||
|
||||
fn shift_statement(statement: &mut Stmt, offset: usize) {
|
||||
shift_span(&mut statement.span, offset);
|
||||
match &mut statement.kind {
|
||||
StmtKind::Binding(binding) => {
|
||||
shift_span(&mut binding.span, offset);
|
||||
shift_span(&mut binding.name.span, offset);
|
||||
shift_expression(&mut binding.value, offset);
|
||||
}
|
||||
StmtKind::Return(returned) => {
|
||||
shift_span(&mut returned.span, offset);
|
||||
if let Some(expression) = &mut returned.value {
|
||||
shift_expression(expression, offset);
|
||||
}
|
||||
}
|
||||
StmtKind::Expression(expression) => shift_expression(expression, offset),
|
||||
StmtKind::Declaration(declaration) => shift_declaration(declaration, offset),
|
||||
}
|
||||
}
|
||||
|
||||
fn shift_declaration(declaration: &mut Decl, offset: usize) {
|
||||
shift_span(&mut declaration.span, offset);
|
||||
let DeclKind::Function(function) = &mut declaration.kind;
|
||||
shift_span(&mut function.span, offset);
|
||||
shift_span(&mut function.name.span, offset);
|
||||
shift_span(&mut function.return_type.span, offset);
|
||||
for parameter in &mut function.parameters {
|
||||
shift_span(&mut parameter.span, offset);
|
||||
shift_span(&mut parameter.name.span, offset);
|
||||
shift_span(&mut parameter.type_name.span, offset);
|
||||
}
|
||||
shift_span(&mut function.body.span, offset);
|
||||
for statement in &mut function.body.statements {
|
||||
shift_statement(statement, offset);
|
||||
}
|
||||
}
|
||||
|
||||
fn shift_expression(expression: &mut Expr, offset: usize) {
|
||||
shift_span(&mut expression.span, offset);
|
||||
match &mut expression.kind {
|
||||
ExprKind::Name(_) | ExprKind::Number(_) | ExprKind::String(_) => {}
|
||||
ExprKind::Symbol(name) => shift_span(&mut name.span, offset),
|
||||
ExprKind::Unary { operand, .. } => shift_expression(operand, offset),
|
||||
ExprKind::Binary { left, right, .. } => {
|
||||
shift_expression(left, offset);
|
||||
shift_expression(right, offset);
|
||||
}
|
||||
ExprKind::Member { object, name } => {
|
||||
shift_expression(object, offset);
|
||||
shift_span(&mut name.span, offset);
|
||||
}
|
||||
ExprKind::Call { callee, arguments } => {
|
||||
shift_expression(callee, offset);
|
||||
for argument in arguments {
|
||||
shift_expression(argument, offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user