| author | immler@in.tum.de | 
| Fri, 20 Feb 2009 11:04:18 +0100 | |
| changeset 30015 | 1baeda435aa6 | 
| parent 29606 | fedb8be05f24 | 
| child 30573 | 49899f26fbd1 | 
| 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 | |
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 23 | val scan_pos: T list -> Position.T * T list | 
| 27763 | 24 | val scan_comment: (string -> (T list -> T list * T list) -> T list -> T list * T list) -> | 
| 25 | T list -> T list * T list | |
| 26 | val scan_comment_body: (string -> (T list -> T list * T list) -> T list -> T list * T list) -> | |
| 27 | T list -> T list * T list | |
| 28 | val source: Position.T -> (Symbol.symbol, 'a) Source.source -> | |
| 29 | (T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source | |
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 30 | type text = string | 
| 27797 | 31 | val implode: T list -> text | 
| 32 | val range: T list -> Position.range | |
| 33 | 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 | 34 | val explode: text * Position.T -> T list | 
| 27763 | 35 | end; | 
| 36 | ||
| 37 | structure SymbolPos: SYMBOL_POS = | |
| 38 | struct | |
| 39 | ||
| 40 | (* type T *) | |
| 41 | ||
| 42 | type T = Symbol.symbol * Position.T; | |
| 43 | ||
| 44 | fun symbol ((s, _): T) = s; | |
| 27852 | 45 | |
| 46 | ||
| 47 | (* content *) | |
| 48 | ||
| 27797 | 49 | val content = implode o map symbol; | 
| 27763 | 50 | |
| 51 | ||
| 27864 | 52 | val tab_width = (8: int); | 
| 27852 | 53 | |
| 54 | fun untabify ("\t", pos) =
 | |
| 55 | (case Position.column_of pos of | |
| 56 | 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 | 57 | | NONE => Symbol.space) | 
| 27852 | 58 | | untabify (s, _) = s; | 
| 59 | ||
| 60 | val untabify_content = implode o map untabify; | |
| 61 | ||
| 62 | ||
| 27763 | 63 | (* stopper *) | 
| 64 | ||
| 65 | fun mk_eof pos = (Symbol.eof, pos); | |
| 66 | val eof = mk_eof Position.none; | |
| 67 | ||
| 68 | val is_eof = Symbol.is_eof o symbol; | |
| 69 | ||
| 70 | val stopper = | |
| 71 | Scan.stopper (fn [] => eof | inp => mk_eof (List.last inp |-> Position.advance)) is_eof; | |
| 72 | ||
| 73 | ||
| 74 | (* basic scanners *) | |
| 75 | ||
| 76 | fun !!! text scan = | |
| 77 | let | |
| 78 | fun get_pos [] = " (past end-of-text!)" | |
| 79 | | get_pos ((_, pos) :: _) = Position.str_of pos; | |
| 80 | ||
| 81 | fun err (syms, msg) = | |
| 82 | text ^ get_pos syms ^ " at " ^ Symbol.beginning 10 (map symbol syms) ^ | |
| 83 | (case msg of NONE => "" | SOME s => "\n" ^ s); | |
| 84 | in Scan.!! err scan end; | |
| 85 | ||
| 86 | fun $$$ s = Scan.one (fn x => symbol x = s) >> single; | |
| 87 | fun ~$$$ s = Scan.one (fn x => symbol x <> s) >> single; | |
| 88 | ||
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 89 | val scan_pos = Scan.ahead (Scan.one (K true)) >> (fn (_, pos): T => pos); | 
| 27763 | 90 | |
| 91 | ||
| 92 | (* ML-style comments *) | |
| 93 | ||
| 94 | local | |
| 95 | ||
| 96 | val scan_cmt = | |
| 97 |   Scan.depend (fn (d: int) => $$$ "(" @@@ $$$ "*" >> pair (d + 1)) ||
 | |
| 98 | Scan.depend (fn 0 => Scan.fail | d => $$$ "*" @@@ $$$ ")" >> pair (d - 1)) || | |
| 99 | Scan.lift ($$$ "*" --| Scan.ahead (~$$$ ")")) || | |
| 100 | Scan.lift (Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s)) >> single; | |
| 101 | ||
| 102 | val scan_body = Scan.pass 0 (Scan.repeat scan_cmt >> flat); | |
| 103 | ||
| 104 | in | |
| 105 | ||
| 106 | fun scan_comment cut = | |
| 107 |   $$$ "(" @@@ $$$ "*" @@@ cut "missing end of comment" (scan_body @@@ $$$ "*" @@@ $$$ ")");
 | |
| 108 | ||
| 109 | fun scan_comment_body cut = | |
| 110 |   $$$ "(" |-- $$$ "*" |-- cut "missing end of comment" (scan_body --| $$$ "*" --| $$$ ")");
 | |
| 111 | ||
| 112 | end; | |
| 113 | ||
| 114 | ||
| 115 | (* source *) | |
| 116 | ||
| 117 | fun source pos = | |
| 118 | Source.source' pos Symbol.stopper (Scan.bulk (Scan.depend (fn pos => | |
| 119 | Scan.one Symbol.not_eof >> (fn s => (Position.advance s pos, (s, pos)))))) NONE; | |
| 120 | ||
| 121 | ||
| 122 | (* compact representation -- with Symbol.DEL padding *) | |
| 123 | ||
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 124 | type text = string; | 
| 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 125 | |
| 27763 | 126 | fun pad [] = [] | 
| 127 | | pad [(s, _)] = [s] | |
| 128 | | pad ((s1, pos1) :: (rest as (s2, pos2) :: _)) = | |
| 129 | let | |
| 130 | val end_pos1 = Position.advance s1 pos1; | |
| 27797 | 131 | val d = Int.max (0, Position.distance_of end_pos1 pos2); | 
| 27763 | 132 | in s1 :: replicate d Symbol.DEL @ pad rest end; | 
| 133 | ||
| 27797 | 134 | val implode = implode o pad; | 
| 27763 | 135 | |
| 27797 | 136 | fun range (syms as (_, pos) :: _) = | 
| 27763 | 137 | let val pos' = List.last syms |-> Position.advance | 
| 27797 | 138 | in Position.range pos pos' end | 
| 139 | | range [] = Position.no_range; | |
| 27763 | 140 | |
| 27797 | 141 | fun implode_range pos1 pos2 syms = | 
| 142 |   let val syms' = (("", pos1) :: syms @ [("", pos2)])
 | |
| 143 | in (implode syms', range syms') end; | |
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 144 | |
| 27763 | 145 | fun explode (str, pos) = | 
| 27778 
3ec7a4d9ef18
renamed SymbolPos.scan_position to SymbolPos.scan_pos;
 wenzelm parents: 
27763diff
changeset | 146 | 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 | 147 | (Symbol.explode str) (Position.reset_range pos) | 
| 27763 | 148 | |> #1 |> filter_out (fn (s, _) => s = Symbol.DEL); | 
| 149 | ||
| 150 | end; | |
| 151 | ||
| 152 | structure BasicSymbolPos: BASIC_SYMBOL_POS = SymbolPos; (*not open by default*) | |
| 153 |