lex_ocaml.cmx lex_ocaml.o lex_ocaml.cmi : colormap.cmi colormap.cmx lex_ocaml.ml
lex_ruby.cmo lex_ruby.cmi : colormap.cmi lex_ruby.ml
lex_ruby.cmx lex_ruby.o lex_ruby.cmi : colormap.cmi colormap.cmx lex_ruby.ml
-main.cmo main.cmi : main.ml
-main.cmx main.o main.cmi : main.ml
+main.cmo main.cmi : lex_ruby.cmi lex_ocaml.cmi lex_cpp.cmi colormap.cmi main.ml
+main.cmx main.o main.cmi : lex_ruby.cmi lex_ruby.cmx lex_ocaml.cmi lex_ocaml.cmx lex_cpp.cmi lex_cpp.cmx colormap.cmi colormap.cmx main.ml
-(*
-let make lexer =
- let ctx = { lbuf = lexer.lexbuf; pos = 0; } in
- (try while true do lexer.scanfn ctx lexer.lexbuf done
- with Eof -> ());
-*)
+type filetype = {
+ syntax : Colormap.ctx -> Lexing.lexbuf -> unit;
+ names : string list;
+ exts : string list;
+}
-while true do
- try
- let chunk = really_input_string stdin (read_int ()) in
- Printf.printf "chunk: '%s'\n" chunk;
+let filetypes = [
+ {
+ syntax = Lex_cpp.scan;
+ names = [];
+ exts = [".c"; ".h"; ".cpp"; ".hpp"; ".cc"; ".c++"; ".cxx"]
+ };
+ {
+ syntax = Lex_ruby.scan;
+ names = ["Rakefile"; "rakefile"; "gpkgfile"];
+ exts = [".rb"]
+ };
+ {
+ syntax = Lex_ocaml.scan;
+ names = [];
+ exts = [".ml"; ".mll"; ".mli"]
+ }
+]
+
+let pick_syntax path =
+ let name = Filename.basename path in
+ let ext = Filename.extension path in
+ let match_ftype ftype =
+ (List.exists ((=) name) ftype.names) ||
+ (List.exists ((=) ext) ftype.exts)
+ in
+ (List.find_opt match_ftype filetypes)
+
+let scan_string lexfn string =
+ let lbuf = Lexing.from_string string in
+ let ctx = Colormap.({ lbuf = lbuf; pos = 0; }) in
+ try while true do lexfn ctx lbuf done
+ with Colormap.Eof ->
+ Printf.printf "0,0,0\n";
flush stdout
- with
- | Failure _ -> ()
- | End_of_file -> exit 0
-done
+
+let rec scan_input lexfn =
+ try scan_string lexfn (really_input_string stdin (read_int ())) with
+ | Failure _ -> scan_input lexfn
+ | End_of_file -> ()
+
+let () =
+ if (Array.length Sys.argv) >= 2 then
+ match pick_syntax Sys.argv.(1) with
+ | Some ft -> scan_input ft.syntax
+ | None -> ()