Showing syntax errors

Is there an example I could look through for showing syntax errors inside the editor? I had written a custom language parser for RDF 1.1 Turtle but I would like to also show parsing errors in the editor (squiggly underline or something similar) for when the parsing encounters errors. Is there an example of say lang-javascript or lang-json someone could point me to? Thanks in advance.

You may be looking for the lint addon (demo here).

Any CodeMirror6 examples? Should have clarified that.

Not yet. But the docs for the lint package should get you pretty far.

Hey guys! :smiley: :wave: I am needing to make a linter from my language. And I saw I have to implement a lint function that receives the editor and return an array of Diagnostics. The problem is that i can’t find an example of how to get the parse errors from thee editor view. There is a way to get these errors to show make the diagnostics array ?

I only want to build something similar to the codemirror (5) lint examples, but i don’t find any example :frowning:

I wrote I quick demo on how you can display errors in the editor, however to get useful error messages you’d want to use an external linter as noted in this thread.

import { linter, Diagnostic } from "@codemirror/lint";


function lintExample(view: EditorView): readonly Diagnostic[] {
  const diagnostics: Diagnostic[] = [];

  syntaxTree(view.state).iterate({
    enter: (type, from, to) => {
      if (type.isError) {
        diagnostics.push({
          from,
          to,
          severity: "error",
          message: "Yep. That's a syntax error.",
        });
      }
    },
  });

  return diagnostics;
}
// Run the example as an extension like this.
linter(lintExample)

:open_mouth: thanks Oskar!, yes… I’ve tried something similar but the problem is that the error nodes don’t have more information about the production that tried to apply and that why it fails. Anyway… thanks :slight_smile:

I will give it a look to the Lezer code to think how could be possible to add this data… at least to some data to have more idea about the path followed until the error was produced.

From the thread I linked it sounds like the information you’re after is available in the lezer parser, but thrown away for performance/memory reasons. Marijn also noted that LR parsers aren’t good at giving meaningful error messages and recommends using an external linter.

For my language there is no external linter available, so like you I’m looking for a way to get a least some context on what went wrong. It might be a fools errand but if I make any progress I’ll post an update and I’d be interested to see what you come up with as well

Yes, of course! Well… These last weeks I’ve been thinking that could be a good idea to design and implement a linter generator, similar to a parse generator ( in fact it is quite similar ) but putting focus on be able to define a grammar embedded with language rules. I mean, create a robust tool could take a long time but could be great to give it a chance at least. On the another hand… I’ve been reading this paper Error reporting in Parsing Expression Grammars - ScienceDirect