src/Pure/Isar/token.ML
author wenzelm
Mon Nov 14 16:52:19 2011 +0100 (2011-11-14 ago)
changeset 45488 6d71d9e52369
parent 44658 5bec9c15ef29
child 45666 d83797ef0d2d
permissions -rw-r--r--
pass positions for named targets, for formal links in the document model;
     1 (*  Title:      Pure/Isar/token.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Outer token syntax for Isabelle/Isar.
     5 *)
     6 
     7 signature TOKEN =
     8 sig
     9   datatype kind =
    10     Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
    11     Nat | Float | String | AltString | Verbatim | Space | Comment | InternalValue |
    12     Error of string | Sync | EOF
    13   datatype value =
    14     Text of string | Typ of typ | Term of term | Fact of thm list |
    15     Attribute of morphism -> attribute
    16   type T
    17   val str_of_kind: kind -> string
    18   val position_of: T -> Position.T
    19   val end_position_of: T -> Position.T
    20   val pos_of: T -> string
    21   val range: T list -> Position.range
    22   val eof: T
    23   val is_eof: T -> bool
    24   val not_eof: T -> bool
    25   val not_sync: T -> bool
    26   val stopper: T Scan.stopper
    27   val kind_of: T -> kind
    28   val is_kind: kind -> T -> bool
    29   val keyword_with: (string -> bool) -> T -> bool
    30   val ident_with: (string -> bool) -> T -> bool
    31   val is_proper: T -> bool
    32   val is_semicolon: T -> bool
    33   val is_comment: T -> bool
    34   val is_begin_ignore: T -> bool
    35   val is_end_ignore: T -> bool
    36   val is_blank: T -> bool
    37   val is_newline: T -> bool
    38   val source_of: T -> string
    39   val source_position_of: T -> Symbol_Pos.text * Position.T
    40   val content_of: T -> string
    41   val unparse: T -> string
    42   val text_of: T -> string * string
    43   val get_value: T -> value option
    44   val map_value: (value -> value) -> T -> T
    45   val mk_text: string -> T
    46   val mk_typ: typ -> T
    47   val mk_term: term -> T
    48   val mk_fact: thm list -> T
    49   val mk_attribute: (morphism -> attribute) -> T
    50   val assignable: T -> T
    51   val assign: value option -> T -> unit
    52   val closure: T -> T
    53   val ident_or_symbolic: string -> bool
    54   val source_proper: (T, 'a) Source.source -> (T, (T, 'a) Source.source) Source.source
    55   val source': {do_recover: bool Option.option} -> (unit -> Scan.lexicon * Scan.lexicon) ->
    56     (Symbol_Pos.T, 'a) Source.source -> (T, (Symbol_Pos.T, 'a) Source.source) Source.source
    57   val source: {do_recover: bool Option.option} -> (unit -> Scan.lexicon * Scan.lexicon) ->
    58     Position.T -> (Symbol.symbol, 'a) Source.source -> (T,
    59       (Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source) Source.source
    60   val read_antiq: Scan.lexicon -> (T list -> 'a * T list) -> Symbol_Pos.T list * Position.T -> 'a
    61 end;
    62 
    63 structure Token: TOKEN =
    64 struct
    65 
    66 (** tokens **)
    67 
    68 (* token values *)
    69 
    70 (*The value slot assigns an (optional) internal value to a token,
    71   usually as a side-effect of special scanner setup (see also
    72   args.ML).  Note that an assignable ref designates an intermediate
    73   state of internalization -- it is NOT meant to persist.*)
    74 
    75 datatype value =
    76   Text of string |
    77   Typ of typ |
    78   Term of term |
    79   Fact of thm list |
    80   Attribute of morphism -> attribute;
    81 
    82 datatype slot =
    83   Slot |
    84   Value of value option |
    85   Assignable of value option Unsynchronized.ref;
    86 
    87 
    88 (* datatype token *)
    89 
    90 datatype kind =
    91   Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
    92   Nat | Float | String | AltString | Verbatim | Space | Comment | InternalValue |
    93   Error of string | Sync | EOF;
    94 
    95 datatype T = Token of (Symbol_Pos.text * Position.range) * (kind * string) * slot;
    96 
    97 val str_of_kind =
    98  fn Command => "command"
    99   | Keyword => "keyword"
   100   | Ident => "identifier"
   101   | LongIdent => "long identifier"
   102   | SymIdent => "symbolic identifier"
   103   | Var => "schematic variable"
   104   | TypeIdent => "type variable"
   105   | TypeVar => "schematic type variable"
   106   | Nat => "natural number"
   107   | Float => "floating-point number"
   108   | String => "string"
   109   | AltString => "back-quoted string"
   110   | Verbatim => "verbatim text"
   111   | Space => "white space"
   112   | Comment => "comment text"
   113   | InternalValue => "internal value"
   114   | Error _ => "bad input"
   115   | Sync => "sync marker"
   116   | EOF => "end-of-file";
   117 
   118 
   119 (* position *)
   120 
   121 fun position_of (Token ((_, (pos, _)), _, _)) = pos;
   122 fun end_position_of (Token ((_, (_, pos)), _, _)) = pos;
   123 
   124 val pos_of = Position.str_of o position_of;
   125 
   126 fun range [] = (Position.none, Position.none)
   127   | range toks =
   128       let
   129         val start_pos = position_of (hd toks);
   130         val end_pos = end_position_of (List.last toks);
   131       in (start_pos, end_pos) end;
   132 
   133 
   134 (* control tokens *)
   135 
   136 fun mk_eof pos = Token (("", (pos, Position.none)), (EOF, ""), Slot);
   137 val eof = mk_eof Position.none;
   138 
   139 fun is_eof (Token (_, (EOF, _), _)) = true
   140   | is_eof _ = false;
   141 
   142 val not_eof = not o is_eof;
   143 
   144 fun not_sync (Token (_, (Sync, _), _)) = false
   145   | not_sync _ = true;
   146 
   147 val stopper =
   148   Scan.stopper (fn [] => eof | toks => mk_eof (end_position_of (List.last toks))) is_eof;
   149 
   150 
   151 (* kind of token *)
   152 
   153 fun kind_of (Token (_, (k, _), _)) = k;
   154 fun is_kind k (Token (_, (k', _), _)) = k = k';
   155 
   156 fun keyword_with pred (Token (_, (Keyword, x), _)) = pred x
   157   | keyword_with _ _ = false;
   158 
   159 fun ident_with pred (Token (_, (Ident, x), _)) = pred x
   160   | ident_with _ _ = false;
   161 
   162 fun is_proper (Token (_, (Space, _), _)) = false
   163   | is_proper (Token (_, (Comment, _), _)) = false
   164   | is_proper _ = true;
   165 
   166 fun is_semicolon (Token (_, (Keyword, ";"), _)) = true
   167   | is_semicolon _ = false;
   168 
   169 fun is_comment (Token (_, (Comment, _), _)) = true
   170   | is_comment _ = false;
   171 
   172 fun is_begin_ignore (Token (_, (Comment, "<"), _)) = true
   173   | is_begin_ignore _ = false;
   174 
   175 fun is_end_ignore (Token (_, (Comment, ">"), _)) = true
   176   | is_end_ignore _ = false;
   177 
   178 
   179 (* blanks and newlines -- space tokens obey lines *)
   180 
   181 fun is_blank (Token (_, (Space, x), _)) = not (String.isSuffix "\n" x)
   182   | is_blank _ = false;
   183 
   184 fun is_newline (Token (_, (Space, x), _)) = String.isSuffix "\n" x
   185   | is_newline _ = false;
   186 
   187 
   188 (* token content *)
   189 
   190 fun source_of (Token ((source, (pos, _)), (_, x), _)) =
   191   if YXML.detect x then x
   192   else YXML.string_of (XML.Elem (Markup.token (Position.properties_of pos), [XML.Text source]));
   193 
   194 fun source_position_of (Token ((source, (pos, _)), _, _)) = (source, pos);
   195 
   196 fun content_of (Token (_, (_, x), _)) = x;
   197 
   198 
   199 (* unparse *)
   200 
   201 fun unparse (Token (_, (kind, x), _)) =
   202   (case kind of
   203     String => Symbol_Pos.quote_string_qq x
   204   | AltString => Symbol_Pos.quote_string_bq x
   205   | Verbatim => enclose "{*" "*}" x
   206   | Comment => enclose "(*" "*)" x
   207   | Sync => ""
   208   | EOF => ""
   209   | _ => x);
   210 
   211 fun text_of tok =
   212   if is_semicolon tok then ("terminator", "")
   213   else
   214     let
   215       val k = str_of_kind (kind_of tok);
   216       val s = unparse tok;
   217     in
   218       if s = "" then (k, "")
   219       else if size s < 40 andalso not (exists_string (fn c => c = "\n") s) then (k ^ " " ^ s, "")
   220       else (k, s)
   221     end;
   222 
   223 
   224 
   225 (** associated values **)
   226 
   227 (* access values *)
   228 
   229 fun get_value (Token (_, _, Value v)) = v
   230   | get_value _ = NONE;
   231 
   232 fun map_value f (Token (x, y, Value (SOME v))) = Token (x, y, Value (SOME (f v)))
   233   | map_value _ tok = tok;
   234 
   235 
   236 (* make values *)
   237 
   238 fun mk_value k v = Token ((k, Position.no_range), (InternalValue, k), Value (SOME v));
   239 
   240 val mk_text = mk_value "<text>" o Text;
   241 val mk_typ = mk_value "<typ>" o Typ;
   242 val mk_term = mk_value "<term>" o Term;
   243 val mk_fact = mk_value "<fact>" o Fact;
   244 val mk_attribute = mk_value "<attribute>" o Attribute;
   245 
   246 
   247 (* static binding *)
   248 
   249 (*1st stage: make empty slots assignable*)
   250 fun assignable (Token (x, y, Slot)) = Token (x, y, Assignable (Unsynchronized.ref NONE))
   251   | assignable tok = tok;
   252 
   253 (*2nd stage: assign values as side-effect of scanning*)
   254 fun assign v (Token (_, _, Assignable r)) = r := v
   255   | assign _ _ = ();
   256 
   257 (*3rd stage: static closure of final values*)
   258 fun closure (Token (x, y, Assignable (Unsynchronized.ref v))) = Token (x, y, Value v)
   259   | closure tok = tok;
   260 
   261 
   262 
   263 (** scanners **)
   264 
   265 open Basic_Symbol_Pos;
   266 
   267 fun !!! msg = Symbol_Pos.!!! (fn () => "Outer lexical error: " ^ msg);
   268 
   269 
   270 (* scan symbolic idents *)
   271 
   272 val is_sym_char = member (op =) (raw_explode "!#$%&*+-/<=>?@^_|~");
   273 
   274 val scan_symid =
   275   Scan.many1 (is_sym_char o Symbol_Pos.symbol) ||
   276   Scan.one (Symbol.is_symbolic o Symbol_Pos.symbol) >> single;
   277 
   278 fun is_symid str =
   279   (case try Symbol.explode str of
   280     SOME [s] => Symbol.is_symbolic s orelse is_sym_char s
   281   | SOME ss => forall is_sym_char ss
   282   | _ => false);
   283 
   284 fun ident_or_symbolic "begin" = false
   285   | ident_or_symbolic ":" = true
   286   | ident_or_symbolic "::" = true
   287   | ident_or_symbolic s = Lexicon.is_identifier s orelse is_symid s;
   288 
   289 
   290 (* scan verbatim text *)
   291 
   292 val scan_verb =
   293   $$$ "*" --| Scan.ahead (~$$$ "}") ||
   294   Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s) >> single;
   295 
   296 val scan_verbatim =
   297   (Symbol_Pos.scan_pos --| $$$ "{" --| $$$ "*") -- !!! "missing end of verbatim text"
   298     (Symbol_Pos.change_prompt
   299       ((Scan.repeat scan_verb >> flat) -- ($$$ "*" |-- $$$ "}" |-- Symbol_Pos.scan_pos)));
   300 
   301 
   302 (* scan space *)
   303 
   304 fun is_space s = Symbol.is_blank s andalso s <> "\n";
   305 
   306 val scan_space =
   307   Scan.many1 (is_space o Symbol_Pos.symbol) @@@ Scan.optional ($$$ "\n") [] ||
   308   Scan.many (is_space o Symbol_Pos.symbol) @@@ $$$ "\n";
   309 
   310 
   311 (* scan comment *)
   312 
   313 val scan_comment =
   314   Symbol_Pos.scan_pos -- (Symbol_Pos.scan_comment_body !!! -- Symbol_Pos.scan_pos);
   315 
   316 
   317 
   318 (** token sources **)
   319 
   320 fun source_proper src = src |> Source.filter is_proper;
   321 
   322 local
   323 
   324 fun token_leq ((_, syms1), (_, syms2)) = length syms1 <= length syms2;
   325 
   326 fun token k ss =
   327   Token ((Symbol_Pos.implode ss, Symbol_Pos.range ss), (k, Symbol_Pos.content ss), Slot);
   328 
   329 fun token_range k (pos1, (ss, pos2)) =
   330   Token (Symbol_Pos.implode_range pos1 pos2 ss, (k, Symbol_Pos.content ss), Slot);
   331 
   332 fun scan (lex1, lex2) = !!! "bad input"
   333   (Symbol_Pos.scan_string_qq >> token_range String ||
   334     Symbol_Pos.scan_string_bq >> token_range AltString ||
   335     scan_verbatim >> token_range Verbatim ||
   336     scan_comment >> token_range Comment ||
   337     scan_space >> token Space ||
   338     Scan.one (Symbol.is_sync o Symbol_Pos.symbol) >> (token Sync o single) ||
   339     (Scan.max token_leq
   340       (Scan.max token_leq
   341         (Scan.literal lex2 >> pair Command)
   342         (Scan.literal lex1 >> pair Keyword))
   343       (Lexicon.scan_longid >> pair LongIdent ||
   344         Lexicon.scan_id >> pair Ident ||
   345         Lexicon.scan_var >> pair Var ||
   346         Lexicon.scan_tid >> pair TypeIdent ||
   347         Lexicon.scan_tvar >> pair TypeVar ||
   348         Lexicon.scan_float >> pair Float ||
   349         Lexicon.scan_nat >> pair Nat ||
   350         scan_symid >> pair SymIdent) >> uncurry token));
   351 
   352 fun recover msg =
   353   Scan.many ((Symbol.is_regular andf (not o Symbol.is_blank)) o Symbol_Pos.symbol)
   354   >> (single o token (Error msg));
   355 
   356 in
   357 
   358 fun source' {do_recover} get_lex =
   359   Source.source Symbol_Pos.stopper (Scan.bulk (fn xs => scan (get_lex ()) xs))
   360     (Option.map (rpair recover) do_recover);
   361 
   362 fun source do_recover get_lex pos src =
   363   Symbol_Pos.source pos src
   364   |> source' do_recover get_lex;
   365 
   366 end;
   367 
   368 
   369 (* read_antiq *)
   370 
   371 fun read_antiq lex scan (syms, pos) =
   372   let
   373     fun err msg = cat_error msg ("Malformed antiquotation" ^ Position.str_of pos ^ ":\n" ^
   374       "@{" ^ Symbol_Pos.content syms ^ "}");
   375 
   376     val res =
   377       Source.of_list syms
   378       |> source' {do_recover = NONE} (K (lex, Scan.empty_lexicon))
   379       |> source_proper
   380       |> Source.source stopper (Scan.error (Scan.bulk scan)) NONE
   381       |> Source.exhaust;
   382   in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end;
   383 
   384 end;