From bf6ee55213a3a06329802582cd5c1fdcec5dca00 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 8 Mar 2018 22:19:40 -0500 Subject: [PATCH] implemented driver and scanning loop --- deps.mk | 4 ++-- main.ml | 63 ++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/deps.mk b/deps.mk index 856b14c..3fa2278 100644 --- a/deps.mk +++ b/deps.mk @@ -6,5 +6,5 @@ lex_ocaml.cmo lex_ocaml.cmi : colormap.cmi lex_ocaml.ml 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 diff --git a/main.ml b/main.ml index 3907fff..86d808e 100644 --- a/main.ml +++ b/main.ml @@ -1,17 +1,52 @@ -(* -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 -> () -- 2.52.0