src/Pure/Isar/token.ML
author wenzelm
Fri Apr 01 17:37:46 2016 +0200 (2016-04-01)
changeset 62797 e08c44eed27f
parent 62782 057e8dbe4326
child 62799 46e6f91c4da1
permissions -rw-r--r--
tuned signature;
wenzelm@36959
     1
(*  Title:      Pure/Isar/token.ML
wenzelm@5825
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5825
     3
wenzelm@36959
     4
Outer token syntax for Isabelle/Isar.
wenzelm@5825
     5
*)
wenzelm@5825
     6
wenzelm@36959
     7
signature TOKEN =
wenzelm@5825
     8
sig
wenzelm@36959
     9
  datatype kind =
wenzelm@59081
    10
    (*immediate source*)
wenzelm@59081
    11
    Command | Keyword | Ident | Long_Ident | Sym_Ident | Var | Type_Ident | Type_Var | Nat |
wenzelm@59081
    12
    Float | Space |
wenzelm@59081
    13
    (*delimited content*)
wenzelm@59081
    14
    String | Alt_String | Verbatim | Cartouche | Comment |
wenzelm@59081
    15
    (*special content*)
wenzelm@61819
    16
    Error of string | EOF
wenzelm@58012
    17
  val str_of_kind: kind -> string
wenzelm@55788
    18
  type file = {src_path: Path.T, lines: string list, digest: SHA1.digest, pos: Position.T}
wenzelm@58012
    19
  type T
wenzelm@61814
    20
  type src = T list
wenzelm@61814
    21
  type name_value = {name: string, kind: string, print: Proof.context -> Markup.T * xstring}
wenzelm@27814
    22
  datatype value =
wenzelm@58012
    23
    Source of src |
wenzelm@57944
    24
    Literal of bool * Markup.T |
wenzelm@61814
    25
    Name of name_value * morphism |
wenzelm@57944
    26
    Typ of typ |
wenzelm@57944
    27
    Term of term |
wenzelm@57942
    28
    Fact of string option * thm list |
wenzelm@57944
    29
    Attribute of morphism -> attribute |
wenzelm@58017
    30
    Declaration of declaration |
wenzelm@57944
    31
    Files of file Exn.result list
wenzelm@55708
    32
  val pos_of: T -> Position.T
wenzelm@55709
    33
  val range_of: T list -> Position.range
wenzelm@36959
    34
  val eof: T
wenzelm@36959
    35
  val is_eof: T -> bool
wenzelm@36959
    36
  val not_eof: T -> bool
wenzelm@36959
    37
  val stopper: T Scan.stopper
wenzelm@36959
    38
  val kind_of: T -> kind
wenzelm@36959
    39
  val is_kind: kind -> T -> bool
wenzelm@46811
    40
  val is_command: T -> bool
wenzelm@48867
    41
  val is_name: T -> bool
wenzelm@59924
    42
  val keyword_with: (string -> bool) -> T -> bool
wenzelm@59939
    43
  val is_command_modifier: T -> bool
wenzelm@59924
    44
  val ident_with: (string -> bool) -> T -> bool
wenzelm@36959
    45
  val is_proper: T -> bool
wenzelm@51266
    46
  val is_improper: T -> bool
wenzelm@36959
    47
  val is_comment: T -> bool
wenzelm@36959
    48
  val is_begin_ignore: T -> bool
wenzelm@36959
    49
  val is_end_ignore: T -> bool
wenzelm@48749
    50
  val is_error: T -> bool
wenzelm@48771
    51
  val is_space: T -> bool
wenzelm@36959
    52
  val is_blank: T -> bool
wenzelm@36959
    53
  val is_newline: T -> bool
wenzelm@59795
    54
  val content_of: T -> string
wenzelm@59809
    55
  val input_of: T -> Input.source
wenzelm@59795
    56
  val inner_syntax_of: T -> string
wenzelm@56202
    57
  val keyword_markup: bool * Markup.T -> string -> Markup.T
wenzelm@55915
    58
  val completion_report: T -> Position.report_text list
wenzelm@59125
    59
  val reports: Keyword.keywords -> T -> Position.report_text list
wenzelm@59125
    60
  val markups: Keyword.keywords -> T -> Markup.T list
wenzelm@36959
    61
  val unparse: T -> string
wenzelm@55745
    62
  val print: T -> string
wenzelm@36959
    63
  val text_of: T -> string * string
wenzelm@54520
    64
  val get_files: T -> file Exn.result list
wenzelm@54520
    65
  val put_files: file Exn.result list -> T -> T
wenzelm@36959
    66
  val get_value: T -> value option
wenzelm@61822
    67
  val reports_of_value: T -> Position.report list
wenzelm@61814
    68
  val name_value: name_value -> value
wenzelm@61814
    69
  val get_name: T -> name_value option
wenzelm@59646
    70
  val declare_maxidx: T -> Proof.context -> Proof.context
wenzelm@61820
    71
  val map_facts: (string option -> thm list -> thm list) -> T -> T
wenzelm@58011
    72
  val transform: morphism -> T -> T
wenzelm@55914
    73
  val init_assignable: T -> T
wenzelm@61814
    74
  val assign: value option -> T -> T
wenzelm@61814
    75
  val evaluate: ('a -> value) -> (T -> 'a) -> T -> 'a
wenzelm@36959
    76
  val closure: T -> T
wenzelm@58012
    77
  val pretty_value: Proof.context -> T -> Pretty.T
wenzelm@61814
    78
  val name_of_src: src -> string * Position.T
wenzelm@61814
    79
  val args_of_src: src -> T list
wenzelm@61814
    80
  val checked_src: src -> bool
wenzelm@61814
    81
  val check_src: Proof.context -> (Proof.context -> 'a Name_Space.table) -> src -> src * 'a
wenzelm@58011
    82
  val pretty_src: Proof.context -> src -> Pretty.T
wenzelm@27814
    83
  val ident_or_symbolic: string -> bool
wenzelm@61614
    84
  val source': bool -> Keyword.keywords -> (Symbol_Pos.T, 'a) Source.source ->
