author | wenzelm |
Thu, 09 Aug 2012 22:31:04 +0200 | |
changeset 48749 | c197b3c3e7fa |
parent 48743 | a72f8ffecf31 |
child 48764 | 4fe0920d5049 |
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:
32738
diff
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:
32738
diff
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:
32738
diff
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:
32738
diff
changeset
|
9 |
datatype kind = |
27814 | 10 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | |
40290
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents:
38229
diff
changeset
|
11 |
Nat | Float | String | AltString | Verbatim | Space | Comment | InternalValue | |
40958
755f8fe7ced9
eliminated obsolete Token.Malformed -- subsumed by Token.Error;
wenzelm
parents:
40627
diff
changeset
|
12 |
Error of string | Sync | EOF |
27814 | 13 |
datatype value = |
14 |
Text of string | Typ of typ | Term of term | Fact of thm list | |
|
15 |
Attribute of morphism -> attribute |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
16 |
type T |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
17 |
val str_of_kind: kind -> string |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
18 |
val position_of: T -> Position.T |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
19 |
val end_position_of: T -> Position.T |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
20 |
val pos_of: T -> string |
44658
5bec9c15ef29
more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents:
43947
diff
changeset
|
21 |
val range: 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:
32738
diff
changeset
|
22 |
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
|
23 |
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
|
24 |
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
|
25 |
val not_sync: 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
|
26 |
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
|
27 |
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
|
28 |
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
|
29 |
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
|
30 |
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
|
31 |
val is_command: 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
|
32 |
val is_proper: 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
|
33 |
val is_semicolon: 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
|
34 |
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
|
35 |
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
|
36 |
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
|
37 |
val is_error: 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
|
38 |
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
|
39 |
val is_newline: 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
|
40 |
val source_of: T -> string |
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 source_position_of: T -> Symbol_Pos.text * Position.T |
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 content_of: T -> string |
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 unparse: T -> string |
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 text_of: T -> string * string |
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 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
|
46 |
val map_value: (value -> value) -> T -> T |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
47 |
val mk_text: string -> T |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
48 |
val mk_typ: typ -> T |
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 mk_term: term -> T |
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
50 |
val mk_fact: thm list -> T |
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 mk_attribute: (morphism -> attribute) -> T |
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 assignable: T -> T |
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 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
|
54 |
val closure: T -> T |
27814 | 55 |
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
|
56 |
val source_proper: (T, 'a) Source.source -> (T, (T, 'a) Source.source) Source.source |
48741 | 57 |
val source': {do_recover: bool option} -> (unit -> Scan.lexicon * Scan.lexicon) -> |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
58 |
(Symbol_Pos.T, 'a) Source.source -> (T, (Symbol_Pos.T, 'a) Source.source) Source.source |
48741 | 59 |
val source: {do_recover: bool option} -> (unit -> Scan.lexicon * Scan.lexicon) -> |
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
60 |
Position.T -> (Symbol.symbol, 'a) Source.source -> (T, |
30573 | 61 |
(Symbol_Pos.T, Position.T * (Symbol.symbol, 'a) Source.source) 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:
32738
diff
changeset
|
62 |
val read_antiq: Scan.lexicon -> (T list -> 'a * T list) -> Symbol_Pos.T list * Position.T -> 'a |
5825 | 63 |
end; |
64 |
||
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 |
structure Token: TOKEN = |
5825 | 66 |
struct |
67 |
||
68 |
(** tokens **) |
|
69 |
||
27814 | 70 |
(* token values *) |
71 |
||
72 |
(*The value slot assigns an (optional) internal value to a token, |
|
73 |
usually as a side-effect of special scanner setup (see also |
|
74 |
args.ML). Note that an assignable ref designates an intermediate |
|
75 |
state of internalization -- it is NOT meant to persist.*) |
|
76 |
||
77 |
datatype value = |
|
78 |
Text of string | |
|
79 |
Typ of typ | |
|
80 |
Term of term | |
|
81 |
Fact of thm list | |
|
82 |
Attribute of morphism -> attribute; |
|
83 |
||
84 |
datatype slot = |
|
85 |
Slot | |
|
86 |
Value of value option | |
|
32738 | 87 |
Assignable of value option Unsynchronized.ref; |
27814 | 88 |
|
89 |
||
5825 | 90 |
(* datatype token *) |
91 |
||
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
92 |
datatype kind = |
27814 | 93 |
Command | Keyword | Ident | LongIdent | SymIdent | Var | TypeIdent | TypeVar | |
40290
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents:
38229
diff
changeset
|
94 |
Nat | Float | String | AltString | Verbatim | Space | Comment | InternalValue | |
40958
755f8fe7ced9
eliminated obsolete Token.Malformed -- subsumed by Token.Error;
wenzelm
parents:
40627
diff
changeset
|
95 |
Error of string | Sync | EOF; |
5825 | 96 |
|
36959
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents:
32738
diff
changeset
|
97 |
datatype T = Token of (Symbol_Pos.text * Position.range) * (kind * string) * slot; |
5825 | 98 |
|
99 |
val str_of_kind = |
|
7026 | 100 |
fn Command => "command" |
101 |
| Keyword => "keyword" |
|
5825 | 102 |
| Ident => "identifier" |
103 |
| LongIdent => "long identifier" |
|
104 |
| SymIdent => "symbolic identifier" |
|
105 |
| Var => "schematic variable" |
|
106 |
| TypeIdent => "type variable" |
|
107 |
| TypeVar => "schematic type variable" |
|
40290
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents:
38229
diff
changeset
|
108 |
| Nat => "natural number" |
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents:
38229
diff
changeset
|
109 |
| Float => "floating-point number" |
5825 | 110 |
| String => "string" |
17164
a786e1a1ce02
added AltString token (delimited by ASCII back-quotes);
wenzelm
parents:
17069
diff
changeset
|
111 |
| AltString => "back-quoted string" |
5825 | 112 |
| Verbatim => "verbatim text" |
7682
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
113 |
| Space => "white space" |
46de8064c93c
added Space, Comment token kinds (keep actual text);
wenzelm
parents:
7477
diff
changeset
|
114 |
| Comment => "comment text" |
27814 | 115 |
| InternalValue => "internal value" |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
116 |
| Error _ => "bad input" |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
117 |
| Sync => "sync marker" |
5825 | 118 |
| EOF => "end-of-file"; |
119 |
||
120 |
||
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
121 |
(* position *) |
5825 | 122 |
|
27814 | 123 |
fun position_of (Token ((_, (pos, _)), _, _)) = pos; |
124 |
fun end_position_of (Token ((_, (_, pos)), _, _)) = pos; |
|
27663 | 125 |
|
5825 | 126 |
val pos_of = Position.str_of o position_of; |
127 |
||
44658
5bec9c15ef29
more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents:
43947
diff
changeset
|
128 |
fun range [] = (Position.none, Position.none) |
5bec9c15ef29
more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents:
43947
diff
changeset
|
129 |
| range toks = |
5bec9c15ef29
more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents:
43947
diff
changeset
|
130 |
let |
5bec9c15ef29
more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents:
43947
diff
changeset
|
131 |
val start_pos = position_of (hd toks); |
5bec9c15ef29
more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents:
43947
diff
changeset
|
132 |
val end_pos = end_position_of (List.last toks); |
5bec9c15ef29
more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents:
43947
diff
changeset
|
133 |
in (start_pos, end_pos) end; |
5bec9c15ef29
more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents:
43947
diff
changeset
|
134 |
|
5825 | 135 |
|
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
136 |
(* control tokens *) |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
137 |
|
27814 | 138 |
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
|
139 |
val eof = mk_eof Position.none; |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
140 |
|
27814 | 141 |
fun is_eof (Token (_, (EOF, _), _)) = true |
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
142 |
| is_eof _ = false; |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
143 |
|
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
144 |
val not_eof = not o is_eof; |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
145 |
|
27814 | 146 |
fun not_sync (Token (_, (Sync, _), _)) = false |
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
147 |
| not_sync _ = true; |
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
148 |
|
27752
ea7d573e565f
removed obsolete range_of (already included in position);
wenzelm
parents:
27747
diff
changeset
|
149 |
val stopper = |
ea7d573e565f
removed obsolete range_of (already included in position);
wenzelm
parents:
27747
diff
changeset
|
150 |
Scan.stopper (fn [] => eof | toks => mk_eof (end_position_of (List.last toks))) is_eof; |
27733
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
151 |
|
d3d7038fb7b5
abstract type Scan.stopper, position taken from last input token;
wenzelm
parents:
27663
diff
changeset
|
152 |
|
5825 | 153 |
(* kind of token *) |
154 |
||
27814 | 155 |
fun kind_of (Token (_, (k, _), _)) = k; |
156 |
fun is_kind k (Token (_, (k', _), _)) = k = k'; |
|
5825 | 157 |
|
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
|
158 |
val is_command = is_kind Command; |
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
|
159 |
|
27814 | 160 |
fun keyword_with pred (Token (_, (Keyword, x), _)) = pred x |
7026 | 161 |
| keyword_with _ _ = false; |
5825 | 162 |
|
27814 | 163 |
fun ident_with pred (Token (_, (Ident, x), _)) = pred x |
16029 | 164 |
| ident_with _ _ = false; |
165 |
||
27814 | 166 |
fun is_proper (Token (_, (Space, _), _)) = false |
167 |
| is_proper (Token (_, (Comment, _), _)) = false |
|
5825 | 168 |
| is_proper _ = true; |
169 |
||
27814 | 170 |
fun is_semicolon (Token (_, (Keyword, ";"), _)) = true |
9130 | 171 |
| is_semicolon _ = false; |
172 |
||
27814 | 173 |
fun is_comment (Token (_, (Comment, _), _)) = true |
17069 | 174 |
| is_comment _ = false; |
175 |
||
27814 | 176 |
fun is_begin_ignore (Token (_, (Comment, "<"), _)) = true |
8580 | 177 |
| is_begin_ignore _ = false; |
178 |
||
27814 | 179 |
fun is_end_ignore (Token (_, (Comment, ">"), _)) = true |
8580 | 180 |
| is_end_ignore _ = false; |
181 |
||
48749
c197b3c3e7fa
some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents:
48743
diff
changeset
|
182 |
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
|
183 |
| 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
|
184 |
|
8651 | 185 |
|
17069 | 186 |
(* blanks and newlines -- space tokens obey lines *) |
8651 | 187 |
|
27814 | 188 |
fun is_blank (Token (_, (Space, x), _)) = not (String.isSuffix "\n" x) |
17069 | 189 |
| is_blank _ = false; |
190 |
||
27814 | 191 |
fun is_newline (Token (_, (Space, x), _)) = String.isSuffix "\n" x |
8651 | 192 |
| is_newline _ = false; |
193 |
||
5825 | 194 |
|
14991 | 195 |
(* token content *) |
9155 | 196 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43709
diff
changeset
|
197 |
fun source_of (Token ((source, (pos, _)), (_, x), _)) = |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43709
diff
changeset
|
198 |
if YXML.detect x then x |
45666 | 199 |
else |
200 |
YXML.string_of |
|
201 |
(XML.Elem (Isabelle_Markup.token (Position.properties_of pos), [XML.Text source])); |
|
25642
ebdff0dca2a5
text_of: made even more robust against recurrent errors;
wenzelm
parents:
25582
diff
changeset
|
202 |
|
27885 | 203 |
fun source_position_of (Token ((source, (pos, _)), _, _)) = (source, pos); |
27873 | 204 |
|
27814 | 205 |
fun content_of (Token (_, (_, x), _)) = x; |
27747
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
206 |
|
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
207 |
|
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
208 |
(* unparse *) |
d41abb7bc08a
token: maintain of source, which retains original position information;
wenzelm
parents:
27733
diff
changeset
|
209 |
|
27814 | 210 |
fun unparse (Token (_, (kind, x), _)) = |
14991 | 211 |
(case kind of |
43773 | 212 |
String => Symbol_Pos.quote_string_qq x |
213 |
| AltString => Symbol_Pos.quote_string_bq x |
|
214 |
| Verbatim => enclose "{*" "*}" x |
|
215 |
| Comment => enclose "(*" "*)" x |
|
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
216 |
| Sync => "" |
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
217 |
| EOF => "" |
14991 | 218 |
| _ => x); |
219 |
||
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
220 |
fun text_of tok = |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
221 |
if is_semicolon tok then ("terminator", "") |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
222 |
else |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
223 |
let |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
224 |
val k = str_of_kind (kind_of tok); |
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40290
diff
changeset
|
225 |
val s = unparse tok; |
23788
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
226 |
in |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
227 |
if s = "" then (k, "") |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
228 |
else if size s < 40 andalso not (exists_string (fn c => c = "\n") s) then (k ^ " " ^ s, "") |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
229 |
else (k, s) |
54ce229dc858
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
wenzelm
parents:
23729
diff
changeset
|
230 |
end; |
23729
d1ba656978c5
separated Malformed (symbolic char) from Error (bad input);
wenzelm
parents:
23721
diff
changeset
|
231 |
|
5825 | 232 |
|
233 |
||
27814 | 234 |
(** associated values **) |
235 |
||
236 |
(* access values *) |
|
237 |
||
238 |
fun get_value (Token (_, _, Value v)) = v |
|
239 |
| get_value _ = NONE; |
|
240 |
||
241 |
fun map_value f (Token (x, y, Value (SOME v))) = Token (x, y, Value (SOME (f v))) |
|
242 |
| map_value _ tok = tok; |
|
243 |
||
244 |
||
245 |
(* make values *) |
|
246 |
||
247 |
fun mk_value k v = Token ((k, Position.no_range), (InternalValue, k), Value (SOME v)); |
|
248 |
||
249 |
val mk_text = mk_value "<text>" o Text; |
|
250 |
val mk_typ = mk_value "<typ>" o Typ; |
|
251 |
val mk_term = mk_value "<term>" o Term; |
|
252 |
val mk_fact = mk_value "<fact>" o Fact; |
|
253 |
val mk_attribute = mk_value "<attribute>" o Attribute; |
|
254 |
||
255 |
||
256 |
(* static binding *) |
|
257 |
||
258 |
(*1st stage: make empty slots assignable*) |
|
32738 | 259 |
fun assignable (Token (x, y, Slot)) = Token (x, y, Assignable (Unsynchronized.ref NONE)) |
27814 | 260 |
| assignable tok = tok; |
261 |
||
262 |
(*2nd stage: assign values as side-effect of scanning*) |
|
263 |
fun assign v (Token (_, _, Assignable r)) = r := v |
|
264 |
| assign _ _ = (); |
|
265 |
||
266 |
(*3rd stage: static closure of final values*) |
|
32738 | 267 |
fun closure (Token (x, y, Assignable (Unsynchronized.ref v))) = Token (x, y, Value v) |
27814 | 268 |
| closure tok = tok; |
269 |
||
270 |
||
271 |
||
5825 | 272 |
(** scanners **) |
273 |
||
30573 | 274 |
open Basic_Symbol_Pos; |
5825 | 275 |
|
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43773
diff
changeset
|
276 |
fun !!! msg = Symbol_Pos.!!! (fn () => "Outer lexical error: " ^ msg); |
5825 | 277 |
|
278 |
||
279 |
(* scan symbolic idents *) |
|
280 |
||
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40531
diff
changeset
|
281 |
val is_sym_char = member (op =) (raw_explode "!#$%&*+-/<=>?@^_|~"); |
5825 | 282 |
|
8231 | 283 |
val scan_symid = |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
284 |
Scan.many1 (is_sym_char o Symbol_Pos.symbol) || |
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
285 |
Scan.one (Symbol.is_symbolic o Symbol_Pos.symbol) >> single; |
5825 | 286 |
|
8231 | 287 |
fun is_symid str = |
288 |
(case try Symbol.explode str of |
|
15531 | 289 |
SOME [s] => Symbol.is_symbolic s orelse is_sym_char s |
290 |
| SOME ss => forall is_sym_char ss |
|
8231 | 291 |
| _ => false); |
292 |
||
27814 | 293 |
fun ident_or_symbolic "begin" = false |
294 |
| ident_or_symbolic ":" = true |
|
295 |
| ident_or_symbolic "::" = true |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40958
diff
changeset
|
296 |
| ident_or_symbolic s = Lexicon.is_identifier s orelse is_symid s; |
5825 | 297 |
|
298 |
||
299 |
(* scan verbatim text *) |
|
300 |
||
301 |
val scan_verb = |
|
27769 | 302 |
$$$ "*" --| Scan.ahead (~$$$ "}") || |
303 |
Scan.one (fn (s, _) => s <> "*" andalso Symbol.is_regular s) >> single; |
|
5825 | 304 |
|
305 |
val scan_verbatim = |
|
30573 | 306 |
(Symbol_Pos.scan_pos --| $$$ "{" --| $$$ "*") -- !!! "missing end of verbatim text" |
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
307 |
(Symbol_Pos.change_prompt |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
308 |
((Scan.repeat scan_verb >> flat) -- ($$$ "*" |-- $$$ "}" |-- Symbol_Pos.scan_pos))); |
5825 | 309 |
|
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
48741
diff
changeset
|
310 |
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
|
311 |
$$$ "{" @@@ $$$ "*" @@@ (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
|
312 |
|
5825 | 313 |
|
314 |
(* scan space *) |
|
315 |
||
19305 | 316 |
fun is_space s = Symbol.is_blank s andalso s <> "\n"; |
5825 | 317 |
|
318 |
val scan_space = |
|
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
319 |
Scan.many1 (is_space o Symbol_Pos.symbol) @@@ Scan.optional ($$$ "\n") [] || |
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
320 |
Scan.many (is_space o Symbol_Pos.symbol) @@@ $$$ "\n"; |
5825 | 321 |
|
322 |
||
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
323 |
(* scan comment *) |
5825 | 324 |
|
325 |
val scan_comment = |
|
30573 | 326 |
Symbol_Pos.scan_pos -- (Symbol_Pos.scan_comment_body !!! -- Symbol_Pos.scan_pos); |
5825 | 327 |
|
328 |
||
27663 | 329 |
|
27769 | 330 |
(** token sources **) |
5825 | 331 |
|
27769 | 332 |
fun source_proper src = src |> Source.filter is_proper; |
5825 | 333 |
|
23678 | 334 |
local |
335 |
||
27769 | 336 |
fun token_leq ((_, syms1), (_, syms2)) = length syms1 <= length syms2; |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
337 |
|
27799 | 338 |
fun token k ss = |
43709
717e96cf9527
discontinued special treatment of hard tabulators;
wenzelm
parents:
42503
diff
changeset
|
339 |
Token ((Symbol_Pos.implode ss, Symbol_Pos.range ss), (k, Symbol_Pos.content ss), Slot); |
27799 | 340 |
|
341 |
fun token_range k (pos1, (ss, pos2)) = |
|
43709
717e96cf9527
discontinued special treatment of hard tabulators;
wenzelm
parents:
42503
diff
changeset
|
342 |
Token (Symbol_Pos.implode_range pos1 pos2 ss, (k, Symbol_Pos.content ss), Slot); |
23678 | 343 |
|
27769 | 344 |
fun scan (lex1, lex2) = !!! "bad input" |
42503 | 345 |
(Symbol_Pos.scan_string_qq >> token_range String || |
346 |
Symbol_Pos.scan_string_bq >> token_range AltString || |
|
27799 | 347 |
scan_verbatim >> token_range Verbatim || |
348 |
scan_comment >> token_range Comment || |
|
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
349 |
scan_space >> token Space || |
40525
14a2e686bdac
eliminated slightly odd pervasive Symbol_Pos.symbol;
wenzelm
parents:
40523
diff
changeset
|
350 |
Scan.one (Symbol.is_sync o Symbol_Pos.symbol) >> (token Sync o single) || |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
351 |
(Scan.max token_leq |
27769 | 352 |
(Scan.max token_leq |
353 |
(Scan.literal lex2 >> pair Command) |
|
354 |
(Scan.literal lex1 >> pair Keyword)) |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40958
diff
changeset
|
355 |
(Lexicon.scan_longid >> pair LongIdent || |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40958
diff
changeset
|
356 |
Lexicon.scan_id >> pair Ident || |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40958
diff
changeset
|
357 |
Lexicon.scan_var >> pair Var || |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40958
diff
changeset
|
358 |
Lexicon.scan_tid >> pair TypeIdent || |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40958
diff
changeset
|
359 |
Lexicon.scan_tvar >> pair TypeVar || |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40958
diff
changeset
|
360 |
Lexicon.scan_float >> pair Float || |
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
40958
diff
changeset
|
361 |
Lexicon.scan_nat >> pair Nat || |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
362 |
scan_symid >> pair SymIdent) >> uncurry token)); |
27769 | 363 |
|
364 |
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
|
365 |
(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
|
366 |
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
|
367 |
recover_verbatim || |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
48741
diff
changeset
|
368 |
Symbol_Pos.recover_comment || |
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
48741
diff
changeset
|
369 |
Scan.one (Symbol.is_regular o Symbol_Pos.symbol) >> single) |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
370 |
>> (single o token (Error msg)); |
23678 | 371 |
|
372 |
in |
|
5825 | 373 |
|
27835
ff8b8513965a
Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents:
27814
diff
changeset
|
374 |
fun source' {do_recover} get_lex = |
30573 | 375 |
Source.source Symbol_Pos.stopper (Scan.bulk (fn xs => scan (get_lex ()) xs)) |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
376 |
(Option.map (rpair recover) do_recover); |
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
377 |
|
5825 | 378 |
fun source do_recover get_lex pos src = |
30573 | 379 |
Symbol_Pos.source pos src |
27780
7d0910f662f7
more precise positions due to SymbolsPos.implode_delim;
wenzelm
parents:
27769
diff
changeset
|
380 |
|> source' do_recover get_lex; |
23678 | 381 |
|
382 |
end; |
|
5825 | 383 |
|
30586
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
384 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
385 |
(* read_antiq *) |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
386 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
387 |
fun read_antiq lex scan (syms, pos) = |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
388 |
let |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
389 |
fun err msg = cat_error msg ("Malformed antiquotation" ^ Position.str_of pos ^ ":\n" ^ |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
390 |
"@{" ^ Symbol_Pos.content syms ^ "}"); |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
391 |
|
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
392 |
val res = |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
393 |
Source.of_list syms |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
394 |
|> source' {do_recover = NONE} (K (lex, Scan.empty_lexicon)) |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
395 |
|> source_proper |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
396 |
|> Source.source stopper (Scan.error (Scan.bulk scan)) NONE |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
397 |
|> Source.exhaust; |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
398 |
in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end; |
9674f64a0702
moved basic change_prompt, scan_string, scan_alt_string, scan_quoted to symbol_pos.ML;
wenzelm
parents:
30573
diff
changeset
|
399 |
|
5825 | 400 |
end; |