Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

camlp4 syntax extension, parser error

Tags:

ocaml

camlp4

I created a syntax extension that allow the definition of a type as

type.yjson type_name {
  /* type_declaration */
}

to be able to build a record value directly from a json file. The syntax extension insert a module and the function necessary to do so. Until here, no problem. The syntax extension do exactly what I wanted.

I start having some issue if I want to use "yjson" at some other place in my code (i.e:function parameter).

Here what I tried:

EXTEND Gram
str_item:
    [
      [ KEYWORD "type"; KEYWORD "."; "yjson"; tdl_raw = type_declaration ->

Here the error I get when I use "yjson" as a function parameter

[fun_binding] expected after [ipatt] (in [let_binding])

I don't really understand what happen here. It doesn't seems like the rule have been match, so why do I get a parse error ?

like image 839
Marc Simon Avatar asked Jan 13 '23 06:01

Marc Simon


1 Answers

I do not perfectly understand the P4's mechanism around this, but [ [ "blahblah" -> ... makes blahblah as a new keyword of the language, so you can no longer use blahblah as a function argument.

To see this, try preprocess your pa_*.ml by camlp4of and see how "blahblah" is expanded to Gram.Skeyword "blahblah". It seems that this Skeyword _ is passed to Structure.using via Insert.insert of P4 and the string is registered as a new keyword.

To keep yjson usable as a normal variable, use id = LIDENT instead of "yjson" in your rule, then check id's content is "yjson" or not in your action.

like image 164
camlspotter Avatar answered Feb 12 '23 01:02

camlspotter