src/Pure/General/position.ML
changeset 27764 e0ee3cc240fe
parent 27759 ffb1b5f2690f
child 27777 7a63d1b50b88
equal deleted inserted replaced
27763:f49f6275cefa 27764:e0ee3cc240fe
    13   val file_of: T -> string option
    13   val file_of: T -> string option
    14   val none: T
    14   val none: T
    15   val line_file: int -> string -> T
    15   val line_file: int -> string -> T
    16   val line: int -> T
    16   val line: int -> T
    17   val file: string -> T
    17   val file: string -> T
    18   val advance: Symbol.symbol list -> T -> T
    18   val advance: Symbol.symbol -> T -> T
    19   val get_id: T -> string option
    19   val get_id: T -> string option
    20   val put_id: string -> T -> T
    20   val put_id: string -> T -> T
    21   val of_properties: Markup.property list -> T
    21   val of_properties: Markup.property list -> T
    22   val properties_of: T -> Markup.property list
    22   val properties_of: T -> Markup.property list
    23   val default_properties: T -> Markup.property list -> Markup.property list
    23   val default_properties: T -> Markup.property list -> Markup.property list
       
    24   val report: Markup.T -> T -> unit
    24   val str_of: T -> string
    25   val str_of: T -> string
       
    26   type range = T * T
       
    27   val encode_range: range -> T
       
    28   val range: T -> T -> range
    25   val thread_data: unit -> T
    29   val thread_data: unit -> T
    26   val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b
    30   val setmp_thread_data: T -> ('a -> 'b) -> 'a -> 'b
    27   val setmp_thread_data_seq: T -> ('a -> 'b Seq.seq) -> 'a -> 'b Seq.seq
    31   val setmp_thread_data_seq: T -> ('a -> 'b Seq.seq) -> 'a -> 'b Seq.seq
    28   type range = T * T
       
    29   val encode_range: range -> T
       
    30   val report: Markup.T -> T -> unit
       
    31 end;
    32 end;
    32 
    33 
    33 structure Position: POSITION =
    34 structure Position: POSITION =
    34 struct
    35 struct
    35 
    36 
    58 fun advance_count "\n" (m: int, _: int) = (m + 1, 1)
    59 fun advance_count "\n" (m: int, _: int) = (m + 1, 1)
    59   | advance_count s (m, n) =
    60   | advance_count s (m, n) =
    60       if Symbol.is_regular s andalso not (Symbol.is_utf8_trailer s)
    61       if Symbol.is_regular s andalso not (Symbol.is_utf8_trailer s)
    61       then (m, n + 1) else (m, n);
    62       then (m, n + 1) else (m, n);
    62 
    63 
    63 fun advance syms (Pos (SOME count, props)) = Pos (SOME (fold advance_count syms count), props)
    64 fun advance sym (Pos (SOME count, props)) = Pos (SOME (advance_count sym count), props)
    64   | advance _ pos = pos;
    65   | advance _ pos = pos;
    65 
    66 
    66 
    67 
    67 (* markup properties *)
    68 (* markup properties *)
    68 
    69 
    90 
    91 
    91 fun default_properties default props =
    92 fun default_properties default props =
    92   if exists (member (op =) Markup.position_properties o #1) props then props
    93   if exists (member (op =) Markup.position_properties o #1) props then props
    93   else properties_of default @ props;
    94   else properties_of default @ props;
    94 
    95 
       
    96 fun report markup pos =
       
    97   if pos = none then ()
       
    98   else Output.status (Markup.markup (Markup.properties (properties_of pos) markup) "");
       
    99 
    95 
   100 
    96 (* str_of *)
   101 (* str_of *)
    97 
   102 
    98 fun str_of pos =
   103 fun str_of pos =
    99   let
   104   let
   107     if null props then ""
   112     if null props then ""
   108     else (if s = "" then "" else " ") ^ Markup.markup (Markup.properties props Markup.position) s
   113     else (if s = "" then "" else " ") ^ Markup.markup (Markup.properties props Markup.position) s
   109   end;
   114   end;
   110 
   115 
   111 
   116 
   112 (* thread data *)
       
   113 
       
   114 local val tag = Universal.tag () : T Universal.tag in
       
   115 
       
   116 fun thread_data () = the_default none (Multithreading.get_data tag);
       
   117 
       
   118 fun setmp_thread_data pos f x =
       
   119   if ! Output.debugging then f x
       
   120   else Library.setmp_thread_data tag (thread_data ()) pos f x;
       
   121 
       
   122 fun setmp_thread_data_seq pos f seq =
       
   123   setmp_thread_data pos f seq |> Seq.wrap (fn pull => setmp_thread_data pos pull ());
       
   124 
       
   125 end;
       
   126 
       
   127 
       
   128 (* range *)
   117 (* range *)
   129 
   118 
   130 type range = T * T;
   119 type range = T * T;
   131 
   120 
   132 fun encode_range (Pos (count, props), Pos (end_count, _)) =
   121 fun encode_range (Pos (count, props), Pos (end_count, _)) =
   134     (case end_count of
   123     (case end_count of
   135       NONE => []
   124       NONE => []
   136     | SOME (m, n) => [(Markup.end_lineN, string_of_int m), (Markup.end_columnN, string_of_int n)])
   125     | SOME (m, n) => [(Markup.end_lineN, string_of_int m), (Markup.end_columnN, string_of_int n)])
   137   in Pos (count, props') end;
   126   in Pos (count, props') end;
   138 
   127 
       
   128 fun range pos pos' = (encode_range (pos, pos'), pos');
   139 
   129 
   140 (* report markup *)
       
   141 
   130 
   142 fun report markup pos =
   131 (* thread data *)
   143   if pos = none then ()
   132 
   144   else Output.status (Markup.markup (Markup.properties (properties_of pos) markup) "");
   133 local val tag = Universal.tag () : T Universal.tag in
       
   134 
       
   135 fun thread_data () = the_default none (Multithreading.get_data tag);
       
   136 
       
   137 fun setmp_thread_data pos f x =
       
   138   if ! Output.debugging then f x
       
   139   else Library.setmp_thread_data tag (thread_data ()) pos f x;
       
   140 
       
   141 fun setmp_thread_data_seq pos f x =
       
   142   setmp_thread_data pos f x |> Seq.wrap (fn pull => setmp_thread_data pos pull ());
   145 
   143 
   146 end;
   144 end;
       
   145 
       
   146 end;