X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fparser.ml;h=f9e7989176ce0402bf25c056124fa08c0ebe3433;hb=0197354468e40860a3a104b9ea5b68854a22cfca;hp=79650955de2f5c5f7fa73606dee2e68a9613f73d;hpb=e76ebd06143e44b57e2da545c2570cf7f7200908;p=fireball-separation.git diff --git a/ocaml/parser.ml b/ocaml/parser.ml index 7965095..f9e7989 100644 --- a/ocaml/parser.ml +++ b/ocaml/parser.ml @@ -1,7 +1,7 @@ exception ParsingError of string;; let mk_app x y = Num.mk_app x y;; -let mk_lam x = `Lam(true, x);; +let mk_lam x g = `Lam(true, x, g);; let mk_var x = `Var(x, -666);; let isAlphaNum c = let n = Char.code c in @@ -10,30 +10,31 @@ let isSpace c = c = ' ' || c = '\n' || c = '\t' ;; (* FIXME *) let mk_var' (bound, free) x = - if List.mem x bound + if x <> "@" && List.mem x bound then free, mk_var (Util.index_of x bound) - else if List.mem x free + else if x <> "@" && List.mem x free then free, mk_var (List.length bound + Util.index_of x free) else (free @ [x]), mk_var (List.length bound + List.length free) ;; let mk_app' = function - | [] -> raise (ParsingError "bug") + | [] -> assert false | t :: ts -> List.fold_left mk_app t ts ;; let explode s = + let len = String.length s in let rec aux i l = - if i < 0 then l else aux (i - 1) (s.[i] :: l) - in aux (String.length s - 1) [] + if i >= len || s.[i] = '#' then l else aux (i+1) (s.[i] :: l) + in List.rev (aux 0 []) ;; let implode l = - let res = String.create (List.length l) in + let res = Bytes.create (List.length l) in let rec aux i = function | [] -> res - | c :: l -> res.[i] <- c; aux (i + 1) l in - aux 0 l + | c :: l -> Bytes.set res i c; aux (i + 1) l in + Bytes.to_string (aux 0 l) ;; let rec strip_spaces = function @@ -44,7 +45,12 @@ let rec strip_spaces = function let read_var s = let rec aux = function | [] -> None, [] - | c::cs as x -> if isAlphaNum c + | c::cs as x -> + if c = '@' then + (if cs <> [] && (let hd = List.hd cs in hd = '@' || isAlphaNum hd) + then raise (ParsingError ("Unexpected `"^String.make 1 (List.hd cs)^"` after `@`.")) + else Some['@'], cs) + else if isAlphaNum c then match aux cs with | (Some x), cs' -> Some (c :: x), cs' | None, cs' -> (Some [c]), cs' @@ -59,7 +65,7 @@ let read_var' (bound, free as vars) s = | Some varname, cs -> let free, v = mk_var' vars varname in Some [v], cs, (bound, free) - | _, _ -> raise (ParsingError ("Can't read variable")) + | None, _ -> raise (ParsingError ("Can't read variable")) ;; let rec read_smt vars = @@ -68,18 +74,27 @@ let rec read_smt vars = | Some x, cs -> match strip_spaces cs with | [] -> false | c::_ -> c = '.' + in let rec read_garbage (bound, free) = function + | ',' :: cs -> + (match read_smt (bound,free) cs with + | Some [x], cs, (_, free) -> + let g, cs, free = read_garbage (bound,free) cs in x::g, cs, free + | _, _, _ -> raise (ParsingError "Garbage expected after ','")) + | cs -> [], cs, free in let read_lambda (bound, free) cs = ( match read_var (strip_spaces cs) with | Some varname, cs -> - let vars' = (varname::bound, free) in + let bound' = varname::bound in (match strip_spaces cs with - | [] -> raise (ParsingError "manca dopo variabile lambda") - | c::cs -> (if c = '.' then (match read_smt vars' cs with - | None, _, _ -> raise (ParsingError "manca corpo lambda") - | Some [x], y, (_, free) -> Some [mk_lam x], y, (bound, free) - | Some _, _, _ -> raise (ParsingError "???") - ) else raise (ParsingError "manca . nel lambda") - )) + | [] -> raise (ParsingError "Lambda expression incomplete") + | '.' :: cs -> (match read_smt (bound', free) cs with + | None, _, _ -> raise (ParsingError "Lambda body expected") + | Some [t], cs, (_, free) -> + let g, cs, free = read_garbage (bound', free) (strip_spaces cs) in + Some [mk_lam t g], cs, (bound, free) + | Some _, _, _ -> assert false + ) + | _::_ -> raise (ParsingError "Expected `.` after variable in lambda")) | _, _ -> assert false ) in let rec aux vars cs = match strip_spaces cs with @@ -87,7 +102,7 @@ let rec read_smt vars = | c::_ as x -> let tms, cs, vars = ( if c = '(' then read_pars vars x - else if c = ')' then (None, x, vars) + else if c = ')' || c = ',' then (None, x, vars) else if check_if_lambda x then read_lambda vars x else read_var' vars x) in match tms with @@ -96,7 +111,7 @@ let rec read_smt vars = | None, cs, vars -> Some [tm], cs, vars | Some ts, cs, vars -> Some (tm :: ts), cs, vars ) - | Some _ -> raise (ParsingError "bug") + | Some _ -> assert false | None -> None, x, vars in fun cs -> match aux vars cs with | None, cs, vars -> None, cs, vars @@ -108,21 +123,21 @@ and read_pars vars = function let cs = strip_spaces cs in match cs with | [] -> None, [], vars - | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError ") mancante") - ) else raise (ParsingError "???") + | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError "Missing `)`") + ) else assert false ;; let parse x = match read_smt ([],[]) (explode x) with | Some [y], [], _ -> y - | _, _, _ -> raise (ParsingError "???") + | _, _, _ -> assert false ;; let parse_many strs = let f (x, y) z = match read_smt y (explode z) with | Some[tm], [], vars -> (tm :: x, vars) - | _, _, _ -> raise (ParsingError "???") - in let aux = List.fold_left f ([], ([], [])) (* index zero is reserved *) + | _, _, _ -> assert false + in let aux = List.fold_left f ([], ([], [])) in let (tms, (_, free)) = aux strs in (List.rev tms, free) ;; @@ -156,7 +171,7 @@ let problem_of_string s = let strs = [div] @ ps @ conv in if List.length ps = 0 && List.length conv = 0 - then raise (ParsingError "empty problem"); + then raise (ParsingError "Parsed empty problem"); (* parse' *) let (tms, free) = parse_many strs in @@ -174,7 +189,7 @@ let problem_of_string s = let rec aux lev : nf -> nf = function | `I((n,_), args) -> `I((n,(if lev = 0 then 0 else 1) + Listx.length args), Listx.map (aux lev) args) | `Var(n,_) -> fix lev n - | `Lam(_,t) -> `Lam (true, aux (lev+1) t) + | `Lam(_,t,g) -> `Lam (true, aux (lev+1) t, List.map (aux (lev+1)) g) | `Match _ | `N _ -> assert false in let all_tms = List.map (aux 0) (tms :> Num.nf list) in @@ -185,17 +200,17 @@ let div = if not div_provided then None else match div with | `I _ as t -> Some t - | _ -> raise (ParsingError "div is not an inert or BOT in the initial problem") in + | _ -> raise (ParsingError "D is not an inert in the initial problem") in let conv = Util.filter_map ( function | #i_n_var as t -> Some t | `Lam _ -> None - | _ -> raise (ParsingError "A term in conv is not i_n_var") + | _ -> raise (ParsingError "A C-term is not i_n_var") ) conv in let ps = List.map ( function | #i_n_var as y -> y - | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y)) + | _ -> raise (ParsingError "An N-term is not i_n_var") ) ps in name, div, conv, ps, free ;;