Commits

camlspotter  committed c4779f8

now it uses spotlib+planck

  • Participants
  • Parent commits abcaeb9
  • Branches dev

Comments (0)

Files changed (7)

 OCAML_BYTE_LINK_FLAGS +=
 OCAML_NATIVE_LINK_FLAGS +=
 
-CAMLP4PACKS[]= sexplib compiler-libs.common
+CAMLP4PACKS[]= planck compiler-libs.common
 
-OCAMLPACKS[]= sexplib compiler-libs.common
+OCAMLPACKS[]= planck compiler-libs.common
 
 OCAMLDEPFLAGS= -syntax camlp4o -package sexplib.syntax
 OCAMLPPFLAGS= -syntax camlp4o -package sexplib.syntax
     pos
     reader
     tokenstr
+    result
     xparser
     machine
     args
 let paths, debug, lines, showstate, cursor =
   let rev_paths = ref [] in
   let debug = ref false in
-  let lines = ref None in
+  let lines = ref Pos.Lines.WholeFile in
   let showstate = ref false in
   let cursor = ref None in
   Arg.parse [
                             int_of_string (String.sub s (pos+1) (String.length s - pos - 1))) in
         if start <= 0 || end_ <= 0 || start > end_ then
           failwith (Printf.sprintf "Wrong -lines specification: %s" s);
-        lines := Some (start, end_)
+        lines := Pos.Lines.Lines (start, end_)
       with
       | _ -> failwith (Printf.sprintf "Wrong -lines specification: %s" s)),
      "lines, ex. 10-12") ;
   begin match paths, !lines with
   | [], _ -> assert false
   | [_], _ -> ()
-  | _, Some _ -> failwith "Region can be specified with at most one file"
+  | _, Pos.Lines.Lines _ -> failwith "Region can be specified with at most one file"
   | _ -> ()
   end;
   paths, !debug, !lines, !showstate, !cursor
   | _ :: bs -> unwind_top bs
   | [] -> []
 
+open Planck.Lazylist (* for Cons and Null *)
+
 (* true means I found something to make the token a block *)
-let rec lparen_read_ahead lnum str =
-  match Tokenstr.destr str with
-  | None -> false
-  | Some (i, _) when Region.lnum i.Tokenstr.region <> lnum -> false
-  | Some (i, str) ->
+let rec lparen_read_ahead lnum = function
+  | lazy Null _ -> false
+  | lazy Cons (i, _, _) when Region.lnum i.Tokenstr.region <> lnum -> false
+  | lazy Cons (i, _, str) ->
       match i.Tokenstr.token with
       | COMMENT _ -> lparen_read_ahead lnum str
       | FUN -> false
       | FUNCTION -> false
       | _ -> true
 
-let lbrace_read_ahead lnum str =
-  match Tokenstr.destr str with
-  | None -> false
-  | Some (i, _) when Region.lnum i.Tokenstr.region <> lnum -> false
-  | Some (i, str) ->
+let lbrace_read_ahead lnum = function
+  | lazy Null _ -> false
+  | lazy Cons (i, _, _) when Region.lnum i.Tokenstr.region <> lnum -> false
+  | lazy Cons (i, _,str) ->
       match i.Tokenstr.token with
       | COMMENT _ -> lparen_read_ahead lnum str
       | _ -> true
 open Pos
-open Args
 module State = Machine.State
 module Stack = Machine.Stack
 
 module Sexp = Sexplib.Sexp (* No open Sexplib, since Parser corrides with Sexplib.Parser *)
 
-type region = (int * int) option (* [None] means the whole file *)
-
-(** [l] is in the interested area or not? *)
-let check_line : int -> region -> [> `Before | `Inside | `Over ] = fun l -> function
-  | None -> `Inside
-  | Some (start, end_) ->
-      if l < start then `Before
-      else if l <= end_ then `Inside
-      else `Over
-
 module Printer : sig
-  val print_debug  : region -> int-> string -> unit
-  val print_string : region -> int -> string -> unit
+  val print_debug  : Lines.t -> int-> string -> unit
+  val print_string : Lines.t -> int -> string -> unit
 end = struct
 
-  let print_debug t lnum s = match check_line lnum t with
+  let print_debug t lnum s = match Lines.is_between lnum t with
     | `Before | `Over -> ()
     | `Inside -> print_string s
 
       different number of lines from the original.
   *)
   let print_string t lnum s =
