Commit 0a13da1c authored by Kim Nguyễn's avatar Kim Nguyễn

Extend the q_symbol preprocessor to add a DEBUG directive.

parent 09c83a5e
......@@ -67,9 +67,9 @@ ifeq ($(INTERFACE), true)
OPT += -passopt -i
endif
ifeq ($(DEBUG), true)
ifneq ($(strip $(DEBUG)),)
OPT += -g
SYNTAX += -symbol DEBUG=\"1\"
SYNTAX += -symbol cduce_debug=\"$(DEBUG)\"
endif
OCAMLFIND = ocamlfind
......
......@@ -2,20 +2,43 @@ open Camlp4.PreCast
module Caml_syntax = Syntax
let symbols = ref []
let debug_symbols = ref []
let debug = ref false
let split_string c s =
let res = ref [] in
let buff = Buffer.create 32 in
let add s = if s <> "" then res:= s :: !res in
for i = 0 to String.length s - 1 do
match s.[i] with
d when d == c -> let f = Buffer.contents buff in
Buffer.clear buff; add f
| ('a'..'z' | '0'..'9' | '_' | 'A'..'Z') as d -> Buffer.add_char buff d
| _ -> ()
done;
let f = Buffer.contents buff in
add f;
!res
let define s =
let i =
try String.index 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_str = String.sub s (i + 1) (String.length s - i - 1) in
let value =
Gram.parse_string
Caml_syntax.expr (Loc.mk "<from-string>") (String.sub s (i + 1) (String.length s - i - 1))
Caml_syntax.expr (Loc.mk "<from-string>") value_str
in
symbols := (symbol, value) :: !symbols
symbols := (symbol, value) :: !symbols;
if symbol = "cduce_debug" then begin
debug := true;
let l = split_string ',' value_str in
debug_symbols := l @ !debug_symbols
end
EXTEND Caml_syntax.Gram
GLOBAL: Caml_syntax.str_item;
GLOBAL: Caml_syntax.str_item Caml_syntax.expr;
Caml_syntax.str_item: FIRST
[ [ "ifdef"; c = UIDENT; "then"; e1 = SELF;
......@@ -29,6 +52,22 @@ EXTEND Caml_syntax.Gram
| "ifndef"; c = UIDENT; "then"; e1 = SELF ->
if List.mem_assoc c !symbols then <:str_item<>> else e1
] ];
Caml_syntax.expr: BEFORE "simple"
[ [
"DEBUG" ; x = OPT [ x = LIDENT -> x ]; "("; e = Caml_syntax.expr; ")" ->
let flag =
match x with
None -> ""
| Some s -> s
in
let test = !debug && (flag = "" || List.mem flag !debug_symbols) in
Format.eprintf "Found Debug symbol %s, test = %b @\n%!" flag test;
if test then
e
else <:expr< () >>
]
];
END
let expr _ _ s =
......@@ -37,5 +76,5 @@ let expr _ _ s =
let _ =
Quotation.add "symbol" Quotation.DynAst.expr_tag expr;
Camlp4.Options.add "-symbol" (Arg.String define)
Camlp4.Options.add "-symbol" (Arg.String define)
"<symbol=value> Define a symbol"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment