src/Pure/General/symbol_pos.ML
author wenzelm
Thu Nov 19 22:06:14 2015 +0100 (2015-11-19)
changeset 61705 546e6494049f
parent 61502 760e21900b01
child 61707 d5ddd022a451
permissions -rw-r--r--
trim lines for @{theory_text} similarly to @{text};
     1 (*  Title:      Pure/General/symbol_pos.ML
     2     Author:     Makarius
     3 
     4 Symbols with explicit position information.
     5 *)
     6 
     7 signature SYMBOL_POS =
     8 sig
     9   type T = Symbol.symbol * Position.T
    10   val symbol: T -> Symbol.symbol
    11   val $$ : Symbol.symbol -> T list -> T * T list
    12   val ~$$ : Symbol.symbol -> T list -> T * T list
    13   val $$$ : Symbol.symbol -> T list -> T list * T list
    14   val ~$$$ : Symbol.symbol -> T list -> T list * T list
    15   val content: T list -> string
    16   val range: T list -> Position.range
    17   val split_lines: T list -> T list list
    18   val trim_blanks: T list -> T list
    19   val trim_lines: T list -> T list
    20   val is_eof: T -> bool
    21   val stopper: T Scan.stopper
    22   val !!! : Scan.message -> (T list -> 'a) -> T list -> 'a
    23   val scan_pos: T list -> Position.T * T list
    24   val scan_string_q: string -> T list -> (Position.T * (T list * Position.T)) * T list
    25   val scan_string_qq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    26   val scan_string_bq: string -> T list -> (Position.T * (T list * Position.T)) * T list
    27   val recover_string_q: T list -> T list * T list
    28   val recover_string_qq: T list -> T list * T list
    29   val recover_string_bq: T list -> T list * T list
    30   val quote_string_q: string -> string
    31   val quote_string_qq: string -> string
    32   val quote_string_bq: string -> string
    33   val scan_cartouche: string -> T list -> T list * T list
    34   val recover_cartouche: T list -> T list * T list
    35   val cartouche_content: T list -> T list
    36   val scan_comment: string -> T list -> T list * T list
    37   val scan_comment_body: string -> T list -> T list * T list
    38   val recover_comment: T list -> T list * T list
    39   val source: Position.T -> (Symbol.symbol, 'a) Source.source ->
    40     (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source
    41   type text = string
    42   val implode: T list -> text
    43   val implode_range: Position.range -> T list -> text * Position.range
    44   val explode: text * Position.T -> T list
    45   val scan_ident: T list -> T list * T list
    46   val is_identifier: string -> bool
    47 end;
    48 
    49 structure Symbol_Pos: SYMBOL_POS =
    50 struct
    51 
    52 (* type T *)
    53 
    54 type T = Symbol.symbol * Position.T;
    55 
    56 fun symbol ((s, _): T) = s;
    57 
    58 val content = implode o map symbol;
    59 
    60 fun range (syms as (_, pos) :: _) =
    61       let val pos' = List.last syms |-> Position.advance
    62       in Position.range pos pos' end
    63   | range [] = Position.no_range;
    64 
    65 
    66 (* lines and blanks *)
    67 
    68 fun split_lines [] = []
    69   | split_lines (list: T list) =
    70       let
    71         fun split syms =
    72           (case take_prefix (fn (s, _) => s <> "\n") syms of
    73             (line, []) => [line]
    74           | (line, nl :: rest) => line :: split rest);
    75       in split list end;
    76 
    77 val trim_blanks =
    78   take_prefix (Symbol.is_blank o symbol) #> #2 #>
    79   take_suffix (Symbol.is_blank o symbol) #> #1;
    80 
    81 val trim_lines =
    82   split_lines #> map trim_blanks #> separate [(Symbol.space, Position.none)] #> flat;
    83 
    84 
    85 (* stopper *)
    86 
    87 fun mk_eof pos = (Symbol.eof, pos);
    88 val eof = mk_eof Position.none;
    89 
    90 val is_eof = Symbol.is_eof o symbol;
    91 
    92 val stopper =
    93   Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof;
    94 
    95 
    96 (* basic scanners *)
    97 
    98 fun !!! text scan =
    99   let
   100     fun get_pos [] = " (end-of-input)"
   101       | get_pos ((_, pos) :: _) = Position.here pos;
   102 
   103     fun err (syms, msg) = fn () =>
   104       text () ^ get_pos syms ^
   105       Markup.markup Markup.no_report (" at " ^ Symbol.beginning 10 (map symbol syms)) ^
   106       (case msg of NONE => "" | SOME m => "\n" ^ m ());
   107   in Scan.!! err scan end;
   108 
   109 fun $$ s = Scan.one (fn x => symbol x = s);
   110 fun ~$$ s = Scan.one (fn x => symbol x <> s);
   111 
   112 fun $$$ s = Scan.one (fn x => symbol x = s) >> single;
   113 fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single;
   114 
   115 val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos);
   116 
   117 
   118 (* scan string literals *)
   119 
   120 local
   121 
   122 val char_code =
   123   Scan.one (Symbol.is_ascii_digit o symbol) --
   124   Scan.one (Symbol.is_ascii_digit o symbol) --
   125   Scan.one (Symbol.is_ascii_digit o symbol) :|--
   126   (fn (((a, pos), (b, _)), (c, _)) =>
   127     let val (n, _) = Library.read_int [a, b, c]
   128     in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end);
   129 
   130 fun scan_str q err_prefix =
   131   $$$ "\\" |-- !!! (fn () => err_prefix ^ "bad escape character in string")
   132     ($$$ q || $$$ "\\" || char_code) ||
   133   Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.not_eof s) >> single;
   134 
   135 fun scan_strs q err_prefix =
   136   Scan.ahead ($$ q) |--
   137     !!! (fn () => err_prefix ^ "unclosed string literal")
   138       ((scan_pos --| $$$ q) -- (Scan.repeats (scan_str q err_prefix) -- ($$$ q |-- scan_pos)));
   139 
   140 fun recover_strs q =
   141   $$$ q @@@ Scan.repeats (Scan.permissive (scan_str q ""));
   142 
   143 in
   144 
   145 val scan_string_q = scan_strs "'";
   146 val scan_string_qq = scan_strs "\"";
   147 val scan_string_bq = scan_strs "`";
   148 
   149 val recover_string_q = recover_strs "'";
   150 val recover_string_qq = recover_strs "\"";
   151 val recover_string_bq = recover_strs "`";
   152 
   153 end;
   154 
   155 
   156 (* quote string literals *)
   157 
   158 local
   159 
   160 fun char_code i =
   161   (if i < 10 then "00" else if i < 100 then "0" else "") ^ string_of_int i;
   162 
   163 fun quote_str q s =
   164   if Symbol.is_ascii_control s then "\\" ^ char_code (ord s)
   165   else if s = q orelse s = "\\" then "\\" ^ s
   166   else s;
   167 
   168 fun quote_string q = enclose q q o implode o map (quote_str q) o Symbol.explode;
   169 
   170 in
   171 
   172 val quote_string_q = quote_string "'";
   173 val quote_string_qq = quote_string "\"";
   174 val quote_string_bq = quote_string "`";
   175 
   176 end;
   177 
   178 
   179 (* nested text cartouches *)
   180 
   181 val scan_cartouche_depth =
   182   Scan.repeat1 (Scan.depend (fn (depth: int option) =>
   183     (case depth of
   184       SOME d =>
   185         $$ "\\<open>" >> pair (SOME (d + 1)) ||
   186           (if d > 0 then
   187             Scan.one (fn (s, _) => s <> "\\<close>" andalso Symbol.not_eof s) >> pair depth ||
   188             $$ "\\<close>" >> pair (if d = 1 then NONE else SOME (d - 1))
   189           else Scan.fail)
   190     | NONE => Scan.fail)));
   191 
   192 fun scan_cartouche err_prefix =
   193   Scan.ahead ($$ "\\<open>") |--
   194     !!! (fn () => err_prefix ^ "unclosed text cartouche")
   195       (Scan.provide is_none (SOME 0) scan_cartouche_depth);
   196 
   197 val recover_cartouche = Scan.pass (SOME 0) scan_cartouche_depth;
   198 
   199 fun cartouche_content syms =
   200   let
   201     fun err () =
   202       error ("Malformed text cartouche: "
   203         ^ quote (content syms) ^ Position.here (#1 (range syms)));
   204   in
   205     (case syms of
   206       ("\\<open>", _) :: rest =>
   207         (case rev rest of
   208           ("\\<close>", _) :: rrest => rev rrest
   209         | _ => err ())
   210     | _ => err ())
   211   end;
   212 
   213 
   214 (* ML-style comments *)
   215 
   216 local
   217 
   218 val scan_cmt =
   219   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
   220   Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) ||
   221   Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) ||
   222   Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.not_eof s)) >> single;
   223 
   224 val scan_cmts = Scan.pass 0 (Scan.repeats scan_cmt);
   225 
   226 in
   227 
   228 fun scan_comment err_prefix =
   229   Scan.ahead ($$ "(" -- $$ "*") |--
   230     !!! (fn () => err_prefix ^ "unclosed comment")
   231       ($$$ "(" @@@ $$$ "*" @@@ scan_cmts @@@ $$$ "*" @@@ $$$ ")");
   232 
   233 fun scan_comment_body err_prefix =
   234   Scan.ahead ($$ "(" -- $$ "*") |--
   235     !!! (fn () => err_prefix ^ "unclosed comment")
   236       ($$ "(" |-- $$ "*" |-- scan_cmts --| $$ "*" --| $$ ")");
   237 
   238 val recover_comment =
   239   $$$ "(" @@@ $$$ "*" @@@ scan_cmts;
   240 
   241 end;
   242 
   243 
   244 (* source *)
   245 
   246 fun source pos =
   247   Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos =>
   248     Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos))))));
   249 
   250 
   251 (* compact representation -- with Symbol.DEL padding *)
   252 
   253 type text = string;
   254 
   255 fun pad [] = []
   256   | pad [(s, _)] = [s]
   257   | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) =
   258       let
   259         val end_pos1 = Position.advance s1 pos1;
   260         val d = Int.max (0, Position.distance_of end_pos1 pos2);
   261       in s1 :: replicate d Symbol.DEL @ pad rest end;
   262 
   263 val implode = implode o pad;
   264 
   265 fun implode_range (pos1, pos2) syms =
   266   let val syms' = (("", pos1) :: syms @ [("", pos2)])
   267   in (implode syms', range syms') end;
   268 
   269 fun explode (str, pos) =
   270   let
   271     val (res, _) =
   272       fold (fn s => fn (res, p) => ((s, p) :: res, Position.advance s p))
   273         (Symbol.explode str) ([], Position.reset_range pos);
   274   in fold (fn (s, p) => if s = Symbol.DEL then I else cons (s, p)) res [] end;
   275 
   276 
   277 (* identifiers *)
   278 
   279 local
   280 
   281 val letter = Scan.one (symbol #> Symbol.is_letter);
   282 val letdigs1 = Scan.many1 (symbol #> Symbol.is_letdig);
   283 
   284 val sub = Scan.one (symbol #> (fn s => s = "\\<^sub>"));
   285 
   286 in
   287 
   288 val scan_ident = letter ::: Scan.repeats (letdigs1 || sub ::: letdigs1);
   289 
   290 end;
   291 
   292 fun is_identifier s =
   293   Symbol.is_ascii_identifier s orelse
   294     (case try (Scan.finite stopper scan_ident) (explode (s, Position.none)) of
   295       SOME (_, []) => true
   296     | _ => false);
   297 
   298 end;
   299 
   300 structure Basic_Symbol_Pos =   (*not open by default*)
   301 struct
   302   val $$ = Symbol_Pos.$$;
   303   val ~$$ = Symbol_Pos.~$$;
   304   val $$$ = Symbol_Pos.$$$;
   305   val ~$$$ = Symbol_Pos.~$$$;
   306 end;
   307