Override Ruby's base C code from Gem

Viewed 102

For the sake of experimentation, I am looking for a way to modify some of Ruby's base code, specifically the parser. I was wondering if this was possible to do at all, let alone using a Gem.

I have narrowed the code I need to change to static int yylex() within parser.c. I was going to try to use an alias, but that seems to require that I change parser.h, which cannot be done within a Gem, as I understand.

Can this be done from a Gem?

1 Answers

No.

The only base C code that gems have access to is that exposed by the Ruby headers. The parsing/lexing code is not exposed there.

If you want to define custom syntax, I would try (in order):

  1. Loosen your requirements a bit and define a DSL. Ruby has insanely powerful metaprogramming features that can take anything you might do statically in a script and instead do it dynamically during runtime
  2. Write your custom parser in Ruby and emit valid Ruby which you then eval. Ugly, and probably a little slow, but will allow you to do anything you want.
  3. Modify the mruby parser instead. mruby is designed for embedded applications where you want to be able to highly customize the capabilities of the VM. I doubt that they had the parser in mind, but still it might be more feasible than messing around with MRI.
Related