caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Matthieu Wipliez <mwipliez@yahoo.fr>
To: O'Caml Mailing List <caml-list@yquem.inria.fr>
Subject: Re : [Caml-list] Re: camlp4 stream parser syntax
Date: Sun, 8 Mar 2009 15:55:47 +0000 (GMT)	[thread overview]
Message-ID: <567172.30775.qm@web27002.mail.ukl.yahoo.com> (raw)
In-Reply-To: <1089D291-76A7-408C-BCA4-91F6B47843C4@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1452 bytes --]

> I don't think this will work elegantly.
> 
> Static first makes a Structure (is make the right term?) and then makes a bunch 
> of other modules using it. A custom Structure will be needed to downcase the 
> keywords before inserting them into the hash table, so Static will need to be 
> duplicated as well.

Well I just duplicated Static to Static1 (and added Camlp4.Struct.Grammar where necessary) and replaced:
  module Structure = Camlp4.Struct.Grammar.Structure.Make Lexer;
by:
  module Structure = struct
        include Camlp4.Struct.Grammar.Structure.Make Lexer;
        
        value using { gkeywords = table; gfilter = filter } kwd =
            let kwd = String.lowercase kwd in
        let r = try Hashtbl.find table kwd with
                [ Not_found ->
                    let r = ref 0 in do { Hashtbl.add table kwd r; r } ]
        in do { Token.Filter.keyword_added filter kwd (r.val = 0);
                incr r };
    end;

This way, I redefine "using" to my liking, the only modification being the lower-casing on the first line.

Structure is then passed to other functors as usual.
Note that you need to compile Static1 with camlp4r because it is revised syntax (in ocamlbuild _tags this is camlp4r, use_camlp4).
This seems to work (you need the lowercase in match_keyword too btw): I have "acTIon" and "actiON" in the parser, and parses "action" in input files.

Cheers,
Matthieu



      

