Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

camlp4o differs from ocamlc because it has quotations enabled #5508

Closed
vicuna opened this issue Feb 16, 2012 · 13 comments
Closed

camlp4o differs from ocamlc because it has quotations enabled #5508

vicuna opened this issue Feb 16, 2012 · 13 comments

Comments

@vicuna
Copy link

vicuna commented Feb 16, 2012

Original bug ID: 5508
Reporter: Hendrik Tews
Assigned to: @diml
Status: closed (set by @diml on 2016-12-07T17:27:38Z)
Resolution: open
Priority: normal
Severity: minor
Version: 3.12.1
Target version: later
Category: -for Camlp4 use https://github.com/ocaml/camlp4/issues
Monitored by: @bobzhang

Bug description

camlp4o has quotations enabled by default, therefore it dies on legal ocaml code such as

let ( << ) a b = a + b in
print_int (4 << 5)

I believe Camlp4_config.quotations should be false by default and
the modules Camlp4QuotationExpander,
Camlp4OCamlRevisedQuotationExpander and
Camlp4OCamlOriginalQuotationExpander should set it to true when
they are loaded.

@vicuna
Copy link
Author

vicuna commented Feb 17, 2012

Comment author: @diml

None of these modules is loaded when you use custom quotations.

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: gerd

I think the issue deserves to think twice about it. Quotations are one of the camlp4 features that could also be ported to the standard parser, and having a clean syntax seems to be desirable.

We have both <:name< ... >> and << ... >>. It's only the latter form that makes problems here. One solution could be to disable this latter form by default, and to define a new command-line switch for enabling it (e.g. "-anon-quote name" for choosing the quotation "name" as the anonmyous one). This would work for both built-in and for custom quotations.

Of course, it would introduce a slight incompatibility with current code, but I think it's worth it for cleaning a messy corner of the language up.

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: @diml

Actually there is a problem with the first form too. For example this is legal with the standard parser:

let ( <: ) x y = x + y
let x = 1
let y = 1<:x<2

but not with camlp4.

By the way it is already possible to tell camlp4o not to parse quotations: there is the -no_quot option.

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: gerd

This is only a spacing problem, let y = 1 <: y < 2 is accepted. This is the same sort of problems as e.g. let (*) x y = x+y, and I don't think it needs fixing.

-no_quot opens the path to a paractical workaround, but IMHO the existence of this switch is only the proof that something is broken in the (lexical) design.

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: Hendrik Tews

I don't understand the remark about custom quotations. What do
you mean by that?

I would like to note that the problem is also present without
strange infix operators:

let a = (* << *) 5

is legal OCaml, but not if you parse with camlp4o.

I don't agree that 1 <:y< 2 is a spacing problem. The point is
that 1 <:y< 2 is legal OCaml but is not accepted by camlp4o. In
contrast, let (*) x y = ... is not legal OCaml (without spaces)
and ocamlc and camlp4o agree on that fact.

There is not so much documentation available about camlp4o. But
what is available says camlp4o parses standard OCaml plus stream
parsers. It is nowhere stated that you should use -no_quot or
that "<<" is not permitted inside comments in standard OCaml.

As long as the documentation claims that camlp4o is standard
OCaml, quotations should be disabled by default. Note that there
is a similar flag for lexing antiquotations. It is off by default
and turned on by those parsing extensions that add antiquotations
to the grammar.

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: @diml

I don't understand the remark about custom quotations. What do you mean by that?

Camlp4*QuotationExpander modules are not loaded when you use a syntax extension that defines quotations (other than the ones distributed with ocaml), and so quotations won't be parsed if Camlp4_config.quotations defaults to false. This is going to break a lot of code.

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: Hendrik Tews

Could you name a typical example for this custom quotation code?

Does it use Camlp4QuotationCommon?

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: @diml

Could you name a typical example for this custom quotation code?

The tyxml project from ocsigen uses quotations for example.

Does it use Camlp4QuotationCommon?

No. All theses modules are for expanding camlp4 ast quotations ("expr", "str_item", ...).

I think the doc need to be fixed. Where is it stated that camlp4o parses standard OCaml plus stream parsers ?

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: gerd

An example for a custom quotation parser is my pxp-pp XML preprocessor, see https://godirepo.camlcity.org/svn/lib-pxp/trunk/src/pxp-pp/ for code and http://projects.camlcity.org/projects/dl/pxp-1.2.2/doc/manual/html/ref/Intro_preprocessor.html for the manual. Generally, quotations are a good choice for any DSL you want to include and that ís totally foreign to OCaml.

The parsers I wrote just added the quotations with Quotation.add, and sometimes they recursively invoked the OCaml parser with Syntax.Gram.parse.

So yes, these custom quotation expanders exist, and they have nothing to do with Camlp4's internal use of quotations (for generating OCaml ASTs).

Btw, I don't think camlp4 must be absolutely compatible with the standard parser. It must just be predictable where it deviates, and it is better when the chance for surprises is low. However, camlp4 is too much of a hack to allow a full specification of it. (I guess this is the reason why the standard parser still exists.)

@vicuna
Copy link
Author

vicuna commented Feb 27, 2012

Comment author: Hendrik Tews

I don't think adding one line to all custom quotation parsers (to
set Camlp4_config.quotations) is asking too much. There have been
much bigger incompatible changes in the camlp4 past.

Besides ... why not letting Quotation.add set the flag?

I know of 3 places that state/suggest that camlp4o and
camlp4o.cma are compatible with standard OCaml:

@vicuna
Copy link
Author

vicuna commented Feb 28, 2012

Comment author: @diml

Besides ... why not letting Quotation.add set the flag?

I don't know, the user may want to explicitly disable quotations...

But i don't really see the problem here, when you use camlp4 you must expect camlp4 syntax, and quotations are part of the camlp4 syntax. If you want to parse regular ocaml with camlp4, just disable quotations. And quotations are not the only difference between the two parsers, for example:

  • ocaml silently convert (max_int+1) literals to min_int but not camlp4,
  • camlp4 accept { f x with ... } but ocaml requires parenthesis,
    ...

@vicuna
Copy link
Author

vicuna commented Sep 21, 2012

Comment author: @damiendoligez

Note that section 6.1 of the manual also says that <<, <:, and >> should not be used as identifiers if you're concerned with camlp4 compatibility.

@vicuna
Copy link
Author

vicuna commented Dec 7, 2016

Comment author: @diml

Camlp4 is now a separate project. Please re-open a ticket on github [1] if you are still interested in seeing the original issue fixed.

[1] https://github.com/ocaml/camlp4/issues

@vicuna vicuna closed this as completed Dec 7, 2016
@vicuna vicuna added the camlp4 label Mar 14, 2019
@vicuna vicuna assigned ghost Mar 14, 2019
@vicuna vicuna added the bug label Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant