3 ms·
Does Vale’s parser tolerate syntax errors well? Keywords are great because they provide redundancy in the grammar that allows for good syntax error messages an
by jez 4y ago
Does Vale’s parser tolerate syntax errors well?
Keywords are great because they provide redundancy in the grammar that allows for good syntax error messages and recovery.
I recently lamented the absence of a let token in Ruby while working on making Sorbet’s parser more tolerant of syntax errors. Consider this Ruby program:
x.
y = 0
It doesn’t have a syntax error. Ruby treats this as if the user had written
x.y=(0)
But because of how the line is broken, probably the user meant to have two separate statements, and is in the middle of inserting a new call like
x.foo()
y = 1
Or consider another:
class
y = 1
The syntax error Ruby reports here is “class names must start with a capital letter.” It then fails to produce any parse result because there is no end keyword to match the class, throwing away the (properly parsing) `x = 1` subexpression in the parse tree.
In these cases it’s frequently possible to hack around the issues with weird contortions. But in basically every such case, if Ruby had used an explicit let keyword it would have been much easier to for the parser to both report syntax errors in places that might otherwise be ambiguous as well as produce some parse tree for the ones that don’t parse.
- verdagon 4y agoA great point, I agree that extra syntax can help with parser errors. Vale happens to require semicolons at the end of every line, which helps with this (and a few other corner cases around custom binary operators).
- alpaca128 4y agoI once removed all semicolons from a JS codebase when they became optional and encountered situations where this lead to errors as the same code was parsed differently afterwards. So yeah, long story short I'm also a proponent of semicolons. They help avoid ambiguity for both humans and machines, and make splitting lines less awkward.