-    let add_line t lnum s = match check_line lnum t with
+    let add_line t lnum s = match Lines.is_between lnum t with
       | `Before -> ()
       | `Over -> raise Exit
       | `Inside -> print_string s
 end
 
 open Tokenstr
+open Planck.Lazylist
 
 let indent_file path =
-  let print_string = Printer.print_string lines in
-  let print_debug = Printer.print_debug lines in
+  let print_string = Printer.print_string Args.lines in
+  let print_debug  = Printer.print_debug  Args.lines in
 
-  let str = if path = "" then Tokenstr.of_channel stdin else Tokenstr.of_path path in
+  let tkstr = if path = "" then Tokenstr.of_channel stdin else Tokenstr.of_path path in
+  let stream = stream tkstr in
 
   let flush_remaining_space info =
     print_string (Region.lnum (fst info.space)) (snd info.space)
   in
 
-  let rec loop last_orig_region state str = match Tokenstr.destr str with
-    | None -> state
+  let rec loop last_orig_region state = function
+    | lazy Null _ -> state
 
-    | Some (({ token = Parser.EOF } as info), _) ->
+    | lazy Cons (({ token = Parser.EOF } as info), _, _) ->
         flush_remaining_space info;
         state
 
-    | Some (({ token = t;
-               region = orig_region;
-               space = (_space_between_region, space_between);
-               substr } as info), str) ->
+    | lazy Cons (({ token = t;
+                    region = orig_region;
+                    space = (_space_between_region, space_between);
+                    substr } as info), 
+                 _, str) ->
 
-        match check_line (Region.lnum orig_region) lines with
+        match Lines.is_between (Region.lnum orig_region) Args.lines with
         | `Over ->
             (* The token is outside of our interest.
                Print the remaining things and go out *)
             in
 
             (* Where the cursor move *)
-            let _cursor_info = match cursor with
+            let _cursor_info = match Args.cursor with
               | None -> None
               | Some lines_cols ->
                   match
               (* CR jfuruse: can be a bug. something from space_between_region *)
               print_string last_line spaces;
 
-              if debug then begin
+              if Args.debug then begin
                 print_string current_line indent_string;
                 if pre == post then
                   print_debug current_line
 
             loop orig_region post str
   in
-  let final_state = loop Region.zero State.init str in
-  if showstate then State.print final_state;
-  Tokenstr.close str
+  let final_state = loop Region.zero State.init stream in
+  if Args.showstate then State.print final_state;
+  Tokenstr.close tkstr
 
-let _ = List.iter indent_file paths
+let () = List.iter indent_file Args.paths
     | -1, _ -> `Out_left
     | _ -> assert false
 end
+
+module Lines = struct
+  type t = WholeFile | Lines of int * int
+
+  (** [l] is in the interested area or not? *)
+  let is_between : int -> t -> [> `Before | `Inside | `Over ] = 
+    fun l -> function
+      | WholeFile -> `Inside
+      | Lines (start, end_) ->
+          if l < start then `Before
+          else if l <= end_ then `Inside
+          else `Over
+end
 open Reader
 module Parser = Xparser
 open Parser
+open Planck.Lazylist
 
-module Stream = struct
-  type 'a desc = 
-    | Null
-    | Cons of 'a * 'a t
-
-  and 'a t = 'a desc lazy_t
-end
-
-type 'a info = {
-  token        : 'a;
+type info = {
+  token        : Parser.token;
   region       : Region.t;
   substr       : string;
   space        : Region.t * string;
-  mutable chan : in_channel option;
 }
 
-let close i = match i.chan with
-  | None -> ()
-  | Some ch -> close_in ch; i.chan <- None
+type t = (info, unit) zlist * in_channel
+
+let stream = fst
+let close (_, ic) = close_in ic
 
 let of_channel ic = 
   let reader = LexReader.create_from_channel ic in
     in
     let space_between_region = (snd last_region, fst region) in
 
-    Cons ({ token; region; substr; space = space_between_region, space_between },
-          begin match token with
-          | EOF -> 
-              let null = Lazy.lazy_from_val (Null icoptref) in
-              close null;
-              null
-          | _ -> lazy (loop region)
-          end,
-            icoptref)
+    Cons ({ token; region; substr; space = space_between_region, space_between }, (),
+          match token with
+          | EOF -> null ()
+          | _ -> lazy (loop region))
   in
-  { 
-    chan = Some ic }
-    lazy (loop Region.zero)
-  with
-  | e -> raise e
+  lazy (loop Region.zero), ic
 
 let of_path path =
   let ic = open_in path in
   of_channel ic
-
-let destr = function
-  | lazy Null _ -> None
-  | lazy (Cons (car, cdr, _)) -> Some (car, cdr)

File tokenstr.mli

-type 'a t
+open Planck.Lazylist
+open Pos
 
-type 'a info = {
-  token : 'a;
-  region : Pos.Region.t;
-  substr : string;
-  space : Pos.Region.t * string
+type info = {
+  token  : Parser.token;     (** parsed token *)
+  region : Region.t;         (** the position of the token *)
+  substr : string;           (** the actual string for the token *)
+  space  : Region.t * string (** space before the token *)
 }
 
-val of_channel : in_channel -> Parser.token info t
+type t
 
-val of_path : string -> Parser.token info t
-
-val close : 'a t -> unit
-
-val destr : 'a t -> ('a * 'a t) option
+val stream : t -> (info, unit) zlist
+val close : t -> unit
+val of_channel : in_channel -> t
+val of_path : string -> t