Skip to content

feat: add highlighting mode that includes unparsed syntax - #138

Merged
david-christiansen merged 13 commits into
leanprover:mainfrom
jrr6:highlight-unparsed
Jun 25, 2025
Merged

david-christiansen merged 13 commits into
leanprover:mainfrom
jrr6:highlight-unparsed

Conversation

@jrr6

@jrr6 jrr6 commented Jun 19, 2025

Copy link
Copy Markdown
Contributor

This PR adds a highlightIncludingUnparsed method to the Highlighting module, which will insert unparsed syntax regions into the output Highlighted value. Unparsed regions are determined using the positions on the input syntax and are filled in using the file map. This is useful for demonstrating parsing errors alongside elaboration errors in a single code block.

@david-christiansen

Copy link
Copy Markdown
Collaborator

This looks really good!

A couple questions before merge:

  1. Is there any use case for not including this information? That is, should the includeUnparsed setting go away? From what I can see, this is strictly better than the current implementation - for the cases that work today, this does the same thing, but it now can work for more cases! This would also make highlightIncludingUnparsed redundant, simplifying the API, and remove the need for more options on the command-line tools.
  2. I think there should be a new constructor for unparsed regions, because I think there's a good reason that clients of this information may want to distinguish between whitespace that the parser ignored and regions that were skipped due to things like parser error recovery. For example, the code that extracts matching tokens for showing quotes in text skips text nodes because they're whitespace, which allows the matching to be modulo whitespace, but they should probably not skip these regions. Am I wrong here? I realize this is a bit more work and touches areas that you don't know as well, so I can also make this change on top of the PR if you agree.
  3. I know the testing infrastructure here is not good, but a test would be lovely :-)

Comment thread src/SubVerso/Highlighting/Code.lean Outdated
if let some lastPos := (← get).lastPos? then
if lastPos < pos then
let text ← getFileMap
openUntil <| text.toPosition lastPos

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think parser errors may need a little more clever handling here. They may occur in a substring of the unparsed region, after all. I think this code should probably segment the unparsed region according to the messages that occur in it, and emit it accordingly. Does that make sense?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think 768d912 should address this? (As I note in the TODO, it's not entirely clear to me where messages without an end position should be terminated, since the existing highlighter does this automatically by virtue of token emission. I figured whitespace was a close enough heuristic for now.)

@jrr6

jrr6 commented Jun 22, 2025 •

Copy link
Copy Markdown
Contributor Author

Is there any use case for not including this information?

Yes, I'm pretty certain we still want the old highlight behavior for %examples and other situations where the input source is not verbatim what we want to render. Since highlightIncludingUnparsed pulls the source from the file map, the pre-preprocessing versions of this syntax will make it through into the output. Perhaps there's some way to make highlightIncludingUnparsed more robust through a more sophisticated means of indicating what the "original source" should be (i.e., after processing %ex annotations and the like), but I imagine that would be a good deal more involved than the current approach.

I think there should be a new constructor for unparsed regions, because I think there's a good reason that clients of this information may want to distinguish between whitespace that the parser ignored and regions that were skipped due to things like parser error recovery. For example, the code that extracts matching tokens for showing quotes in text skips text nodes because they're whitespace, which allows the matching to be modulo whitespace, but they should probably not skip these regions. Am I wrong here? I realize this is a bit more work and touches areas that you don't know as well, so I can also make this change on top of the PR if you agree.

Got it; that makes sense to me. I've tried to at least lay the foundation for this in 2a2a8d5, but if this misses the mark, I can easily revert it and let you handle this part. (I had to make some educated guesses about the intended behavior of a few of the relevant functions on unparsed inputs, which may have been incorrect.)

testing infrastructure

I've added a test here, albeit with slightly ad-hoc infrastructure (the existing "example" commands, being elaborators, don't support unparseable inputs).

@david-christiansen

Copy link
Copy Markdown
Collaborator

Ad-hoc infrastructure is the name of the game when you want to support every Lean :-)

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.

2 participants