# Show syntax error from Lezer parse

**URL:** https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346
**Category:** v6
**Created:** [November 24, 2022, 1:00pm UTC](https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346 "2022-11-24T13:00:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tslocke](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/tslocke/32/2736_2.png) [@tslocke](https://discuss.codemirror.net/u/tslocke)
#### Post date: [November 24, 2022, 1:00pm UTC](https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346/1 "2022-11-24T13:00:03Z")

</div>

I’m using CodeMirror for a custom language. I’ve successfully written a Lezer grammar and have got syntax highlighting working.

I was hoping the building blocks would be provided to get in-editor indication of syntax errors from the same grammar, but it looks like not. If I’ve understood correctly, error indicators in PM come from the linter support, and there’s no automatic way to get a linter from a lezer parser.

I’ve seen mention in this forum that an LR parser may not be the best tool for the job, but surely it must be way better than nothing?!

I’m thinking it should be fairly easy to:

- get highest character offset that the parser made it to without hitting a parse error,
- write a very simple linter that reports that offset as an error

Any advice on how to accomplish that? Or is there a better way to go?

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)
#### Post date: [November 24, 2022, 1:19pm UTC](https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346/2 "2022-11-24T13:19:49Z")

</div>

This is doable, but indeed, the parser won’t give any useful error message about a parse failure. What you’d do is write a lint [source](https://codemirror.net/docs/ref/#lint.LintSource) that takes the current [syntax tree](https://codemirror.net/docs/ref/#language.syntaxTree), checks if it [covers](https://lezer.codemirror.net/docs/ref/#common.Tree.length) the whole document (so that it doesn’t report errors for partial parses that were aborted due to running out of time), and then [iterates](https://lezer.codemirror.net/docs/ref/#common.Tree.iterate) through the tree to find the first error marker ([`node.type.isError`](https://lezer.codemirror.net/docs/ref/#common.NodeType.isError)).

---

<div class="post-metadata">

### Author: ![tslocke](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/tslocke/32/2736_2.png) [@tslocke](https://discuss.codemirror.net/u/tslocke)
#### Post date: [November 24, 2022, 2:45pm UTC](https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346/3 "2022-11-24T14:45:20Z")

</div>

Amazingly helpful answer in four lines!

In case this is useful to others, here is a working extension:

```auto
import {syntaxTree} from "@codemirror/language"
import {linter} from '@codemirror/lint'

function simpleLezerLinter() {
  return linter(view => {
    const {state} = view
    const tree = syntaxTree(state)
    if (tree.length === state.doc.length) {
      let pos = null
      tree.iterate({enter: n => {
        if (pos == null && n.type.isError) {
          pos = n.from
          return false
        }
      }})

      if (pos != null)
        return [{from: pos, to: pos+1, severity: 'error', message: 'syntax error'}]
    } 

    return []
  })
}

```

---

<div class="post-metadata">

### Author: ![visheshgarg797](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/visheshgarg797/32/3446_2.png) [@visheshgarg797](https://discuss.codemirror.net/u/visheshgarg797)
#### Post date: [June 21, 2023, 10:21am UTC](https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346/4 "2023-06-21T10:21:20Z")

</div>

where to implement it in editor ? do i have to add it in extensions ?

---

<div class="post-metadata">

### Author: ![tslocke](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/tslocke/32/2736_2.png) [@tslocke](https://discuss.codemirror.net/u/tslocke)
#### Post date: [June 21, 2023, 10:59am UTC](https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346/5 "2023-06-21T10:59:18Z")

</div>

Yes, the return value from that function goes into the extensions array.

---

<div class="post-metadata">

### Author: ![visheshgarg797](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/visheshgarg797/32/3446_2.png) [@visheshgarg797](https://discuss.codemirror.net/u/visheshgarg797)
#### Post date: [June 21, 2023, 11:10am UTC](https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346/6 "2023-06-21T11:10:46Z")

</div>

actually i used my own antlr4 grammer as language for the same i want to show syntax error in that , can you help me in that ?

---

<div class="post-metadata">

### Author: ![AlexErrant](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/alexerrant/32/4287_2.png) [@AlexErrant](https://discuss.codemirror.net/u/AlexErrant)
#### Post date: [March 18, 2024, 8:43pm UTC](https://discuss.codemirror.net/t/show-syntax-error-from-lezer-parse/5346/7 "2024-03-18T20:43:52Z")

</div>

Related, slightly simpler answer here [Showing syntax errors - #6 by OskarDamkjaer](https://discuss.codemirror.net/t/showing-syntax-errors/3111/6)
