Skip to main content

Validator Utilities

The @prism-lang/validator package includes utility classes for error formatting and AST manipulation.

ErrorFormatter

The ErrorFormatter class transforms validation errors into LLM-friendly messages with fix suggestions and code examples.

Import

import { ErrorFormatter } from '@prism-lang/validator';

formatForLLM()

Convert a single error into a structured message with fix guidance.

static formatForLLM(
error: SyntaxError | ParseError | ConfidenceIssue | TypeError | LintResult
): ErrorMessage

Returns: ErrorMessage with fix suggestions and examples

Example:

import { ErrorFormatter } from '@prism-lang/validator';

const error = {
line: 5,
column: 10,
message: 'Missing confidence branches',
code: 'MISSING_CONFIDENCE_BRANCHES'
};

const formatted = ErrorFormatter.formatForLLM(error);

console.log(formatted);
// {
// error: 'MISSING_CONFIDENCE_BRANCHES',
// line: 5,
// column: 10,
// message: 'Missing confidence branches',
// fix: "Add a 'low { }' block after the 'medium' block",
// example: `uncertain if (confidence > 0.5) {
// high { return "very confident" }
// medium { return "somewhat confident" }
// low { return "not confident" }
// }`
// }

Supported Error Codes

Error CodeFix Suggestion
SYNTAX_ERRORCheck for missing semicolons, brackets, or parentheses
MISSING_CONFIDENCE_BRANCHESAdd missing low { } block
CONFIDENCE_WITHOUT_VALUEAssign confidence using value @ 0.8
CONFIDENCE_OPERATOR_WITHOUT_VALUEEnsure variable has confidence before using operators
UNCERTAIN_WITHOUT_CONFIDENCEUse confidence expression in the test
INVALID_CONFIDENCE_VALUEConfidence must be between 0 and 1
UNDEFINED_VARIABLEDeclare variable before using it
NOT_A_FUNCTIONEnsure calling a function, not a value
INVALID_BINARY_OPERANDConvert types before using operator
INCOMPLETE_CONFIDENCE_BRANCHESAdd all three branches (high, medium, low)
VARIABLE_MISSING_CONFIDENCEInitialize with confidence before using operators
WRONG_ARGUMENT_COUNTProvide correct number of arguments
no-infinite-loopsAdd break condition or termination logic
confidence-rangeUse value between 0 and 1
no-unused-variablesRemove or prefix with underscore
uncertain-completenessAdd at least one confidence branch
no-constant-conditionReplace constant with variable

formatMultipleErrors()

Format and group multiple errors by type.

static formatMultipleErrors(
errors: Array<SyntaxError | ParseError | ConfidenceIssue | TypeError | LintResult>
): ErrorMessage[]

Example:

const errors = [
{ line: 1, column: 1, message: 'Undefined variable: x', code: 'UNDEFINED_VARIABLE' },
{ line: 3, column: 1, message: 'Undefined variable: y', code: 'UNDEFINED_VARIABLE' },
{ line: 5, column: 10, message: 'Missing brackets', code: 'SYNTAX_ERROR' }
];

const formatted = ErrorFormatter.formatMultipleErrors(errors);

console.log(formatted);
// [
// {
// error: 'UNDEFINED_VARIABLE',
// line: 1,
// message: 'Multiple UNDEFINED_VARIABLE errors (2 instances)',
// suggestion: 'Fix all 2 instances of this error type'
// },
// {
// error: 'SYNTAX_ERROR',
// line: 5,
// message: 'Missing brackets',
// fix: 'Check for missing semicolons, brackets, or parentheses'
// }
// ]

generateFixSuggestion()

Generate a prioritized fix suggestion for multiple errors.

static generateFixSuggestion(errors: ErrorMessage[]): string

Errors are prioritized:

  1. Syntax/Parse errors (fix first)
  2. Undefined variables, function errors
  3. Argument count errors
  4. Confidence-related errors

Example:

const formatted = ErrorFormatter.formatMultipleErrors(errors);
const suggestion = ErrorFormatter.generateFixSuggestion(formatted);

console.log(suggestion);
// "Start by fixing the SYNTAX_ERROR at line 5: Check for missing semicolons, brackets, or parentheses"

ASTUtils

Simple utilities for working with Prism AST nodes.

Import

import { getNodeLocation, getLine, getColumn } from '@prism-lang/validator';

getNodeLocation()

Get the location of an AST node.

function getNodeLocation(node: ASTNode): { line: number; column: number }

Example:

import { parse } from '@prism-lang/core';
import { getNodeLocation } from '@prism-lang/validator';

const ast = parse('const x = 42');
const location = getNodeLocation(ast.body[0]);

console.log(location); // { line: 1, column: 1 }

getLine()

Get just the line number from a node.

function getLine(node: ASTNode): number

getColumn()

Get just the column number from a node.

function getColumn(node: ASTNode): number

Example:

import { getLine, getColumn } from '@prism-lang/validator';

const line = getLine(node); // 1
const col = getColumn(node); // 1

ErrorMessage Type

interface ErrorMessage {
error: string; // Error code
line: number; // Line number
column: number; // Column number
message: string; // Human-readable message
fix?: string; // How to fix the error
example?: string; // Code example showing correct usage
suggestion?: string; // Additional guidance
}

LLM Error Correction Example

Use ErrorFormatter to help an LLM fix code:

import { UnifiedValidator, ErrorFormatter } from '@prism-lang/validator';
import { llm } from '@prism-lang/llm';

async function autoFixCode(code: string): Promise<string> {
const validator = new UnifiedValidator();
const result = validator.validateAll(code);

if (result.valid) {
return code;
}

// Format errors for LLM
const allErrors = [
...result.syntax.errors,
...result.types.errors,
...result.confidence.issues,
...result.lint.results.filter(r => r.severity === 'error')
];

const formatted = ErrorFormatter.formatMultipleErrors(allErrors);
const suggestion = ErrorFormatter.generateFixSuggestion(formatted);

// Build prompt for LLM
const prompt = `
Fix the following Prism code. ${suggestion}

Errors found:
${formatted.map(e => `- Line ${e.line}: ${e.message}\n Fix: ${e.fix}`).join('\n')}

Original code:
\`\`\`prism
${code}
\`\`\`

Provide only the corrected code:`;

const fixed = await llm(prompt);
return fixed;
}

Prism Integration

// error-helper.prism
import {UnifiedValidator, ErrorFormatter} from "@prism-lang/validator"

function explainErrors(code) {
let validator = new UnifiedValidator()
let result = validator.validateAll(code)

if result.valid {
return "Code is valid!" ~> 1.0
}

let errors = result.syntax.errors
let formatted = ErrorFormatter.formatMultipleErrors(errors)

let explanation = ""
for error in formatted {
explanation = explanation + "Line " + error.line + ": " + error.message + "\n"
if error.fix {
explanation = explanation + " Fix: " + error.fix + "\n"
}
if error.example {
explanation = explanation + " Example:\n" + error.example + "\n"
}
}

return explanation ~> 0.9
}

// Usage
let code = "uncertain if (x { high { } }"
let help = explainErrors(code)
print(help)