Camlp4 syntax extension, parser error

I created a syntax extension that allows you to define a type as

type.yjson type_name { /* type_declaration */ } 

to be able to create a record value directly from a json file. A syntax extension inserts the module and function necessary for this. Still no problem. The syntax extension does exactly what I wanted.

I am having some problem if I want to use "yjson" in some other place in my code (for example, a function parameter).

Here is what I tried:

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

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

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

I really don’t understand what is going on here. This doesn't seem to be a coincidence, so why can I get a parsing error?

+4
source share
2 answers

I don’t quite understand the P4 mechanism around this, but [ [ "blahblah" -> ... makes blahblah as a new language keyword, so you can no longer use blahblah as an argument to a function.

To see this, try the preliminary process pa _ *. ml using camlp4of and see how "blahblah" will be expanded to Gram.Skeyword "blahblah" . It looks like this Skeyword _ is passed to Structure.using via Insert.insert from P4, and the string is registered as a new keyword.

To keep yjson usable as a regular variable, use id = LIDENT instead of "yjson" in your rule, then check the id contents of "yjson" or not in your action.

+5
source

If I can make a small off-topic remark, I think it’s wrong to create custom syntax for generating type-oriented code when there are already two different syntaxes (one for type_conv and one for output), one of which (type-conv) becomes the de facto standard.

 type foo = { ... } with json 

If you choose the syntax for this, you should use it unless you have very good reasons. In fact, type-conv itself is a helper utility that allows you to write your own type-oriented code generators, so you can also use type-conv directly for what you're trying to do.

(You probably know about Martin Jambon Atdgen , who made a conscious choice not to use Camlp4, Allen Frisch's work continues to support annotations directly in OCaml syntax, but which is not yet ready for consumption.)

+4
source

Source: https://habr.com/ru/post/1483465/


All Articles