3 ms·
I disagree substantively with this paragraph: > Ruby has no well defined grammar. All the Ruby implementations today reuse Matz's original parsing code. There
by woodruffw 9y ago
I disagree substantively with this paragraph:
> Ruby has no well defined grammar. All the Ruby implementations today reuse Matz's original parsing code. There are various BNF grammars people have written for the language, but they may or may not match the actual implementation. Nor does Ruby provide a mechanism to turn code into an abstract syntax tree for you as Python and Lisp do. Anyone writing tools beyond a unit testing library must solve this problem first, before ever doing any real work.
Ruby's grammar is well-defined, it's just implementation defined rather than specification defined. That alone is a problem, but I think the author makes a mistake in associating an external specification with the state of being well-defined[1].
Being able to turn your code into an AST can be useful, but I think the author overstates its value. Personally, I've never wanted or needed to inspect Ruby's AST in a practical application. When I'm fiddling around, I use whitequark's parser[2][3], which has always worked well.
[1]: It's also worth noting that Ruby does have a formal ISO specification (https://www.iso.org/standard/59579.html https://www.iso.org/standard/59579.html), albeit for 1.9.x syntax (IIRC).
[2]: https://github.com/whitequark/parser https://github.com/whitequark/parser
[3]: Which, notably, just takes Ruby's `rubyNN.y` and exposes the AST as Ruby objects.
- mistercow 9y agoLinters typically operate on the AST for your code, though, and differences between your linter's grammar and the real grammar can cause headaches. More ambitiously, I think there's a huge amount we could be doing with ASTs, where right now we're mostly kludging along with line-based tools. Merging/diffing is one example. I've run up against some truly gnarly merge situations, in particular with indentation based languages, where automatic merging did what could best be described as "stirring" the code, and manual merging was like pulling teeth. It's bizarre to me that in 2017, we're still using tools that act like our source code files are made of unstructured lines of text, instead of exploiting the precise structure that the language imposes. I don't know of any remotely mature tools that try to solve this problem, although I've seen some more research-like projects. The potential gains seem huge. For example, imagine renaming a method throughout your project, and having no merge conflicts with your teammates, even when they change the same lines where that method is called. (Or at least, imagine having a merge tool that proposes the correct merge for your approval.) But if we're going to move toward tools like that, languages without formal specifications are an obstacle.
- delhanty 9y ago>... there's a huge amount we could be doing with ASTs, where right now we're mostly kludging along with line-based tools. Merging/diffing is one example. ... Strongly agree - seems a strange situation to me. The line-based thinking that still dominates the majority *nix has some advantages but also does seem to impede progress. It reminds me that I meant to read Rob Pikes "Structural Regular Expressions" [0]. (ht #pmoriarty - from HN mid-November [1]) Furthermore, even if there is well-defined AST available, it doesn't mean it will be used. XML based formats for example - e.g. see here: [2] If we could even semi-automatically merge Microsoft's Office Open XML formats [3] anywhere using the associated AST that would surely create huge value for lots of businesses. [0] http://doc.cat-v.org/bell_labs/structural_regexps/se.pdf http://doc.cat-v.org/bell_labs/structural_regexps/se.pdf [1] https://news.ycombinator.com/item?id=15714169 https://news.ycombinator.com/item?id=15714169 [2] https://joepairman.com/posts/xml-in-git-mercurial-watch-out-for-merges https://joepairman.com/posts/xml-in-git-mercurial-watch-out-... [3] https://en.wikipedia.org/wiki/Office_Open_XML https://en.wikipedia.org/wiki/Office_Open_XML
- kazinator 9y agoFor a grammar (or anything else) to be worse than implementation-defined, it would have to be outright flaky: not parse the same way twice based on uninitialized bits in RAM, phase of moon, etc.