[-- Attachment #2: Static1.ml --]
[-- Type: application/octet-stream, Size: 3481 bytes --]

(****************************************************************************)
(*                                                                          *)
(*                              Objective Caml                              *)
(*                                                                          *)
(*                            INRIA Rocquencourt                            *)
(*                                                                          *)
(*  Copyright  2006   Institut National de Recherche  en  Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed under   *)
(*  the terms of the GNU Library General Public License, with the special   *)
(*  exception on linking described in LICENSE at the top of the Objective   *)
(*  Caml source tree.                                                       *)
(*                                                                          *)
(****************************************************************************)

(* Authors:
 * - Daniel de Rauglaudre: initial version
 * - Nicolas Pouillard: refactoring
*)

open Camlp4;

value uncurry f (x,y) = f x y;
value flip f x y = f y x;

module Make (Lexer : Sig.Lexer)
: Sig.Grammar.Static with module Loc = Lexer.Loc
                        and module Token = Lexer.Token
= struct
  module Structure = struct
		include Camlp4.Struct.Grammar.Structure.Make Lexer;
		
		value using { gkeywords = table; gfilter = filter } kwd =
			let kwd = String.lowercase kwd in
	    let r = try Hashtbl.find table kwd with
	            [ Not_found ->
	                let r = ref 0 in do { Hashtbl.add table kwd r; r } ]
	    in do { Token.Filter.keyword_added filter kwd (r.val = 0);
	            incr r };
	end;
  module Delete = Camlp4.Struct.Grammar.Delete.Make Structure;
  module Insert = Camlp4.Struct.Grammar.Insert.Make Structure;
  module Fold = Camlp4.Struct.Grammar.Fold.Make Structure;
  include Structure;

  value gram =
    let gkeywords = Hashtbl.create 301 in
    {
      gkeywords = gkeywords;
      gfilter = Token.Filter.mk (Hashtbl.mem gkeywords);
      glexer = Lexer.mk ();
      warning_verbose = ref True; (* FIXME *)
      error_verbose = Camlp4_config.verbose
    };

  module Entry = struct
    module E = Camlp4.Struct.Grammar.Entry.Make Structure;
    type t 'a = E.t 'a;
    value mk = E.mk gram;
    value of_parser name strm = E.of_parser gram name strm;
    value setup_parser = E.setup_parser;
    value name = E.name;
    value print = E.print;
    value clear = E.clear;
    value dump = E.dump;
    value obj x = x;
  end;

  value get_filter () = gram.gfilter;

  value lex loc cs = gram.glexer loc cs;

  value lex_string loc str = lex loc (Stream.of_string str);

  value filter ts = Token.Filter.filter gram.gfilter ts;

  value parse_tokens_after_filter entry ts = Entry.E.parse_tokens_after_filter entry ts;

  value parse_tokens_before_filter entry ts = parse_tokens_after_filter entry (filter ts);

  value parse entry loc cs = parse_tokens_before_filter entry (lex loc cs);

  value parse_string entry loc str = parse_tokens_before_filter entry (lex_string loc str);

  value delete_rule = Delete.delete_rule;

  value srules e rl =
    Stree (List.fold_left (flip (uncurry (Insert.insert_tree e))) DeadEnd rl);
  value sfold0 = Fold.sfold0;
  value sfold1 = Fold.sfold1;
  value sfold0sep = Fold.sfold0sep;
  (* value sfold1sep = Fold.sfold1sep; *)

  value extend = Insert.extend;

end;

  reply	other threads:[~2009-03-08 15:55 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-07 22:38 Joel Reymont
2009-03-07 22:52 ` Joel Reymont
2009-03-07 23:21   ` Re : [Caml-list] " Matthieu Wipliez
2009-03-07 23:42     ` Joel Reymont
2009-03-08  0:40     ` Joel Reymont
2009-03-08  1:08       ` Re : " Matthieu Wipliez
2009-03-08  8:25         ` Joel Reymont
2009-03-08  9:37           ` Daniel de Rauglaudre
2009-03-08  9:51             ` Joel Reymont
2009-03-08 10:27               ` Daniel de Rauglaudre
2009-03-08 10:35                 ` Joel Reymont
2009-03-08 11:07                   ` Joel Reymont
2009-03-08 11:28                     ` Daniel de Rauglaudre
2009-03-08 11:45           ` Re : Re : " Matthieu Wipliez
2009-03-08 11:52             ` Joel Reymont
2009-03-08 13:33               ` Re : " Matthieu Wipliez
2009-03-08 13:59                 ` Joel Reymont
2009-03-08 14:09                   ` Re : " Matthieu Wipliez
2009-03-08 14:30                     ` Joel Reymont
2009-03-08 15:07                       ` Re : " Matthieu Wipliez
2009-03-08 15:24                         ` Joel Reymont
2009-03-08 15:32                           ` Re : " Matthieu Wipliez
2009-03-08 15:39                             ` Joel Reymont
2009-03-08 15:46                             ` Joel Reymont
2009-03-08 15:55                               ` Matthieu Wipliez [this message]
2009-03-08 16:58                                 ` Re : " Joel Reymont
2009-03-08 17:04                                   ` Re : " Matthieu Wipliez
2009-03-08 17:15                                     ` Joel Reymont
2009-03-08  9:34         ` Joel Reymont
2009-03-07 23:52 ` [Caml-list] " Jon Harrop
2009-03-07 23:53   ` Joel Reymont
2009-03-08  0:12     ` Jon Harrop
2009-03-08  0:20       ` Re : " Matthieu Wipliez
2009-03-08  0:29         ` Jon Harrop
2009-03-08  0:30         ` Re : " Joel Reymont
2009-03-08  0:37           ` Re : " Matthieu Wipliez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=567172.30775.qm@web27002.mail.ukl.yahoo.com \
    --to=mwipliez@yahoo.fr \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).