renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
1 (* Title: Pure/Isar/outer_lex.ML
2 Author: Markus Wenzel, TU Muenchen
4 Outer lexical syntax for Isabelle/Isar.
10 Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
11 Nat | String | AltString | Verbatim | Space | Comment | InternalValue |
12 Malformed | Error of string | Sync | EOF
14 Text of string | Typ of typ | Term of term | Fact of thm list |
15 Attribute of morphism -> attribute
17 val str_of_kind: token_kind -> string
18 val position_of: token -> Position.T
19 val end_position_of: token -> Position.T
20 val pos_of: token -> string
22 val is_eof: token -> bool
23 val not_eof: token -> bool
24 val not_sync: token -> bool
25 val stopper: token Scan.stopper
26 val kind_of: token -> token_kind
27 val is_kind: token_kind -> token -> bool
28 val keyword_with: (string -> bool) -> token -> bool
29 val ident_with: (string -> bool) -> token -> bool
30 val is_proper: token -> bool
31 val is_semicolon: token -> bool
32 val is_comment: token -> bool
33 val is_begin_ignore: token -> bool
34 val is_end_ignore: token -> bool
35 val is_blank: token -> bool
36 val is_newline: token -> bool
37 val source_of: token -> string
38 val source_position_of: token -> Symbol_Pos.text * Position.T
39 val content_of: token -> string
40 val unparse: token -> string
41 val text_of: token -> string * string
42 val get_value: token -> value option
43 val map_value: (value -> value) -> token -> token
44 val mk_text: string -> token
45 val mk_typ: typ -> token
46 val mk_term: term -> token
47 val mk_fact: thm list -> token
48 val mk_attribute: (morphism -> attribute) -> token
49 val assignable: token -> token
50 val assign: value option -> token -> unit
51 val closure: token -> token
52 val ident_or_symbolic: string -> bool
53 val !!! : string -> (Symbol_Pos.T list -> 'a) -> Symbol_Pos.T list -> 'a
54 val source_proper: (token, 'a) Source.source -> (token, (token, 'a) Source.source) Source.source
55 val source': {do_recover: bool Option.option} -> (unit -> Scan.lexicon * Scan.lexicon) ->
56 (Symbol_Pos.T, 'a) Source.source -> (token, (Symbol_Pos.T, 'a) Source.source) Source.source
57 val source: {do_recover: bool Option.option} -> (unit -> Scan.lexicon * Scan.lexicon) ->
58 Position.T -> (Symbol.symbol, 'a) Source.source -> (token,
59 (Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source) Source.source) Source.source
60 val read_antiq: Scan.lexicon -> (token list -> 'a * token list) ->
61 Symbol_Pos.T list * Position.T -> 'a
64 structure OuterLex: OUTER_LEX =
71 (*The value slot assigns an (optional) internal value to a token,
72 usually as a side-effect of special scanner setup (see also
73 args.ML). Note that an assignable ref designates an intermediate
74 state of internalization -- it is NOT meant to persist.*)
81 Attribute of morphism -> attribute;
85 Value of value option |
86 Assignable of value option Unsynchronized.ref;
92 Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar |
93 Nat | String | AltString | Verbatim | Space | Comment | InternalValue |
94 Malformed | Error of string | Sync | EOF;
96 datatype token = Token of (Symbol_Pos.text * Position.range) * (token_kind * string) * slot;
99 fn Command => "command"
100 | Keyword => "keyword"
101 | Ident => "identifier"
102 | LongIdent => "long identifier"
103 | SymIdent => "symbolic identifier"
104 | Var => "schematic variable"
105 | TypeIdent => "type variable"
106 | TypeVar => "schematic type variable"
109 | AltString => "back-quoted string"
110 | Verbatim => "verbatim text"
111 | Space => "white space"
112 | Comment => "comment text"
113 | InternalValue => "internal value"
114 | Malformed => "malformed symbolic character"
115 | Error _ => "bad input"
116 | Sync => "sync marker"
117 | EOF => "end-of-file";
122 fun position_of (Token ((_, (pos, _)), _, _)) = pos;
123 fun end_position_of (Token ((_, (_, pos)), _, _)) = pos;
125 val pos_of = Position.str_of o position_of;
130 fun mk_eof pos = Token (("", (pos, Position.none)), (EOF, ""), Slot);
131 val eof = mk_eof Position.none;
133 fun is_eof (Token (_, (EOF, _), _)) = true
136 val not_eof = not o is_eof;
138 fun not_sync (Token (_, (Sync, _), _)) = false
142 Scan.stopper (fn [] => eof | toks => mk_eof (end_position_of (List.last toks))) is_eof;
147 fun kind_of (Token (_, (k, _), _)) = k;
148 fun is_kind k (Token (_, (k', _), _)) = k = k';
150 fun keyword_with pred (Token (_, (Keyword, x), _)) = pred x
151 | keyword_with _ _ = false;
153 fun ident_with pred (Token (_, (Ident, x), _)) = pred x
154 | ident_with _ _ = false;
156 fun is_proper (Token (_, (Space, _), _)) = false
157 | is_proper (Token (_, (Comment, _), _)) = false
158 | is_proper _ = true;
160 fun is_semicolon (Token (_, (Keyword, ";"), _)) = true
161 | is_semicolon _ = false;
163 fun is_comment (Token (_, (Comment, _), _)) = true
164 | is_comment _ = false;
166 fun is_begin_ignore (Token (_, (Comment, "<"), _)) = true
167 | is_begin_ignore _ = false;
169 fun is_end_ignore (Token (_, (Comment, ">"), _)) = true
170 | is_end_ignore _ = false;
173 (* blanks and newlines -- space tokens obey lines *)
175 fun is_blank (Token (_, (Space, x), _)) = not (String.isSuffix "\n" x)
176 | is_blank _ = false;
178 fun is_newline (Token (_, (Space, x), _)) = String.isSuffix "\n" x
179 | is_newline _ = false;
184 fun source_of (Token ((source, (pos, _)), _, _)) =
185 YXML.string_of (XML.Elem (Markup.tokenN, Position.properties_of pos, [XML.Text source]));
187 fun source_position_of (Token ((source, (pos, _)), _, _)) = (source, pos);
189 fun content_of (Token (_, (_, x), _)) = x;
195 implode o map (fn s => if s = q orelse s = "\\" then "\\" ^ s else s) o Symbol.explode;
197 fun unparse (Token (_, (kind, x), _)) =
199 String => x |> quote o escape "\""
200 | AltString => x |> enclose "`" "`" o escape "`"
201 | Verbatim => x |> enclose "{*" "*}"
202 | Comment => x |> enclose "(*" "*)"
203 | Malformed => space_implode " " (explode x)
209 if is_semicolon tok then ("terminator", "")
212 val k = str_of_kind (kind_of tok);
214 handle ERROR _ => Symbol.separate_chars (content_of tok);
216 if s = "" then (k, "")
217 else if size s < 40 andalso not (exists_string (fn c => c = "\n") s) then (k ^ " " ^ s, "")
223 (** associated values **)
227 fun get_value (Token (_, _, Value v)) = v
228 | get_value _ = NONE;
230 fun map_value f (Token (x, y, Value (SOME v))) = Token (x, y, Value (SOME (f v)))
231 | map_value _ tok = tok;
236 fun mk_value k v = Token ((k, Position.no_range), (InternalValue, k), Value (SOME v));
238 val mk_text = mk_value "<text>" o Text;
239 val mk_typ = mk_value "<typ>" o Typ;
240 val mk_term = mk_value "<term>" o Term;
241 val mk_fact = mk_value "<fact>" o Fact;
242 val mk_attribute = mk_value "<attribute>" o Attribute;
247 (*1st stage: make empty slots assignable*)
248 fun assignable (Token (x, y, Slot)) = Token (x, y, Assignable (Unsynchronized.ref NONE))
249 | assignable tok = tok;
251 (*2nd stage: assign values as side-effect of scanning*)
252 fun assign v (Token (_, _, Assignable r)) = r := v
255 (*3rd stage: static closure of final values*)
256 fun closure (Token (x, y, Assignable (Unsynchronized.ref v))) = Token (x, y, Value v)
263 open Basic_Symbol_Pos;
265 fun !!! msg = Symbol_Pos.!!! ("Outer lexical error: " ^ msg);
268 (* scan symbolic idents *)
270 val is_sym_char = member (op =) (explode "!#$%&*+-/<=>?@^_|~");
273 Scan.many1 (is_sym_char o symbol) ||
274 Scan.one (Symbol.is_symbolic o symbol) >> single;
277 (case try Symbol.explode str of
278 SOME [s] => Symbol.is_symbolic s orelse is_sym_char s
279 | SOME ss => forall is_sym_char ss
282 fun ident_or_symbolic "begin" = false
283 | ident_or_symbolic ":" = true
284 | ident_or_symbolic "::" = true
285 | ident_or_symbolic s = Syntax.is_identifier s orelse is_symid s;
288 (* scan verbatim text *)
291 $$$ "*" --| Scan.ahead (~$$$ "}") ||
292 Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s) >> single;
295 (Symbol_Pos.scan_pos --| $$$ "{" --| $$$ "*") -- !!! "missing end of verbatim text"
296 (Symbol_Pos.change_prompt
297 ((Scan.repeat scan_verb >> flat) -- ($$$ "*" |-- $$$ "}" |-- Symbol_Pos.scan_pos)));
302 fun is_space s = Symbol.is_blank s andalso s <> "\n";
305 Scan.many1 (is_space o symbol) @@@ Scan.optional ($$$ "\n") [] ||
306 Scan.many (is_space o symbol) @@@ $$$ "\n";
312 Symbol_Pos.scan_pos -- (Symbol_Pos.scan_comment_body !!! -- Symbol_Pos.scan_pos);
315 (* scan malformed symbols *)
318 $$$ Symbol.malformed |--
319 Symbol_Pos.change_prompt (Scan.many (Symbol.is_regular o symbol))
320 --| Scan.option ($$$ Symbol.end_malformed);
324 (** token sources **)
326 fun source_proper src = src |> Source.filter is_proper;
330 fun token_leq ((_, syms1), (_, syms2)) = length syms1 <= length syms2;
333 Token ((Symbol_Pos.implode ss, Symbol_Pos.range ss), (k, Symbol_Pos.untabify_content ss), Slot);
335 fun token_range k (pos1, (ss, pos2)) =
336 Token (Symbol_Pos.implode_range pos1 pos2 ss, (k, Symbol_Pos.untabify_content ss), Slot);
338 fun scan (lex1, lex2) = !!! "bad input"
339 (Symbol_Pos.scan_string >> token_range String ||
340 Symbol_Pos.scan_alt_string >> token_range AltString ||
341 scan_verbatim >> token_range Verbatim ||
342 scan_comment >> token_range Comment ||
343 scan_space >> token Space ||
344 scan_malformed >> token Malformed ||
345 Scan.one (Symbol.is_sync o symbol) >> (token Sync o single) ||
348 (Scan.literal lex2 >> pair Command)
349 (Scan.literal lex1 >> pair Keyword))
350 (Syntax.scan_longid >> pair LongIdent ||
351 Syntax.scan_id >> pair Ident ||
352 Syntax.scan_var >> pair Var ||
353 Syntax.scan_tid >> pair TypeIdent ||
354 Syntax.scan_tvar >> pair TypeVar ||
355 Syntax.scan_nat >> pair Nat ||
356 scan_symid >> pair SymIdent) >> uncurry token));
359 Scan.many ((Symbol.is_regular andf (not o Symbol.is_blank)) o symbol)
360 >> (single o token (Error msg));
364 fun source' {do_recover} get_lex =
365 Source.source Symbol_Pos.stopper (Scan.bulk (fn xs => scan (get_lex ()) xs))
366 (Option.map (rpair recover) do_recover);
368 fun source do_recover get_lex pos src =
369 Symbol_Pos.source pos src
370 |> source' do_recover get_lex;
377 fun read_antiq lex scan (syms, pos) =
379 fun err msg = cat_error msg ("Malformed antiquotation" ^ Position.str_of pos ^ ":\n" ^
380 "@{" ^ Symbol_Pos.content syms ^ "}");
384 |> source' {do_recover = NONE} (K (lex, Scan.empty_lexicon))
386 |> Source.source stopper (Scan.error (Scan.bulk scan)) NONE
388 in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end;