#load "pa_extend.cmo";; open Location open Ast open Ident open Printf (* let () = Grammar.error_verbose := true *) let tloc (i,j) = (i.Lexing.pos_cnum,j.Lexing.pos_cnum) let nopos = (Lexing.dummy_pos, Lexing.dummy_pos) let mk loc x = Location.mk (tloc loc) x exception Error of string let error (i,j) s = Location.raise_loc i j (Error s) let error loc s = error (tloc loc) s let gram = Grammar.gcreate Ulexer.lex let parse_ident = U.mk let id_dummy = ident (U.mk "$$$") let label = parse_ident let ident s = ident (parse_ident s) let prog = Grammar.Entry.create gram "prog" let top_phrases = Grammar.Entry.create gram "toplevel phrases" let expr = Grammar.Entry.create gram "expression" let pat = Grammar.Entry.create gram "type/pattern expression" let regexp = Grammar.Entry.create gram "type/pattern regexp" let keyword = Grammar.Entry.create gram "keyword" let exp pos e = LocatedExpr (loc_of_pos (tloc pos),e) let rec multi_prod loc = function | [ x ] -> x | x :: l -> mk loc (Prod (x, multi_prod loc l)) | [] -> assert false let rec tuple = function | [ x ] -> x | x :: l -> Pair (x, tuple l) | [] -> assert false let tuple_queue = List.fold_right (fun x q -> Pair (x, q)) let char = mknoloc (Internal (Types.char Chars.any)) let string_regexp = Star (Elem char) let seq_of_string s = let s = Encodings.Utf8.mk s in let rec aux i j = if Encodings.Utf8.equal_index i j then [] else let (c,i) = Encodings.Utf8.next s i in c :: (aux i j) in aux (Encodings.Utf8.start_index s) (Encodings.Utf8.end_index s) let parse_char loc s = match seq_of_string s with | [ c ] -> c | _ -> error loc "Character litteral must have length 1" let include_stack = ref [] let protect_exn f g = try let x = f () in g (); x with e -> g (); raise e let localize_exn f = try f () with | Stdpp.Exc_located (_, (Location _ as e)) -> raise e (* | Stdpp.Exc_located ((i,j), e) -> raise_loc i j e *) | Stdpp.Exc_located ((i,j), e) -> raise_loc i.Lexing.pos_cnum j.Lexing.pos_cnum e let is_fun_decl = Grammar.Entry.of_parser gram "[is_fun_decl]" (fun strm -> match Stream.npeek 3 strm with | [ ("", "fun"); ("IDENT", _); ("", "(") ] | [ ("IDENT", _) ; ("", "(") ; _ ] -> () | _ -> raise Stream.Failure ) let if_then_else cond e1 e2 = Match (cond, [pat_true,e1; pat_false,e2]) let logical_and e1 e2 = if_then_else e1 e2 cst_false let logical_or e1 e2 = if_then_else e1 cst_true e2 let logical_not e = if_then_else e cst_false cst_true let apply_op2_noloc op e1 e2 = Apply (Apply (Var (parse_ident op), e1), e2) let apply_op2 loc op e1 e2 = exp loc (apply_op2_noloc op e1 e2) EXTEND GLOBAL: top_phrases prog expr pat regexp keyword; top_phrases: [ [ l = LIST0 phrase; ";;" -> List.flatten l ] ]; prog: [ [ l = LIST0 [ p = phrase ; OPT ";;" -> p ]; EOI -> List.flatten l ] ]; phrase: [ [ (f,p,e) = let_binding -> if f then [ mk loc (FunDecl e) ] else [ mk loc (LetDecl (p,e)) ] | (_,p,e1) = let_binding; "in"; e2 = expr LEVEL "top"-> [ mk loc (EvalStatement (exp loc (Match (e1,[p,e2])))) ] | "type"; x = IDENT; "="; t = pat -> [ mk loc (TypeDecl (ident x,t)) ] | "using"; name = IDENT; "="; cu = [ IDENT | STRING2 ] -> [ mk loc (Using (U.mk name, U.mk cu)) ] | "schema"; name = IDENT; "="; uri = STRING2 -> protect_op "schema"; [ mk loc (SchemaDecl (U.mk name, uri)) ] | (name,ns) = namespace_binding -> [ mk loc (Namespace (name, ns)) ] | (name,ns) = namespace_binding; "in"; e2 = expr LEVEL "top" -> let e = exp loc (NamespaceIn (name, ns, e2)) in [ mk loc (EvalStatement (exp loc e)) ] | "debug"; d = debug_directive -> [ mk loc (Directive (`Debug d)) ] | DIRECTIVE "#utf8" -> Ulexer.enc := Ulexing.Utf8; [ ] | DIRECTIVE "#latin1" -> Ulexer.enc := Ulexing.Latin1; [ ] | DIRECTIVE "#ascii" -> Ulexer.enc := Ulexing.Ascii; [ ] | DIRECTIVE "#quit" -> [ mk loc (Directive `Quit) ] | DIRECTIVE "#env" -> [ mk loc (Directive `Env) ] | DIRECTIVE "#print_schema"; name = IDENT -> [ mk loc (Directive (`Print_schema (U.mk name))) ] | DIRECTIVE "#print_type"; name = IDENT; schema_part = OPT [ "#"; typ = [ IDENT | keyword ]; kind = OPT [ "as"; k = schema_kind -> k] -> (kind, typ) ] -> (match schema_part with | None -> [ mk loc (Directive (`Print_type (U.mk name))) ] | Some (kind, typ) -> [ mk loc (Directive (`Print_schema_type (kind, U.mk name, U.mk typ))) ]) | DIRECTIVE "#dump_value"; e = expr -> [ mk loc (Directive (`Dump e)) ] | DIRECTIVE "#reinit_ns" -> [ mk loc (Directive `Reinit_ns) ] | DIRECTIVE "#help" -> [ mk loc (Directive `Help) ] | "include"; s = STRING2 -> let s = if Filename.is_relative s then Filename.concat (Location.current_dir ()) s else s in protect_op "File inclusion"; (* avoid looping; should issue an error ? *) (* it is possible to have looping with x/../x/../x/.. .... Need to canonicalize filename *) if List.mem s !include_stack then [] else ( include_stack := s :: !include_stack; Location.push_source (`File s); let saved_enc = !Ulexer.enc in Ulexer.enc := Ulexing.Latin1; protect_exn (fun () -> let chan = open_in s in protect_exn (fun () -> let input = Stream.of_channel chan in localize_exn (fun () -> Grammar.Entry.parse prog input)) (fun () -> close_in chan)) (fun () -> Ulexer.enc := saved_enc; Location.pop_source (); include_stack := List.tl !include_stack) ) ] | [ e = expr -> [ mk loc (EvalStatement e) ] ] ]; debug_directive: [ [ IDENT "filter"; t = pat; p = pat -> `Filter(t,p) | IDENT "accept"; p = pat -> `Accept p | IDENT "compile"; t = pat; p = LIST1 pat -> `Compile (t,p) | IDENT "sample"; t = pat -> `Sample t | IDENT "subtype"; t1 = pat; t2 = pat -> `Subtype (t1,t2) | IDENT "explain"; t0 = pat; t = pat; e = expr -> `Explain (t0,t,e) | IDENT "single"; t = pat -> `Single t | IDENT "approx"; p = pat; t = pat -> `Approx (p,t) ] ]; keyword: [ [ a = [ "map" | "match" | "with" | "try" | "xtransform" | "if" | "then" | "else" | "transform" | "fun" | "in" | "let" | "type" | "debug" | "include" | "and" | "validate" | "schema" | "namespace" | "ref" | "alias" | "not" | "as" | "where" | "external" ] -> a ] ]; expr: [ "top" RIGHTA [ "match"; e = SELF; "with"; b = branches -> exp loc (Match (e,b)) | "try"; e = SELF; "with"; b = branches -> exp loc (Try (e,b)) | "map"; e = SELF; "with"; b = branches -> exp loc (Map (e,b)) | "xtransform"; e = SELF; "with"; b = branches -> exp loc (Xtrans (e,b)) | "if"; e = SELF; "then"; e1 = SELF; "else"; e2 = SELF -> exp loc (if_then_else e e1 e2) | "transform"; e = SELF; "with"; b = branches -> exp loc (Transform (e,b)) | "validate"; e = SELF; "with"; (kind, schema, typ) = schema_ref -> exp loc (Validate (e, kind, schema, typ)) | "fun"; (f,a,b) = fun_decl -> exp loc (Abstraction { fun_name = f; fun_iface = a; fun_body = b }) | "external"; s = STRING2 -> exp loc (External (s,[])) | "external"; "{"; s = STRING2; pl = LIST0 pat; "}" -> exp loc (External (s,pl)) | (_,p,e1) = let_binding; "in"; e2 = expr LEVEL "top"-> exp loc (Match (e1,[p,e2])) | (name,ns) = namespace_binding; "in"; e2 = expr LEVEL "top" -> exp loc (NamespaceIn (name, ns, e2)) | e = expr; ":"; p = pat -> exp loc (Forget (e,p)) | e = expr; ":"; "?"; p = pat -> exp loc (Check (e,p)) | e1 = expr; ";"; e2 = expr -> exp loc (Match (e1, [pat_nil,e2])) | "ref"; p = pat; e = expr -> exp loc (Ref (e,p)) | "not"; e = expr -> exp loc (logical_not e) ] | [ e1 = expr; ":="; e2 = expr -> exp loc (Apply (Dot (e1, U.mk "set"), e2)) ] | [ e1 = expr; op = ["=" | "<=" | "<<" | ">>" | ">=" ]; e2 = expr -> let op = match op with | "<<" -> "<" | ">>" -> ">" | s -> s in apply_op2 loc op e1 e2 ] | [ e1 = expr; op = ["+" | "-" | "@" ]; e2 = expr -> apply_op2 loc op e1 e2 | e1 = expr; "||"; e2 = expr -> exp loc (logical_or e1 e2) | e = expr; "\\"; l = [IDENT | keyword ] -> exp loc (RemoveField (e, label l)) ] | [ e1 = expr; op = ["*"]; e2 = expr -> apply_op2 loc op e1 e2 | e1 = expr; "&&"; e2 = expr -> exp loc (logical_and e1 e2) | e = expr; op = "/"; p = pat LEVEL "simple" -> let tag = mk loc (Internal (Types.atom (Atoms.any))) in let att = mk loc (Internal Types.Record.any) in let any = mk loc (Internal (Types.any)) in let re = Star(Alt(SeqCapture(id_dummy,Elem p), Elem any)) in let ct = mk loc (Regexp re) in let p = mk loc (XmlT (tag, multi_prod loc [att;ct])) in let b = (p, Var (Id.value id_dummy)) in exp loc (Transform (e,[b])) ] | [ e = expr; "."; l = [IDENT | keyword ] -> exp loc (Dot (e, label l)) ] | [ e1 = SELF; IDENT "div"; e2 = expr -> apply_op2 loc "/" e1 e2 | e1 = SELF; IDENT "mod"; e2 = expr -> apply_op2 loc "mod" e1 e2 | e1 = SELF; e2 = expr -> exp loc (Apply (e1,e2)) ] | "no_appl" [ "("; l = LIST1 expr SEP ","; ")" -> exp loc (tuple l) | "["; l = LIST0 seq_elem; e = OPT [ ";"; e = expr -> e ]; loc_end = ["]" -> loc] -> let e = match e with Some e -> e | None -> cst_nil in let e = exp loc_end e in let (_,loc_end) = loc_end in let l = List.fold_right (fun x q -> match x with | `String (loc,i,j,s) -> exp loc (String (i,j,s,q)) | `Elems ((loc,_),x) -> exp (loc,loc_end) (Pair(x,q)) | `Explode x -> apply_op2_noloc "@" x q ) l e in exp loc l | "<"; t = [ "("; e = expr; ")" -> e | a = tag -> exp loc a ]; a = expr_attrib_spec; ">"; c = expr -> exp loc (Xml (t, Pair (a,c))) | "{"; r = [ expr_record_spec | -> exp loc (RecordLitt []) ]; "}" -> r | s = STRING2 -> let s = U.mk s in exp loc (String (U.start_index s, U.end_index s, s, cst_nil)) | a = IDENT -> exp loc (Var (U.mk a)) | "!"; e = expr -> exp loc (Apply (Dot (e, U.mk "get"), cst_nil)) | i = INT -> exp loc (Integer (Intervals.V.mk i)) | "`"; a = tag -> a | c = char -> exp loc (Char c) ] ]; tag: [ [ a = [ IDENT | keyword ] -> exp loc (Atom (parse_ident a)) ] ]; tag_type: [ [ IDENT "_" -> mk loc (Internal (Types.atom Atoms.any)) | a = [ IDENT | keyword ] -> mk loc (Cst (Atom (parse_ident a))) | t = ANY_IN_NS -> mk loc (NsT (parse_ident t)) ] ]; seq_elem: [ [ x = STRING1 -> let s = U.mk x in `String (loc, U.start_index s, U.end_index s, s) | e = expr LEVEL "no_appl" -> `Elems (loc,e) | "!"; e = expr LEVEL "no_appl" -> `Explode e ] ]; namespace_binding: [ [ "namespace"; name = [ name = [ IDENT | keyword ]; "=" -> parse_ident name | -> U.mk "" ]; uri = STRING2 -> let ns = Ns.mk (parse_ident uri) in (name,ns) ] ]; let_binding: [ [ "let"; is_fun_decl; OPT "fun"; (f,a,b) = fun_decl -> let f = match f with Some x -> x | None -> assert false in let p = mk loc (PatVar (Id.value f)) in let abst = { fun_name = Some f; fun_iface = a; fun_body = b } in let e = exp loc (Abstraction abst) in (true,p,e) | "let"; p = pat; "="; e = expr -> (false,p,e) | "let"; p = pat; ":"; t = pat; "="; e = expr -> (false,p, Forget (e,t)) | "let"; p = pat; ":"; "?"; t = pat; "="; e = expr -> (false,p, Check (e,t)) ] ]; fun_decl_after_lparen: [ (* need an hack to do this, because both productions would match [ OPT IDENT; "("; pat ] .... *) [ p1 = pat LEVEL "no_arrow"; res = [ "->"; p2 = pat; a = [ ";"; a = LIST0 arrow SEP ";" -> a | -> [] ]; ")"; b = branches -> `Classic (p2,a,b) | ":"; targ1 = pat; args = LIST0 [ ","; arg = pat; ":"; targ = pat -> (arg,targ) ]; ")"; others = LIST0 [ "("; args = LIST1 [ arg = pat; ":"; targ = pat -> (arg,targ) ] SEP ","; ")" -> args ]; ":"; tres = pat ; "="; body = expr -> `Compact (targ1,args,others,tres,body) ] -> match res with | `Classic (p2,a,b) -> (p1,p2)::a,b | `Compact (targ1,args,others,tres,body) -> let mkfun args = multi_prod nopos (List.map snd args), multi_prod nopos (List.map fst args) in let (tres,body) = List.fold_right (fun args (tres,body) -> let (targ,arg) = mkfun args in let e = Abstraction { fun_name = None; fun_iface = [targ,tres]; fun_body = [arg,body] } in let t = mknoloc (Arrow (targ,tres)) in (t,e) ) others (tres,body) in let (targ,arg) = mkfun ((p1,targ1) :: args) in [(targ,tres)],[(arg,body)] ] ]; fun_decl: [ [ f = OPT IDENT; "("; (a,b) = fun_decl_after_lparen -> let f = match f with Some x -> Some (ident x) | None -> None in (f,a,b) ] ]; arrow: [ [ t1 = pat LEVEL "no_arrow"; "->"; t2 = pat -> (t1,t2)] ]; branches: [ [ OPT "|"; l = LIST1 branch SEP "|" -> l ] ]; branch: [ [ p = pat LEVEL "no_arrow"; "->"; e = expr -> (p,e) ] ]; regexp: [ [ x = regexp; "|"; y = regexp -> match (x,y) with | Elem x, Elem y -> Elem (mk loc (Or (x,y))) | _ -> Alt (x,y) ] | [ x = regexp; y = regexp -> Seq (x,y) ] | [ x = regexp; "&"; y = regexp -> match (x,y) with | Elem x, Elem y -> Elem (mk loc (And (x,y))) | _ -> error loc "Conjunction not allowed in regular expression" ] | [ a = IDENT; "::"; x = regexp -> SeqCapture (ident a,x) ] | [ x = regexp; "*" -> Star x | x = regexp; "*?" -> WeakStar x | x = regexp; "+" -> Seq (x, Star x) | x = regexp; "+?" -> Seq (x, WeakStar x) | x = regexp; "?" -> Alt (x, Epsilon) | x = regexp; "??" -> Alt (Epsilon, x) ] | [ "("; x = LIST1 regexp SEP ","; ")" -> (match x with | [ x ] -> x | _ -> let x = List.map (function | Elem x -> x | _ -> error loc "Mixing regular expressions and products") x in Elem (multi_prod loc x)) | "("; a = IDENT; ":="; c = expr; ")" -> Elem (mk loc (Constant ((ident a,c)))) | "/"; p = pat LEVEL "simple" -> Guard p | IDENT "PCDATA" -> string_regexp | i = STRING1; "--"; j = STRING1 -> let i = Chars.V.mk_int (parse_char loc i) and j = Chars.V.mk_int (parse_char loc j) in Elem (mk loc (Internal (Types.char (Chars.char_class i j)))) | s = STRING1 -> List.fold_right (fun c accu -> let c = Chars.V.mk_int c in let c = Chars.atom c in Seq (Elem (mknoloc (Internal (Types.char c))), accu)) (seq_of_string s) Epsilon ] | [ e = pat LEVEL "simple" -> Elem e ] ]; schema_kind : [ [ IDENT "element" -> `Element | "type" -> `Type | IDENT "attribute" -> `Attribute | IDENT "attribute_group" -> `Attribute_group | IDENT "model_group" -> `Model_group ] ]; schema_ref: [ [ schema = IDENT; "#"; typ = [ IDENT | keyword ]; kind = OPT [ "as"; k = schema_kind -> k] -> (kind, U.mk schema, U.mk typ) ] ]; pat: [ [ x = pat; "where"; b = LIST1 [ a = IDENT; "="; y = pat -> (ident a,y) ] SEP "and" -> mk loc (Recurs (x,b)) ] | RIGHTA [ x = pat; "->"; y = pat -> mk loc (Arrow (x,y)) ] | "no_arrow" [ x = pat; "|"; y = pat -> mk loc (Or (x,y)) ] | "simple" [ x = pat; "&"; y = pat -> mk loc (And (x,y)) | x = pat; "\\"; y = pat -> mk loc (Diff (x,y)) ] | [ "{"; r = record_spec; "}" -> mk loc (Record (true,r)) | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r)) | "ref"; p = pat -> let get_fun = mk loc (Arrow (pat_nil, p)) and set_fun = mk loc (Arrow (p, pat_nil))in let fields = [ label "get", (get_fun, None); label "set", (set_fun, None) ] in mk loc (Record (false, fields)) | IDENT "_" -> mk loc (Internal Types.any) | "("; a = IDENT; ":="; c = expr; ")" -> mk loc (Constant (ident a,c)) | schema = IDENT; "#"; typ = [ IDENT | keyword ]; kind = OPT [ "as"; k = schema_kind -> k] -> mk loc (SchemaVar (kind, U.mk schema, U.mk typ)) | "!"; a = IDENT -> mk loc (Internal (Types.abstract (Types.Abstract.atom a))) | a = IDENT -> mk loc (PatVar (U.mk a)) | i = INT ; "--"; j = INT -> let i = Intervals.V.mk i and j = Intervals.V.mk j in mk loc (Internal (Types.interval (Intervals.bounded i j))) | i = INT -> let i = Intervals.V.mk i in mk loc (Internal (Types.interval (Intervals.atom i))) | "*"; "--"; j = INT -> let j = Intervals.V.mk j in mk loc (Internal (Types.interval (Intervals.left j))) | i = INT; "--"; "*" -> let i = Intervals.V.mk i in mk loc (Internal (Types.interval (Intervals.right i))) | i = char -> mk loc (Internal (Types.char (Chars.char_class i i))) | i = char ; "--"; j = char -> mk loc (Internal (Types.char (Chars.char_class i j))) | "`"; c = tag_type -> c | "("; l = LIST1 pat SEP ","; ")" -> multi_prod loc l | "["; r = [ r = regexp -> r | -> Epsilon ]; q = [ ";"; q = pat -> Some q | -> None ]; "]" -> let r = match q with | Some q -> let any = mk loc (Internal (Types.any)) in Seq(r,Seq(Guard q, Star (Elem any))) | None -> r in mk loc (Regexp r) | "<"; t = [ x = tag_type -> x | "("; t = pat; ")" -> t ]; a = attrib_spec; ">"; c = pat -> mk loc (XmlT (t, multi_prod loc [a;c])) | s = STRING2 -> let s = List.map (fun c -> mknoloc (Internal (Types.char (Chars.atom (Chars.V.mk_int c))))) (seq_of_string s) in let s = s @ [mknoloc (Internal (Sequence.nil_type))] in multi_prod loc s ] ]; or_else : [ [ OPT [ "else"; y = pat -> y ] ] ]; opt_field_pat: [ [ OPT [ "="; o = [ "?" -> true | -> false]; x = pat; y = or_else -> (o,x,y) ] ] ]; record_spec: [ [ r = LIST0 [ l = [IDENT | keyword ]; f = opt_field_pat -> let (o,x,y) = match f with | None -> (false, mknoloc (PatVar (U.mk l)), None) | Some z -> z in let x = if o then mk loc (Optional x) else x in (label l, (x,y)) ] SEP ";" -> r ] ]; char: [ [ c = STRING1 -> Chars.V.mk_int (parse_char loc c) ] ]; attrib_spec: [ [ r = LIST0 [ l = [IDENT | keyword ]; f = opt_field_pat; OPT ";" -> let (o,x,y) = match f with | None -> (false, mknoloc (PatVar (U.mk l)), None) | Some z -> z in let x = if o then mk loc (Optional x) else x in (label l, (x, y)) ] -> mk loc (Record (true,r)) | "("; t = pat; ")" -> t | "{"; r = record_spec; "}" -> mk loc (Record (true,r)) | "{|"; r = record_spec; "|}" -> mk loc (Record (false,r)) ] ]; opt_field_expr: [ [ OPT [ "="; x = expr LEVEL "no_appl" -> x ] ] ]; expr_record_spec: [ [ r = LIST1 [ l = [IDENT | keyword ]; x = opt_field_expr -> let x = match x with Some x -> x | None -> Var (U.mk l) in (label l,x) ] SEP ";" -> exp loc (RecordLitt r) ] ]; expr_attrib_spec: [ [ r = LIST1 [ l = [IDENT | keyword ]; x = opt_field_expr; OPT ";" -> let x = match x with Some x -> x | None -> Var (U.mk l) in (label l,x) ] -> exp loc (RecordLitt r) ] | [ e = expr LEVEL "no_appl" -> e | -> exp loc (RecordLitt []) ] ]; END module Hook = struct let expr = expr let pat = pat let keyword = keyword end let pat = Grammar.Entry.parse pat and expr = Grammar.Entry.parse expr and prog = Grammar.Entry.parse prog and top_phrases = Grammar.Entry.parse top_phrases let sync () = match !Ulexer.lexbuf with | None -> () | Some lb -> let rec aux () = match !Ulexer.last_tok with | ("",";;") | ("EOI","") -> () | _ -> Ulexer.last_tok := fst (Ulexer.token lb); aux () in aux () let sync () = try sync () with Ulexing.Error -> ()