| author | haftmann | 
| Thu, 11 Mar 2010 09:09:51 +0100 | |
| changeset 35709 | 267e15230a31 | 
| parent 32784 | 1a5dde5079ac | 
| child 36957 | cdb9e83abfbe | 
| permissions | -rw-r--r-- | 
| 27763 | 1 | (* Title: Pure/General/symbol_pos.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 4 | Symbols with explicit position information. | |
| 5 | *) | |
| 6 | ||
| 7 | signature BASIC_SYMBOL_POS = | |
| 8 | sig | |
| 9 | type T = Symbol.symbol * Position.T | |
| 10 | val symbol: T -> Symbol.symbol | |
| 11 | val $$$ : Symbol.symbol -> T list -> T list * T list | |
| 12 | val ~$$$ : Symbol.symbol -> T list -> T list * T list | |
| 13 | end | |
| 14 | ||
| 15 | signature SYMBOL_POS = | |
| 16 | sig | |
| 17 | include BASIC_SYMBOL_POS | |
| 27797 | 18 | val content: T list -> string | 
| 27852 | 19 | val untabify_content: T list -> string | 
| 27763 | 20 | val is_eof: T -> bool | 
| 21 | val stopper: T Scan.stopper | |
| 22 | val !!! : string -> (T list -> 'a) -> T list -> 'a | |
| 30586 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 23 |   val change_prompt: ('a -> 'b) -> 'a -> 'b
 | 
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 24 | val scan_pos: T list -> Position.T * T list | 
| 30586 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 25 | val scan_string: T list -> (Position.T * (T list * Position.T)) * T list | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 26 | val scan_alt_string: T list -> (Position.T * (T list * Position.T)) * T list | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 27 | val scan_quoted: T list -> T list * T list | 
| 27763 | 28 | val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) -> | 
| 29 | T list -> T list * T list | |
| 30 | val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) -> | |
| 31 | T list -> T list * T list | |
| 32 | val source: Position.T -> (Symbol.symbol, 'a) Source.source -> | |
| 33 | (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source | |
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 34 | type text = string | 
| 27797 | 35 | val implode: T list -> text | 
| 36 | val range: T list -> Position.range | |
| 37 | val implode_range: Position.T -> Position.T -> T list -> text * Position.range | |
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 38 | val explode: text * Position.T -> T list | 
| 27763 | 39 | end; | 
| 40 | ||
| 30573 | 41 | structure Symbol_Pos: SYMBOL_POS = | 
| 27763 | 42 | struct | 
| 43 | ||
| 44 | (* type T *) | |
| 45 | ||
| 46 | type T = Symbol.symbol * Position.T; | |
| 47 | ||
| 48 | fun symbol ((s, _): T) = s; | |
| 27852 | 49 | |
| 50 | ||
| 51 | (* content *) | |
| 52 | ||
| 27797 | 53 | val content = implode o map symbol; | 
| 27763 | 54 | |
| 55 | ||
| 27864 | 56 | val tab_width = (8: int); | 
| 27852 | 57 | |
| 58 | fun untabify ("\t", pos) =
 | |
| 59 | (case Position.column_of pos of | |
| 60 | SOME n => Symbol.spaces (tab_width - ((n - 1) mod tab_width)) | |
| 27984 
b4dd58cff97c
untabify: silently turn tab into space if column information is unavailable;
 wenzelm parents: 
27864diff
changeset | 61 | | NONE => Symbol.space) | 
| 27852 | 62 | | untabify (s, _) = s; | 
| 63 | ||
| 64 | val untabify_content = implode o map untabify; | |
| 65 | ||
| 66 | ||
| 27763 | 67 | (* stopper *) | 
| 68 | ||
| 69 | fun mk_eof pos = (Symbol.eof, pos); | |
| 70 | val eof = mk_eof Position.none; | |
| 71 | ||
| 72 | val is_eof = Symbol.is_eof o symbol; | |
| 73 | ||
| 74 | val stopper = | |
| 75 | Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof; | |
| 76 | ||
| 77 | ||
| 78 | (* basic scanners *) | |
| 79 | ||
| 80 | fun !!! text scan = | |
| 81 | let | |
| 82 | fun get_pos [] = " (past end-of-text!)" | |
| 83 | | get_pos ((_, pos) :: _) = Position.str_of pos; | |
| 84 | ||
| 85 | fun err (syms, msg) = | |
| 86 | text ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^ | |
| 87 | (case msg of NONE => "" | SOME s => "\n" ^ s); | |
| 88 | in Scan.!! err scan end; | |
| 89 | ||
| 30586 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 90 | fun change_prompt scan = Scan.prompt "# " scan; | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 91 | |
| 27763 | 92 | fun $$$ s = Scan.one (fn x => symbol x = s) >> single; | 
| 93 | fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single; | |
| 94 | ||
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 95 | val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos); | 
| 27763 | 96 | |
| 97 | ||
| 30586 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 98 | (* Isabelle strings *) | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 99 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 100 | local | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 101 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 102 | val char_code = | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 103 | Scan.one (Symbol.is_ascii_digit o symbol) -- | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 104 | Scan.one (Symbol.is_ascii_digit o symbol) -- | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 105 | Scan.one (Symbol.is_ascii_digit o symbol) :|-- | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 106 | (fn (((a, pos), (b, _)), (c, _)) => | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 107 | let val (n, _) = Library.read_int [a, b, c] | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 108 | in if n <= 255 then Scan.succeed [(chr n, pos)] else Scan.fail end); | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 109 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 110 | fun scan_str q = | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 111 | $$$ "\\" |-- !!! "bad escape character in string" ($$$ q || $$$ "\\" || char_code) || | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 112 | Scan.one (fn (s, _) => s <> q andalso s <> "\\" andalso Symbol.is_regular s) >> single; | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 113 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 114 | fun scan_strs q = | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 115 | (scan_pos --| $$$ q) -- !!! "missing quote at end of string" | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 116 | (change_prompt ((Scan.repeat (scan_str q) >> flat) -- ($$$ q |-- scan_pos))); | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 117 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 118 | in | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 119 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 120 | val scan_string = scan_strs "\""; | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 121 | val scan_alt_string = scan_strs "`"; | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 122 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 123 | val scan_quoted = Scan.trace (scan_string || scan_alt_string) >> #2; | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 124 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 125 | end; | 
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 126 | |
| 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 127 | |
| 27763 | 128 | (* ML-style comments *) | 
| 129 | ||
| 130 | local | |
| 131 | ||
| 132 | val scan_cmt = | |
| 133 |   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
 | |
| 134 | Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) || | |
| 135 | Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) || | |
| 136 | Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single; | |
| 137 | ||
| 30586 
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
 wenzelm parents: 
