Skip to content

Comments

exceptions: pretty diagnostics#228

Draft
xnacly wants to merge 9 commits intomaplant:mainfrom
xnacly:main
Draft

exceptions: pretty diagnostics#228
xnacly wants to merge 9 commits intomaplant:mainfrom
xnacly:main

Conversation

@xnacly
Copy link

@xnacly xnacly commented Feb 11, 2026

This pr adds pretty printing for exceptions::SyntaxViolation and wraps the main with an entry_point function to catch all exceptions::Exception and pass them to print_exception at a single point.

Progress:

  • Exceptions
    • SyntaxViolation
    • StackTrace
  • Lexer
    • UnexpectedEof
    • InvalidCharacterInHexEscape
    • UnexpectedCharacter
    • BadEscapeCharacter
    • ReadError
  • Parser
    • UnexpectedEof
    • ExpectedClosingParen
    • UnexpectedClosingParen
    • InvalidPeriodLocation
    • NonByte
    • UnclosedParen
    • CharTryFrom
    • Lex
    • ParseNumberError
    • UnexpectedToken

These changes result in the following output for a list of faulty inputs:

Uncaught exception:
 - Message: undefined variable

--> fact.scm:8:1:
007 |        (iter b (+ a b) (- count 1))))
008 |  (ite 0 1 n))
    |  ~ here
> i
Uncaught exception:
 - Message: undefined variable

--> <prompt>:1:0:
001 | i
    | ~ here
Uncaught exception:
 - Message: No such file or directory (os error 2)
 - Condition: #<&assertion>
 - Condition: #<&i/o>

This commit replaces SyntaxViolation.debug with

    /// pretty print a SyntaxViolation, heavily inspired by
    /// [T8Err::render](https://github.com/xnacly/tango8/blob/master/shared/src/err.rs#L11)
    pub fn pretty_print<'r, W, S>(&self, w: &'r mut W, lines: &'r [S]) -> io::Result<()>
    where
        W: std::io::Write,
        S: std::fmt::Display,

    /// The repl doesnt allow us to capture all input lines via the fs, so we need a fallback for
    /// only printing the offending line, we have access to via gc_inner
    pub fn pretty_print_no_lines<W>(&self, w: &mut W) -> io::Result<()>
    where
        W: std::io::Write,

Enabling the pretty exception printing to support both the repl and the
file based execution mode. Both functions print context, specifically
the previous and following line relative to the offending name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant