use crate::compile::repl::{DeclEntry, REPL_MODULE};
use crate::diag::{DiagArg, DiagCode, Diagnostic, Severity};
use crate::syntax::ast::{Expr, Pos, STATEMENT_METHODS, Stmt};
use crate::syntax::expr::first_word;
use crate::syntax::parser::{IdentMap, parse_expr_line, parse_repl_body, parse_with};
#[derive(Debug, Clone)]
pub(super) enum Parsed {
Empty,
Command,
Expr(Expr),
Stmts(Vec<Stmt>),
Decls(Vec<DeclEntry>),
}
fn diag(line: u32, col: u32, code: DiagCode, what: &str) -> Diagnostic {
Diagnostic {
module: REPL_MODULE.to_string(),
line,
col,
severity: Severity::Error,
code,
args: vec![("what", DiagArg::Str(what.to_string()))],
}
}
pub(super) fn not_allowed(pos: Pos, what: &str) -> Diagnostic {
diag(pos.line, pos.col, DiagCode::ReplNotAllowed, what)
}
pub(super) fn classify(
text: &str,
idents: &IdentMap,
scan_ast: bool,
) -> Result<Parsed, Vec<Diagnostic>> {
let lines: Vec<&str> = text
.split('\n')
.map(|l| l.strip_suffix('\r').unwrap_or(l))
.collect();
let blank = |l: &str| l.trim().is_empty() || l.trim_start().starts_with("//");
let Some(fi) = lines.iter().position(|l| !blank(l)) else {
return Ok(Parsed::Empty);
};
let first = lines[fi];
let indent = first.len() - first.trim_start().len();
let t = &first[indent..];
if t.starts_with(':') {
return Ok(Parsed::Command);
}
let last = lines
.iter()
.rposition(|l| !l.trim().is_empty())
.unwrap_or(fi);
let single = lines[fi..=last].iter().skip(1).all(|l| l.trim().is_empty());
if single {
let line_text = first.trim_end();
if let Ok(e) = parse_expr_line(line_text, REPL_MODULE, fi as u32 + 1) {
let stmt = match &e {
Expr::Command(_) | Expr::MisplacedCommand(_) | Expr::LabelCall(_) => true,
Expr::Method(_, name, _, _) | Expr::QueryCall(_, name, _, _) => {
STATEMENT_METHODS.contains(&name.as_str())
}
_ => false,
};
if !stmt {
return Ok(Parsed::Expr(e));
}
}
}
let word = first_word(t);
let pos = Pos {
line: fi as u32 + 1,
col: first[..indent].chars().count() as u32 + 1,
};
match word {
"save" | "persistent" | "label" => Err(vec![not_allowed(pos, word)]),
"fn" | "struct" | "enum" | "import" | "identity" => {
let (m, diags) = parse_with(text, REPL_MODULE, idents);
if !diags.is_empty() {
return Err(diags);
}
let mut extra = Vec::new();
for v in &m.vars {
extra.push(not_allowed(
v.pos,
if v.persistent { "persistent" } else { "save" },
));
}
for l in &m.labels {
extra.push(not_allowed(l.pos, "label"));
}
if !extra.is_empty() {
extra.sort_by_key(|d| (d.line, d.col));
return Err(extra);
}
let mut out: Vec<DeclEntry> = Vec::new();
out.extend(m.imports.into_iter().map(DeclEntry::Import));
out.extend(m.identities.into_iter().map(DeclEntry::Identity));
out.extend(m.types.into_iter().map(DeclEntry::Type));
out.extend(m.fns.into_iter().map(DeclEntry::Fn));
out.sort_by_key(|d| (d.pos().line, d.pos().col));
Ok(Parsed::Decls(out))
}
_ => {
let (body, diags) = parse_repl_body(text, REPL_MODULE, idents);
if !diags.is_empty() {
return Err(diags);
}
if scan_ast {
let mut found = Vec::new();
scan(&body, &mut found);
if !found.is_empty() {
found.sort_by_key(|(p, _)| (p.line, p.col));
return Err(found.into_iter().map(|(p, w)| not_allowed(p, w)).collect());
}
}
Ok(Parsed::Stmts(body))
}
}
}
fn scan(body: &[Stmt], out: &mut Vec<(Pos, &'static str)>) {
for st in body {
match st {
Stmt::Jump { pos, .. } => out.push((*pos, "jump")),
Stmt::End(pos) => out.push((*pos, "end")),
Stmt::Return { pos, .. } => out.push((*pos, "return")),
Stmt::Plain { id: Some(id), .. } | Stmt::Quote { id: Some(id), .. } => {
out.push((id.pos, "stable_id"));
}
Stmt::Command(call) => {
if let Some(id) = &call.id {
out.push((id.pos, "stable_id"));
}
}
Stmt::Let { init: value, .. } | Stmt::Assign { value, .. } => {
if let Expr::Command(call) = value
&& let Some(id) = &call.id
{
out.push((id.pos, "stable_id"));
}
}
Stmt::If {
arms, else_body, ..
} => {
for (_, b) in arms {
scan(b, out);
}
scan(else_body, out);
}
Stmt::While { body, .. } | Stmt::For { body, .. } => scan(body, out),
Stmt::Match { arms, .. } => {
for a in arms {
scan(&a.body, out);
}
}
Stmt::Choice { options, .. } => {
for o in options {
if let Some(p) = o.once {
out.push((p, "once"));
}
if let Some(id) = &o.id {
out.push((id.pos, "stable_id"));
}
scan(&o.body, out);
}
}
_ => {}
}
}
}