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