Skip to content

Commit

Permalink
Compat with older OCamls (up to 4.00)
Browse files Browse the repository at this point in the history
- remove 'match with exception' :(
- add a dumb preprocessor for string quotations (quicker and easier than other rewriting solutions)
  • Loading branch information
AltGr committed Sep 14, 2015
1 parent 2d8ddc6 commit 8a8335c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@ opam-user-setup: _build/ousMain.native
.PHONY: ALWAYS
ALWAYS:

NEEDPP != ocaml -vnum | awk -F. '{if ($$1<4 || ($$1==4 && $$2<=1)) print "yes"}'
ifeq ($(NEEDPP),yes)
PP = -pp $(shell pwd)/pp_401.ml
endif

PACKAGES = unix re re.pcre cmdliner

SUBDIRS = ocamltop emacs vim sublime gedit

_build/ousMain.native: ALWAYS
ocamlbuild -r -I . -Is ocamltop,emacs,vim,sublime,gedit -use-ocamlfind -tag debug -pkgs unix,re,re.pcre,cmdliner ousMain.native
ocamlbuild -r \
-tag debug \
-I . $(patsubst %,-I %,$(SUBDIRS)) \
-use-ocamlfind $(patsubst %,-pkg %,$(PACKAGES)) \
$(PP) \
ousMain.native

user-setup.install: ALWAYS
echo 'bin: "opam-user-setup"' > $@
Expand Down
2 changes: 1 addition & 1 deletion opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ depends: [
"re"
]
depopts: ["tuareg" "merlin" "ocp-indent" "ocp-index"]
available: [ocaml-version >= "4.02"]
available: [ocaml-version >= "4.00"]
post-messages: [
"To setup or update your editors, run 'opam user-setup install'."
{success}
Expand Down
15 changes: 9 additions & 6 deletions ousMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ module Chunk(E: EditorConfig) = struct
let rec aux acc = function
| [] -> List.rev acc
| line :: lines ->
match Re.exec re_head line with
| exception Not_found -> aux (Line line :: acc) lines
| ss ->
let r = try Some (Re.exec re_head line) with Not_found -> None in
match r with
| None -> aux (Line line :: acc) lines
| Some ss ->
let tool = Re.get ss 1 in
let md5 = Digest.from_hex (Re.get ss 2) in
let rec read_chunk chunk_contents = function
Expand All @@ -81,10 +82,12 @@ module Chunk(E: EditorConfig) = struct
aux (Line line :: acc)
(List.rev_append chunk_contents (cline::lines))
) else
match Re.exec re_foot cline with
| exception Not_found ->
let r = try Some (Re.exec re_foot cline)
with Not_found -> None in
match r with
| None ->
read_chunk (cline::chunk_contents) lines
| ss ->
| Some ss ->
let endtool = Re.get ss 1 in
if endtool <> tool then
msg "Warning: chunk for %S closed as %S in %s"
Expand Down
11 changes: 8 additions & 3 deletions ousMisc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ open OusTypes

let msg fmt = Printf.kprintf print_endline fmt

external (@@) : ('a -> 'b) -> 'a -> 'b = "%apply"

external (|>) : 'a -> ('a -> 'b) -> 'b = "%revapply"

let (/) = Filename.concat

let (@>) f g x = g (f x)
Expand All @@ -15,9 +19,10 @@ let lines_of_string s =

let lines_of_channel ic =
let rec aux acc =
match input_line ic with
| s -> aux (s::acc)
| exception End_of_file -> acc
let l = try Some (input_line ic) with End_of_file -> None in
match l with
| Some s -> aux (s::acc)
| None -> acc
in
List.rev (aux [])

Expand Down
6 changes: 6 additions & 0 deletions ousMisc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ val (/): string -> string -> string
(** Reverse composition *)
val (@>): ('a -> 'b) -> ('b -> 'c) -> ('a -> 'c)

(** Function application, with lower priority *)
val (@@): ('a -> 'b) -> 'a -> 'b

(** Reverse function application *)
val (|>): 'a -> ('a -> 'b) -> 'b

module StringMap: Map.S with type key = string
type 'a stringmap = 'a StringMap.t

Expand Down
50 changes: 50 additions & 0 deletions pp_401.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env ocaml

let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#require "re";;

(** This is a dumb, limited rewriter for the [{xxx| |xxx}] quotations, that
weren't available in OCaml 4.01. Only one beginning/end token is allowed per
line, no error handling, probably escaping bugs... But this is enough
for just allowing to compile on earlier OCaml versions. *)

let start_quot_re = Re.(compile (seq [char '{'; group (rep alnum); char '|']))
let end_quot_re name = Re.(compile (seq [char '|'; str name; char '}']))

let rec next ic =
let line = input_line ic in
let quot = try Some (Re.exec start_quot_re line) with Not_found -> None in
match quot with
| Some subs ->
let match_start, match_end = Re.get_ofs subs 0 in
print_string (String.sub line 0 match_start);
let end_re = end_quot_re (Re.get subs 1) in
let rec get_s acc =
let line = input_line ic in
try
let subs = Re.exec end_re line in
let match_start, match_end = Re.get_ofs subs 0 in
let ss = List.rev (String.sub line 0 match_start :: acc) in
print_char '\"';
List.iter (fun s ->
print_string (String.escaped s);
print_string "\\n\\\n")
ss;
print_char '\"';
print_string
(String.sub line match_end (String.length line - match_end));
print_char '\n'
with Not_found -> get_s (line::acc)
in
get_s [String.sub line match_end (String.length line - match_end)];
next ic
| None ->
print_string line; print_char '\n'; next ic

let () =
let ic = open_in (Sys.argv.(1)) in
try next ic with End_of_file -> close_in ic

0 comments on commit 8a8335c

Please sign in to comment.