3 ms·
Yeah, Ruby's grammar is very much a result of piling features into the canonical implementation ("MRI" - Matz Ruby Interpreter - it's what you'll find as "ruby"
by vidarh 10y ago
Yeah, Ruby's grammar is very much a result of piling features into the canonical implementation ("MRI" - Matz Ruby Interpreter - it's what you'll find as "ruby" most places) to make it nice for developers, without formalizing the grammar in any way whatsoever.
There's been some work in formalizing it later - there's an ISO standard for a subset of Ruby - and people have tried to document it, but the language in general is so convoluted that e.g. a number of years back I wrote an article on the Ruby object model that covered eigenclasses by instrumenting the then-current version of MRI.
Then I got "corrected" - because apparently the documented inheritance hierarchy was different. Except the interpreter hadn't followed the docs for several years, and nobody had noticed...
The "canonical" documentation of the Ruby parser is basically a 6000+ line Bison file where most of the linecount is taken up with handling exceptions... It's awful.
You mentioned here-docs I think - in Ruby you can do this:
foo(<<HERE1, <<HERE2)
Part of HERE1
HERE1
Part of HERE2
HERE2
I find here-docs that actually start right then and there ugly enough, but that's taking it a bit too far for me...
Another favourite abomination: "% 42 " parses as the string "42", while "x % 42 " parses as "x modulo 42". Basically if % occurs as a prefix operator, it starts a quoted string where the following character will be the quote character, with some exceptions (e.g. left braces, brackets and parantheses indicates it'll end with the opposite) - and even space and linefeed works...
One of those things that probably seemed useful at the time, and was added without any restrictions, and it's just become an accepted part of the language (if I ever see anyone using it, I might become homicidal).
In terms of naming of parsers, my operator precedence parser is specifically bottom up, so a variation over Djikstra's shunting yard algorithm, just to add another variant. I might have to look at the top down variants more too.
- chubot 10y agoYup, the multiple here docs on a line thing is also in the POSIX spec for sh. I had no idea that was possible before implementing the shell! And I'm pretty sure it does NOT appear in the million lines of shell code that I've parsed. They use here docs in weird ways, but they don't have multiple here docs on a line. This led me to find the algorithm for parsing here docs, which surprisingly all shells I've tested support, even though almost no programs use it. http://www.oilshell.org/blog/2016/10/17.html http://www.oilshell.org/blog/2016/10/17.html The algorithm is: process the here docs in a post-order traversal of the AST. There is an example in my blog with 3 here docs, with one of them being in the while CONDITION (which is itself a command): while cat <<EOF1; read line; do echo " -> read '$line'"; cat <<EOF2; done <<EOF3 ... In shell at least, you can put the ENTIRE Program on one line. Change all the newlines to semi-colons. You could have 1000 here docs in a program. So you could put 1000 here docs beginnings on one line, and then you process the ends in that specific order. Ruby must have borrowed this from shell... I guess Matz knows shell REALLY WELL.
- zzzcpan 10y agoRuby borrowed them from Perl, which borrowed them from shell. I used mutliple heredocs once or twice in both perl and shell myself, but they break syntax highlighting, so it's hard to keep using them.
- vidarh 10y agoI did not know that about sh. And I agree with the sibling comment that it's probably been inherited via Perl (which I also didn't realise had it, but then that fits totally into every stereotype about Perl...) - Ruby has a whole lot of Perl-ism in it, most of which we try not to talk about and pretend aren't there ;) In terms of influences, Ruby is basically what happens if Perl and Smalltalk has a prettier baby.