Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
Last active August 29, 2015 14:15
Show Gist options
  • Save PuercoPop/dd5fb114aad284cd1e15 to your computer and use it in GitHub Desktop.
Save PuercoPop/dd5fb114aad284cd1e15 to your computer and use it in GitHub Desktop.
(require ometa)
(define-ometa-namespace parser)
(define-ometa py-parser
(literal (seq (list (atom lit) (bind value (atom anything)))
(-> value))))
(omatch
py-parser
literal
'(lit 1)
parser)
@vkz
Copy link

vkz commented Feb 17, 2015

how about that:

(define-ometa py-parser
  (literal (seq (list (seq 
                       (seq (bind left (apply anything)) (->? (equal? left 'lit)))
                       (bind right (apply anything))))
                (-> right))))

If you look carefully the grammar rule for (list e) expects valid ometa expression as argument. That is it has to be one of these:

e ==   (empty)
       (atom a)
       (apply A)
       (seq e1 e2)
       (alt e1 e2)
       (many e)
       (~ e)
       (bind x e)
       (-> t)
       (list e)

So you need to wrap a sequence of patterns in a seq or some such. This is annoying, of course. Writing your variant would be more natural. ometa-racket at the state I left it is very much an assembler. You can slap as much sugar on top of it as you like. Shouldn't be too difficult to teach it your syntax.

On a separate note, I must warn you it's not a complete implementation. For instance it won't understand indirectly left-recursive rules. Basically, there's a bunch of stuff that I'd like to happen before I'd ever use it for anything serious. Not to mention it's effectively an interpreter - not a Rackety way of doing stuff like this. But as @DalekBaldwin experience with https://github.com/DalekBaldwin/clometa showed the code is clean enough for anyone to dive in and tweak it to their fancy.

@vkz
Copy link

vkz commented Feb 17, 2015

argh, did a bunch of edits to my reply. Sorry, I haven't touched this code in a long while and got confused 😄

@vkz
Copy link

vkz commented Feb 17, 2015

in fact for something like you want above, you don't even have to touch Racket code, but rather define a helper rule or grammar that defines the kind of list you mean in your code. So you can and probably should stay in the realm of OMeta for that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment