src/Pure/Isar/token.ML
author wenzelm
Thu, 02 Apr 2015 12:24:30 +0200
changeset 59908 fdf6957f8635
parent 59906 158c4123f5cc
child 59913 a7f6359665c6
permissions -rw-r--r--
operation on embedded sources for Eisbach;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
     1
(*  Title:      Pure/Isar/token.ML
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
     3
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
     4
Outer token syntax for Isabelle/Isar.
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
     5
*)
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
     6
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
     7
signature TOKEN =
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
     8
sig
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
     9
  datatype kind =
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
    10
    (*immediate source*)
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
    11
    Command | Keyword | Ident | Long_Ident | Sym_Ident | Var | Type_Ident | Type_Var | Nat |
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
    12
    Float | Space |
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
    13
    (*delimited content*)
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
    14
    String | Alt_String | Verbatim | Cartouche | Comment |
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
    15
    (*special content*)
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
    16
    Error of string | Internal_Value | EOF
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    17
  val str_of_kind: kind -> string
55788
67699e08e969 store blobs / inlined files as separate text lines: smaller values are more healthy for the Poly/ML RTS and allow implicit sharing;
wenzelm
parents: 55750
diff changeset
    18
  type file = {src_path: Path.T, lines: string list, digest: SHA1.digest, pos: Position.T}
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    19
  type T
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    20
  type src
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
    21
  datatype value =
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    22
    Source of src |
57944
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
    23
    Literal of bool * Markup.T |
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
    24
    Name of string * morphism |
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
    25
    Typ of typ |
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
    26
    Term of term |
57942
e5bec882fdd0 more informative Token.Fact: retain name of dynamic fact (without selection);
wenzelm
parents: 56202
diff changeset
    27
    Fact of string option * thm list |
57944
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
    28
    Attribute of morphism -> attribute |
58017
5bdb58845eab support for declaration within token source;
wenzelm
parents: 58012
diff changeset
    29
    Declaration of declaration |
57944
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
    30
    Files of file Exn.result list
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
    31
  val name0: string -> value
55708
f4b114070675 tuned signature;
wenzelm
parents: 55111
diff changeset
    32
  val pos_of: T -> Position.T
55709
4e5a83a46ded clarified Token.range_of in accordance to Symbol_Pos.range;
wenzelm
parents: 55708
diff changeset
    33
  val range_of: T list -> Position.range
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    34
  val src: xstring * Position.T -> T list -> src
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    35
  val name_of_src: src -> string * Position.T
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    36
  val args_of_src: src -> T list
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    37
  val range_of_src: src -> Position.T
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    38
  val check_src: Proof.context -> 'a Name_Space.table -> src -> src * 'a
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    39
  val eof: T
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    40
  val is_eof: T -> bool
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    41
  val not_eof: T -> bool
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    42
  val stopper: T Scan.stopper
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    43
  val kind_of: T -> kind
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    44
  val is_kind: kind -> T -> bool
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    45
  val keyword_with: (string -> bool) -> T -> bool
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    46
  val ident_with: (string -> bool) -> T -> bool
46811
03a2dc9e0624 clarified command span: include trailing whitespace/comments and thus reduce number of ignored spans with associated transactions and states (factor 2);
wenzelm
parents: 45666
diff changeset
    47
  val is_command: T -> bool
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 48771
diff changeset
    48
  val is_name: T -> bool
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    49
  val is_proper: T -> bool
51266
3007d0bc9cb1 unified Command.is_proper in ML with Scala (see also 123be08eed88);
wenzelm
parents: 50239
diff changeset
    50
  val is_improper: T -> bool
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    51
  val is_comment: T -> bool
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    52
  val is_begin_ignore: T -> bool
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    53
  val is_end_ignore: T -> bool
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 48743
diff changeset
    54
  val is_error: T -> bool
48771
2ea997196d04 clarified Command.range vs. Command.proper_range according to Scala version, which is potentially relevant for command status markup;
wenzelm
parents: 48764
diff changeset
    55
  val is_space: T -> bool
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    56
  val is_blank: T -> bool
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    57
  val is_newline: T -> bool
59795
d453c69596cc clarified input source;
wenzelm
parents: 59666
diff changeset
    58
  val content_of: T -> string
59809
87641097d0f3 tuned signature;
wenzelm
parents: 59795
diff changeset
    59
  val input_of: T -> Input.source
59795
d453c69596cc clarified input source;
wenzelm
parents: 59666
diff changeset
    60
  val inner_syntax_of: T -> string
56202
0a11d17eeeff more markup for improper elements;
wenzelm
parents: 56064
diff changeset
    61
  val keyword_markup: bool * Markup.T -> string -> Markup.T
55915
607948c90bf0 suppress short abbreviations more uniformly, for outer and quasi-outer syntax;
wenzelm
parents: 55914
diff changeset
    62
  val completion_report: T -> Position.report_text list
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
    63
  val reports: Keyword.keywords -> T -> Position.report_text list
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
    64
  val markups: Keyword.keywords -> T -> Markup.T list
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    65
  val unparse: T -> string
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    66
  val unparse_src: src -> string list
55745
b865c3035d5c tuned message -- more markup;
wenzelm
parents: 55744
diff changeset
    67
  val print: T -> string
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    68
  val text_of: T -> string * string
54520
cee77d2e9582 release file errors at runtime: Command.eval instead of Command.read;
wenzelm
parents: 54519
diff changeset
    69
  val get_files: T -> file Exn.result list
cee77d2e9582 release file errors at runtime: Command.eval instead of Command.read;
wenzelm
parents: 54519
diff changeset
    70
  val put_files: file Exn.result list -> T -> T
58025
wenzelm
parents: 58017
diff changeset
    71
  val make_value: string -> value -> T
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    72
  val get_value: T -> value option
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    73
  val map_value: (value -> value) -> T -> T
55914
c5b752d549e3 clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents: 55828
diff changeset
    74
  val reports_of_value: T -> Position.report list
59908
fdf6957f8635 operation on embedded sources for Eisbach;
wenzelm
parents: 59906
diff changeset
    75
  val map_values: (value -> value) -> src -> src
59646
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
    76
  val declare_maxidx: T -> Proof.context -> Proof.context
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
    77
  val declare_maxidx_src: src -> Proof.context -> Proof.context
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
    78
  val transform: morphism -> T -> T
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    79
  val transform_src: morphism -> src -> src
55914
c5b752d549e3 clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents: 55828
diff changeset
    80
  val init_assignable: T -> T
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    81
  val init_assignable_src: src -> src
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    82
  val assign: value option -> T -> unit
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    83
  val closure: T -> T
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    84
  val closure_src: src -> src
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
    85
  val pretty_value: Proof.context -> T -> Pretty.T
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
    86
  val pretty_src: Proof.context -> src -> Pretty.T
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
    87
  val ident_or_symbolic: string -> bool
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    88
  val source_proper: (T, 'a) Source.source -> (T, (T, 'a) Source.source) Source.source
