mod layout;
mod precedence;
use crate::compiler::ast::{Builtin, NixExpr, NixKind};
use crate::ir::Origin;
use layout::{Doc, attributed, concat, group, hard_line, line, nest, text};
use precedence::{Context, precedence};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SourceSpan {
pub start: usize,
pub end: usize,
pub origin: Origin,
#[serde(default)]
pub enclosing: Vec<Origin>,
#[serde(default)]
pub diagnostic_site: bool,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Generated {
pub source: String,
pub spans: Vec<SourceSpan>,
#[doc(hidden)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub backend_metadata: Option<serde_json::Value>,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct RenderOptions {
pub origin_comments: bool,
}
impl Generated {
pub fn origin(&self, id: &str) -> Option<&Origin> {
self.spans
.iter()
.find(|span| span.origin.id == id)
.map(|span| &span.origin)
}
pub fn at_position(&self, line: usize, column: usize) -> Option<&Origin> {
self.span_at_position(line, column).map(|span| &span.origin)
}
pub fn span_at_position(&self, line: usize, column: usize) -> Option<&SourceSpan> {
if line == 0 || column == 0 {
return None;
}
let start = if line == 1 {
0
} else {
self.source.match_indices('\n').nth(line - 2)?.0 + 1
};
let line_end = self.source[start..]
.find('\n')
.map_or(self.source.len(), |n| start + n);
let offset = start.checked_add(column - 1)?;
if offset >= line_end {
return None;
}
self.spans
.iter()
.filter(|span| span.start <= offset && offset < span.end)
.min_by_key(|span| span.end - span.start)
}
pub(crate) fn diagnostic_span_at_position(
&self,
line: usize,
column: usize,
boundary: Option<&str>,
) -> Option<&SourceSpan> {
let smallest = self.span_at_position(line, column)?;
if smallest.diagnostic_site {
return Some(smallest);
}
let limit = self
.spans
.iter()
.filter(|span| {
Some(span.origin.id.as_str()) == boundary
&& span.start <= smallest.start
&& span.end >= smallest.end
})
.min_by_key(|span| span.end - span.start);
self.spans
.iter()
.filter(|span| {
span.diagnostic_site
&& span.start <= smallest.start
&& span.end >= smallest.end
&& limit.is_none_or(|limit| span.start >= limit.start && span.end <= limit.end)
})
.min_by_key(|span| span.end - span.start)
.or(Some(smallest))
}
}
pub fn render(ast: &NixExpr) -> Generated {
render_with_options(ast, RenderOptions::default())
}
pub fn render_with_options(ast: &NixExpr, options: RenderOptions) -> Generated {
let document = expression(ast, options);
layout::render(&document, 100)
}
fn expression(expr: &NixExpr, options: RenderOptions) -> Doc<'_> {
in_context(expr, options, Context::Expression)
}
fn in_context(expr: &NixExpr, options: RenderOptions, context: Context) -> Doc<'_> {
let mut body = syntax(&expr.kind, options);
let runtime_context = expr.error_context && expr.origin.is_some();
if let Some(origin) = &expr.origin
&& runtime_context
{
body = application(
text("builtins.addErrorContext"),
vec![
text(quote(&origin.id)),
group_for(body, precedence::kind(&expr.kind), Context::Simple),
],
);
}
if let Some(origin) = &expr.origin {
let diagnostic_site = !expr.error_context
&& match &expr.kind {
NixKind::Call(..)
| NixKind::Apply(..)
| NixKind::Binary(..)
| NixKind::If(..)
| NixKind::Assert(..) => true,
NixKind::Select(_, path) | NixKind::ArgumentSelect(_, path) => !path.is_empty(),
_ => false,
};
body = attributed(origin, diagnostic_site, body);
if options.origin_comments {
body = concat(vec![text(format!("# {}", origin.id)), hard_line(), body]);
}
}
if context.requires_parentheses(precedence(expr))
|| matches!(context, Context::SelectionBase)
&& (precedence::path_base(expr) || precedence::selection_boundary(expr))
{
parens(body)
} else {
body
}
}
fn group_for(body: Doc<'_>, precedence: precedence::Precedence, context: Context) -> Doc<'_> {
if context.requires_parentheses(precedence) {
parens(body)
} else {
body
}
}
fn parens(body: Doc<'_>) -> Doc<'_> {
concat(vec![text("("), body, text(")")])
}
fn join<'a>(parts: Vec<Doc<'a>>, separator: impl Fn() -> Doc<'a>) -> Doc<'a> {
let mut joined = Vec::new();
for (index, part) in parts.into_iter().enumerate() {
if index > 0 {
joined.push(separator());
}
joined.push(part);
}
concat(joined)
}
fn collection<'a>(open: &str, items: Vec<Doc<'a>>, close: &str) -> Doc<'a> {
if items.is_empty() {
return text(format!("{open} {close}"));
}
group(concat(vec![
text(open),
nest(concat(vec![line(), join(items, line)])),
line(),
text(close),
]))
}
fn application<'a>(head: Doc<'a>, arguments: Vec<Doc<'a>>) -> Doc<'a> {
let mut parts = vec![head];
for argument in arguments {
parts.push(nest(concat(vec![line(), argument])));
}
group(concat(parts))
}
fn binding<'a>(key: String, value: Doc<'a>) -> Doc<'a> {
group(concat(vec![
text(format!("{key} =")),
nest(concat(vec![line(), value])),
text(";"),
]))
}
fn syntax(kind: &NixKind, options: RenderOptions) -> Doc<'_> {
let child = |expr| expression(expr, options);
match kind {
NixKind::Bool(v) => text(if *v { "true" } else { "false" }),
NixKind::Int(i64::MIN) => text("(-9223372036854775807 - 1)"),
NixKind::Int(v) => text(v.to_string()),
NixKind::Float(v) => {
let mut literal = format!("{v:?}");
if let Some(exponent) = literal.find('e')
&& !literal[..exponent].contains('.')
{
literal.insert_str(exponent, ".0");
}
text(literal)
}
NixKind::Null => text("null"),
NixKind::String(v) => text(quote(v)),
NixKind::Path(v) | NixKind::Variable(v) => text(v.clone()),
NixKind::Select(value, path) | NixKind::ArgumentSelect(value, path) => {
if path.is_empty() {
return child(value);
}
let mut parts = vec![in_context(value, options, Context::SelectionBase)];
parts.extend(path.iter().map(|part| {
let attribute = if attribute_identifier(part) {
part.clone()
} else {
quote(part)
};
text(format!(".{attribute}"))
}));
concat(parts)
}
NixKind::Lambda(argument, body) => group(concat(vec![
text(format!("{argument}:")),
nest(concat(vec![line(), child(body)])),
])),
NixKind::Function(arguments, body) => {
let mut parameters: Vec<_> = arguments.iter().map(|name| text(name.clone())).collect();
if parameters.is_empty() {
parameters.push(text(""));
}
parameters.push(text("..."));
function(parameters, child(body))
}
NixKind::ArgumentFunction(arguments, body) => {
let parameters = arguments
.iter()
.map(|(name, default)| match default {
None => text(name.clone()),
Some(value) => group(concat(vec![
text(format!("{name} ?")),
nest(concat(vec![line(), child(value)])),
])),
})
.collect();
function(parameters, child(body))
}
NixKind::Group(value) => parens(child(value)),
NixKind::List(items) => collection(
"[",
items
.iter()
.map(|item| in_context(item, options, Context::Simple))
.collect(),
"]",
),
NixKind::AttrSet(bindings) => collection(
"{",
bindings
.iter()
.map(|(path, value)| {
let key = path
.iter()
.map(|part| quote(part))
.collect::<Vec<_>>()
.join(".");
binding(key, child(value))
})
.collect(),
"}",
),
NixKind::Call(builtin, args) => application(
text(match builtin {
Builtin::Div => "builtins.div",
Builtin::Throw => "builtins.throw",
Builtin::Import => "builtins.import",
Builtin::GetAttr => "builtins.getAttr",
Builtin::ToPath => "builtins.toPath",
Builtin::ToString => "builtins.toString",
}),
args.iter()
.map(|arg| in_context(arg, options, Context::Simple))
.collect(),
),
NixKind::Binary(op, left, right) => {
let (operator, left_context, right_context) = precedence::binary(*op);
group(concat(vec![
in_context(left, options, left_context),
nest(concat(vec![
line(),
text(format!("{operator} ")),
in_context(right, options, right_context),
])),
]))
}
NixKind::If(condition, yes, no) => group(concat(vec![
text("if "),
child(condition),
text(" then"),
nest(concat(vec![line(), child(yes)])),
line(),
text("else"),
nest(concat(vec![line(), child(no)])),
])),
NixKind::Assert(condition, value) => group(concat(vec![
text("assert "),
child(condition),
text(";"),
nest(concat(vec![line(), child(value)])),
])),
NixKind::Let(name, value, body) => group(concat(vec![
text("let"),
nest(concat(vec![line(), binding(name.clone(), child(value))])),
line(),
text("in"),
nest(concat(vec![line(), child(body)])),
])),
NixKind::Apply(function, argument) => application(
in_context(function, options, Context::ApplicationFunction),
vec![in_context(argument, options, Context::Simple)],
),
}
}
fn function<'a>(parameters: Vec<Doc<'a>>, body: Doc<'a>) -> Doc<'a> {
let pattern = if parameters.is_empty() {
text("{ }:")
} else {
group(concat(vec![
text("{"),
nest(concat(vec![
line(),
join(parameters, || concat(vec![text(","), line()])),
])),
line(),
text("}:"),
]))
};
group(concat(vec![pattern, nest(concat(vec![line(), body]))]))
}
fn attribute_identifier(name: &str) -> bool {
let mut chars = name.chars();
chars
.next()
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& chars.all(|c| c.is_ascii_alphanumeric() || "_-'".contains(c))
&& ![
"if", "then", "else", "assert", "with", "let", "in", "rec", "inherit", "or",
]
.contains(&name)
}
pub(crate) fn quote(value: &str) -> String {
let mut out = String::from("\"");
let mut chars = value.chars().peekable();
while let Some(c) = chars.next() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
'$' if chars.peek() == Some(&'{') => out.push_str("\\$"),
_ => out.push(c),
}
}
out.push('"');
out
}
#[cfg(all(test, feature = "evaluation"))]
mod tests;
#[cfg(all(test, feature = "evaluation"))]
mod precedence_tests;