use tatara_lisp::{Atom, Sexp, Spanned, SpannedForm};
use crate::lex::{lex, Span, Token, TokenKind};
#[derive(Clone, Debug, PartialEq)]
pub struct ParseError {
pub message: String,
pub span: Span,
pub line: usize,
pub col: usize,
}
impl ParseError {
fn new(message: impl Into<String>, span: Span) -> Self {
Self {
message: message.into(),
span,
line: 0,
col: 0,
}
}
fn located(mut self, src: &str) -> Self {
let (line, col) = Span::line_col(src, self.span.start.min(src.len()));
self.line = line;
self.col = col;
self
}
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}: {}", self.line, self.col, self.message)
}
}
impl std::error::Error for ParseError {}
impl From<crate::lex::LexError> for ParseError {
fn from(e: crate::lex::LexError) -> Self {
Self::new(e.message, e.span)
}
}
pub fn parse_program(src: &str) -> Result<Vec<Sexp>, ParseError> {
parse_program_with_depth(src, MAX_EXPR_DEPTH)
}
pub fn parse_program_tree(src: &str) -> Result<Vec<Spanned>, ParseError> {
parse_program_tree_with_depth(src, MAX_EXPR_DEPTH)
}
pub fn parse_program_tree_with_depth(
src: &str,
max_depth: usize,
) -> Result<Vec<Spanned>, ParseError> {
let toks: Vec<Token> = lex(src)
.map_err(|e| ParseError::from(e).located(src))?
.into_iter()
.filter(|t| !matches!(t.kind, TokenKind::Comment(_)))
.collect();
let mut p = Parser {
toks,
pos: 0,
depth: 0,
max_depth,
};
p.program().map_err(|e| e.located(src))
}
pub fn parse_program_in(src: &str, pack: &crate::yakugo::Yakugo) -> Result<Vec<Sexp>, ParseError> {
Ok(parse_program_tree_in(src, pack)?
.iter()
.map(Spanned::to_sexp)
.collect())
}
pub fn parse_program_tree_in(
src: &str,
pack: &crate::yakugo::Yakugo,
) -> Result<Vec<Spanned>, ParseError> {
let toks: Vec<Token> = crate::yakugo::canonical_tokens(src, pack)
.map_err(|e| ParseError::from(e).located(src))?
.into_iter()
.filter(|t| !matches!(t.kind, TokenKind::Comment(_)))
.collect();
let mut p = Parser {
toks,
pos: 0,
depth: 0,
max_depth: MAX_EXPR_DEPTH,
};
p.program().map_err(|e| e.located(src))
}
pub fn parse_program_with_depth(src: &str, max_depth: usize) -> Result<Vec<Sexp>, ParseError> {
Ok(parse_program_tree_with_depth(src, max_depth)?
.iter()
.map(Spanned::to_sexp)
.collect())
}
pub fn parse_program_spanned(src: &str) -> Result<Vec<(Sexp, Span)>, ParseError> {
parse_program_spanned_with_depth(src, MAX_EXPR_DEPTH)
}
pub fn parse_program_spanned_with_depth(
src: &str,
max_depth: usize,
) -> Result<Vec<(Sexp, Span)>, ParseError> {
Ok(parse_program_tree_with_depth(src, max_depth)?
.iter()
.map(|f| (f.to_sexp(), f.span))
.collect())
}
pub fn comments(src: &str) -> Vec<Comment> {
lex(src)
.map(|toks| {
toks.iter()
.filter_map(|t| match &t.kind {
TokenKind::Comment(text) => Some(Comment {
text: text.clone(),
span: t.span,
own_line: line_before_is_blank(src, t.span.start),
}),
_ => None,
})
.collect()
})
.unwrap_or_default()
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Comment {
pub text: String,
pub span: Span,
pub own_line: bool,
}
fn line_before_is_blank(src: &str, start: usize) -> bool {
src[..start.min(src.len())]
.rsplit('\n')
.next()
.is_some_and(|prefix| prefix.trim().is_empty())
}
pub fn parse_expr(src: &str) -> Result<Sexp, ParseError> {
let forms = parse_program(src)?;
match forms.len() {
1 => Ok(forms.into_iter().next().expect("checked len")),
n => Err(ParseError::new(
format!("expected exactly one expression, found {n}"),
Span::new(0, src.len()),
)
.located(src)),
}
}
#[derive(Clone, Copy, Debug)]
pub struct Infix {
pub op: &'static str,
pub power: (u8, u8),
pub callee: &'static str,
pub doc: &'static str,
}
pub const INFIX: &[Infix] = &[
Infix {
op: "||",
power: (1, 2),
callee: "or",
doc: "Logical or: the left value if truthy, otherwise the right; the right is evaluated only when needed.",
},
Infix {
op: "&&",
power: (3, 4),
callee: "and",
doc: "Logical and: the right value if the left is truthy, otherwise the left; short-circuits.",
},
Infix {
op: "==",
power: (5, 6),
callee: "equal?",
doc: "Structural equality over every value (okite D0001): 4 == 4.0, lists and maps by content, nil only equals nil.",
},
Infix {
op: "!=",
power: (5, 6),
callee: "not=",
doc: "Exactly not(a == b) (okite D0002).",
},
Infix {
op: "<",
power: (5, 6),
callee: "<",
doc: "Numeric less-than. A string operand is a type error: order text with compare(a, b).",
},
Infix {
op: "<=",
power: (5, 6),
callee: "<=",
doc: "Numeric less-than-or-equal. Numbers only, like <.",
},
Infix {
op: ">",
power: (5, 6),
callee: ">",
doc: "Numeric greater-than. Numbers only, like <.",
},
Infix {
op: ">=",
power: (5, 6),
callee: ">=",
doc: "Numeric greater-than-or-equal. Numbers only, like <.",
},
Infix {
op: "+",
power: (7, 8),
callee: "+",
doc: "Numeric addition; an Int and a Float give a Float. Not string concatenation: interpolate instead.",
},
Infix {
op: "-",
power: (7, 8),
callee: "-",
doc: "Numeric subtraction. Written before an operand with nothing on its left, it is unary minus: -n.",
},
Infix {
op: "*",
power: (9, 10),
callee: "*",
doc: "Numeric multiplication.",
},
Infix {
op: "/",
power: (9, 10),
callee: "/",
doc: "Division that never truncates (okite D0008): 7 / 2 is 3.5, while 6 / 2 stays the Int 3. Integer division is floor(a / b).",
},
Infix {
op: "%",
power: (9, 10),
callee: "mod",
doc: "Euclidean remainder: never negative, so (0 - 7) % 3 is 2, not -1. A zero divisor is a runtime error.",
},
];
pub const LOWERED_ASSERT: &str = "blue-assert";
pub const LOWERED_MAP: &str = "hash-map";
pub const LOWERED_CONCAT: &str = "concat";
pub const SURFACE_KEYWORDS: &[&str] = &[
"if",
"unless",
"def",
"defmacro",
"quote",
"unquote",
"unquote_splice",
"test",
"assert",
"fn",
"case",
];
pub const BLOCK_KEYWORDS: &[&str] = &["do", "end", "else", "elsif", "true", "false", "nil"];
pub fn is_reserved_word(name: &str) -> bool {
SURFACE_KEYWORDS.contains(&name) || BLOCK_KEYWORDS.contains(&name)
}
pub fn is_callable_name(name: &str) -> bool {
if qualified(name).is_some() {
return true;
}
!is_reserved_word(name)
&& matches!(
lex(name).as_deref(),
Ok([t, e]) if matches!(&t.kind, TokenKind::Ident(n) if n == name)
&& e.kind == TokenKind::Eof
)
}
pub const QUALIFIER: char = '/';
#[must_use]
pub fn qualify(pkg: &str, name: &str) -> String {
format!("{pkg}{QUALIFIER}{name}")
}
#[must_use]
pub fn qualified(sym: &str) -> Option<(&str, &str)> {
let (pkg, name) = sym.split_once(QUALIFIER)?;
let ident = |s: &str| {
matches!(
lex(s).as_deref(),
Ok([t, e]) if matches!(&t.kind, TokenKind::Ident(n) if n == s) && e.kind == TokenKind::Eof
)
};
let named = |s: &str| ident(s) || matches!(s, "true" | "false" | "nil");
(ident(pkg) && !is_reserved_word(pkg) && named(name)).then_some((pkg, name))
}
#[must_use]
pub fn keyword_doc(word: &str) -> &'static str {
match word {
"if" => "`if c … elsif d … else … end`: an expression; its value is the branch taken, nil when none is.",
"unless" => "`unless c … end`: `if !c`. The formatter writes it as `if !c`.",
"def" => "`def name(a, b) … end`: a named function; its value is the last expression. Parameters may be typed: `def f(n: Int) -> Int`.",
"defmacro" => "`defmacro name(x) quote … unquote(x) … end end`: a macro, a rewrite of the unevaluated form.",
"quote" => "`quote … end`: the form itself as data, not its value. Used inside `defmacro`.",
"unquote" => "`unquote(x)`: inside `quote`, splice in the value of `x`.",
"unquote_splice" => "`unquote_splice(xs)`: inside `quote`, splice the items of the form `xs` in place, not the form itself.",
"test" => "`test \"what must hold\" … end`: a test block, run by `blue test` and ignored by `blue run`.",
"assert" => "`assert expr`: inside a test, fail it unless `expr` is truthy; the failure quotes the expression.",
"fn" => "`fn(x) x * 2 end`: an anonymous function, a value. Blue has no brace or do blocks.",
"case" => "`case v when 1 … when 2 … else … end`: compares with `==`; it does not destructure.",
"do" => "Reserved and refused: blue has no do-blocks. Pass `fn(x) … end` instead.",
"end" => "Closes `def`, `defmacro`, `if`, `case`, `fn`, `test` and `quote`.",
"else" => "The branch of `if` or `case` taken when nothing before it matched.",
"elsif" => "A further condition in an `if` chain; the formatter writes nested `else if` as `elsif`.",
"true" => "The boolean true.",
"false" => "The boolean false. Only `false` and `nil` are falsy; 0, \"\" and [] are truthy.",
"nil" => "The absent value. It is not `[]` (okite D0004), and only `nil == nil`.",
_ => "",
}
}
fn infix(op: &str) -> Option<&'static Infix> {
INFIX.iter().find(|i| i.op == op)
}
const PIPE_POWER: (u8, u8) = (0, 1);
struct Parser {
toks: Vec<Token>,
pos: usize,
depth: usize,
max_depth: usize,
}
pub const MAX_EXPR_DEPTH: usize = 256;
type Fail = Box<ParseError>;
fn fail(message: impl Into<String>, span: Span) -> Fail {
Box::new(ParseError::new(message, span))
}
impl Parser {
fn peek(&self) -> &TokenKind {
&self.toks[self.pos.min(self.toks.len() - 1)].kind
}
fn peek_span(&self) -> Span {
self.toks[self.pos.min(self.toks.len() - 1)].span
}
fn bump(&mut self) -> TokenKind {
let k = self.toks[self.pos.min(self.toks.len() - 1)].kind.clone();
if self.pos < self.toks.len() {
self.pos += 1;
}
k
}
fn at(&self, k: &TokenKind) -> bool {
self.peek() == k
}
fn eat(&mut self, k: &TokenKind) -> bool {
if self.at(k) {
self.bump();
true
} else {
false
}
}
fn expect(&mut self, k: &TokenKind, what: &str) -> Result<(), Fail> {
if self.eat(k) {
Ok(())
} else {
Err(self.error(format!("expected {what}, found {}", self.peek())))
}
}
fn error(&self, message: impl Into<String>) -> Fail {
fail(message, self.peek_span())
}
fn skip_newlines(&mut self) {
while matches!(self.peek(), TokenKind::Newline) {
self.bump();
}
}
fn at_ident(&self, name: &str) -> bool {
matches!(self.peek(), TokenKind::Ident(n) if n == name)
}
fn mark(&self) -> usize {
self.peek_span().start
}
fn span_since(&self, start: usize) -> Span {
let end = self
.toks
.get(self.pos.saturating_sub(1))
.map_or(start, |t| t.span.end);
Span::new(start, end.max(start))
}
fn program(&mut self) -> Result<Vec<Spanned>, Fail> {
let mut out = Vec::new();
loop {
self.skip_newlines();
if matches!(self.peek(), TokenKind::Eof) {
break;
}
out.push(self.statement()?);
self.end_of_statement(&[])?;
}
Ok(out)
}
fn end_of_statement(&self, terminators: &[&str]) -> Result<(), Fail> {
let next = self.peek();
if matches!(next, TokenKind::Newline | TokenKind::Eof)
|| terminators.iter().any(|t| self.at_ident(t))
{
return Ok(());
}
let prev = self.toks.get(self.pos.wrapping_sub(1)).map(|t| &t.kind);
let prev_name = match prev {
Some(TokenKind::Ident(n)) => Some(n.as_str()),
_ => None,
};
let callee = prev_name.unwrap_or("f");
let why = match (prev, next) {
(Some(TokenKind::Ident(t)), _) if t == "then" => {
"blue's `if` has no `then`: put the branch on the next line".to_string()
}
(_, TokenKind::Ident(d)) if d == "do" => "blue has no `do` blocks: pass a function \
as an argument, `f(xs, fn(x) … end)`"
.to_string(),
(_, TokenKind::LBracket) => {
let index = match (self.toks.get(self.pos + 1), self.toks.get(self.pos + 2)) {
(Some(i), Some(close)) if close.kind == TokenKind::RBracket => match &i.kind {
TokenKind::Int(n) => n.to_string(),
TokenKind::Ident(n) => n.clone(),
_ => "i".to_string(),
},
_ => "i".to_string(),
};
let list = prev_name.unwrap_or("xs");
format!(
"blue has no `xs[i]` indexing: write `nth({index}, {list})` \
(or `first({list})`, `last({list})`)"
)
}
(_, TokenKind::LBrace) => "blue has no brace blocks: pass a function as an \
argument, `map(xs, fn(x) … end)`"
.to_string(),
(
Some(TokenKind::Ident(_)),
TokenKind::Ident(_)
| TokenKind::Int(_)
| TokenKind::Float(_)
| TokenKind::Str(_)
| TokenKind::InterpolatedStr { .. }
| TokenKind::Sym(_)
| TokenKind::True
| TokenKind::False
| TokenKind::Nil,
) => {
let arg = match next {
TokenKind::Ident(a) => a.clone(),
_ => "x".to_string(),
};
format!("a call needs parentheses: write `{callee}({arg})`")
}
_ => "put each statement on its own line".to_string(),
};
Err(self.error(format!("expected the end of the line, found {next}: {why}")))
}
fn statement(&mut self) -> Result<Spanned, Fail> {
let max = self.max_depth;
if self.depth >= max {
return Err(self.error(format!(
"statement nests deeper than {max}; refusing to \
recurse further (this is a limit, not a syntax error)"
)));
}
self.depth += 1;
let r = self.statement_inner();
self.depth -= 1;
r
}
fn statement_inner(&mut self) -> Result<Spanned, Fail> {
if let TokenKind::Ident(name) = self.peek().clone() {
if self.peek_at(1) == "=" && !is_reserved_word(&name) {
let name_span = self.peek_span();
self.bump(); self.bump(); self.skip_newlines();
let value = self.expr(0)?;
return Ok(list_at(
self.span_since(name_span.start),
vec![sym_at(name_span, "define"), sym_at(name_span, &name), value],
));
}
}
self.expr(0)
}
fn peek_at(&self, n: usize) -> String {
match self.toks.get(self.pos + n).map(|t| &t.kind) {
Some(TokenKind::Op(o)) => o.clone(),
_ => String::new(),
}
}
fn expr(&mut self, min_bp: u8) -> Result<Spanned, Fail> {
let max = self.max_depth;
if self.depth >= max {
return Err(self.error(format!(
"expression nests deeper than {max}; refusing to \
recurse further (this is a limit, not a syntax error)"
)));
}
self.depth += 1;
let r = self.expr_inner(min_bp);
self.depth -= 1;
r
}
fn expr_inner(&mut self, min_bp: u8) -> Result<Spanned, Fail> {
let start = self.mark();
let mut lhs = self.prefix()?;
loop {
match self.peek() {
TokenKind::Dot => {
self.bump();
lhs = self.finish_send(start, lhs)?;
continue;
}
TokenKind::LParen => {
let args = self.paren_args()?;
let mut list = vec![lhs];
list.extend(args);
lhs = list_at(self.span_since(start), list);
continue;
}
_ => {}
}
let op_span = self.peek_span();
let (callee, (lbp, rbp)) = match self.peek() {
TokenKind::Pipe => (None, PIPE_POWER),
TokenKind::Op(o) => match infix(o) {
Some(i) => (Some(i.callee), i.power),
None => break,
},
_ => break,
};
if lbp < min_bp {
break;
}
self.bump();
self.skip_newlines();
let rhs = self.expr(rbp)?;
lhs = if callee.is_none() {
let rhs_span = rhs.span;
match rhs.form {
SpannedForm::List(mut items) if !items.is_empty() => {
items.insert(1, lhs);
list_at(self.span_since(start), items)
}
callee => list_at(
self.span_since(start),
vec![Spanned::new(rhs_span, callee), lhs],
),
}
} else {
list_at(
self.span_since(start),
vec![sym_at(op_span, callee.unwrap()), lhs, rhs],
)
};
}
Ok(lhs)
}
fn prefix(&mut self) -> Result<Spanned, Fail> {
let span = self.peek_span();
match self.bump() {
TokenKind::Int(v) => Ok(atom_at(span, Atom::Int(v))),
TokenKind::Float(v) => Ok(atom_at(span, Atom::Float(v))),
TokenKind::Str(s) => Ok(atom_at(span, Atom::Str(s))),
TokenKind::InterpolatedStr { parts, exprs } => {
let mut acc = atom_at(span, Atom::Str(parts[0].clone()));
for (i, raw) in exprs.iter().enumerate() {
let inner = parse_expr(raw).map_err(|e| {
fail(
format!("in interpolation `#{{{raw}}}`: {}", e.message),
span,
)
})?;
let inner = Spanned::from_sexp_at(&inner, span);
acc = list_at(span, vec![sym_at(span, LOWERED_CONCAT), acc, inner]);
acc = list_at(
span,
vec![
sym_at(span, LOWERED_CONCAT),
acc,
atom_at(span, Atom::Str(parts[i + 1].clone())),
],
);
}
Ok(acc)
}
TokenKind::Sym(s) => Ok(atom_at(span, Atom::Keyword(s))),
TokenKind::True => Ok(atom_at(span, Atom::Bool(true))),
TokenKind::False => Ok(atom_at(span, Atom::Bool(false))),
TokenKind::Nil => Ok(Spanned::new(span, SpannedForm::Nil)),
TokenKind::Op(o) if o == "-" => {
let rhs = self.expr(11)?; Ok(list_at(
self.span_since(span.start),
vec![sym_at(span, "-"), atom_at(span, Atom::Int(0)), rhs],
))
}
TokenKind::Op(o) if o == "!" => {
let rhs = self.expr(11)?;
Ok(list_at(
self.span_since(span.start),
vec![sym_at(span, "not"), rhs],
))
}
TokenKind::LParen => {
self.skip_newlines();
let inner = self.expr(0)?;
self.skip_newlines();
self.expect(&TokenKind::RParen, "`)`")?;
Ok(inner)
}
TokenKind::LBracket => self.list_literal(span),
TokenKind::LBrace => self.map_literal(span),
TokenKind::Ident(name) if self.at(&TokenKind::PathSep) => {
self.qualified_name(span, &name)
}
TokenKind::PathSep => Err(fail(
"a qualified name is `package::name`, with the package written before `::`",
span,
)),
TokenKind::Ident(name) => match name.as_str() {
"if" => self.if_form(span, false),
"unless" => self.if_form(span, true),
"def" => self.def_form(span),
"defmacro" => self.defmacro_form(span),
"case" => self.case_form(span),
"fn" => self.lambda_form(span),
"test" => self.test_form(span),
"assert" => self.assert_form(span),
"quote" => self.quote_form(span),
"unquote" => self.unquote_form(span, false),
"unquote_splice" => self.unquote_form(span, true),
"do" => Err(fail(
"blue has no `do` blocks: pass a function as an argument, \
`f(xs, fn(x) … end)`",
span,
)),
"end" => Err(fail(
"unexpected `end`: nothing open here for it to close",
span,
)),
_ => Ok(sym_at(span, &name)),
},
other => Err(fail(format!("expected an expression, found {other}"), span)),
}
}
fn qualified_name(&mut self, pkg_span: Span, pkg: &str) -> Result<Spanned, Fail> {
if is_reserved_word(pkg) {
return Err(fail(
format!("`{pkg}` is a reserved word and cannot name a package"),
pkg_span,
));
}
self.bump(); let name_span = self.peek_span();
let name = match self.bump() {
TokenKind::Ident(n) => n,
TokenKind::True => "true".to_string(),
TokenKind::False => "false".to_string(),
TokenKind::Nil => "nil".to_string(),
other => {
return Err(fail(
format!("expected a name after `{pkg}::`, found {other}"),
name_span,
))
}
};
if self.at(&TokenKind::PathSep) {
return Err(self.error(format!(
"a qualified name has one qualifier: `package::name`, and `{pkg}::{name}::…` has two"
)));
}
Ok(sym_at(
self.span_since(pkg_span.start),
&qualify(pkg, &name),
))
}
fn finish_send(&mut self, start: usize, recv: Spanned) -> Result<Spanned, Fail> {
let name_span = self.peek_span();
let name = match self.bump() {
TokenKind::Ident(n) if is_reserved_word(&n) => {
return Err(self.error(format!(
"`{n}` is a reserved word and cannot be a method name — \
`recv.{n}` would lower onto the `{n}` form itself"
)))
}
TokenKind::Ident(n) => n,
other => {
return Err(fail(
format!("expected a method name after `.`, found {other}"),
name_span,
))
}
};
let mut list = vec![sym_at(name_span, &name), recv];
if self.at(&TokenKind::LParen) {
list.extend(self.paren_args()?);
}
Ok(list_at(self.span_since(start), list))
}
fn paren_args(&mut self) -> Result<Vec<Spanned>, Fail> {
self.expect(&TokenKind::LParen, "`(`")?;
let mut args = Vec::new();
self.skip_newlines();
if self.eat(&TokenKind::RParen) {
return Ok(args);
}
loop {
self.skip_newlines();
args.push(self.expr(0)?);
self.skip_newlines();
if self.eat(&TokenKind::Comma) {
continue;
}
self.expect(&TokenKind::RParen, "`,` or `)`")?;
break;
}
Ok(args)
}
fn list_literal(&mut self, open: Span) -> Result<Spanned, Fail> {
let mut items = vec![sym_at(open, "list")];
self.skip_newlines();
if self.eat(&TokenKind::RBracket) {
return Ok(list_at(self.span_since(open.start), items));
}
loop {
self.skip_newlines();
items.push(self.expr(0)?);
self.skip_newlines();
if self.eat(&TokenKind::Comma) {
continue;
}
self.expect(&TokenKind::RBracket, "`,` or `]`")?;
break;
}
Ok(list_at(self.span_since(open.start), items))
}
fn map_literal(&mut self, open: Span) -> Result<Spanned, Fail> {
let mut items = vec![sym_at(open, LOWERED_MAP)];
self.skip_newlines();
if self.eat(&TokenKind::RBrace) {
return Ok(list_at(self.span_since(open.start), items));
}
loop {
self.skip_newlines();
match self.peek().clone() {
TokenKind::Label(name) => {
let label_span = self.peek_span();
self.bump();
self.skip_newlines();
items.push(atom_at(label_span, Atom::Keyword(name)));
items.push(self.expr(0)?);
}
_ => {
let k = self.expr(0)?;
self.skip_newlines();
self.expect(&TokenKind::Rocket, "`=>` in a map literal")?;
self.skip_newlines();
items.push(k);
items.push(self.expr(0)?);
}
}
self.skip_newlines();
if self.eat(&TokenKind::Comma) {
continue;
}
self.expect(&TokenKind::RBrace, "`,` or `}`")?;
break;
}
Ok(list_at(self.span_since(open.start), items))
}
fn if_chain(&mut self, head: Span) -> Result<Spanned, Fail> {
let cond = self.expr(0)?;
let then = self.body(&["elsif", "else", "end"])?;
let els = if self.at_ident("elsif") {
let elsif = self.peek_span();
self.bump();
Some(self.if_chain(elsif)?)
} else if self.at_ident("else") {
self.bump();
let e = self.body(&["end"])?;
self.expect_ident("end")?;
Some(e)
} else {
self.expect_ident("end")?;
None
};
let mut out = vec![sym_at(head, "if"), cond, then];
if let Some(e) = els {
out.push(e);
}
Ok(list_at(self.span_since(head.start), out))
}
fn if_form(&mut self, head: Span, negate: bool) -> Result<Spanned, Fail> {
let cond = self.expr(0)?;
let cond = if negate {
let inner = cond.span;
list_at(head.merge(inner), vec![sym_at(head, "not"), cond])
} else {
cond
};
let then = self.body(&["elsif", "else", "end"])?;
let els = if self.at_ident("elsif") {
let elsif = self.peek_span();
self.bump();
Some(self.if_chain(elsif)?)
} else if self.at_ident("else") {
self.bump();
let e = self.body(&["end"])?;
self.expect_ident("end")?;
Some(e)
} else {
self.expect_ident("end")?;
None
};
let mut out = vec![sym_at(head, "if"), cond, then];
if let Some(e) = els {
out.push(e);
}
Ok(list_at(self.span_since(head.start), out))
}
fn case_form(&mut self, head: Span) -> Result<Spanned, Fail> {
let subject = self.expr(0)?;
self.skip_newlines();
let subject_var = "case-subject";
let mut arms: Vec<Spanned> = Vec::new();
let mut otherwise: Option<Spanned> = None;
let mut else_kw: Option<Span> = None;
loop {
self.skip_newlines();
if self.at_ident("end") {
break;
}
let maybe_else = self.peek_span();
if self.eat_ident("else") {
else_kw = Some(maybe_else);
otherwise = Some(self.body(&["end"])?);
continue;
}
let when = self.peek_span();
if !self.eat_ident("when") {
return Err(self.error(format!(
"expected `when`, `else` or `end` in a case, found {}",
self.peek()
)));
}
self.skip_newlines();
let pattern = self.expr(0)?;
let body = self.body(&["when", "else", "end"])?;
let test = list_at(
when.merge(pattern.span),
vec![sym_at(when, "equal?"), sym_at(when, subject_var), pattern],
);
arms.push(list_at(when.merge(body.span), vec![test, body]));
}
self.expect_ident("end")?;
if arms.is_empty() && otherwise.is_none() {
return Err(self.error("a case needs at least one `when` or an `else`".to_string()));
}
let mut cond = vec![sym_at(head, "cond")];
cond.extend(arms);
let else_span = else_kw.unwrap_or(head);
let else_body = otherwise.unwrap_or_else(|| Spanned::new(else_span, SpannedForm::Nil));
cond.push(list_at(
else_span.merge(else_body.span),
vec![sym_at(else_span, "else"), else_body],
));
let whole = self.span_since(head.start);
let binding = list_at(
head.merge(subject.span),
vec![sym_at(head, subject_var), subject],
);
Ok(list_at(
whole,
vec![
sym_at(head, "let"),
list_at(binding.span, vec![binding]),
list_at(whole, cond),
],
))
}
fn lambda_form(&mut self, head: Span) -> Result<Spanned, Fail> {
let mut params: Vec<Spanned> = Vec::new();
if self.at(&TokenKind::LParen) {
self.bump();
self.skip_newlines();
if !self.eat(&TokenKind::RParen) {
loop {
self.skip_newlines();
let p_span = self.peek_span();
match self.bump() {
TokenKind::Ident(p) => params.push(sym_at(p_span, &p)),
other => {
return Err(fail(
format!("expected a parameter name, found {other}"),
p_span,
))
}
}
self.skip_newlines();
if self.eat(&TokenKind::Comma) {
continue;
}
self.expect(&TokenKind::RParen, "`,` or `)`")?;
break;
}
}
}
let params_span = cover(¶ms, head);
let body = self.body(&["end"])?;
self.expect_ident("end")?;
Ok(list_at(
self.span_since(head.start),
vec![sym_at(head, "lambda"), list_at(params_span, params), body],
))
}
fn test_form(&mut self, head: Span) -> Result<Spanned, Fail> {
let name_span = self.peek_span();
let name = match self.bump() {
TokenKind::Str(s) => s,
other => {
return Err(fail(
format!("expected a string name after `test`, found {other}"),
name_span,
))
}
};
let body = self.body(&["end"])?;
self.expect_ident("end")?;
Ok(list_at(
self.span_since(head.start),
vec![
sym_at(head, "deftest"),
atom_at(name_span, Atom::Str(name)),
body,
],
))
}
fn assert_form(&mut self, head: Span) -> Result<Spanned, Fail> {
let e = self.expr(0)?;
let quoted = Spanned::new(e.span, SpannedForm::Quote(Box::new(e.clone())));
Ok(list_at(
self.span_since(head.start),
vec![sym_at(head, LOWERED_ASSERT), quoted, e],
))
}
fn defmacro_form(&mut self, head: Span) -> Result<Spanned, Fail> {
let name_span = self.peek_span();
let name = match self.bump() {
TokenKind::Ident(n) => n,
other => {
return Err(fail(
format!("expected a name after `defmacro`, found {other}"),
name_span,
))
}
};
let mut params: Vec<Spanned> = Vec::new();
if self.at(&TokenKind::LParen) {
self.bump();
self.skip_newlines();
if !self.eat(&TokenKind::RParen) {
loop {
self.skip_newlines();
let p_span = self.peek_span();
match self.bump() {
TokenKind::Ident(p) => params.push(sym_at(p_span, &p)),
TokenKind::Label(p) => {
return Err(self.error(format!(
"macro parameter `{p}` cannot be typed: a macro receives \
source forms, not values"
)))
}
other => {
return Err(fail(
format!("expected a parameter name, found {other}"),
p_span,
))
}
}
self.skip_newlines();
if self.eat(&TokenKind::Comma) {
continue;
}
self.expect(&TokenKind::RParen, "`,` or `)`")?;
break;
}
}
}
if matches!(self.peek(), TokenKind::Op(o) if o == "->") {
return Err(self.error(
"a macro has no return type: it produces source forms, not values".to_string(),
));
}
let params_span = cover(¶ms, name_span);
let body = self.body(&["end"])?;
self.expect_ident("end")?;
Ok(list_at(
self.span_since(head.start),
vec![
sym_at(head, "defmacro"),
sym_at(name_span, &name),
list_at(params_span, params),
body,
],
))
}
fn quote_form(&mut self, head: Span) -> Result<Spanned, Fail> {
let body = self.body(&["end"])?;
self.expect_ident("end")?;
Ok(Spanned::new(
self.span_since(head.start),
SpannedForm::Quasiquote(Box::new(body)),
))
}
fn unquote_form(&mut self, head: Span, splice: bool) -> Result<Spanned, Fail> {
self.expect(&TokenKind::LParen, "`(` after unquote")?;
self.skip_newlines();
let inner = self.expr(0)?;
self.skip_newlines();
self.expect(&TokenKind::RParen, "`)`")?;
let span = self.span_since(head.start);
Ok(Spanned::new(
span,
if splice {
SpannedForm::UnquoteSplice(Box::new(inner))
} else {
SpannedForm::Unquote(Box::new(inner))
},
))
}
fn def_form(&mut self, head: Span) -> Result<Spanned, Fail> {
let name_span = self.peek_span();
let name = match self.bump() {
TokenKind::Ident(n) => n,
other => {
return Err(fail(
format!("expected a name after `def`, found {other}"),
name_span,
))
}
};
let mut params: Vec<(String, Span, Option<Spanned>)> = Vec::new();
if self.at(&TokenKind::LParen) {
self.bump();
self.skip_newlines();
if !self.eat(&TokenKind::RParen) {
loop {
self.skip_newlines();
let p_span = self.peek_span();
match self.bump() {
TokenKind::Ident(p) => params.push((p, p_span, None)),
TokenKind::Label(p) => {
self.skip_newlines();
let ty = self.type_expr()?;
params.push((p, p_span, Some(ty)));
}
other => {
return Err(fail(
format!("expected a parameter name, found {other}"),
p_span,
))
}
}
self.skip_newlines();
if self.eat(&TokenKind::Comma) {
continue;
}
self.expect(&TokenKind::RParen, "`,` or `)`")?;
break;
}
}
}
let sig_span = self.span_since(name_span.start);
let ret = if matches!(self.peek(), TokenKind::Op(o) if o == "->") {
self.bump();
self.skip_newlines();
Some(self.type_expr()?)
} else {
None
};
let body = self.body(&["end"])?;
self.expect_ident("end")?;
let whole = self.span_since(head.start);
let annotated = ret.is_some() || params.iter().any(|(_, _, t)| t.is_some());
if !annotated {
let mut sig = vec![sym_at(name_span, &name)];
sig.extend(params.into_iter().map(|(p, s, _)| sym_at(s, &p)));
return Ok(list_at(
whole,
vec![sym_at(head, "define"), list_at(sig_span, sig), body],
));
}
let mut sig = vec![sym_at(name_span, &name)];
for (p, s, t) in params {
let ty = t.unwrap_or_else(|| sym_at(s, "dyn"));
sig.push(list_at(s.merge(ty.span), vec![sym_at(s, &p), ty]));
}
Ok(list_at(
whole,
vec![
sym_at(head, "define-typed"),
list_at(sig_span, sig),
ret.unwrap_or_else(|| sym_at(head, "dyn")),
body,
],
))
}
fn type_expr(&mut self) -> Result<Spanned, Fail> {
let name_span = self.peek_span();
let name = match self.bump() {
TokenKind::Ident(n) => n,
other => {
return Err(fail(
format!("expected a type name, found {other}"),
name_span,
))
}
};
if self.at(&TokenKind::LParen) {
let args = self.paren_args()?;
let mut list = vec![sym_at(name_span, &name)];
list.extend(args);
return Ok(list_at(self.span_since(name_span.start), list));
}
Ok(sym_at(name_span, &name))
}
fn eat_ident(&mut self, name: &str) -> bool {
if self.at_ident(name) {
self.bump();
true
} else {
false
}
}
fn expect_ident(&mut self, name: &str) -> Result<(), Fail> {
if self.at_ident(name) {
self.bump();
Ok(())
} else {
Err(self.error(format!("expected `{name}`, found {}", self.peek())))
}
}
fn body(&mut self, terminators: &[&str]) -> Result<Spanned, Fail> {
let start = self.mark();
let mut forms = Vec::new();
loop {
self.skip_newlines();
if matches!(self.peek(), TokenKind::Eof) {
return Err(self.error(format!(
"unterminated block: expected {} before the end of the file",
one_of(terminators)
)));
}
if terminators.iter().any(|t| self.at_ident(t)) {
break;
}
forms.push(self.statement()?);
self.end_of_statement(terminators)?;
}
Ok(match forms.len() {
0 => Spanned::new(Span::new(start, start), SpannedForm::Nil),
1 => forms.into_iter().next().expect("checked len"),
_ => {
let span = cover(&forms, Span::new(start, start));
let head = Span::new(span.start, span.start);
let mut list = vec![sym_at(head, "begin")];
list.extend(forms);
list_at(span, list)
}
})
}
}
fn one_of(words: &[&str]) -> String {
let quoted: Vec<String> = words.iter().map(|w| format!("`{w}`")).collect();
match quoted.split_last() {
None => String::new(),
Some((last, [])) => last.clone(),
Some((last, rest)) => format!("{} or {last}", rest.join(", ")),
}
}
fn sym_at(span: Span, s: &str) -> Spanned {
Spanned::new(span, SpannedForm::Atom(Atom::Symbol(s.to_string())))
}
fn atom_at(span: Span, a: Atom) -> Spanned {
Spanned::new(span, SpannedForm::Atom(a))
}
fn list_at(span: Span, items: Vec<Spanned>) -> Spanned {
Spanned::new(span, SpannedForm::List(items))
}
fn cover(items: &[Spanned], fallback: Span) -> Span {
let merged = items
.iter()
.fold(Span::synthetic(), |acc, s| acc.merge(s.span));
if merged.is_synthetic() {
fallback
} else {
merged
}
}
#[cfg(test)]
mod tests {
use super::*;
fn q(src: &str) -> String {
parse_expr(src)
.map(|s| s.to_string())
.unwrap_or_else(|e| panic!("{src:?}: {e}"))
}
#[test]
fn arithmetic_respects_precedence() {
assert_eq!(q("1 + 2 * 3"), "(+ 1 (* 2 3))");
assert_eq!(q("(1 + 2) * 3"), "(* (+ 1 2) 3)");
}
#[test]
fn comparison_binds_looser_than_arithmetic() {
assert_eq!(q("a + 1 < b"), "(< (+ a 1) b)");
}
#[test]
fn logical_operators_bind_loosest_and_lower_to_tataras_names() {
assert_eq!(q("a && b || c"), "(or (and a b) c)");
}
#[test]
fn left_associativity() {
assert_eq!(q("1 - 2 - 3"), "(- (- 1 2) 3)");
}
#[test]
fn method_call_without_parens_is_a_send() {
assert_eq!(q("user.name"), "(name user)");
}
#[test]
fn method_call_with_args() {
assert_eq!(q("user.greet(1, 2)"), "(greet user 1 2)");
}
#[test]
fn chained_sends_read_left_to_right() {
assert_eq!(q("a.b.c"), "(c (b a))");
}
#[test]
fn plain_call() {
assert_eq!(q("f(1, 2)"), "(f 1 2)");
}
#[test]
fn pipeline_threads_into_first_argument() {
assert_eq!(q("x |> f"), "(f x)");
assert_eq!(q("x |> f(1)"), "(f x 1)");
assert_eq!(q("x |> f |> g"), "(g (f x))");
}
#[test]
fn pipeline_binds_looser_than_arithmetic() {
assert_eq!(q("1 + 2 |> f"), "(f (+ 1 2))");
}
#[test]
fn label_and_rocket_produce_the_same_tree_for_a_symbol_key() {
assert_eq!(q("{a: 1}"), q("{:a => 1}"));
assert_eq!(q("{a: 1}"), "(hash-map :a 1)");
}
#[test]
fn a_string_key_has_no_shorthand() {
assert_eq!(q(r#"{"k" => 1}"#), r#"(hash-map "k" 1)"#);
}
#[test]
fn list_literal() {
assert_eq!(q("[1, 2, 3]"), "(list 1 2 3)");
assert_eq!(q("[]"), "(list)");
}
#[test]
fn if_else_end() {
assert_eq!(q("if a\n 1\nelse\n 2\nend"), "(if a 1 2)");
}
#[test]
fn if_without_else() {
assert_eq!(q("if a\n 1\nend"), "(if a 1)");
}
#[test]
fn unless_is_a_negated_if() {
assert_eq!(q("unless a\n 1\nend"), "(if (not a) 1)");
}
#[test]
fn multi_statement_body_becomes_begin() {
assert_eq!(q("if a\n 1\n 2\nend"), "(if a (begin 1 2))");
}
#[test]
fn def_lowers_to_define() {
assert_eq!(
q("def add(a, b)\n a + b\nend"),
"(define (add a b) (+ a b))"
);
}
#[test]
fn def_with_no_params() {
assert_eq!(q("def zero()\n 0\nend"), "(define (zero) 0)");
}
#[test]
fn literals_lower_to_atoms() {
assert_eq!(q("42"), "42");
assert_eq!(q("true"), "#t");
assert_eq!(q(":ok"), ":ok");
assert_eq!(q(r#""hi""#), r#""hi""#);
}
#[test]
fn unary_minus_and_not() {
assert_eq!(q("-x"), "(- 0 x)");
assert_eq!(q("!x"), "(not x)");
}
#[test]
fn a_program_is_a_sequence_of_forms() {
let forms = parse_program("def f()\n 1\nend\nf()").expect("parse");
assert_eq!(forms.len(), 2);
assert_eq!(forms[1].to_string(), "(f)");
}
#[test]
fn unterminated_block_is_an_error_naming_what_was_expected() {
let e = parse_program("if a\n 1").expect_err("must fail");
assert!(e.message.contains("unterminated"), "{}", e.message);
}
#[test]
fn a_parse_error_carries_a_span_into_the_source() {
let src = "1 + )";
let e = parse_program(src).expect_err("must fail");
assert!(e.span.start < src.len(), "span {:?} outside source", e.span);
}
#[test]
fn postfix_indexing_is_refused_naming_nth() {
let e = parse_program("y = xs[0]").expect_err("must not split into two statements");
assert!(e.message.contains("`nth(0, xs)`"), "{}", e.message);
assert_eq!((e.line, e.col), (1, 7), "{e}");
}
#[test]
fn a_call_without_parentheses_is_refused_naming_the_call() {
let e = parse_program("a = f x").expect_err("must not split into two statements");
assert!(e.message.contains("`f(x)`"), "{}", e.message);
assert_eq!((e.line, e.col), (1, 7), "{e}");
}
#[test]
fn two_statements_on_one_line_are_refused_in_a_body() {
for src in [
"def f(xs)\n xs[0]\nend",
"if a\n f x\nend",
"fn(x) x 1 end",
"test \"t\"\n assert a b\nend",
"1 2",
] {
assert!(parse_program(src).is_err(), "{src:?} must not parse");
}
}
#[test]
fn a_statement_may_end_at_a_newline_the_end_or_a_closing_keyword() {
for src in [
"fn(x) x + 1 end",
"map(fn(x) x end, xs)",
"if a\n 1\nelse\n 2\nend",
"if a 1 else 2 end",
"case x\nwhen 1 :a\nwhen 2 :b\nelse :c\nend",
"x = 1\ny = 2",
"x = 1",
] {
assert!(
parse_program(src).is_ok(),
"{src:?}: {:?}",
parse_program(src)
);
}
}
#[test]
fn then_is_refused_by_name() {
let e = parse_program("x = if c then 1 else 2 end").expect_err("then is not a keyword");
assert!(e.message.contains("`then`"), "{}", e.message);
}
#[test]
fn a_brace_block_is_refused_naming_fn() {
let e = parse_program("xs.map { |x| x }").expect_err("no brace blocks");
assert!(e.message.contains("fn(x)"), "{}", e.message);
}
#[test]
fn a_qualified_name_is_one_symbol() {
assert_eq!(q("retsu::first(xs)"), "(retsu/first xs)");
assert_eq!(q("retsu::first"), "retsu/first");
assert_eq!(q("xs |> retsu::first"), "(retsu/first xs)");
assert_eq!(q("blue::length(xs)"), "(blue/length xs)");
}
#[test]
fn whitespace_around_the_qualifier_is_accepted() {
assert_eq!(q("retsu :: first(xs)"), "(retsu/first xs)");
}
#[test]
fn a_reserved_word_after_the_qualifier_is_a_name() {
assert_eq!(q("kueri::if(a, b)"), "(kueri/if a b)");
assert_eq!(q("heni::test(x)"), "(heni/test x)");
assert_eq!(q("sabi::fn"), "sabi/fn");
}
#[test]
fn a_qualified_name_has_exactly_one_qualifier() {
for (src, says) in [
("::first(xs)", "`package::name`"),
("a::b::c", "one qualifier"),
("if::x", "reserved word"),
("retsu::1", "expected a name after `retsu::`"),
] {
let e = parse_program(src).expect_err(src);
assert!(e.message.contains(says), "{src}: {}", e.message);
}
}
#[test]
fn qualified_reads_only_the_package_name_shape() {
assert_eq!(qualified("retsu/first"), Some(("retsu", "first")));
assert_eq!(qualified("kueri/if"), Some(("kueri", "if")));
assert_eq!(qualified("nisshi/pair?"), Some(("nisshi", "pair?")));
for not in ["/", "a/", "/a", "a/b/c", "if/x", "hash-map", "a"] {
assert_eq!(qualified(not), None, "{not}");
}
assert!(is_callable_name("kueri/if"));
}
#[test]
fn a_symbol_may_end_in_a_question_or_bang() {
assert_eq!(q("[:pair?, :set!]"), "(list :pair? :set!)");
assert_eq!(q("use(\"n\", [:empty?])"), "(use \"n\" (list :empty?))");
assert_eq!(q(":a != :b"), "(not= :a :b)");
}
#[test]
fn a_parse_error_displays_line_col_and_the_source_text() {
let e = parse_program("x = 1\ny = [1, 2 y]").expect_err("must fail");
assert_eq!(e.to_string(), "2:11: expected `,` or `]`, found `y`");
assert_eq!(e.span, Span::new(16, 17), "the span stays, as data");
}
#[test]
fn no_error_message_names_a_rust_variant() {
for src in [
"f(1 2)",
"{a: 1 b}",
"def 1\nend",
"x.1",
"if a\n",
"fn(1) 2 end",
"[1 :b]",
"1 +",
"(1",
] {
let e = parse_program(src).expect_err(src);
let shown = e.to_string();
for tell in [
"Ident(", "Int(", "Op(", "Label(", "Sym(", "Str(", "LParen", "RBracket", "Eof",
"Newline", "..",
] {
assert!(!shown.contains(tell), "{src:?} -> {shown}");
}
assert!(e.line >= 1 && e.col >= 1, "{src:?} not located: {shown}");
}
}
#[test]
fn a_lex_error_is_located_too() {
let e = parse_program("x = 1\ny = 2; z = 3").expect_err("no semicolons");
assert_eq!((e.line, e.col), (2, 6), "{e}");
assert!(e.message.contains("new line"), "{e}");
}
#[test]
fn the_parser_rejects_garbage() {
assert!(parse_program("def").is_err());
assert!(parse_program("(1").is_err());
assert!(parse_program("end").is_err());
}
}