58904
f49c4f883c58 eliminated pointless dynamic keywords (TTY legacy);
wenzelm
parents: 58903
diff changeset
    89
  val source: Keyword.keywords ->
58864
505a8150368a recover via scanner;
wenzelm
parents: 58863
diff changeset
    90
    Position.T -> (Symbol.symbol, 'a) Source.source -> (T,
505a8150368a recover via scanner;
wenzelm
parents: 58863
diff changeset
    91
      (Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source) Source.source
58904
f49c4f883c58 eliminated pointless dynamic keywords (TTY legacy);
wenzelm
parents: 58903
diff changeset
    92
  val source_strict: Keyword.keywords ->
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
    93
    Position.T -> (Symbol.symbol, 'a) Source.source -> (T,
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
    94
      (Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source) Source.source
59083
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
    95
  val explode: Keyword.keywords -> Position.T -> string -> T list
59085
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
    96
  val make: (int * int) * string -> Position.T -> T * Position.T
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
    97
  type 'a parser = T list -> 'a * T list
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
    98
  type 'a context_parser = Context.generic * T list -> 'a * (Context.generic * T list)
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58865
diff changeset
    99
  val read_no_commands: Keyword.keywords -> 'a parser -> Symbol_Pos.T list -> 'a list
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58865
diff changeset
   100
  val read_antiq: Keyword.keywords -> 'a parser -> Symbol_Pos.T list * Position.T -> 'a
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   101
  val syntax_generic: 'a context_parser -> src -> Context.generic -> 'a * Context.generic
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   102
  val syntax: 'a context_parser -> src -> Proof.context -> 'a * Proof.context
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   103
end;
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   104
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
   105
structure Token: TOKEN =
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   106
struct
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   107
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   108
(** tokens **)
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   109
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   110
(* token kind *)
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   111
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 32738
diff changeset
   112
datatype kind =
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   113
  (*immediate source*)
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   114
  Command | Keyword | Ident | Long_Ident | Sym_Ident | Var | Type_Ident | Type_Var | Nat |
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   115
  Float | Space |
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   116
  (*delimited content*)
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   117
  String | Alt_String | Verbatim | Cartouche | Comment |
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   118
  (*special content*)
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   119
  Error of string | Internal_Value | EOF;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   120
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   121
val str_of_kind =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6859
diff changeset
   122
 fn Command => "command"
69724548fad1 separate command tokens;
wenzelm
parents: 6859
diff changeset
   123
  | Keyword => "keyword"
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   124
  | Ident => "identifier"
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   125
  | Long_Ident => "long identifier"
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   126
  | Sym_Ident => "symbolic identifier"
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   127
  | Var => "schematic variable"
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   128
  | Type_Ident => "type variable"
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   129
  | Type_Var => "schematic type variable"
40290
47f572aff50a support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents: 38229
diff changeset
   130
  | Nat => "natural number"
47f572aff50a support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents: 38229
diff changeset
   131
  | Float => "floating-point number"
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   132
  | Space => "white space"
55103
57d87ec3da4c tuned errors;
wenzelm
parents: 55033
diff changeset
   133
  | String => "quoted string"
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   134
  | Alt_String => "back-quoted string"
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   135
  | Verbatim => "verbatim text"
55103
57d87ec3da4c tuned errors;
wenzelm
parents: 55033
diff changeset
   136
  | Cartouche => "text cartouche"
7682
46de8064c93c added Space, Comment token kinds (keep actual text);
wenzelm
parents: 7477
diff changeset
   137
  | Comment => "comment text"
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   138
  | Internal_Value => "internal value"
23729
d1ba656978c5 separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents: 23721
diff changeset
   139
  | Error _ => "bad input"
48911
5debc3e4fa81 tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents: 48905
diff changeset
   140
  | EOF => "end-of-input";
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   141
59085
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   142
val immediate_kinds =
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   143
  Vector.fromList
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   144
    [Command, Keyword, Ident, Long_Ident, Sym_Ident, Var, Type_Ident, Type_Var, Nat, Float, Space];
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   145
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   146
val delimited_kind = member (op =) [String, Alt_String, Verbatim, Cartouche, Comment];
55828
42ac3cfb89f6 clarified language markup: added "delimited" property;
wenzelm
parents: 55788
diff changeset
   147
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   148
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   149
(* datatype token *)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   150
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   151
(*The value slot assigns an (optional) internal value to a token,
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   152
  usually as a side-effect of special scanner setup (see also
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   153
  args.ML).  Note that an assignable ref designates an intermediate
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   154
  state of internalization -- it is NOT meant to persist.*)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   155
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   156
type file = {src_path: Path.T, lines: string list, digest: SHA1.digest, pos: Position.T};
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   157
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   158
datatype T = Token of (Symbol_Pos.text * Position.range) * (kind * string) * slot
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   159
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   160
and src =
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   161
  Src of
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   162
   {name: string * Position.T,
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   163
    args: T list,
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   164
    checked_markup: (string * Markup.T) option}
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   165
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   166
and slot =
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   167
  Slot |
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   168
  Value of value option |
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   169
  Assignable of value option Unsynchronized.ref
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   170
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   171
and value =
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   172
  Source of src |
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   173
  Literal of bool * Markup.T |
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   174
  Name of string * morphism |
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   175
  Typ of typ |
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   176
  Term of term |
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   177
  Fact of string option * thm list |  (*optional name for dynamic fact, i.e. fact "variable"*)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   178
  Attribute of morphism -> attribute |
58017
5bdb58845eab support for declaration within token source;
wenzelm
parents: 58012
diff changeset
   179
  Declaration of declaration |
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   180
  Files of file Exn.result list;
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   181
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   182
fun name0 a = Name (a, Morphism.identity);
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   183
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   184
27733
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   185
(* position *)
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   186
55708
f4b114070675 tuned signature;
wenzelm
parents: 55111
diff changeset
   187
fun pos_of (Token ((_, (pos, _)), _, _)) = pos;
f4b114070675 tuned signature;
wenzelm
parents: 55111
diff changeset
   188
fun end_pos_of (Token ((_, (_, pos)), _, _)) = pos;
27663
098798321622 maintain token range;
wenzelm
parents: 27358
diff changeset
   189
55709
4e5a83a46ded clarified Token.range_of in accordance to Symbol_Pos.range;
wenzelm
parents: 55708
diff changeset
   190
fun range_of (toks as tok :: _) =
4e5a83a46ded clarified Token.range_of in accordance to Symbol_Pos.range;
wenzelm
parents: 55708
diff changeset
   191
      let val pos' = end_pos_of (List.last toks)
4e5a83a46ded clarified Token.range_of in accordance to Symbol_Pos.range;
wenzelm
parents: 55708
diff changeset
   192
      in Position.range (pos_of tok) pos' end
4e5a83a46ded clarified Token.range_of in accordance to Symbol_Pos.range;
wenzelm
parents: 55708
diff changeset
   193
  | range_of [] = Position.no_range;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   194
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   195
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   196
(* src *)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   197
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   198
fun src name args = Src {name = name, args = args, checked_markup = NONE};
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   199
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   200
fun map_args f (Src {name, args, checked_markup}) =
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   201
  Src {name = name, args = map f args, checked_markup = checked_markup};
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   202
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   203
fun name_of_src (Src {name, ...}) = name;
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   204
fun args_of_src (Src {args, ...}) = args;
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   205
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   206
fun range_of_src (Src {name = (_, pos), args, ...}) =
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   207
  if null args then pos
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   208
  else Position.set_range (pos, #2 (range_of args));
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   209
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   210
fun check_src _ table (src as Src {name = (name, _), checked_markup = SOME _, ...}) =
59666
0e9f303d1515 clarified Token.check_src: intern at most once;
wenzelm
parents: 59646
diff changeset
   211
      (src, Name_Space.get table name)
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   212
  | check_src ctxt table (Src {name = (xname, pos), args, checked_markup = NONE}) =
59666
0e9f303d1515 clarified Token.check_src: intern at most once;
wenzelm
parents: 59646
diff changeset
   213
      let
0e9f303d1515 clarified Token.check_src: intern at most once;
wenzelm
parents: 59646
diff changeset
   214
        val (name, x) = Name_Space.check (Context.Proof ctxt) table (xname, pos);
0e9f303d1515 clarified Token.check_src: intern at most once;
wenzelm
parents: 59646
diff changeset
   215
        val space = Name_Space.space_of_table table;
0e9f303d1515 clarified Token.check_src: intern at most once;
wenzelm
parents: 59646
diff changeset
   216
        val kind = Name_Space.kind_of space;
0e9f303d1515 clarified Token.check_src: intern at most once;
wenzelm
parents: 59646
diff changeset
   217
        val markup = Name_Space.markup space name;
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   218
      in (Src {name = (name, pos), args = args, checked_markup = SOME (kind, markup)}, x) end;
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   219
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   220
58855
2885e2eaa0fb removed pointless markup;
wenzelm
parents: 58854
diff changeset
   221
(* stopper *)
27733
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   222
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   223
fun mk_eof pos = Token (("", (pos, Position.none)), (EOF, ""), Slot);
27733
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   224
val eof = mk_eof Position.none;
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   225
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   226
fun is_eof (Token (_, (EOF, _), _)) = true
27733
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   227
  | is_eof _ = false;
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   228
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   229
val not_eof = not o is_eof;
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   230
27752
ea7d573e565f removed obsolete range_of (already included in position);
wenzelm
parents: 27747
diff changeset
   231
val stopper =
55708
f4b114070675 tuned signature;
wenzelm
parents: 55111
diff changeset
   232
  Scan.stopper (fn [] => eof | toks => mk_eof (end_pos_of (List.last toks))) is_eof;
27733
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   233
d3d7038fb7b5 abstract type Scan.stopper, position taken from last input token;
wenzelm
parents: 27663
diff changeset
   234
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   235
(* kind of token *)
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   236
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   237
fun kind_of (Token (_, (k, _), _)) = k;
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   238
fun is_kind k (Token (_, (k', _), _)) = k = k';
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   239
46811
03a2dc9e0624 clarified command span: include trailing whitespace/comments and thus reduce number of ignored spans with associated transactions and states (factor 2);
wenzelm
parents: 45666
diff changeset
   240
val is_command = is_kind Command;
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59112
diff changeset
   241
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   242
val is_name = is_kind Ident orf is_kind Sym_Ident orf is_kind String orf is_kind Nat;
46811
03a2dc9e0624 clarified command span: include trailing whitespace/comments and thus reduce number of ignored spans with associated transactions and states (factor 2);
wenzelm
parents: 45666
diff changeset
   243
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   244
fun keyword_with pred (Token (_, (Keyword, x), _)) = pred x
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6859
diff changeset
   245
  | keyword_with _ _ = false;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   246
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   247
fun ident_with pred (Token (_, (Ident, x), _)) = pred x
16029
070ed43b86f8 added ident_with;
wenzelm
parents: 15531
diff changeset
   248
  | ident_with _ _ = false;
070ed43b86f8 added ident_with;
wenzelm
parents: 15531
diff changeset
   249
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   250
fun is_proper (Token (_, (Space, _), _)) = false
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   251
  | is_proper (Token (_, (Comment, _), _)) = false
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   252
  | is_proper _ = true;
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   253
51266
3007d0bc9cb1 unified Command.is_proper in ML with Scala (see also 123be08eed88);
wenzelm
parents: 50239
diff changeset
   254
val is_improper = not o is_proper;
3007d0bc9cb1 unified Command.is_proper in ML with Scala (see also 123be08eed88);
wenzelm
parents: 50239
diff changeset
   255
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   256
fun is_comment (Token (_, (Comment, _), _)) = true
17069
ee08b2466a09 clarify is_newline vs. is_blank;
wenzelm
parents: 16029
diff changeset
   257
  | is_comment _ = false;
ee08b2466a09 clarify is_newline vs. is_blank;
wenzelm
parents: 16029
diff changeset
   258
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   259
fun is_begin_ignore (Token (_, (Comment, "<"), _)) = true
8580
e79ee31d3936 added is_begin/end_ignore;
wenzelm
parents: 8231
diff changeset
   260
  | is_begin_ignore _ = false;
e79ee31d3936 added is_begin/end_ignore;
wenzelm
parents: 8231
diff changeset
   261
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   262
fun is_end_ignore (Token (_, (Comment, ">"), _)) = true
8580
e79ee31d3936 added is_begin/end_ignore;
wenzelm
parents: 8231
diff changeset
   263
  | is_end_ignore _ = false;
e79ee31d3936 added is_begin/end_ignore;
wenzelm
parents: 8231
diff changeset
   264
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 48743
diff changeset
   265
fun is_error (Token (_, (Error _, _), _)) = true
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 48743
diff changeset
   266
  | is_error _ = false;
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 48743
diff changeset
   267
8651
f095f3b8181a added is_newline;
wenzelm
parents: 8580
diff changeset
   268
17069
ee08b2466a09 clarify is_newline vs. is_blank;
wenzelm
parents: 16029
diff changeset
   269
(* blanks and newlines -- space tokens obey lines *)
8651
f095f3b8181a added is_newline;
wenzelm
parents: 8580
diff changeset
   270
48771
2ea997196d04 clarified Command.range vs. Command.proper_range according to Scala version, which is potentially relevant for command status markup;
wenzelm
parents: 48764
diff changeset
   271
fun is_space (Token (_, (Space, _), _)) = true
2ea997196d04 clarified Command.range vs. Command.proper_range according to Scala version, which is potentially relevant for command status markup;
wenzelm
parents: 48764
diff changeset
   272
  | is_space _ = false;
2ea997196d04 clarified Command.range vs. Command.proper_range according to Scala version, which is potentially relevant for command status markup;
wenzelm
parents: 48764
diff changeset
   273
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   274
fun is_blank (Token (_, (Space, x), _)) = not (String.isSuffix "\n" x)
17069
ee08b2466a09 clarify is_newline vs. is_blank;
wenzelm
parents: 16029
diff changeset
   275
  | is_blank _ = false;
ee08b2466a09 clarify is_newline vs. is_blank;
wenzelm
parents: 16029
diff changeset
   276
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   277
fun is_newline (Token (_, (Space, x), _)) = String.isSuffix "\n" x
8651
f095f3b8181a added is_newline;
wenzelm
parents: 8580
diff changeset
   278
  | is_newline _ = false;
f095f3b8181a added is_newline;
wenzelm
parents: 8580
diff changeset
   279
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   280
14991
26fb63c4acb5 added unparse;
wenzelm
parents: 14981
diff changeset
   281
(* token content *)
9155
adfa40218e06 OuterLex.name_of: include val;
wenzelm
parents: 9130
diff changeset
   282
59795
d453c69596cc clarified input source;
wenzelm
parents: 59666
diff changeset
   283
fun content_of (Token (_, (_, x), _)) = x;
25642
ebdff0dca2a5 text_of: made even more robust against recurrent errors;
wenzelm
parents: 25582
diff changeset
   284
59809
87641097d0f3 tuned signature;
wenzelm
parents: 59795
diff changeset
   285
fun input_of (Token ((source, range), (kind, _), _)) =
59064
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 58978
diff changeset
   286
  Input.source (delimited_kind kind) source range;
27873
34d61938e27a added source_of';
wenzelm
parents: 27856
diff changeset
   287
59795
d453c69596cc clarified input source;
wenzelm
parents: 59666
diff changeset
   288
fun inner_syntax_of tok =
d453c69596cc clarified input source;
wenzelm
parents: 59666
diff changeset
   289
  let val x = content_of tok
59809
87641097d0f3 tuned signature;
wenzelm
parents: 59795
diff changeset
   290
  in if YXML.detect x then x else Syntax.implode_input (input_of tok) end;
27747
d41abb7bc08a token: maintain of source, which retains original position information;
wenzelm
parents: 27733
diff changeset
   291
d41abb7bc08a token: maintain of source, which retains original position information;
wenzelm
parents: 27733
diff changeset
   292
55915
607948c90bf0 suppress short abbreviations more uniformly, for outer and quasi-outer syntax;
wenzelm
parents: 55914
diff changeset
   293
(* markup reports *)
55744
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   294
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   295
local
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   296
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   297
val token_kind_markup =
59124
wenzelm
parents: 59123
diff changeset
   298
 fn Var => (Markup.var, "")
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   299
  | Type_Ident => (Markup.tfree, "")
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   300
  | Type_Var => (Markup.tvar, "")
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   301
  | String => (Markup.string, "")
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   302
  | Alt_String => (Markup.alt_string, "")
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   303
  | Verbatim => (Markup.verbatim, "")
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   304
  | Cartouche => (Markup.cartouche, "")
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   305
  | Comment => (Markup.comment, "")
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   306
  | Error msg => (Markup.bad, msg)
59124
wenzelm
parents: 59123
diff changeset
   307
  | _ => (Markup.empty, "");
55744
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   308
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   309
fun keyword_reports tok = map (fn markup => ((pos_of tok, markup), ""));
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59112
diff changeset
   310
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   311
fun command_markups keywords x =
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   312
  if Keyword.is_theory_end keywords x then [Markup.keyword2]
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   313
  else if Keyword.is_proof_asm keywords x then [Markup.keyword3]
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   314
  else if Keyword.is_improper keywords x then [Markup.keyword1, Markup.improper]
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   315
  else [Markup.keyword1];
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59112
diff changeset
   316
56063
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   317
in
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   318
56202
0a11d17eeeff more markup for improper elements;
wenzelm
parents: 56064
diff changeset
   319
fun keyword_markup (important, keyword) x =
0a11d17eeeff more markup for improper elements;
wenzelm
parents: 56064
diff changeset
   320
  if important orelse Symbol.is_ascii_identifier x then keyword else Markup.delimiter;
55919
2eb8c13339a5 more explicit quasi_keyword markup, for Args.$$$ material, which is somewhere in between of outer and inner syntax;
wenzelm
parents: 55916
diff changeset
   321
55915
607948c90bf0 suppress short abbreviations more uniformly, for outer and quasi-outer syntax;
wenzelm
parents: 55914
diff changeset
   322
fun completion_report tok =
55914
c5b752d549e3 clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents: 55828
diff changeset
   323
  if is_kind Keyword tok
55915
607948c90bf0 suppress short abbreviations more uniformly, for outer and quasi-outer syntax;
wenzelm
parents: 55914
diff changeset
   324
  then map (fn m => ((pos_of tok, m), "")) (Completion.suppress_abbrevs (content_of tok))
607948c90bf0 suppress short abbreviations more uniformly, for outer and quasi-outer syntax;
wenzelm
parents: 55914
diff changeset
   325
  else [];
55744
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   326
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   327
fun reports keywords tok =
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59112
diff changeset
   328
  if is_command tok then
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   329
    keyword_reports tok (command_markups keywords (content_of tok))
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59112
diff changeset
   330
  else if is_kind Keyword tok then
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   331
    keyword_reports tok [keyword_markup (false, Markup.keyword2) (content_of tok)]
55915
607948c90bf0 suppress short abbreviations more uniformly, for outer and quasi-outer syntax;
wenzelm
parents: 55914
diff changeset
   332
  else
607948c90bf0 suppress short abbreviations more uniformly, for outer and quasi-outer syntax;
wenzelm
parents: 55914
diff changeset
   333
    let val (m, text) = token_kind_markup (kind_of tok)
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   334
    in [((pos_of tok, m), text)] end;
55915
607948c90bf0 suppress short abbreviations more uniformly, for outer and quasi-outer syntax;
wenzelm
parents: 55914
diff changeset
   335
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   336
fun markups keywords = map (#2 o #1) o reports keywords;
55914
c5b752d549e3 clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents: 55828
diff changeset
   337
55744
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   338
end;
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   339
4a4e5686e091 clarified token markup: keyword1/keyword2 is for syntax, and "command" the entity kind;
wenzelm
parents: 55709
diff changeset
   340
27747
d41abb7bc08a token: maintain of source, which retains original position information;
wenzelm
parents: 27733
diff changeset
   341
(* unparse *)
d41abb7bc08a token: maintain of source, which retains original position information;
wenzelm
parents: 27733
diff changeset
   342
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   343
fun unparse (Token (_, (kind, x), _)) =
14991
26fb63c4acb5 added unparse;
wenzelm
parents: 14981
diff changeset
   344
  (case kind of
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43731
diff changeset
   345
    String => Symbol_Pos.quote_string_qq x
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   346
  | Alt_String => Symbol_Pos.quote_string_bq x
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43731
diff changeset
   347
  | Verbatim => enclose "{*" "*}" x
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   348
  | Cartouche => cartouche x
43773
e8ba493027a3 more precise Symbol_Pos.quote_string;
wenzelm
parents: 43731
diff changeset
   349
  | Comment => enclose "(*" "*)" x
23729
d1ba656978c5 separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents: 23721
diff changeset
   350
  | EOF => ""
14991
26fb63c4acb5 added unparse;
wenzelm
parents: 14981
diff changeset
   351
  | _ => x);
26fb63c4acb5 added unparse;
wenzelm
parents: 14981
diff changeset
   352
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   353
fun unparse_src (Src {args, ...}) = map unparse args;
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   354
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   355
fun print tok = Markup.markups (markups Keyword.empty_keywords tok) (unparse tok);
55745
b865c3035d5c tuned message -- more markup;
wenzelm
parents: 55744
diff changeset
   356
23788
54ce229dc858 Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents: 23729
diff changeset
   357
fun text_of tok =
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58855
diff changeset
   358
  let
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58855
diff changeset
   359
    val k = str_of_kind (kind_of tok);
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   360
    val ms = markups Keyword.empty_keywords tok;
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58855
diff changeset
   361
    val s = unparse tok;
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58855
diff changeset
   362
  in
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58855
diff changeset
   363
    if s = "" then (k, "")
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58855
diff changeset
   364
    else if size s < 40 andalso not (exists_string (fn c => c = "\n") s)
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   365
    then (k ^ " " ^ Markup.markups ms s, "")
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   366
    else (k, Markup.markups ms s)
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58855
diff changeset
   367
  end;
23729
d1ba656978c5 separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents: 23721
diff changeset
   368
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   369
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   370
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   371
(** associated values **)
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   372
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 48771
diff changeset
   373
(* inlined file content *)
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 48771
diff changeset
   374
54519
5fed81762406 maintain blobs within document state: digest + text in ML, digest-only in Scala;
wenzelm
parents: 51266
diff changeset
   375
fun get_files (Token (_, _, Value (SOME (Files files)))) = files
5fed81762406 maintain blobs within document state: digest + text in ML, digest-only in Scala;
wenzelm
parents: 51266
diff changeset
   376
  | get_files _ = [];
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 48771
diff changeset
   377
54519
5fed81762406 maintain blobs within document state: digest + text in ML, digest-only in Scala;
wenzelm
parents: 51266
diff changeset
   378
fun put_files [] tok = tok
5fed81762406 maintain blobs within document state: digest + text in ML, digest-only in Scala;
wenzelm
parents: 51266
diff changeset
   379
  | put_files files (Token (x, y, Slot)) = Token (x, y, Value (SOME (Files files)))
55708
f4b114070675 tuned signature;
wenzelm
parents: 55111
diff changeset
   380
  | put_files _ tok = raise Fail ("Cannot put inlined files here" ^ Position.here (pos_of tok));
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 48771
diff changeset
   381
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 48771
diff changeset
   382
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   383
(* access values *)
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   384
58025
wenzelm
parents: 58017
diff changeset
   385
fun make_value name v =
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   386
  Token ((name, Position.no_range), (Internal_Value, name), Value (SOME v));
58025
wenzelm
parents: 58017
diff changeset
   387
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   388
fun get_value (Token (_, _, Value v)) = v
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   389
  | get_value _ = NONE;
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   390
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   391
fun map_value f (Token (x, y, Value (SOME v))) = Token (x, y, Value (SOME (f v)))
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   392
  | map_value _ tok = tok;
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   393
55914
c5b752d549e3 clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents: 55828
diff changeset
   394
fun reports_of_value tok =
c5b752d549e3 clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents: 55828
diff changeset
   395
  (case get_value tok of
56063
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   396
    SOME (Literal markup) =>
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   397
      let
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   398
        val pos = pos_of tok;
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   399
        val x = content_of tok;
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   400
      in
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   401
        if Position.is_reported pos then
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   402
          map (pair pos) (keyword_markup markup x :: Completion.suppress_abbrevs x)
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   403
        else []
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   404
      end
38f13d055107 more explicit markup for Token.Literal;
wenzelm
parents: 55919
diff changeset
   405
  | _ => []);
55914
c5b752d549e3 clarified init_assignable: make double-sure that initial values are reset;
wenzelm
parents: 55828
diff changeset
   406
59908
fdf6957f8635 operation on embedded sources for Eisbach;
wenzelm
parents: 59906
diff changeset
   407
fun map_values f =
fdf6957f8635 operation on embedded sources for Eisbach;
wenzelm
parents: 59906
diff changeset
   408
  (map_args o map_value) (fn Source src => Source (map_values f src) | x => f x);
fdf6957f8635 operation on embedded sources for Eisbach;
wenzelm
parents: 59906
diff changeset
   409
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   410
59646
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   411
(* maxidx *)
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   412
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   413
fun declare_maxidx tok =
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   414
  (case get_value tok of
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   415
    SOME (Source src) => declare_maxidx_src src
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   416
  | SOME (Typ T) => Variable.declare_maxidx (Term.maxidx_of_typ T)
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   417
  | SOME (Term t) => Variable.declare_maxidx (Term.maxidx_of_term t)
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   418
  | SOME (Fact (_, ths)) => fold (Variable.declare_maxidx o Thm.maxidx_of) ths
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   419
  | SOME (Attribute _) => I  (* FIXME !? *)
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   420
  | SOME (Declaration decl) =>
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   421
      (fn ctxt =>
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   422
        let val ctxt' = Context.proof_map (Morphism.form decl) ctxt
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   423
        in Variable.declare_maxidx (Variable.maxidx_of ctxt') ctxt end)
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   424
  | _ => I)
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   425
and declare_maxidx_src src = fold declare_maxidx (args_of_src src);
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   426
48d400469bcb added declare_maxidx operations for Eisbach;
wenzelm
parents: 59125
diff changeset
   427
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   428
(* transform *)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   429
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   430
fun transform phi =
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   431
  map_value (fn v =>
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   432
    (case v of
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   433
      Source src => Source (transform_src phi src)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   434
    | Literal _ => v
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   435
    | Name (a, psi) => Name (a, psi $> phi)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   436
    | Typ T => Typ (Morphism.typ phi T)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   437
    | Term t => Term (Morphism.term phi t)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   438
    | Fact (a, ths) => Fact (a, Morphism.fact phi ths)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   439
    | Attribute att => Attribute (Morphism.transform phi att)
58017
5bdb58845eab support for declaration within token source;
wenzelm
parents: 58012
diff changeset
   440
    | Declaration decl => Declaration (Morphism.transform phi decl)
58012
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   441
    | Files _ => v))
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   442
and transform_src phi = map_args (transform phi);
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   443
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   444
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   445
(* static binding *)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   446
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   447
(*1st stage: initialize assignable slots*)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   448
fun init_assignable (Token (x, y, Slot)) = Token (x, y, Assignable (Unsynchronized.ref NONE))
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   449
  | init_assignable (tok as Token (_, _, Assignable r)) = (r := NONE; tok)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   450
  | init_assignable tok = tok;
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   451
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   452
val init_assignable_src = map_args init_assignable;
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   453
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   454
(*2nd stage: assign values as side-effect of scanning*)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   455
fun assign v (Token (_, _, Assignable r)) = r := v
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   456
  | assign _ _ = ();
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   457
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   458
(*3rd stage: static closure of final values*)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   459
fun closure (Token (x, y, Assignable (Unsynchronized.ref v))) = Token (x, y, Value v)
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   460
  | closure tok = tok;
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   461
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   462
val closure_src = map_args closure;
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   463
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   464
0b0519c41229 support for nested Token.src within Token.T;
wenzelm
parents: 58011
diff changeset
   465
(* pretty *)
57944
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   466
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   467
fun pretty_value ctxt tok =
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   468
  (case get_value tok of
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   469
    SOME (Literal markup) =>
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   470
      let val x = content_of tok
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   471
      in Pretty.mark_str (keyword_markup markup x, x) end
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   472
  | SOME (Name (a, _)) => Pretty.str (quote a)
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   473
  | SOME (Typ T) => Syntax.pretty_typ ctxt T
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   474
  | SOME (Term t) => Syntax.pretty_term ctxt t
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   475
  | SOME (Fact (_, ths)) =>
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   476
      Pretty.enclose "(" ")" (Pretty.breaks (map (Pretty.backquote o Display.pretty_thm ctxt) ths))
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   477
  | _ => Pretty.marks_str (markups Keyword.empty_keywords tok, unparse tok));
57944
fff8d328da56 more informative Token.Name with history of morphisms;
wenzelm
parents: 57942
diff changeset
   478
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   479
fun pretty_src ctxt src =
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   480
  let
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   481
    val Src {name = (name, _), args, checked_markup} = src;
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   482
    val prt_name =
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   483
      (case checked_markup of
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   484
        NONE => Pretty.str name
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   485
      | SOME (_, markup) => Pretty.mark_str (markup, name));
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   486
    val prt_arg = pretty_value ctxt;
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   487
  in Pretty.block (Pretty.breaks (prt_name :: map prt_arg args)) end;
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   488
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   489
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59124
diff changeset
   490
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   491
(** scanners **)
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   492
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 29606
diff changeset
   493
open Basic_Symbol_Pos;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   494
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48749
diff changeset
   495
val err_prefix = "Outer lexical error: ";
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48749
diff changeset
   496
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48749
diff changeset
   497
fun !!! msg = Symbol_Pos.!!! (fn () => err_prefix ^ msg);
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   498
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   499
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   500
(* scan symbolic idents *)
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   501
8231
fa93309ff27e symid: include single symbolic char;
wenzelm
parents: 7902
diff changeset
   502
val scan_symid =
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   503
  Scan.many1 (Symbol.is_symbolic_char o Symbol_Pos.symbol) ||
40525
14a2e686bdac eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents: 40523
diff changeset
   504
  Scan.one (Symbol.is_symbolic o Symbol_Pos.symbol) >> single;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   505
8231
fa93309ff27e symid: include single symbolic char;
wenzelm
parents: 7902
diff changeset
   506
fun is_symid str =
fa93309ff27e symid: include single symbolic char;
wenzelm
parents: 7902
diff changeset
   507
  (case try Symbol.explode str of
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   508
    SOME [s] => Symbol.is_symbolic s orelse Symbol.is_symbolic_char s
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   509
  | SOME ss => forall Symbol.is_symbolic_char ss
8231
fa93309ff27e symid: include single symbolic char;
wenzelm
parents: 7902
diff changeset
   510
  | _ => false);
fa93309ff27e symid: include single symbolic char;
wenzelm
parents: 7902
diff changeset
   511
27814
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   512
fun ident_or_symbolic "begin" = false
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   513
  | ident_or_symbolic ":" = true
05a50886dacb unified Args.T with OuterLex.token;
wenzelm
parents: 27799
diff changeset
   514
  | ident_or_symbolic "::" = true
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
   515
  | ident_or_symbolic s = Symbol_Pos.is_identifier s orelse is_symid s;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   516
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   517
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   518
(* scan verbatim text *)
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   519
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   520
val scan_verb =
55107
1a29ea173bf9 tuned signature;
wenzelm
parents: 55106
diff changeset
   521
  $$$ "*" --| Scan.ahead (~$$ "}") ||
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 58850
diff changeset
   522
  Scan.one (fn (s, _) => s <> "*" andalso Symbol.not_eof s) >> single;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   523
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   524
val scan_verbatim =
55107
1a29ea173bf9 tuned signature;
wenzelm
parents: 55106
diff changeset
   525
  Scan.ahead ($$ "{" -- $$ "*") |--
55106
080c0006e917 tuned error messages, more accurate position;
wenzelm
parents: 55105
diff changeset
   526
    !!! "unclosed verbatim text"
55107
1a29ea173bf9 tuned signature;
wenzelm
parents: 55106
diff changeset
   527
      ((Symbol_Pos.scan_pos --| $$ "{" --| $$ "*") --
58850
1bb0ad7827b4 discontinued obsolete tty and prompt;
wenzelm
parents: 58045
diff changeset
   528
        ((Scan.repeat scan_verb >> flat) -- ($$ "*" |-- $$ "}" |-- Symbol_Pos.scan_pos)));
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   529
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48741
diff changeset
   530
val recover_verbatim =
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48741
diff changeset
   531
  $$$ "{" @@@ $$$ "*" @@@ (Scan.repeat scan_verb >> flat);
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48741
diff changeset
   532
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   533
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   534
(* scan cartouche *)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   535
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   536
val scan_cartouche =
55104
8284c0d5bf52 clarified scan_cartouche_depth, according to Scala version;
wenzelm
parents: 55103
diff changeset
   537
  Symbol_Pos.scan_pos --
55105
75815b3b38a1 tuned -- more direct err_prefix;
wenzelm
parents: 55104
diff changeset
   538
    ((Symbol_Pos.scan_cartouche err_prefix >> Symbol_Pos.cartouche_content) -- Symbol_Pos.scan_pos);
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   539
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   540
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   541
(* scan space *)
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   542
48771
2ea997196d04 clarified Command.range vs. Command.proper_range according to Scala version, which is potentially relevant for command status markup;
wenzelm
parents: 48764
diff changeset
   543
fun space_symbol (s, _) = Symbol.is_blank s andalso s <> "\n";
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   544
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   545
val scan_space =
48771
2ea997196d04 clarified Command.range vs. Command.proper_range according to Scala version, which is potentially relevant for command status markup;
wenzelm
parents: 48764
diff changeset
   546
  Scan.many1 space_symbol @@@ Scan.optional ($$$ "\n") [] ||
2ea997196d04 clarified Command.range vs. Command.proper_range according to Scala version, which is potentially relevant for command status markup;
wenzelm
parents: 48764
diff changeset
   547
  Scan.many space_symbol @@@ $$$ "\n";
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   548
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   549
27780
7d0910f662f7 more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents: 27769
diff changeset
   550
(* scan comment *)
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   551
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   552
val scan_comment =
55105
75815b3b38a1 tuned -- more direct err_prefix;
wenzelm
parents: 55104
diff changeset
   553
  Symbol_Pos.scan_pos -- (Symbol_Pos.scan_comment_body err_prefix -- Symbol_Pos.scan_pos);
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   554
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   555
27663
098798321622 maintain token range;
wenzelm
parents: 27358
diff changeset
   556
27769
ad50c38ef842 improved position handling due to SymbolPos.T;
wenzelm
parents: 27752
diff changeset
   557
(** token sources **)
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   558
27769
ad50c38ef842 improved position handling due to SymbolPos.T;
wenzelm
parents: 27752
diff changeset
   559
fun source_proper src = src |> Source.filter is_proper;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   560
23678
f5d315390edc Malformed token: error msg;
wenzelm
parents: 22873
diff changeset
   561
local
f5d315390edc Malformed token: error msg;
wenzelm
parents: 22873
diff changeset
   562
27769
ad50c38ef842 improved position handling due to SymbolPos.T;
wenzelm
parents: 27752
diff changeset
   563
fun token_leq ((_, syms1), (_, syms2)) = length syms1 <= length syms2;
27780
7d0910f662f7 more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents: 27769
diff changeset
   564
27799
52f07d5292cd tuned SymbolPos interface;
wenzelm
parents: 27780
diff changeset
   565
fun token k ss =
43709
717e96cf9527 discontinued special treatment of hard tabulators;
wenzelm
parents: 42503
diff changeset
   566
  Token ((Symbol_Pos.implode ss, Symbol_Pos.range ss), (k, Symbol_Pos.content ss), Slot);
27799
52f07d5292cd tuned SymbolPos interface;
wenzelm
parents: 27780
diff changeset
   567
52f07d5292cd tuned SymbolPos interface;
wenzelm
parents: 27780
diff changeset
   568
fun token_range k (pos1, (ss, pos2)) =
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59085
diff changeset
   569
  Token (Symbol_Pos.implode_range (pos1, pos2) ss, (k, Symbol_Pos.content ss), Slot);
23678
f5d315390edc Malformed token: error msg;
wenzelm
parents: 22873
diff changeset
   570
59083
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   571
fun scan_token keywords = !!! "bad input"
48764
4fe0920d5049 proper error prefixes;
wenzelm
parents: 48749
diff changeset
   572
  (Symbol_Pos.scan_string_qq err_prefix >> token_range String ||
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   573
    Symbol_Pos.scan_string_bq err_prefix >> token_range Alt_String ||
27799
52f07d5292cd tuned SymbolPos interface;
wenzelm
parents: 27780
diff changeset
   574
    scan_verbatim >> token_range Verbatim ||
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   575
    scan_cartouche >> token_range Cartouche ||
27799
52f07d5292cd tuned SymbolPos interface;
wenzelm
parents: 27780
diff changeset
   576
    scan_comment >> token_range Comment ||
27780
7d0910f662f7 more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents: 27769
diff changeset
   577
    scan_space >> token Space ||
7d0910f662f7 more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents: 27769
diff changeset
   578
    (Scan.max token_leq
27769
ad50c38ef842 improved position handling due to SymbolPos.T;
wenzelm
parents: 27752
diff changeset
   579
      (Scan.max token_leq
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58865
diff changeset
   580
        (Scan.literal (Keyword.major_keywords keywords) >> pair Command)
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58865
diff changeset
   581
        (Scan.literal (Keyword.minor_keywords keywords) >> pair Keyword))
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   582
      (Lexicon.scan_longid >> pair Long_Ident ||
42290
b1f544c84040 discontinued special treatment of structure Lexicon;
wenzelm
parents: 40958
diff changeset
   583
        Lexicon.scan_id >> pair Ident ||
b1f544c84040 discontinued special treatment of structure Lexicon;
wenzelm
parents: 40958
diff changeset
   584
        Lexicon.scan_var >> pair Var ||
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   585
        Lexicon.scan_tid >> pair Type_Ident ||
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   586
        Lexicon.scan_tvar >> pair Type_Var ||
42290
b1f544c84040 discontinued special treatment of structure Lexicon;
wenzelm
parents: 40958
diff changeset
   587
        Lexicon.scan_float >> pair Float ||
b1f544c84040 discontinued special treatment of structure Lexicon;
wenzelm
parents: 40958
diff changeset
   588
        Lexicon.scan_nat >> pair Nat ||
59081
2ceb05ee0331 clarified token kind;
wenzelm
parents: 59064
diff changeset
   589
        scan_symid >> pair Sym_Ident) >> uncurry token));
27769
ad50c38ef842 improved position handling due to SymbolPos.T;
wenzelm
parents: 27752
diff changeset
   590
ad50c38ef842 improved position handling due to SymbolPos.T;
wenzelm
parents: 27752
diff changeset
   591
fun recover msg =
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48741
diff changeset
   592
  (Symbol_Pos.recover_string_qq ||
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48741
diff changeset
   593
    Symbol_Pos.recover_string_bq ||
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48741
diff changeset
   594
    recover_verbatim ||
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54520
diff changeset
   595
    Symbol_Pos.recover_cartouche ||
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48741
diff changeset
   596
    Symbol_Pos.recover_comment ||
58854
b979c781c2db discontinued obsolete \<^sync> marker;
wenzelm
parents: 58850
diff changeset
   597
    Scan.one (Symbol.not_eof o Symbol_Pos.symbol) >> single)
27780
7d0910f662f7 more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents: 27769
diff changeset
   598
  >> (single o token (Error msg));
23678
f5d315390edc Malformed token: error msg;
wenzelm
parents: 22873
diff changeset
   599
f5d315390edc Malformed token: error msg;
wenzelm
parents: 22873
diff changeset
   600
in
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   601
58904
f49c4f883c58 eliminated pointless dynamic keywords (TTY legacy);
wenzelm
parents: 58903
diff changeset
   602
fun source' strict keywords =
58864
505a8150368a recover via scanner;
wenzelm
parents: 58863
diff changeset
   603
  let
59083
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   604
    val scan_strict = Scan.bulk (scan_token keywords);
58864
505a8150368a recover via scanner;
wenzelm
parents: 58863
diff changeset
   605
    val scan = if strict then scan_strict else Scan.recover scan_strict recover;
505a8150368a recover via scanner;
wenzelm
parents: 58863
diff changeset
   606
  in Source.source Symbol_Pos.stopper scan end;
27780
7d0910f662f7 more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents: 27769
diff changeset
   607
58904
f49c4f883c58 eliminated pointless dynamic keywords (TTY legacy);
wenzelm
parents: 58903
diff changeset
   608
fun source keywords pos src = Symbol_Pos.source pos src |> source' false keywords;
f49c4f883c58 eliminated pointless dynamic keywords (TTY legacy);
wenzelm
parents: 58903
diff changeset
   609
fun source_strict keywords pos src = Symbol_Pos.source pos src |> source' true keywords;
23678
f5d315390edc Malformed token: error msg;
wenzelm
parents: 22873
diff changeset
   610
f5d315390edc Malformed token: error msg;
wenzelm
parents: 22873
diff changeset
   611
end;
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   612
30586
9674f64a0702 moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents: 30573
diff changeset
   613
59083
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   614
(* explode *)
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   615
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   616
fun explode keywords pos =
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   617
  Source.of_string #>
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   618
  Symbol.source #>
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   619
  source keywords pos #>
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   620
  Source.exhaust;
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   621
88b0b1f28adc tuned signature;
wenzelm
parents: 59081
diff changeset
   622
59085
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   623
(* make *)
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   624
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   625
fun make ((k, n), s) pos =
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   626
  let
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   627
    val pos' = Position.advance_offset n pos;
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   628
    val range = Position.range pos pos';
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   629
    val tok =
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   630
      if k < Vector.length immediate_kinds then
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   631
        Token ((s, range), (Vector.sub (immediate_kinds, k), s), Slot)
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   632
      else
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   633
        (case explode Keyword.empty_keywords pos s of
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   634
          [tok] => tok
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   635
        | _ => Token ((s, range), (Error (err_prefix ^ "exactly one token expected"), s), Slot))
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   636
  in (tok, pos') end;
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   637
08a6901eb035 clarified define_command: send tokens more directly, without requiring keywords in ML;
wenzelm
parents: 59083
diff changeset
   638
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   639
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   640
(** parsers **)
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   641
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   642
type 'a parser = T list -> 'a * T list;
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   643
type 'a context_parser = Context.generic * T list -> 'a * (Context.generic * T list);
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   644
58045
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   645
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   646
(* read source *)
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   647
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58865
diff changeset
   648
fun read_no_commands keywords scan syms =
58045
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   649
  Source.of_list syms
58904
f49c4f883c58 eliminated pointless dynamic keywords (TTY legacy);
wenzelm
parents: 58903
diff changeset
   650
  |> source' true (Keyword.no_command_keywords keywords)
58045
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   651
  |> source_proper
58864
505a8150368a recover via scanner;
wenzelm
parents: 58863
diff changeset
   652
  |> Source.source stopper (Scan.error (Scan.bulk scan))
58045
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   653
  |> Source.exhaust;
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   654
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58865
diff changeset
   655
fun read_antiq keywords scan (syms, pos) =
58045
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   656
  let
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   657
    fun err msg =
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   658
      cat_error msg ("Malformed antiquotation" ^ Position.here pos ^ ":\n" ^
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   659
        "@{" ^ Symbol_Pos.content syms ^ "}");
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58865
diff changeset
   660
    val res = read_no_commands keywords scan syms handle ERROR msg => err msg;
58045
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   661
  in (case res of [x] => x | _ => err "") end;
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   662
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   663
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   664
(* wrapped syntax *)
ab2483fad861 tuned signature;
wenzelm
parents: 58025
diff changeset
   665
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   666
fun syntax_generic scan (Src {name = (name, pos), args = args0, checked_markup}) context =
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   667
  let
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   668
    val args1 = map init_assignable args0;
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   669
    fun reported_text () =
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   670
      if Context_Position.is_visible_generic context then
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   671
        ((pos, Markup.operator) :: maps (reports_of_value o closure) args1)
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   672
        |> map (fn (p, m) => Position.reported_text p m "")
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   673
      else [];
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   674
  in
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   675
    (case Scan.error (Scan.finite' stopper (Scan.option scan)) (context, args1) of
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   676
      (SOME x, (context', [])) =>
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   677
        let val _ = Output.report (reported_text ())
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   678
        in (x, context') end
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   679
    | (_, (_, args2)) =>
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   680
        let
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   681
          val print_name =
59906
158c4123f5cc tuned -- emphasize semantics of already checked src;
wenzelm
parents: 59809
diff changeset
   682
            (case checked_markup of
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   683
              NONE => quote name
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   684
            | SOME (kind, markup) => plain_words kind ^ " " ^ quote (Markup.markup markup name));
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   685
          val print_args =
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   686
            if null args2 then "" else ":\n  " ^ space_implode " " (map print args2);
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   687
        in
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   688
          error ("Bad arguments for " ^ print_name ^ Position.here pos ^ print_args ^
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   689
            Markup.markup_report (implode (reported_text ())))
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   690
        end)
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   691
  end;
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   692
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   693
fun syntax scan src = apsnd Context.the_proof o syntax_generic scan src o Context.Proof;
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   694
5825
24e4b1780d33 Outer lexical syntax for Isabelle/Isar.
wenzelm
parents:
diff changeset
   695
end;
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   696
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   697
type 'a parser = 'a Token.parser;
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   698
type 'a context_parser = 'a Token.context_parser;
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57944
diff changeset
   699