3

I need to create a parser for Ruby so I can use the origami folding package. To make matters more complex, I use enh-ruby-mode, so I 'd like to build the parser for that, if it makes a difference.

The package has the following instructions:

It should be fairly easy to write a parser. An origami parser is a function that takes a 'create function' and returns a function taking the string to be parsed. The returned function should return a list of fold nodes. Fold nodes are created using the passed-in create function. Best to use an example:

 (defun my-amazing-parser (create)
   (lambda (content)
     (list (funcall create beginning-of-the-fold-node-point-position ; inclusive
                           end-of-the-fold-node-point-position ; exclusive
                           offset  ; this allows you to show some of the start of the folded text
                           child-nodes))))

The origami package has a parsers file that has the built-in examples, and they also support regular expressions.

Unfortunately, I am a little too weak with elisp yet to understand it (and it looks like he uses some libraries with functions like ->> and I don't know how to go and find out what they are or where they came from).

My first guess would be something like this:

 (defun origami-ruby-parser (create)
   (lambda (content)
     (list (funcall create (enh-ruby-beginning-of-block) ; inclusive
                           (enh-ruby-end-of-block) ; exclusive
                           10  ; this allows you to show some of the start of the folded text
                           child-nodes))))

I don't know what child-nodes is supposed to be.

And from there, the question will be "can I create more complex matching rules using if/else/etc ?"

Trevoke
  • 2,375
  • 21
  • 34
  • I don't know an answer to your other questions but I know what `->>` does. It's a macro from `dash.el` https://github.com/magnars/dash.el#--x-form-rest-more. I think it was inspired by Closure which has a similar macro, which is, if put in words, second and later terms, interpreted as functions an composed are applied to the first term. – wvxvw Apr 22 '15 at 14:23

1 Answers1

3

Sorry, my documentation - if you can even call it that - is terrible. I imagine this is intimidating even if you're not new to elisp.

->> is a threading macro from the dash library, as wvxvw correctly points out. The easiest way to explore elisp is to use C-h f to read about functions you aren't familiar with. There should be a link to the source code here too, if you want to dig deeper.

Your first guess isn't bad. You'll only create a single fold here though. Essentially you're trying to create a list of trees from the buffer text. Each tree is a description of nested folds in the buffer. A node tells origami where the fold starts and ends and which folds are included inside of this - this is what the child nodes are.

And from there, the question will be "can I create more complex matching rules using if/else/etc ?"

This is a very general api that will allow you to pretty much do anything and create any fold structure you like.

Hopefully I've answered here your initial questions. I've created an issue to improve the documentation.

https://github.com/gregsexton/origami.el/issues/12

Greg Sexton
  • 223
  • 2
  • 5