30573diff
changeset | 138 | val scan_body = change_prompt (Scan.pass 0 (Scan.repeat scan_cmt >> flat)); | 
| 27763 | 139 | |
| 140 | in | |
| 141 | ||
| 142 | fun scan_comment cut = | |
| 143 |   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
 | |
| 144 | ||
| 145 | fun scan_comment_body cut = | |
| 146 |   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
 | |
| 147 | ||
| 148 | end; | |
| 149 | ||
| 150 | ||
| 151 | (* source *) | |
| 152 | ||
| 153 | fun source pos = | |
| 154 | Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos => | |
| 155 | Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE; | |
| 156 | ||
| 157 | ||
| 158 | (* compact representation -- with Symbol.DEL padding *) | |
| 159 | ||
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 160 | type text = string; | 
| 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 161 | |
| 27763 | 162 | fun pad [] = [] | 
| 163 | | pad [(s, _)] = [s] | |
| 32784 | 164 | | pad ((s1, pos1) :: (rest as (_, pos2) :: _)) = | 
| 27763 | 165 | let | 
| 166 | val end_pos1 = Position.advance s1 pos1; | |
| 27797 | 167 | val d = Int.max (0, Position.distance_of end_pos1 pos2); | 
| 27763 | 168 | in s1 :: replicate d Symbol.DEL @ pad rest end; | 
| 169 | ||
| 27797 | 170 | val implode = implode o pad; | 
| 27763 | 171 | |
| 27797 | 172 | fun range (syms as (_, pos) :: _) = | 
| 27763 | 173 | let val pos' = List.last syms |-> Position.advance | 
| 27797 | 174 | in Position.range pos pos' end | 
| 175 | | range [] = Position.no_range; | |
| 27763 | 176 | |
| 27797 | 177 | fun implode_range pos1 pos2 syms = | 
| 178 |   let val syms' = (("", pos1) :: syms @ [("", pos2)])
 | |
| 179 | in (implode syms', range syms') end; | |
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 180 | |
| 27763 | 181 | fun explode (str, pos) = | 
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 182 | fold_map (fn s => fn p => ((s, p), (Position.advance s p))) | 
| 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 183 | (Symbol.explode str) (Position.reset_range pos) | 
| 27763 | 184 | |> #1 |> filter_out (fn (s, _) => s = Symbol.DEL); | 
| 185 | ||
| 186 | end; | |
| 187 | ||
| 30573 | 188 | structure Basic_Symbol_Pos: BASIC_SYMBOL_POS = Symbol_Pos; (*not open by default*) | 
| 27763 | 189 |