English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
More problems with simple quotation parser
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-09-21 (18:47)
From: Harrison, John R <john.r.harrison@i...>
Subject: More problems with simple quotation parser
I've hit another problem with the simple string transformation
quotation parser (see my previous message). For the sake of this
example, here is a somewhat simplified variant, which I turn
into "Quotexpander.cma":

  open Camlp4.PreCast;;

  module Caml =

  let quotexpander s = "\"" ^ String.escaped s ^ "\"";;

  let patt_quotexpander loc _loc_name_opt str =
    Gram.parse_string Caml.patt loc (quotexpander str);;

  let expr_quotexpander loc _loc_name_opt str =
    Gram.parse_string Caml.expr loc (quotexpander str);;

  let str_item_quotexpander loc loc_name_opt str =
    <:str_item@loc< $exp: expr_quotexpander loc loc_name_opt str$ >>;;

  let () =
    Syntax.Quotation.add "" Syntax.Quotation.DynAst.expr_tag expr_quotexpander;
    Syntax.Quotation.add "" Syntax.Quotation.DynAst.str_item_tag
    Syntax.Quotation.add "" Syntax.Quotation.DynAst.patt_tag patt_quotexpander;;

This doesn't always seem to work properly when quotations are
subexpressions of larger expressions. Some of the issues seem to
be lexical:

          Objective Caml version 3.10.0

  # #load "camlp4o.cma";;
          Camlp4 Parsing version 3.10.0

  # #load "Quotexpander.cma";;
  # <<ok>>;;
  - : string = "ok"
  # (<<ok>>);;
  - : string = "ok"
  # [<<not ok>>];;
  Parse error: illegal begin of top_phrase
  # [ <<not ok>> ];;
  - : string list = ["not ok"]

Others I'm not so sure about:

  # <<not>> ^ <<ok>>;;
  # Parse error: illegal begin of top_phrase
  # (<<not>>) ^ (<<ok>>);;
  - : string = "notok"

Any idea what's going on? Do I need to put some kind of wrapping
round the transformed phrase?