[go: up one dir, main page]

File: q_symbol.ml

package info (click to toggle)
cduce 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,180 kB
  • ctags: 3,176
  • sloc: ml: 20,028; xml: 5,546; makefile: 427; sh: 133
file content (41 lines) | stat: -rw-r--r-- 1,274 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
open Camlp4.PreCast
module Caml_syntax = Syntax

let symbols = ref []

let define s =
  let i = 
    try String.index s '=' 
    with Not_found -> failwith ("Invalid symbol definition :" ^ s) in
  let symbol = String.sub s 0 i in
  let value =
    Gram.parse_string
      Caml_syntax.expr (Loc.mk "<from-string>") (String.sub s (i + 1) (String.length s - i - 1))
  in
  symbols := (symbol, value) :: !symbols

EXTEND Caml_syntax.Gram
  GLOBAL: Caml_syntax.str_item;

  Caml_syntax.str_item: FIRST
    [ [ "ifdef"; c = UIDENT; "then"; e1 = SELF;
        "else"; e2 = SELF ->
          if List.mem_assoc c !symbols then e1 else e2
      | "ifdef"; c = UIDENT; "then"; e1 = SELF ->
          if List.mem_assoc c !symbols then e1 else <:str_item<>>
      | "ifndef"; c = UIDENT; "then"; e1 = SELF;
        "else"; e2 = SELF ->
          if List.mem_assoc c !symbols then e2 else e1
      | "ifndef"; c = UIDENT; "then"; e1 = SELF ->
          if List.mem_assoc c !symbols then <:str_item<>> else e1
      ] ];
END

let expr _ _ s =
  try List.assoc s !symbols
  with Not_found -> failwith ("No definition for symbol " ^ s)

let _ =
  Quotation.add "symbol" Quotation.DynAst.expr_tag expr;
  Camlp4.Options.add "-symbol" (Arg.String define) 
    "<symbol=value> Define a symbol"