Commits

camlspotter committed eceb3b0

filter => tokenstr

Comments (0)

Files changed (5)

     parser
     lexer
     reader
-    filter
+    tokenstr
     main
 
 lexer.cmi: parser.cmi

filter.ml

-open Sexplib.Conv
-open Pos
-open Reader
-open Parser
-
-type 'a t = 
-  | Cons of 'a * 'a t lazy_t * in_channel
-  | Null
-
-type 'a info = {
-  space : string;
-  token : 'a;
-  region : Region.t;
-  substr : string;
-}
-
-let of_channel ic = 
-  try
-    let reader = LexReader.create_from_channel ic in
-    let rec loop last_region = 
-      let token = LexReader.lex reader Lexer.token in
-      let region = LexReader.region reader in
-      let space_between = 
-        let last_end = (snd last_region).Position.pos_cnum in
-        LexReader.substring 
-          reader last_end ((fst region).Position.pos_cnum - last_end)
-      in
-      (* token's string *)
-      let substr = LexReader.current_substring reader in
-      
-      Cons ({ space = space_between; token; region; substr },
-            lazy (match token with
-            | EOF -> Null
-            | _ -> loop region),
-            ic)
-    in
-    loop Region.zero
-  with
-  | e -> raise e
-
-let of_path path =
-  let ic = open_in path in
-  of_channel ic
-
-let close = function
-  | lazy Null -> ()
-  | lazy (Cons (_, _, ic)) -> close_in ic
-
-let destr = function
-  | lazy Null -> None
-  | lazy (Cons (car, cdr, _ic)) -> Some (car, cdr)
 
 (* true means I found something to make the token a block *)
 let rec lparen_read_ahead lnum str =
-  match Filter.destr str with
+  match Tokenstr.destr str with
   | None -> false
-  | Some (i, _) when Region.lnum i.Filter.region <> lnum -> false
+  | Some (i, _) when Region.lnum i.Tokenstr.region <> lnum -> false
   | Some (i, str) ->
-      match i.Filter.token with
+      match i.Tokenstr.token with
       | FUN -> false
       | FUNCTION -> false
       | COMMENT -> lparen_read_ahead lnum str
       | _ -> true
 
 let rec lbrace_read_ahead lnum str =
-  match Filter.destr str with
+  match Tokenstr.destr str with
   | None -> false
-  | Some (i, _) when Region.lnum i.Filter.region <> lnum -> false
+  | Some (i, _) when Region.lnum i.Tokenstr.region <> lnum -> false
   | Some (i, str) ->
-      match i.Filter.token with
+      match i.Tokenstr.token with
       | COMMENT -> lparen_read_ahead lnum str
       | _ -> true
 
   let printer = lines in
     
   List.iter (fun path ->
-    let str = lazy (if path = "" then Filter.of_channel stdin else Filter.of_path path) in
+    let str = if path = "" then Tokenstr.of_channel stdin else Tokenstr.of_path path in
 
-    let rec loop last_orig_region state str = match Filter.destr str with
+    let rec loop last_orig_region state str = match Tokenstr.destr str with
       | None -> ()
 
-      | Some (({Filter.token = EOF} as i), _) ->
-          let space_between = i.Filter.space in
+      | Some (({Tokenstr.token = EOF} as i), _) ->
+          let space_between = i.Tokenstr.space in
           Printer.add_string printer (Region.lnum last_orig_region) space_between
             
       | Some (i, str) ->
-          let t = i.Filter.token in
-          let orig_region = i.Filter.region in
-          let space_between = i.Filter.space in
-          let substr = i.Filter.substr in
+          let t = i.Tokenstr.token in
+          let orig_region = i.Tokenstr.region in
+          let space_between = i.Tokenstr.space in
+          let substr = i.Tokenstr.substr in
 
           (*
             Format.eprintf "<%s %d>@."
       loop Region.zero State.init str;
       raise Exit
     with
-    | Exit -> Filter.close str
+    | Exit -> Tokenstr.close str
   ) paths
+open Sexplib.Conv
+open Pos
+open Reader
+open Parser
+
+type 'a desc = 
+  | Cons of 'a * 'a t * in_channel
+  | Null
+
+and 'a t = 'a desc lazy_t
+
+type 'a info = {
+  space : string;
+  token : 'a;
+  region : Region.t;
+  substr : string;
+}
+
+let of_channel ic = 
+  try
+    let reader = LexReader.create_from_channel ic in
+    let rec loop last_region = 
+      let token = LexReader.lex reader Lexer.token in
+      let region = LexReader.region reader in
+      let space_between = 
+        let last_end = (snd last_region).Position.pos_cnum in
+        LexReader.substring 
+          reader last_end ((fst region).Position.pos_cnum - last_end)
+      in
+      (* token's string *)
+      let substr = LexReader.current_substring reader in
+      
+      Cons ({ space = space_between; token; region; substr },
+            lazy (match token with
+            | EOF -> Null
+            | _ -> loop region),
+            ic)
+    in
+    lazy (loop Region.zero)
+  with
+  | e -> raise e
+
+let of_path path =
+  let ic = open_in path in
+  of_channel ic
+
+let close = function
+  | lazy Null -> ()
+  | lazy (Cons (_, _, ic)) -> close_in ic
+
+let destr = function
+  | lazy Null -> None
+  | lazy (Cons (car, cdr, _ic)) -> Some (car, cdr)
+type 'a t
+
+type 'a info = {
+  space : string;
+  token : 'a;
+  region : Pos.Region.t;
+  substr : string;
+}
+
+val of_channel : in_channel -> Parser.token info t
+
+val of_path : string -> Parser.token info t
+
+val close : 'a t -> unit
+
+val destr : 'a t -> ('a * 'a t) option