<p>I am thinking of how to write the parser/compiler so it works as a <a href="https://code.visualstudio.com/api/language-extensions/language-server-extension-guide" rel="noreferrer">VSCode Language Server Extension</a>, so I can have it show syntax errors, do autocomplete, and other things, in VSCode. One thing that I've seen recommended is doing <a href="https://supunsetunga.medium.com/writing-a-parser-syntax-error-handling-b71b67a8ac66" rel="noreferrer">syntax error recovery</a> at the parser level.</p>
<p>For example, that last post suggests:</p>
<blockquote>
<ul>
<li><strong>Insert a token</strong>, and see how far the parser can successfully progress. The current context of the parser (i.e: which grammar rule is currently being parsed) and the language’s formal grammar could be used to decide the type of the token to be inserted.</li>
<li><strong>Delete a token</strong> and see how far the parser can successfully progress.
<img src="https://i.sstatic.net/uLJ32.png" /></li>
</ul>
</blockquote>
<p>I can <em>kind of</em> see how this might work, only for the example they provided. But I'm not sure how I can apply this to the language I'm <a href="https://github.com/tunebond/link" rel="noreferrer">working on</a>.</p>
<p>The language I'm working on is basically a markup language and allows you to write <a href="https://github.com/tunebond/link/tree/70600ca54097cccdacfd598e878478f55b6ecfa3#specification" rel="noreferrer">stuff like this (the spec)</a>:</p>
<pre><code>i am tree of terms
# same as
i
  am
    tree
      of
        terms
term-with-multiple-words foo
number 123
code #x123, as hex
decimal 1.23
text &lt;foo bar&gt;
text-with-interpolation &lt;foo {hello-world}&gt;
term-with-{interpolation}
term/path
term/path-{with}/interpolation
can use, commas, too
can
  use
  commas
  to
can(use(parens))
can
  use
    parens
</code></pre>
<p>That's pretty much it. Some more advanced cases for reference are <a href="https://github.com/tunebond/link/tree/70600ca54097cccdacfd598e878478f55b6ecfa3/test/file" rel="noreferrer">here</a>.</p>
<p>The question is, what sorts of &quot;syntax error recovery&quot; techniques could I use for such a language? I can't imagine any so far. For example, the &quot;text&quot; format of <code>&lt;anything except angle brackets&gt;</code> is hard to recover from if you didn't have a closing bracket:</p>
<pre><code>something &lt;foo bar
another term tree
</code></pre>
<p>So that seems unrecoverable.</p>
<p>Even with the interpolation brackets <code>{stuff}</code> or <code>{{stuff}}</code>, you might have:</p>
<pre><code>{stuff
another}
</code></pre>
<p>But what you meant was:</p>
<pre><code>{stuff}
{another}
</code></pre>
<p>So I don't really feel like &quot;syntax error recovery&quot; is practical in this situation, do you? Are there any cases where it could be useful here? I can't see any so far, and plus syntax error recovery would be extremely hard to implement, and it is only useful while writing the code, not running the code, so it may not be that useful anyways. People make it sound like syntax error recovery is a huge important feature to have, but how many languages/editors actually support this (and what are examples) is what I'd wonder then.</p>

