Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 =
    Camlp4OCamlParser.Make
      (Camlp4OCamlRevisedParser.Make
        (Camlp4.OCamlInitSyntax.Make(Ast)(Gram)(Quotation)));;

  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
  str_item_quotexpander;
    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?

John.