How does Ruby Implement General Delimited Inputs? - ruby

After much searching, I can't figure how Ruby implements General Delimited Inputs.
All I can find is Kernel#`, which is used by %x{...}.
Any help would greatly appreciated. Thanks

This is handled in the parsing code, which is written in YACC and C. Check out the source code on GitHub. Specifically, the token that handles this type of quoting begins with tQWORDS_BEG (search within parse.y).
A detailed discussion of the YACC implementation would be long, but if you want to get started, that's where it lives in the code.
Note that the above link is for MRI Ruby. I don't know how other Ruby interpreters handle it, but they all do it in a parser somewhere, and most of those are written in C and likely use YACC to parse. Notable exceptions are JRuby, written in Java, and druby, in OCaml.

Related

Does Ruby have an equivalent to the __pycache__ folder in Python?

In Python, after you run a program, there are caches saved in the folders named __pycache__. For futher explanation of the functionality of these folders, please see this question. Does Ruby have an equivalent of this? If not, why?
Ruby doesn't haven an equivalent. It just wouldn't make sense: Ruby is a programming language. A programming language is an abstract mathematical concept, a specification. Putting such detailed things as the name of the directory of the byte code cache in a language would be way too restrictive: what if somebody wants to implement Ruby on a platform which doesn't have files? What if someone wants to implement Ruby on a platform where underscores are illegal in directory names? What if someone wants to implement Ruby with an interpreter instead of a compiler?
There are, however, some Ruby implementations which do compile to byte code. YARV and Rubinius are two examples of those. YARV only compiles in memory, whereas Rubinius caches the compiled byte code on disk. In fact, it must have the ability to save and read the compiled byte code because the compiler itself is written in Ruby, and otherwise it would have to compile itself in order to be able to compile any code, but in order to compile itself it would first have to compile itself and in order to that it would first have to …
But that is a private internal implementation detail of Rubinius. It is not part of Ruby nor should it be.

Haskell library for parsing Bash scripts?

Does anybody know of a Haskell library which can parse arbitrary Bash scripts?
A cursory search of Hackage indicates that there's a package called bash for writing scripts, but I don't see anything for parsing them.
Basically I've just had a large collection of Bash scripts dumped on me, and I'd like to do some code analysis on it. But the first stage is obviously to be able to parse this stuff.
I don't know Bash very well personally. I suppose I could sit down and wage through the volumous man-page to get the complete BNF grammar for it. (I imagine it's very complex, given the shell's long and backwards-compatible history.) I was just wondering whether somebody else has already done this work for me...
Perhaps extend language-sh.
Language.Sh is a collection of modules for parsing and manipulating
expressions in shell grammar. This is part of a larger project, shsh.
Please note that the API is somewhat unstable until we reach version
1.0.

How to build AST by S-expression in Ruby?

I have no idea how to build S-exp.
I want to do it, because I need to build AST for my langauge.
At the beginning I used RubyParser to parse it to sexp then code gen.
But it must be ruby's subset I think.I cant define the language what I want.
Now I need to implement parser for my language.
So anyone could recommend any ruby tool that building AST for S-expression ?
Thanks!
It is not very clear from your question what exactly do you need, but simple Google search gives some interesting links to check. Maybe after checking these links, if they are not the answer to your question, you can edit question and make it more precise and concrete.
http://thingsaaronmade.com/blog/writing-an-s-expression-parser-in-ruby.html
https://github.com/aarongough/sexpistol
You might try the sxp-ruby gem at http://github.com/bendiken/sxp-ruby. I use it for SPARQL S-Expressions (SSE) and similar methods for managing Abstract Syntax Trees in Ruby.
Maybe you could have a look at this gem named Astrapi.
This is just an experiment :
describe your language elements (concepts) in a "mm" file (abstract syntax)
run astrapi on this file
astrapi generates a parser that is able to fill up your AST, from your input source expressed in s-expression (concrete syntax of your concepts).
I have put a modest documentation here.

Safe to parse user submitted code using Ripper?

I'm using the Ruby 1.9 Ripper library to analyze specific parts of a source code by building it's sexp tree. From what I know, Ripper just uses a lexer / parser to do this.
Is it safe to run Ripper on a user submitted code?
Since it does not actually evaluate any code, yes it is safe.
If you are talking about taking those s-expressions and evaluating them, then most certainly the answer seems to be: Not without cleaning it first. That cleaning process could be especially tricky though.

Ruby Parser

 I want to know whether it is possible to parse ruby language using just
deterministic parser having no backtracking at all ??
Instead of actually having to write a parser, you can always leverage the existing interpreter to do what you want.
For example: ruby2ruby
http://seattlerb.rubyforge.org/ruby2ruby/ ruby2ruby
I don't know any specific details about parsing Ruby, or why you insist on "no backtracking". My guess is that you believe the Ruby grammar isn't LALR(1), e.g., isn't processable by YACC or equivalents.
Regardless, if the problem is to parse a language whose grammar is context-free, one can do this using a GLR parser, which does not backtrack:
http://en.wikipedia.org/wiki/GLR_parser
I've used this to build production parsers for many real languages.

Resources