wenzelm@61614
    85
    (T, (Symbol_Pos.T, 'a) Source.source) Source.source
wenzelm@36959
    86
  val source_proper: (T, 'a) Source.source -> (T, (T, 'a) Source.source) Source.source
wenzelm@58904
    87
  val source: Keyword.keywords ->
wenzelm@58864
    88
    Position.T -> (Symbol.symbol, 'a) Source.source -> (T,
wenzelm@58864
    89
      (Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source) Source.source
wenzelm@58904
    90
  val source_strict: Keyword.keywords ->
wenzelm@36959
    91
    Position.T -> (Symbol.symbol, 'a) Source.source -> (T,
wenzelm@30573
    92
      (Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source) Source.source
wenzelm@61471
    93
  val read_cartouche: Symbol_Pos.T list -> T
wenzelm@59083
    94
  val explode: Keyword.keywords -> Position.T -> string -> T list
wenzelm@59085
    95
  val make: (int * int) * string -> Position.T -> T * Position.T
wenzelm@61814
    96
  val make_string: string * Position.T -> T
wenzelm@61814
    97
  val make_src: string * Position.T -> T list -> src
wenzelm@58011
    98
  type 'a parser = T list -> 'a * T list
wenzelm@58011
    99
  type 'a context_parser = Context.generic * T list -> 'a * (Context.generic * T list)
wenzelm@58903
   100
  val read_no_commands: Keyword.keywords -> 'a parser -> Symbol_Pos.T list -> 'a list
wenzelm@58903
   101
  val read_antiq: Keyword.keywords -> 'a parser -> Symbol_Pos.T list * Position.T -> 'a
wenzelm@58011
   102
  val syntax_generic: 'a context_parser -> src -> Context.generic -> 'a * Context.generic
wenzelm@58011
   103
  val syntax: 'a context_parser -> src -> Proof.context -> 'a * Proof.context
wenzelm@5825
   104
end;
wenzelm@5825
   105
wenzelm@36959
   106
structure Token: TOKEN =
wenzelm@5825
   107
struct
wenzelm@5825
   108
wenzelm@5825
   109
(** tokens **)
wenzelm@5825
   110
wenzelm@58012
   111
(* token kind *)
wenzelm@5825
   112
wenzelm@36959
   113
datatype kind =
wenzelm@59081
   114
  (*immediate source*)
wenzelm@59081
   115
  Command | Keyword | Ident | Long_Ident | Sym_Ident | Var | Type_Ident | Type_Var | Nat |
wenzelm@59081
   116
  Float | Space |
wenzelm@59081
   117
  (*delimited content*)
wenzelm@59081
   118
  String | Alt_String | Verbatim | Cartouche | Comment |
wenzelm@59081
   119
  (*special content*)
wenzelm@61819
   120
  Error of string | EOF;
wenzelm@5825
   121
wenzelm@5825
   122
val str_of_kind =
wenzelm@7026
   123
 fn Command => "command"
wenzelm@7026
   124
  | Keyword => "keyword"
wenzelm@5825
   125
  | Ident => "identifier"
wenzelm@59081
   126
  | Long_Ident => "long identifier"
wenzelm@59081
   127
  | Sym_Ident => "symbolic identifier"
wenzelm@5825
   128
  | Var => "schematic variable"
wenzelm@59081
   129
  | Type_Ident => "type variable"
wenzelm@59081
   130
  | Type_Var => "schematic type variable"
wenzelm@40290
   131
  | Nat => "natural number"
wenzelm@40290
   132
  | Float => "floating-point number"
wenzelm@59081
   133
  | Space => "white space"
wenzelm@55103
   134
  | String => "quoted string"
wenzelm@59081
   135
  | Alt_String => "back-quoted string"
wenzelm@5825
   136
  | Verbatim => "verbatim text"
wenzelm@55103
   137
  | Cartouche => "text cartouche"
wenzelm@7682
   138
  | Comment => "comment text"
wenzelm@23729
   139
  | Error _ => "bad input"
wenzelm@48911
   140
  | EOF => "end-of-input";
wenzelm@5825
   141
wenzelm@59085
   142
val immediate_kinds =
wenzelm@59085
   143
  Vector.fromList
wenzelm@59085
   144
    [Command, Keyword, Ident, Long_Ident, Sym_Ident, Var, Type_Ident, Type_Var, Nat, Float, Space];
wenzelm@59085
   145
wenzelm@59081
   146
val delimited_kind = member (op =) [String, Alt_String, Verbatim, Cartouche, Comment];
wenzelm@55828
   147
wenzelm@5825
   148
wenzelm@58012
   149
(* datatype token *)
wenzelm@58012
   150
wenzelm@58012
   151
(*The value slot assigns an (optional) internal value to a token,
wenzelm@58012
   152
  usually as a side-effect of special scanner setup (see also
wenzelm@58012
   153
  args.ML).  Note that an assignable ref designates an intermediate
wenzelm@58012
   154
  state of internalization -- it is NOT meant to persist.*)
wenzelm@58012
   155
wenzelm@58012
   156
type file = {src_path: Path.T, lines: string list, digest: SHA1.digest, pos: Position.T};
wenzelm@58012
   157
wenzelm@61814
   158
type name_value = {name: string, kind: string, print: Proof.context -> Markup.T * xstring};
wenzelm@58012
   159
wenzelm@61814
   160
datatype T = Token of (Symbol_Pos.text * Position.range) * (kind * string) * slot
wenzelm@58012
   161
wenzelm@58012
   162
and slot =
wenzelm@58012
   163
  Slot |
wenzelm@58012
   164
  Value of value option |
wenzelm@58012
   165
  Assignable of value option Unsynchronized.ref
wenzelm@58012
   166
wenzelm@58012
   167
and value =
wenzelm@61814
   168
  Source of T list |
wenzelm@58012
   169
  Literal of bool * Markup.T |
wenzelm@61814
   170
  Name of name_value * morphism |
wenzelm@58012
   171
  Typ of typ |
wenzelm@58012
   172
  Term of term |
wenzelm@58012
   173
  Fact of string option * thm list |  (*optional name for dynamic fact, i.e. fact "variable"*)
wenzelm@58012
   174
  Attribute of morphism -> attribute |
wenzelm@58017
   175
  Declaration of declaration |
wenzelm@58012
   176
  Files of file Exn.result list;
wenzelm@58012
   177
wenzelm@61814
   178
type src = T list;
wenzelm@58012
   179
wenzelm@58012
   180
wenzelm@27733
   181
(* position *)
wenzelm@5825
   182
wenzelm@55708
   183
fun pos_of (Token ((_, (pos, _)), _, _)) = pos;
wenzelm@55708
   184
fun end_pos_of (Token ((_, (_, pos)), _, _)) = pos;
wenzelm@27663
   185
wenzelm@62797
   186
fun reset_range pos (Token ((x, _), y, z)) =
wenzelm@61814
   187
  let val pos' = Position.reset_range pos
wenzelm@61814
   188
  in Token ((x, (pos', pos')), y, z) end;
wenzelm@61814
   189
wenzelm@55709
   190
fun range_of (toks as tok :: _) =
wenzelm@55709
   191
      let val pos' = end_pos_of (List.last toks)
wenzelm@62797
   192
      in Position.range (pos_of tok, pos') end
wenzelm@55709
   193
  | range_of [] = Position.no_range;
wenzelm@5825
   194
wenzelm@5825
   195
wenzelm@58855
   196
(* stopper *)
wenzelm@27733
   197
wenzelm@27814
   198
fun mk_eof pos = Token (("", (pos, Position.none)), (EOF, ""), Slot);
wenzelm@27733
   199
val eof = mk_eof Position.none;
wenzelm@27733
   200
wenzelm@27814
   201
fun is_eof (Token (_, (EOF, _), _)) = true
wenzelm@27733
   202
  | is_eof _ = false;
wenzelm@27733
   203
wenzelm@27733
   204
val not_eof = not o is_eof;
wenzelm@27733
   205
wenzelm@27752
   206
val stopper =
wenzelm@55708
   207
  Scan.stopper (fn [] => eof | toks => mk_eof (end_pos_of (List.last toks))) is_eof;
wenzelm@27733
   208
wenzelm@27733
   209
wenzelm@5825
   210
(* kind of token *)
wenzelm@5825
   211
wenzelm@27814
   212
fun kind_of (Token (_, (k, _), _)) = k;
wenzelm@27814
   213
fun is_kind k (Token (_, (k', _), _)) = k = k';
wenzelm@5825
   214
wenzelm@46811
   215
val is_command = is_kind Command;
wenzelm@59123
   216
wenzelm@59081
   217
val is_name = is_kind Ident orf is_kind Sym_Ident orf is_kind String orf is_kind Nat;
wenzelm@46811
   218
wenzelm@27814
   219
fun keyword_with pred (Token (_, (Keyword, x), _)) = pred x
wenzelm@7026
   220
  | keyword_with _ _ = false;
wenzelm@5825
   221
wenzelm@59990
   222
val is_command_modifier = keyword_with (fn x => x = "private" orelse x = "qualified");
wenzelm@59924
   223
wenzelm@27814
   224
fun ident_with pred (Token (_, (Ident, x), _)) = pred x
wenzelm@16029
   225
  | ident_with _ _ = false;
wenzelm@16029
   226
wenzelm@27814
   227
fun is_proper (Token (_, (Space, _), _)) = false
wenzelm@27814
   228
  | is_proper (Token (_, (Comment, _), _)) = false
wenzelm@5825
   229
  | is_proper _ = true;
wenzelm@5825
   230
wenzelm@51266
   231
val is_improper = not o is_proper;
wenzelm@51266
   232
wenzelm@27814
   233
fun is_comment (Token (_, (Comment, _), _)) = true
wenzelm@17069
   234
  | is_comment _ = false;
wenzelm@17069
   235
wenzelm@27814
   236
fun is_begin_ignore (Token (_, (Comment, "<"), _)) = true
wenzelm@8580
   237
  | is_begin_ignore _ = false;
wenzelm@8580
   238
wenzelm@27814
   239
fun is_end_ignore (Token (_, (Comment, ">"), _)) = true
wenzelm@8580
   240
  | is_end_ignore _ = false;
wenzelm@8580
   241
wenzelm@48749
   242
fun is_error (Token (_, (Error _, _), _)) = true
wenzelm@48749
   243
  | is_error _ = false;
wenzelm@48749
   244
wenzelm@8651
   245
wenzelm@17069
   246
(* blanks and newlines -- space tokens obey lines *)
wenzelm@8651
   247
wenzelm@48771
   248
fun is_space (Token (_, (Space, _), _)) = true
wenzelm@48771
   249
  | is_space _ = false;
wenzelm@48771
   250
wenzelm@27814
   251
fun is_blank (Token (_, (Space, x), _)) = not (String.isSuffix "\n" x)
wenzelm@17069
   252
  | is_blank _ = false;
wenzelm@17069
   253
wenzelm@27814
   254
fun is_newline (Token (_, (Space, x), _)) = String.isSuffix "\n" x
wenzelm@8651
   255
  | is_newline _ = false;
wenzelm@8651
   256
wenzelm@5825
   257
wenzelm@14991
   258
(* token content *)
wenzelm@9155
   259
wenzelm@59795
   260
fun content_of (Token (_, (_, x), _)) = x;
wenzelm@25642
   261
wenzelm@59809
   262
fun input_of (Token ((source, range), (kind, _), _)) =
wenzelm@59064
   263
  Input.source (delimited_kind kind) source range;
wenzelm@27873
   264
wenzelm@59795
   265
fun inner_syntax_of tok =
wenzelm@59795
   266
  let val x = content_of tok
wenzelm@59809
   267
  in if YXML.detect x then x else Syntax.implode_input (input_of tok) end;
wenzelm@27747
   268
wenzelm@27747
   269
wenzelm@55915
   270
(* markup reports *)
wenzelm@55744
   271
wenzelm@55744
   272
local
wenzelm@55744
   273
wenzelm@55744
   274
val token_kind_markup =
wenzelm@59124
   275
 fn Var => (Markup.var, "")
wenzelm@59081
   276
  | Type_Ident => (Markup.tfree, "")
wenzelm@59081
   277
  | Type_Var => (Markup.tvar, "")
wenzelm@59081
   278
  | String => (Markup.string, "")
wenzelm@59081
   279
  | Alt_String => (Markup.alt_string, "")
wenzelm@59081
   280
  | Verbatim => (Markup.verbatim, "")
wenzelm@59081
   281
  | Cartouche => (Markup.cartouche, "")
wenzelm@59081
   282
  | Comment => (Markup.comment, "")
wenzelm@59081
   283
  | Error msg => (Markup.bad, msg)
wenzelm@59124
   284
  | _ => (Markup.empty, "");
wenzelm@55744
   285
wenzelm@59125
   286
fun keyword_reports tok = map (fn markup => ((pos_of tok, markup), ""));
wenzelm@59123
   287
wenzelm@59125
   288
fun command_markups keywords x =
wenzelm@59125
   289
  if Keyword.is_theory_end keywords x then [Markup.keyword2]
wenzelm@59125
   290
  else if Keyword.is_proof_asm keywords x then [Markup.keyword3]
wenzelm@59125
   291
  else if Keyword.is_improper keywords x then [Markup.keyword1, Markup.improper]
wenzelm@59125
   292
  else [Markup.keyword1];
wenzelm@59123
   293
wenzelm@56063
   294
in
wenzelm@56063
   295
wenzelm@56202
   296
fun keyword_markup (important, keyword) x =
wenzelm@56202
   297
  if important orelse Symbol.is_ascii_identifier x then keyword else Markup.delimiter;
wenzelm@55919
   298
wenzelm@55915
   299
fun completion_report tok =
wenzelm@55914
   300
  if is_kind Keyword tok
wenzelm@55915
   301
  then map (fn m => ((pos_of tok, m), "")) (Completion.suppress_abbrevs (content_of tok))
wenzelm@55915
   302
  else [];
wenzelm@55744
   303
wenzelm@59125
   304
fun reports keywords tok =
wenzelm@59123
   305
  if is_command tok then
wenzelm@59125
   306
    keyword_reports tok (command_markups keywords (content_of tok))
wenzelm@59123
   307
  else if is_kind Keyword tok then
wenzelm@59125
   308
    keyword_reports tok [keyword_markup (false, Markup.keyword2) (content_of tok)]
wenzelm@55915
   309
  else
wenzelm@55915
   310
    let val (m, text) = token_kind_markup (kind_of tok)
wenzelm@59125
   311
    in [((pos_of tok, m), text)] end;
wenzelm@55915
   312
wenzelm@59125
   313
fun markups keywords = map (#2 o #1) o reports keywords;
wenzelm@55914
   314
wenzelm@55744
   315
end;
wenzelm@55744
   316
wenzelm@55744
   317
wenzelm@27747
   318
(* unparse *)
wenzelm@27747
   319
wenzelm@27814
   320
fun unparse (Token (_, (kind, x), _)) =
wenzelm@14991
   321
  (case kind of
wenzelm@43773
   322
    String => Symbol_Pos.quote_string_qq x
wenzelm@59081
   323
  | Alt_String => Symbol_Pos.quote_string_bq x
wenzelm@43773
   324
  | Verbatim => enclose "{*" "*}" x
wenzelm@55033
   325
  | Cartouche => cartouche x
wenzelm@43773
   326
  | Comment => enclose "(*" "*)" x
wenzelm@23729
   327
  | EOF => ""
wenzelm@14991
   328
  | _ => x);
wenzelm@14991
   329
wenzelm@59125
   330
fun print tok = Markup.markups (markups Keyword.empty_keywords tok) (unparse tok);
wenzelm@55745
   331
wenzelm@23788
   332
fun text_of tok =
wenzelm@58861
   333
  let
wenzelm@58861
   334
    val k = str_of_kind (kind_of tok);
wenzelm@59125
   335
    val ms = markups Keyword.empty_keywords tok;
wenzelm@58861
   336
    val s = unparse tok;
wenzelm@58861
   337
  in
wenzelm@58861
   338
    if s = "" then (k, "")
wenzelm@58861
   339
    else if size s < 40 andalso not (exists_string (fn c => c = "\n") s)
wenzelm@59125
   340
    then (k ^ " " ^ Markup.markups ms s, "")
wenzelm@59125
   341
    else (k, Markup.markups ms s)
wenzelm@58861
   342
  end;
wenzelm@23729
   343
wenzelm@5825
   344
wenzelm@5825
   345
wenzelm@27814
   346
(** associated values **)
wenzelm@27814
   347
wenzelm@48867
   348
(* inlined file content *)
wenzelm@48867
   349
wenzelm@54519
   350
fun get_files (Token (_, _, Value (SOME (Files files)))) = files
wenzelm@54519
   351
  | get_files _ = [];
wenzelm@48867
   352
wenzelm@54519
   353
fun put_files [] tok = tok
wenzelm@54519
   354
  | put_files files (Token (x, y, Slot)) = Token (x, y, Value (SOME (Files files)))
wenzelm@55708
   355
  | put_files _ tok = raise Fail ("Cannot put inlined files here" ^ Position.here (pos_of tok));
wenzelm@48867
   356
wenzelm@48867
   357
wenzelm@27814
   358
(* access values *)
wenzelm@27814
   359
wenzelm@27814
   360
fun get_value (Token (_, _, Value v)) = v
wenzelm@27814
   361
  | get_value _ = NONE;
wenzelm@27814
   362
wenzelm@27814
   363
fun map_value f (Token (x, y, Value (SOME v))) = Token (x, y, Value (SOME (f v)))
wenzelm@27814
   364
  | map_value _ tok = tok;
wenzelm@27814
   365
wenzelm@61814
   366
wenzelm@61822
   367
(* reports of value *)
wenzelm@61814
   368
wenzelm@61822
   369
fun get_assignable_value (Token (_, _, Assignable r)) = ! r
wenzelm@61822
   370
  | get_assignable_value (Token (_, _, Value v)) = v
wenzelm@61822
   371
  | get_assignable_value _ = NONE;
wenzelm@60211
   372
wenzelm@55914
   373
fun reports_of_value tok =
wenzelm@60211
   374
  (case get_assignable_value tok of
wenzelm@56063
   375
    SOME (Literal markup) =>
wenzelm@56063
   376
      let
wenzelm@56063
   377
        val pos = pos_of tok;
wenzelm@56063
   378
        val x = content_of tok;
wenzelm@56063
   379
      in
wenzelm@56063
   380
        if Position.is_reported pos then
wenzelm@56063
   381
          map (pair pos) (keyword_markup markup x :: Completion.suppress_abbrevs x)
wenzelm@56063
   382
        else []
wenzelm@56063
   383
      end
wenzelm@56063
   384
  | _ => []);
wenzelm@55914
   385
wenzelm@27814
   386
wenzelm@61822
   387
(* name value *)
wenzelm@61822
   388
wenzelm@61822
   389
fun name_value a = Name (a, Morphism.identity);
wenzelm@61822
   390
wenzelm@61822
   391
fun get_name tok =
wenzelm@61822
   392
  (case get_assignable_value tok of
wenzelm@61822
   393
    SOME (Name (a, _)) => SOME a
wenzelm@61822
   394
  | _ => NONE);
wenzelm@61822
   395
wenzelm@61822
   396
wenzelm@59646
   397
(* maxidx *)
wenzelm@59646
   398
wenzelm@59646
   399
fun declare_maxidx tok =
wenzelm@59646
   400
  (case get_value tok of
wenzelm@61814
   401
    SOME (Source src) => fold declare_maxidx src
wenzelm@59646
   402
  | SOME (Typ T) => Variable.declare_maxidx (Term.maxidx_of_typ T)
wenzelm@59646
   403
  | SOME (Term t) => Variable.declare_maxidx (Term.maxidx_of_term t)
wenzelm@59646
   404
  | SOME (Fact (_, ths)) => fold (Variable.declare_maxidx o Thm.maxidx_of) ths
wenzelm@59646
   405
  | SOME (Attribute _) => I  (* FIXME !? *)
wenzelm@59646
   406
  | SOME (Declaration decl) =>
wenzelm@59646
   407
      (fn ctxt =>
wenzelm@59646
   408
        let val ctxt' = Context.proof_map (Morphism.form decl) ctxt
wenzelm@59646
   409
        in Variable.declare_maxidx (Variable.maxidx_of ctxt') ctxt end)
wenzelm@61814
   410
  | _ => I);
wenzelm@59646
   411
wenzelm@59646
   412
wenzelm@61080
   413
(* fact values *)
wenzelm@61080
   414
wenzelm@61080
   415
fun map_facts f =
wenzelm@61080
   416
  map_value (fn v =>
wenzelm@61080
   417
    (case v of
wenzelm@61814
   418
      Source src => Source (map (map_facts f) src)
wenzelm@61820
   419
    | Fact (a, ths) => Fact (a, f a ths)
wenzelm@61814
   420
    | _ => v));
wenzelm@61080
   421
wenzelm@61080
   422
wenzelm@58012
   423
(* transform *)
wenzelm@58012
   424
wenzelm@58012
   425
fun transform phi =
wenzelm@58012
   426
  map_value (fn v =>
wenzelm@58012
   427
    (case v of
wenzelm@61814
   428
      Source src => Source (map (transform phi) src)
wenzelm@58012
   429
    | Literal _ => v
wenzelm@58012
   430
    | Name (a, psi) => Name (a, psi $> phi)
wenzelm@58012
   431
    | Typ T => Typ (Morphism.typ phi T)
wenzelm@58012
   432
    | Term t => Term (Morphism.term phi t)
wenzelm@58012
   433
    | Fact (a, ths) => Fact (a, Morphism.fact phi ths)
wenzelm@58012
   434
    | Attribute att => Attribute (Morphism.transform phi att)
wenzelm@58017
   435
    | Declaration decl => Declaration (Morphism.transform phi decl)
wenzelm@61814
   436
    | Files _ => v));
wenzelm@58012
   437
wenzelm@58012
   438
wenzelm@58012
   439
(* static binding *)
wenzelm@58012
   440
wenzelm@58012
   441
(*1st stage: initialize assignable slots*)
wenzelm@61814
   442
fun init_assignable tok =
wenzelm@61814
   443
  (case tok of
wenzelm@61814
   444
    Token (x, y, Slot) => Token (x, y, Assignable (Unsynchronized.ref NONE))
wenzelm@61814
   445
  | Token (_, _, Value _) => tok
wenzelm@61814
   446
  | Token (_, _, Assignable r) => (r := NONE; tok));
wenzelm@58012
   447
wenzelm@58012
   448
(*2nd stage: assign values as side-effect of scanning*)
wenzelm@61814
   449
fun assign v tok =
wenzelm@61814
   450
  (case tok of
wenzelm@61814
   451
    Token (x, y, Slot) => Token (x, y, Value v)
wenzelm@61814
   452
  | Token (_, _, Value _) => tok
wenzelm@61814
   453
  | Token (_, _, Assignable r) => (r := v; tok));
wenzelm@61814
   454
wenzelm@61814
   455
fun evaluate mk eval arg =
wenzelm@61814
   456
  let val x = eval arg in (assign (SOME (mk x)) arg; x) end;
wenzelm@58012
   457
wenzelm@58012
   458
(*3rd stage: static closure of final values*)
wenzelm@58012
   459
fun closure (Token (x, y, Assignable (Unsynchronized.ref v))) = Token (x, y, Value v)
wenzelm@58012
   460
  | closure tok = tok;
wenzelm@58012
   461
wenzelm@58012
   462
wenzelm@58012
   463
(* pretty *)
wenzelm@57944
   464
wenzelm@57944
   465
fun pretty_value ctxt tok =
wenzelm@57944
   466
  (case get_value tok of
wenzelm@57944
   467
    SOME (Literal markup) =>
wenzelm@57944
   468
      let val x = content_of tok
wenzelm@57944
   469
      in Pretty.mark_str (keyword_markup markup x, x) end
wenzelm@61814
   470
  | SOME (Name ({print, ...}, _)) => Pretty.quote (Pretty.mark_str (print ctxt))
wenzelm@57944
   471
  | SOME (Typ T) => Syntax.pretty_typ ctxt T
wenzelm@57944
   472
  | SOME (Term t) => Syntax.pretty_term ctxt t
wenzelm@57944
   473
  | SOME (Fact (_, ths)) =>
wenzelm@62094
   474
      Pretty.enclose "(" ")" (Pretty.breaks (map (Pretty.cartouche o Thm.pretty_thm ctxt) ths))
wenzelm@59125
   475
  | _ => Pretty.marks_str (markups Keyword.empty_keywords tok, unparse tok));
wenzelm@57944
   476
wenzelm@61814
   477
wenzelm@61814
   478
(* src *)
wenzelm@61814
   479
wenzelm@61825
   480
fun dest_src ([]: src) = raise Fail "Empty token source"
wenzelm@61814
   481
  | dest_src (head :: args) = (head, args);
wenzelm@61814
   482
wenzelm@61814
   483
fun name_of_src src =
wenzelm@61814
   484
  let
wenzelm@61814
   485
    val head = #1 (dest_src src);
wenzelm@61814
   486
    val name =
wenzelm@61814
   487
      (case get_name head of
wenzelm@61814
   488
        SOME {name, ...} => name
wenzelm@61814
   489
      | NONE => content_of head);
wenzelm@61814
   490
  in (name, pos_of head) end;
wenzelm@61814
   491
wenzelm@61814
   492
val args_of_src = #2 o dest_src;
wenzelm@61814
   493
wenzelm@58011
   494
fun pretty_src ctxt src =
wenzelm@58011
   495
  let
wenzelm@61814
   496
    val (head, args) = dest_src src;
wenzelm@58011
   497
    val prt_name =
wenzelm@61814
   498
      (case get_name head of
wenzelm@61814
   499
        SOME {print, ...} => Pretty.mark_str (print ctxt)
wenzelm@61814
   500
      | NONE => Pretty.str (content_of head));
wenzelm@61814
   501
  in Pretty.block (Pretty.breaks (Pretty.quote prt_name :: map (pretty_value ctxt) args)) end;
wenzelm@61814
   502
wenzelm@61814
   503
fun checked_src (head :: _) = is_some (get_name head)
wenzelm@61814
   504
  | checked_src [] = true;
wenzelm@61814
   505
wenzelm@61814
   506
fun check_src ctxt get_table src =
wenzelm@61814
   507
  let
wenzelm@61814
   508
    val (head, args) = dest_src src;
wenzelm@61814
   509
    val table = get_table ctxt;
wenzelm@61814
   510
  in
wenzelm@61814
   511
    (case get_name head of
wenzelm@61814
   512
      SOME {name, ...} => (src, Name_Space.get table name)
wenzelm@61814
   513
    | NONE =>
wenzelm@61814
   514
        let
wenzelm@61814
   515
          val (name, x) =
wenzelm@61814
   516
            Name_Space.check (Context.Proof ctxt) table (content_of head, pos_of head);
wenzelm@61814
   517
          val kind = Name_Space.kind_of (Name_Space.space_of_table table);
wenzelm@61814
   518
          fun print ctxt' =
wenzelm@61814
   519
            Name_Space.markup_extern ctxt' (Name_Space.space_of_table (get_table ctxt')) name;
wenzelm@61814
   520
          val value = name_value {name = name, kind = kind, print = print};
wenzelm@61814
   521
          val head' = closure (assign (SOME value) head);
wenzelm@61814
   522
        in (head' :: args, x) end)
wenzelm@61814
   523
  end;
wenzelm@58011
   524
wenzelm@58011
   525
wenzelm@59125
   526
wenzelm@5825
   527
(** scanners **)
wenzelm@5825
   528
wenzelm@30573
   529
open Basic_Symbol_Pos;
wenzelm@5825
   530
wenzelm@48764
   531
val err_prefix = "Outer lexical error: ";
wenzelm@48764
   532
wenzelm@48764
   533
fun !!! msg = Symbol_Pos.!!! (fn () => err_prefix ^ msg);
wenzelm@5825
   534
wenzelm@5825
   535
wenzelm@5825
   536
(* scan symbolic idents *)
wenzelm@5825
   537
wenzelm@8231
   538
val scan_symid =
wenzelm@55033
   539
  Scan.many1 (Symbol.is_symbolic_char o Symbol_Pos.symbol) ||
wenzelm@40525
   540
  Scan.one (Symbol.is_symbolic o Symbol_Pos.symbol) >> single;
wenzelm@5825
   541
wenzelm@8231
   542
fun is_symid str =
wenzelm@8231
   543
  (case try Symbol.explode str of
wenzelm@55033
   544
    SOME [s] => Symbol.is_symbolic s orelse Symbol.is_symbolic_char s
wenzelm@55033
   545
  | SOME ss => forall Symbol.is_symbolic_char ss
wenzelm@8231
   546
  | _ => false);
wenzelm@8231
   547
wenzelm@27814
   548
fun ident_or_symbolic "begin" = false
wenzelm@27814
   549
  | ident_or_symbolic ":" = true
wenzelm@27814
   550
  | ident_or_symbolic "::" = true
wenzelm@50239
   551
  | ident_or_symbolic s = Symbol_Pos.is_identifier s orelse is_symid s;
wenzelm@5825
   552
wenzelm@5825
   553
wenzelm@5825
   554
(* scan verbatim text *)
wenzelm@5825
   555
wenzelm@5825
   556
val scan_verb =
wenzelm@55107
   557
  $$$ "*" --| Scan.ahead (~$$ "}") ||
wenzelm@58854
   558
  Scan.one (fn (s, _) => s <> "*" andalso Symbol.not_eof s) >> single;
wenzelm@5825
   559
wenzelm@5825
   560
val scan_verbatim =
wenzelm@55107
   561
  Scan.ahead ($$ "{" -- $$ "*") |--
wenzelm@55106
   562
    !!! "unclosed verbatim text"
wenzelm@55107
   563
      ((Symbol_Pos.scan_pos --| $$ "{" --| $$ "*") --
wenzelm@61476
   564
        (Scan.repeats scan_verb -- ($$ "*" |-- $$ "}" |-- Symbol_Pos.scan_pos)));
wenzelm@5825
   565
wenzelm@48743
   566
val recover_verbatim =
wenzelm@61476
   567
  $$$ "{" @@@ $$$ "*" @@@ Scan.repeats scan_verb;
wenzelm@48743
   568
wenzelm@5825
   569
wenzelm@55033
   570
(* scan cartouche *)
wenzelm@55033
   571
wenzelm@55033
   572
val scan_cartouche =
wenzelm@55104
   573
  Symbol_Pos.scan_pos --
wenzelm@55105
   574
    ((Symbol_Pos.scan_cartouche err_prefix >> Symbol_Pos.cartouche_content) -- Symbol_Pos.scan_pos);
wenzelm@55033
   575
wenzelm@55033
   576
wenzelm@5825
   577
(* scan space *)
wenzelm@5825
   578
wenzelm@48771
   579
fun space_symbol (s, _) = Symbol.is_blank s andalso s <> "\n";
wenzelm@5825
   580
wenzelm@5825
   581
val scan_space =
wenzelm@48771
   582
  Scan.many1 space_symbol @@@ Scan.optional ($$$ "\n") [] ||
wenzelm@48771
   583
  Scan.many space_symbol @@@ $$$ "\n";
wenzelm@5825
   584
wenzelm@5825
   585
wenzelm@27780
   586
(* scan comment *)
wenzelm@5825
   587
wenzelm@5825
   588
val scan_comment =
wenzelm@55105
   589
  Symbol_Pos.scan_pos -- (Symbol_Pos.scan_comment_body err_prefix -- Symbol_Pos.scan_pos);
wenzelm@5825
   590
wenzelm@5825
   591
wenzelm@27663
   592
wenzelm@27769
   593
(** token sources **)
wenzelm@5825
   594
wenzelm@27769
   595
fun source_proper src = src |> Source.filter is_proper;
wenzelm@5825
   596
wenzelm@23678
   597
local
wenzelm@23678
   598
wenzelm@27769
   599
fun token_leq ((_, syms1), (_, syms2)) = length syms1 <= length syms2;
wenzelm@27780
   600
wenzelm@27799
   601
fun token k ss =
wenzelm@43709
   602
  Token ((Symbol_Pos.implode ss, Symbol_Pos.range ss), (k, Symbol_Pos.content ss), Slot);
wenzelm@27799
   603
wenzelm@27799
   604
fun token_range k (pos1, (ss, pos2)) =
wenzelm@59112
   605
  Token (Symbol_Pos.implode_range (pos1, pos2) ss, (k, Symbol_Pos.content ss), Slot);
wenzelm@23678
   606
wenzelm@59083
   607
fun scan_token keywords = !!! "bad input"
wenzelm@48764
   608
  (Symbol_Pos.scan_string_qq err_prefix >> token_range String ||
wenzelm@59081
   609
    Symbol_Pos.scan_string_bq err_prefix >> token_range Alt_String ||
wenzelm@27799
   610
    scan_verbatim >> token_range Verbatim ||
wenzelm@55033
   611
    scan_cartouche >> token_range Cartouche ||
wenzelm@27799
   612
    scan_comment >> token_range Comment ||
wenzelm@27780
   613
    scan_space >> token Space ||
wenzelm@27780
   614
    (Scan.max token_leq
wenzelm@27769
   615
      (Scan.max token_leq
wenzelm@58903
   616
        (Scan.literal (Keyword.major_keywords keywords) >> pair Command)
wenzelm@58903
   617
        (Scan.literal (Keyword.minor_keywords keywords) >> pair Keyword))
wenzelm@59081
   618
      (Lexicon.scan_longid >> pair Long_Ident ||
wenzelm@42290
   619
        Lexicon.scan_id >> pair Ident ||
wenzelm@42290
   620
        Lexicon.scan_var >> pair Var ||
wenzelm@59081
   621
        Lexicon.scan_tid >> pair Type_Ident ||
wenzelm@59081
   622
        Lexicon.scan_tvar >> pair Type_Var ||
wenzelm@62782
   623
        Symbol_Pos.scan_float >> pair Float ||
wenzelm@62782
   624
        Symbol_Pos.scan_nat >> pair Nat ||
wenzelm@59081
   625
        scan_symid >> pair Sym_Ident) >> uncurry token));
wenzelm@27769
   626
wenzelm@27769
   627
fun recover msg =
wenzelm@48743
   628
  (Symbol_Pos.recover_string_qq ||
wenzelm@48743
   629
    Symbol_Pos.recover_string_bq ||
wenzelm@48743
   630
    recover_verbatim ||
wenzelm@55033
   631
    Symbol_Pos.recover_cartouche ||
wenzelm@48743
   632
    Symbol_Pos.recover_comment ||
wenzelm@58854
   633
    Scan.one (Symbol.not_eof o Symbol_Pos.symbol) >> single)
wenzelm@27780
   634
  >> (single o token (Error msg));
wenzelm@23678
   635
wenzelm@23678
   636
in
wenzelm@5825
   637
wenzelm@58904
   638
fun source' strict keywords =
wenzelm@58864
   639
  let
wenzelm@59083
   640
    val scan_strict = Scan.bulk (scan_token keywords);
wenzelm@58864
   641
    val scan = if strict then scan_strict else Scan.recover scan_strict recover;
wenzelm@58864
   642
  in Source.source Symbol_Pos.stopper scan end;
wenzelm@27780
   643
wenzelm@58904
   644
fun source keywords pos src = Symbol_Pos.source pos src |> source' false keywords;
wenzelm@58904
   645
fun source_strict keywords pos src = Symbol_Pos.source pos src |> source' true keywords;
wenzelm@23678
   646
wenzelm@61471
   647
fun read_cartouche syms =
wenzelm@61471
   648
  (case Scan.read Symbol_Pos.stopper (scan_cartouche >> token_range Cartouche) syms of
wenzelm@61471
   649
    SOME tok => tok
wenzelm@61471
   650
  | NONE => error ("Single cartouche expected" ^ Position.here (#1 (Symbol_Pos.range syms))));
wenzelm@61471
   651
wenzelm@23678
   652
end;
wenzelm@5825
   653
wenzelm@30586
   654
wenzelm@59083
   655
(* explode *)
wenzelm@59083
   656
wenzelm@59083
   657
fun explode keywords pos =
wenzelm@59083
   658
  Source.of_string #>
wenzelm@59083
   659
  Symbol.source #>
wenzelm@59083
   660
  source keywords pos #>
wenzelm@59083
   661
  Source.exhaust;
wenzelm@59083
   662
wenzelm@59083
   663
wenzelm@59085
   664
(* make *)
wenzelm@59085
   665
wenzelm@59085
   666
fun make ((k, n), s) pos =
wenzelm@59085
   667
  let
wenzelm@59085
   668
    val pos' = Position.advance_offset n pos;
wenzelm@62797
   669
    val range = Position.range (pos, pos');
wenzelm@59085
   670
    val tok =
wenzelm@61814
   671
      if 0 <= k andalso k < Vector.length immediate_kinds then
wenzelm@59085
   672
        Token ((s, range), (Vector.sub (immediate_kinds, k), s), Slot)
wenzelm@59085
   673
      else
wenzelm@59085
   674
        (case explode Keyword.empty_keywords pos s of
wenzelm@59085
   675
          [tok] => tok
wenzelm@59085
   676
        | _ => Token ((s, range), (Error (err_prefix ^ "exactly one token expected"), s), Slot))
wenzelm@59085
   677
  in (tok, pos') end;
wenzelm@59085
   678
wenzelm@61814
   679
fun make_string (s, pos) =
wenzelm@61814
   680
  #1 (make ((~1, 0), Symbol_Pos.quote_string_qq s) Position.none)
wenzelm@62797
   681
  |> reset_range pos;
wenzelm@61814
   682
wenzelm@61814
   683
fun make_src a args = make_string a :: args;
wenzelm@61814
   684
wenzelm@59085
   685
wenzelm@58011
   686
wenzelm@58011
   687
(** parsers **)
wenzelm@58011
   688
wenzelm@58011
   689
type 'a parser = T list -> 'a * T list;
wenzelm@58011
   690
type 'a context_parser = Context.generic * T list -> 'a * (Context.generic * T list);
wenzelm@58011
   691
wenzelm@58045
   692
wenzelm@61471
   693
(* read antiquotation source *)
wenzelm@58045
   694
wenzelm@58903
   695
fun read_no_commands keywords scan syms =
wenzelm@58045
   696
  Source.of_list syms
wenzelm@58904
   697
  |> source' true (Keyword.no_command_keywords keywords)
wenzelm@58045
   698
  |> source_proper
wenzelm@58864
   699
  |> Source.source stopper (Scan.error (Scan.bulk scan))
wenzelm@58045
   700
  |> Source.exhaust;
wenzelm@58045
   701
wenzelm@58903
   702
fun read_antiq keywords scan (syms, pos) =
wenzelm@58045
   703
  let
wenzelm@58045
   704
    fun err msg =
wenzelm@58045
   705
      cat_error msg ("Malformed antiquotation" ^ Position.here pos ^ ":\n" ^
wenzelm@58045
   706
        "@{" ^ Symbol_Pos.content syms ^ "}");
wenzelm@58903
   707
    val res = read_no_commands keywords scan syms handle ERROR msg => err msg;
wenzelm@58045
   708
  in (case res of [x] => x | _ => err "") end;
wenzelm@58045
   709
wenzelm@58045
   710
wenzelm@58045
   711
(* wrapped syntax *)
wenzelm@58045
   712
wenzelm@61814
   713
fun syntax_generic scan src context =
wenzelm@58011
   714
  let
wenzelm@61814
   715
    val (name, pos) = name_of_src src;
wenzelm@61814
   716
    val args1 = map init_assignable (args_of_src src);
wenzelm@58011
   717
    fun reported_text () =
wenzelm@58011
   718
      if Context_Position.is_visible_generic context then
wenzelm@58011
   719
        ((pos, Markup.operator) :: maps (reports_of_value o closure) args1)
wenzelm@58011
   720
        |> map (fn (p, m) => Position.reported_text p m "")
wenzelm@58011
   721
      else [];
wenzelm@58011
   722
  in
wenzelm@58011
   723
    (case Scan.error (Scan.finite' stopper (Scan.option scan)) (context, args1) of
wenzelm@58011
   724
      (SOME x, (context', [])) =>
wenzelm@58011
   725
        let val _ = Output.report (reported_text ())
wenzelm@58011
   726
        in (x, context') end
wenzelm@61814
   727
    | (_, (context', args2)) =>
wenzelm@58011
   728
        let
wenzelm@58011
   729
          val print_name =
wenzelm@61814
   730
            (case get_name (hd src) of
wenzelm@58011
   731
              NONE => quote name
wenzelm@61814
   732
            | SOME {kind, print, ...} =>
wenzelm@61814
   733
                let
wenzelm@61814
   734
                  val ctxt' = Context.proof_of context';
wenzelm@61814
   735
                  val (markup, xname) = print ctxt';
wenzelm@61814
   736
                in plain_words kind ^ " " ^ quote (Markup.markup markup xname) end);
wenzelm@58011
   737
          val print_args =
wenzelm@58011
   738
            if null args2 then "" else ":\n  " ^ space_implode " " (map print args2);
wenzelm@58011
   739
        in
wenzelm@58011
   740
          error ("Bad arguments for " ^ print_name ^ Position.here pos ^ print_args ^
wenzelm@58011
   741
            Markup.markup_report (implode (reported_text ())))
wenzelm@58011
   742
        end)
wenzelm@58011
   743
  end;
wenzelm@58011
   744
wenzelm@58011
   745
fun syntax scan src = apsnd Context.the_proof o syntax_generic scan src o Context.Proof;
wenzelm@58011
   746
wenzelm@5825
   747
end;
wenzelm@58011
   748
wenzelm@58011
   749
type 'a parser = 'a Token.parser;
wenzelm@58011
   750
type 'a context_parser = 'a Token.context_parser;