Commits

camlspotter committed e6e6a84

cleanup

  • Participants
  • Parent commits 1928b2b
  • Branches 4.01.0

Comments (0)

Files changed (10)

 FILES[] =
     pos
     reader
+    lazyList
     tokenstr
     xparser
     xlexer
     ("-cursor", Arg.String (fun s ->
       try
         let pos = String.index s ':' in
-        let (rows,cols) = (int_of_string (String.sub s 0 pos),
+        let (lines,cols) = (int_of_string (String.sub s 0 pos),
                            int_of_string (String.sub s (pos+1) (String.length s - pos - 1))) in
-        if rows <= 0 || cols < 0 then
+        if lines <= 0 || cols < 0 then
           failwith (Printf.sprintf "Wrong -cursor specification: %s" s);
-        cursor := Some (rows, cols)
+        cursor := Some (lines, cols)
       with
       | _ -> failwith (Printf.sprintf "Wrong -cursor specification: %s" s)),
      "cursor position, ex. 10:12") ;
+type 'a desc = 
+  | Cons of 'a * 'a t
+  | Null
+
+and 'a t = 'a desc lazy_t
+
+let peek = function
+  | lazy Null -> None
+  | lazy (Cons (car, cdr)) -> Some (car, cdr)

File lazyList.mli

+type 'a desc = 
+  | Cons of 'a * 'a t
+  | Null
+
+and 'a t = 'a desc lazy_t
+
+val peek : 'a t -> ('a * 'a t) option
 
 (* true means I found something to make the token a block *)
 let rec lparen_read_ahead lnum str =
-  match Tokenstr.destr str with
+  match LazyList.peek str with
   | None -> false
   | Some (i, _) when Region.lnum i.Tokenstr.region <> lnum -> false
   | Some (i, str) ->
       | _ -> true
 
 let lbrace_read_ahead lnum str =
-  match Tokenstr.destr str with
+  match LazyList.peek str with
   | None -> false
   | Some (i, _) when Region.lnum i.Tokenstr.region <> lnum -> false
   | Some (i, str) ->
 
 open Tokenstr
 
-let indent_file path =
+let with_open_in_or_stdin path f = 
+  match path with
+  | None -> f stdin
+  | Some path ->
+      let ic = open_in path in
+      let res = try `Ok (f ic) with e -> `Error e in
+      close_in ic;
+      match res with
+      | `Ok v -> v
+      | `Error e -> raise e
+
+let indent_ic ic =
   let print_string = Printer.print_string lines in
   let print_debug = Printer.print_debug lines in
 
-  let str = if path = "" then Tokenstr.of_channel stdin else Tokenstr.of_path path in
+  let str = Tokenstr.of_channel ic 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
+  let rec loop last_orig_region state str = match LazyList.peek str with
     | None -> state
 
     | Some (({ token = Parser.EOF } as info), _) ->
               else state
             in
 
-            (* Where the cursor move *)
-            let _cursor_info = match cursor with
-              | None -> None
-              | Some lines_cols ->
-                  match
-                    Region.contain_lines_cols last_orig_region lines_cols,
-                    Region.contain_lines_cols orig_region lines_cols
-                  with
-                  | (`In | `Right), _ -> None                   (* far past *)
-                  | `Left, `Right     -> None                   (* far future *)
-                  | `Left, `Left      -> Some `In_space_between (* cursor in the space_between *)
-                  | `Left, `In        -> Some `In_the_token     (* cursor on the token *)
-                  | _                 -> assert false           (* It must be more tolerant, but for now... *)
-            in
-
             let fix_indent = match line_status with
               | `Inside -> true
               | `Before -> false
 
             let pre, post = Machine.update_state state ~at_new_line ~fix_indent str orig_region t in
 
+            (* cursor *)
+            (* if [fix_indent], and the cursor is on the line, the cursor pos may move *)
+            (* Where the cursor move *)
+(*
+            if fix_indent then begin
+              let cursor_info = match cursor with
+                | None -> None
+                | Some lines_cols ->
+                    match
+                      Region.contains_lines_cols last_orig_region lines_cols,
+                      Region.contains_lines_cols orig_region lines_cols
+                    with
+                    | (`In | `Out_left), _   -> None                   (* far past *)
+                    | `Out_right, `Out_right -> None                   (* far future *)
+                    | `Out_right, `Out_left  -> Some `In_space_between (* cursor in the space_between *)
+                    | `Out_right, `In        -> Some `In_the_token     (* cursor on the token *)
+                    | _                      -> assert false           (* It must be more tolerant, but for now... *)
+              in
+            end;
+*)
+
             (* printing *)
 
             if not at_new_line then print_string current_line space_between
             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
+  if showstate then State.print final_state
+
+let indent_file path = with_open_in_or_stdin (if path = "" then None else Some path) indent_ic
 
 let _ = List.iter indent_file paths
     { p  with pos_cnum = p .pos_cnum + diff },
     { p' with pos_cnum = p'.pos_cnum + diff } 
 
-  let contain (start, end_) pos = 
+  let contains (start, end_) pos = 
     match Position.compare start pos, Position.compare pos end_ with
     | (-1 | 0), (-1 | 0) -> `In
     | _, 1 -> `Out_right
     | -1, _ -> `Out_left
     | _ -> assert false
 
-  let contain_lines_cols (start, end_) l_c = 
+  let contains_lines_cols (start, end_) l_c = 
     match Position.compare_lines_cols start l_c, Position.compare_lines_cols end_ l_c with
     | (-1 | 0), (1 | 0) -> `In
-    | _, -1 -> `Out_right
+    | _, 1 -> `Out_right
     | -1, _ -> `Out_left
-    | _ -> assert false
+    | x,y -> Format.eprintf "ERROR: Pos.contain_lines_cols: %d %d@." x y; assert false
 end
   
   val lnum : Position.t * 'a -> int
   val columns : Position.t * 'a -> int
-  val zero : Position.t * Position.t
-  val move_chars : int -> Position.t * Position.t -> Position.t * Position.t
-  val contain : Position.t * Position.t -> Position.t -> [> `In | `Out_left | `Out_right ]
-  val contain_lines_cols : Position.t * Position.t -> int * int -> [> `In | `Out_left | `Out_right ]
+  val zero : t
+  val move_chars : int -> t -> t
+  val contains : t -> Position.t -> [> `In | `Out_left | `Out_right ]
+  val contains_lines_cols : t -> int * int -> [> `In | `Out_left | `Out_right ]
 end
 open Pos
-module Parser = Xparser
-open Parser
-
-type 'a desc = 
-  | Cons of 'a * 'a t * in_channel option ref
-  | Null of in_channel option ref
-
-and 'a t = 'a desc lazy_t
+open Xparser
+open LazyList
 
 type 'a info = {
   token : 'a;
   space : Region.t * string;
 }
 
-let close = function
-  | lazy (Null icoptref | Cons (_, _, icoptref)) ->
-      match !icoptref with
-      | None -> ()
-      | Some ic -> icoptref := None; close_in ic 
-
 let of_channel ic = 
-  let icoptref = ref (Some ic) in
   try
     let reader = Reader.create_from_channel ic in
     let rec loop last_region = 
 
       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
+            | EOF -> Lazy.lazy_from_val Null
             | _ -> lazy (loop region)
-            end,
-            icoptref)
+            end)
     in
     lazy (loop Region.zero)
   with
   | e -> raise e
 
-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
-
 type 'a info = {
-  token : 'a;
-  region : Pos.Region.t;
-  substr : string;
-  space : Pos.Region.t * string
+  token : 'a;                   (** the token *)
+  region : Pos.Region.t;        (** the region of the token *)
+  substr : string;              (** the actual string of the token *)
+  space : Pos.Region.t * string (** white space (non token) between the previous token and this *)
 }
 
-val of_channel : in_channel -> Xparser.token info t
-
-val of_path : string -> Xparser.token info t
-
-val close : 'a t -> unit
-
-val destr : 'a t -> ('a * 'a t) option
+val of_channel : in_channel -> Xparser.token info LazyList.t