Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grammar to parse module specification

Tags:

raku

Raku modules can be specified in different ways, for example:

MyModule

MyModule:ver<1.0.3>

MyModule:ver<1.0.3>:auth<Name ([email protected])>;

MyModule:ver<1.0.3>:auth<Name <[email protected]>>;

I wrote the below grammar to parse the module spec which works fine for most of specs but it fails if the auth field contains < or >. How can I fix the grammar to match in this case as well?

I can't figure out how to say match everything in between < and > including any < and > as well.

#!/usr/bin/env perl6

grammar Spec {

  token TOP { <spec> }

  token spec { <name> <keyval>* }

  token name { [<-[./:<>()\h]>+]+ % '::' }

  token keyval { ':' <key> <value> }

  proto token key { * }
  token key:sym<ver>     { <sym> }
  token key:sym<version> { <sym> }
  token key:sym<auth>    { <sym> }
  token key:sym<api>     { <sym> }
  token key:sym<from>    { <sym> }

  # BUG: fix specs that contains '<>' inside value;
  token value { '<' ~ '>' $<val>=<-[<>]>* | '(' ~ ')' $<val>=<-[()]>* }

}

my \tests = (
  'MyModule:ver<1.0.3>:auth<Name ([email protected])>',
  'MyModule:ver<1.0.3>:auth<Name <[email protected]>>',
);

for tests -> \spec {

  say so Spec.parse: spec;

}

# Output:
True
False
like image 367
hythm Avatar asked Sep 29 '19 23:09

hythm


1 Answers

If you know that the inner field will basically be in the same format as the value token, you can recursively match for value with $<val>=[.*? <value>?]. This even lets you capture the contents of the inner field seperately:

token value { '<' ~ '>' $<val>=[.*? <value>?] | '(' ~ ')' $<val>=<-[()]>* }

If you don't want the inner contents than you can use the recursive <~~> in place of <value>

token value { '<' ~ '>' $<val>=[.*? <~~>?] | '(' ~ ')' $<val>=<-[()]>* }
like image 159
Jo King Avatar answered Sep 30 '22 17:09

Jo King