Version française
Home     About     Download     Resources     Contact us    
Browse thread
DELETE_RULE arguments?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: blue storm <bluestorm.dylc@g...>
Subject: Re: [Caml-list] DELETE_RULE arguments?
On Sun, Jul 19, 2009 at 2:34 PM, Serge Leblanc<serge.leblanc@orange.fr> wrote:
> Otherwise, is there a way to know if the active syntax, is the revised or
> not?

The way used in Camlp4Parsers/Camlp4ListComprehension.ml is to try to
delete a rule and see if it fails :

  value is_revised =
    try do {
      DELETE_RULE Gram expr: "["; sem_expr_for_list; "::"; expr; "]" END;
      True
    } with [ Not_found -> False ];

It works fine in this case as the grammar rule have to be deleted
anyway, but if you don't want to affect the grammar you could test
parsing for failure. Something like :

  value is_revised =
    try do { Gram.parse_string Syntax.expr Loc.ghost "fun [ x -> x]"; True }
    with [ _ -> False];

Of course, those tests could fail given a syntax that is neither the
classic nor the revised syntax. Eg. if you write an extension adding
the fun [ .. ] syntax to the standard syntax and it would return true.
A safer way to test the used syntaxes is the Register.loaded_modules values :

# #camlp4o;;
# Camlp4.Register.loaded_modules;;
- : string list ref =
{contents =
  ["Camlp4OCamlParserParser"; "Camlp4OCamlRevisedParserParser";
   "Camlp4OCamlParser"; "Camlp4OCamlRevisedParser"]}

# #camlp4r;;
# Camlp4.Register.loaded_modules;
- : ref (list string) =
{val=["Camlp4OCamlRevisedParserParser"; "Camlp4OCamlRevisedParser"]}