<syntax><parsing><error-handling>
What sort of "syntax error recovery" is possible for this indentation based language?
<p>For unclosed delimiters, whether angle brackets, curly braces, or parentheses, I think it would be seen as reasonable for the recovering parser to pretend that the closing delimiter appeared at the end of the line in which the opening delimiter appeared.  That line would be marked as having an unclosed-delimiter syntax error, and the parser would continue parsing the rest of the file, finding any further syntax errors, which would be more helpful than stopping at the unclosed-delimiter error as a naïve parser would.</p>
<p>How any of this would be done depends on what tools, such as a parser combinator library or a parser generator, you use to implement your parser.</p>
<p>If your LSP server renders your markup language, it could refuse to update the rendered version until no syntax errors remain in the input, or it could try to identify regions of the input that have no syntax errors and update only those.  The latter would be more helpful but more difficult to implement correctly. (However, your language looks more like a data definition language, such as JSON or YAML, than a true markup language, such as HTML or Markdown, so rendering it may be irrelevant.)</p>
<p>In the case of</p>
<pre><code>{stuff
another}
</code></pre>
<p>this would be straightforward enough to handle gracefully by pretending that the first line ends with <code>}</code> and the second line starts with <code>{</code>, <em>if</em> line breaks are not allowed inside interpolations. If line breaks are allowed there, then no, I don't suppose you can catch the error if the user intended to write two separate interpolations.</p>
<p>For much more detail, one could read <a href="https://matklad.github.io/2023/05/21/resilient-ll-parsing-tutorial.html" rel="nofollow noreferrer">&quot;Resilient LL Parsing Tutorial&quot;</a> by Alexey Kladov, a proponent and developer of IDEs and the Rust LSP server <code>rust-analyzer</code>.  That article assumes that the language being parsed is a programming language that lacks indentation-based syntax and needs to be type-checked and semantically understood by the LSP server. I'm not sure that your LSP server has much to do beyond parsing and finding syntax errors, and I think your indentation-based syntax inherently may help contain syntax errors (not including unclosed delimiters) compared to the Rust-like syntax featured in Kladov's post, so I don't know that error recovery would be as valuable for you as Kladov considers it.</p>

CC BY-SA 4.0
2023-07-07T23:34:47.300Z
2023-07-07T23:34:47.300Z
user570286
# What sort of "syntax error recovery" is possible for this indentation based language?
Posted by: username_0 on 2023-07-07
I am thinking of how to write the parser/compiler so it works as a VSCode Language Server Extension, so I can have it show syntax errors, do autocomplete, and other things, in VSCode. One thing that I've seen recommended is doing syntax error recovery at the parser level.
For example, that last post suggests:


Insert a token, and see how far the parser can successfully progress. The current context of the parser (i.e: which grammar rule is currently being parsed) and the language’s formal grammar could be used to decide the type of the token to be inserted.
Delete a token and see how far the parser can successfully progress.



I can kind of see how this might work, only for the example they provided. But I'm not sure how I can apply this to the language I'm working on.
The language I'm working on is basically a markup language and allows you to write stuff like this (the spec):
i am tree of terms
# same as
i
  am
    tree
      of
        terms
term-with-multiple-words foo
number 123
code #x123, as hex
decimal 1.23
text 
text-with-interpolation 
term-with-{interpolation}
term/path
term/path-{with}/interpolation
can use, commas, too
can
  use
  commas
  to
can(use(parens))
can
  use
    parens

That's pretty much it. Some more advanced cases for reference are here.
The question is, what sorts of "syntax error recovery" techniques could I use for such a language? I can't imagine any so far. For example, the "text" format of  is hard to recover from if you didn't have a closing bracket:
something 
So that seems unrecoverable.
Even with the interpolation brackets {stuff} or {{stuff}}, you might have:
{stuff
another}

But what you meant was:
{stuff}
{another}

So I don't really feel like "syntax error recovery" is practical in this situation, do you? Are there any cases where it could be useful here? I can't see any so far, and plus syntax error recovery would be extremely hard to implement, and it is only useful while writing the code, not running the code, so it may not be that useful anyways. People make it sound like syntax error recovery is a huge important feature to have, but how many languages/editors actually support this (and what are examples) is what I'd wonder then.
**Tags:** syntax parsing error-handling
---
## 1 answers
### Answer by username_1 on 2023-07-07. Score: 4
For unclosed delimiters, whether angle brackets, curly braces, or parentheses, I think it would be seen as reasonable for the recovering parser to pretend that the closing delimiter appeared at the end of the line in which the opening delimiter appeared.  That line would be marked as having an unclosed-delimiter syntax error, and the parser would continue parsing the rest of the file, finding any further syntax errors, which would be more helpful than stopping at the unclosed-delimiter error as a naïve parser would.
How any of this would be done depends on what tools, such as a parser combinator library or a parser generator, you use to implement your parser.
If your LSP server renders your markup language, it could refuse to update the rendered version until no syntax errors remain in the input, or it could try to identify regions of the input that have no syntax errors and update only those.  The latter would be more helpful but more difficult to implement correctly. (However, your language looks more like a data definition language, such as JSON or YAML, than a true markup language, such as HTML or Markdown, so rendering it may be irrelevant.)
In the case of
{stuff
another}

this would be straightforward enough to handle gracefully by pretending that the first line ends with } and the second line starts with {, if line breaks are not allowed inside interpolations. If line breaks are allowed there, then no, I don't suppose you can catch the error if the user intended to write two separate interpolations.
For much more detail, one could read "Resilient LL Parsing Tutorial" by Alexey Kladov, a proponent and developer of IDEs and the Rust LSP server rust-analyzer.  That article assumes that the language being parsed is a programming language that lacks indentation-based syntax and needs to be type-checked and semantically understood by the LSP server. I'm not sure that your LSP server has much to do beyond parsing and finding syntax errors, and I think your indentation-based syntax inherently may help contain syntax errors (not including unclosed delimiters) compared to the Rust-like syntax featured in Kladov's post, so I don't know that error recovery would be as valuable for you as Kladov considers it.
---
