src/Pure/Syntax/lexicon.ML
changeset 40525 14a2e686bdac
parent 40290 47f572aff50a
child 42046 6341c23baf10
equal deleted inserted replaced
40524:6131d7a78ad3 40525:14a2e686bdac
   101 
   101 
   102 open Basic_Symbol_Pos;
   102 open Basic_Symbol_Pos;
   103 
   103 
   104 fun !!! msg = Symbol_Pos.!!! ("Inner lexical error: " ^ msg);
   104 fun !!! msg = Symbol_Pos.!!! ("Inner lexical error: " ^ msg);
   105 
   105 
   106 val scan_id = Scan.one (Symbol.is_letter o symbol) ::: Scan.many (Symbol.is_letdig o symbol);
   106 val scan_id =
       
   107   Scan.one (Symbol.is_letter o Symbol_Pos.symbol) :::
       
   108   Scan.many (Symbol.is_letdig o Symbol_Pos.symbol);
       
   109 
   107 val scan_longid = scan_id @@@ (Scan.repeat1 ($$$ "." @@@ scan_id) >> flat);
   110 val scan_longid = scan_id @@@ (Scan.repeat1 ($$$ "." @@@ scan_id) >> flat);
   108 val scan_tid = $$$ "'" @@@ scan_id;
   111 val scan_tid = $$$ "'" @@@ scan_id;
   109 
   112 
   110 val scan_nat = Scan.many1 (Symbol.is_digit o symbol);
   113 val scan_nat = Scan.many1 (Symbol.is_digit o Symbol_Pos.symbol);
   111 val scan_int = $$$ "-" @@@ scan_nat || scan_nat;
   114 val scan_int = $$$ "-" @@@ scan_nat || scan_nat;
   112 val scan_natdot = scan_nat @@@ $$$ "." @@@ scan_nat;
   115 val scan_natdot = scan_nat @@@ $$$ "." @@@ scan_nat;
   113 val scan_float = $$$ "-" @@@ scan_natdot || scan_natdot;
   116 val scan_float = $$$ "-" @@@ scan_natdot || scan_natdot;
   114 val scan_hex = $$$ "0" @@@ $$$ "x" @@@ Scan.many1 (Symbol.is_ascii_hex o symbol);
   117 val scan_hex = $$$ "0" @@@ $$$ "x" @@@ Scan.many1 (Symbol.is_ascii_hex o Symbol_Pos.symbol);
   115 val scan_bin = $$$ "0" @@@ $$$ "b" @@@ Scan.many1 (fn (s, _) => s = "0" orelse s = "1");
   118 val scan_bin = $$$ "0" @@@ $$$ "b" @@@ Scan.many1 (fn (s, _) => s = "0" orelse s = "1");
   116 
   119 
   117 val scan_id_nat = scan_id @@@ Scan.optional ($$$ "." @@@ scan_nat) [];
   120 val scan_id_nat = scan_id @@@ Scan.optional ($$$ "." @@@ scan_nat) [];
   118 val scan_var = $$$ "?" @@@ scan_id_nat;
   121 val scan_var = $$$ "?" @@@ scan_id_nat;
   119 val scan_tvar = $$$ "?" @@@ $$$ "'" @@@ scan_id_nat;
   122 val scan_tvar = $$$ "?" @@@ $$$ "'" @@@ scan_id_nat;
   219 
   222 
   220 (* xstr tokens *)
   223 (* xstr tokens *)
   221 
   224 
   222 val scan_chr =
   225 val scan_chr =
   223   $$$ "\\" |-- $$$ "'" ||
   226   $$$ "\\" |-- $$$ "'" ||
   224   Scan.one ((fn s => s <> "\\" andalso s <> "'" andalso Symbol.is_regular s) o symbol) >> single ||
   227   Scan.one
       
   228     ((fn s => s <> "\\" andalso s <> "'" andalso Symbol.is_regular s) o
       
   229       Symbol_Pos.symbol) >> single ||
   225   $$$ "'" --| Scan.ahead (~$$$ "'");
   230   $$$ "'" --| Scan.ahead (~$$$ "'");
   226 
   231 
   227 val scan_str =
   232 val scan_str =
   228   $$$ "'" @@@ $$$ "'" @@@ !!! "missing end of string"
   233   $$$ "'" @@@ $$$ "'" @@@ !!! "missing end of string"
   229     ((Scan.repeat scan_chr >> flat) @@@ $$$ "'" @@@ $$$ "'");
   234     ((Scan.repeat scan_chr >> flat) @@@ $$$ "'" @@@ $$$ "'");
   235 
   240 
   236 fun implode_xstr cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs));
   241 fun implode_xstr cs = enclose "''" "''" (implode (map (fn "'" => "\\'" | c => c) cs));
   237 
   242 
   238 fun explode_xstr str =
   243 fun explode_xstr str =
   239   (case Scan.read Symbol_Pos.stopper scan_str_body (Symbol_Pos.explode (str, Position.none)) of
   244   (case Scan.read Symbol_Pos.stopper scan_str_body (Symbol_Pos.explode (str, Position.none)) of
   240     SOME cs => map symbol cs
   245     SOME cs => map Symbol_Pos.symbol cs
   241   | _ => error ("Inner lexical error: literal string expected at " ^ quote str));
   246   | _ => error ("Inner lexical error: literal string expected at " ^ quote str));
   242 
   247 
   243 
   248 
   244 
   249 
   245 (** tokenize **)
   250 (** tokenize **)
   269 
   274 
   270     val scan_token =
   275     val scan_token =
   271       Symbol_Pos.scan_comment !!! >> token Comment ||
   276       Symbol_Pos.scan_comment !!! >> token Comment ||
   272       Scan.max token_leq scan_lit scan_val ||
   277       Scan.max token_leq scan_lit scan_val ||
   273       scan_str >> token StrSy ||
   278       scan_str >> token StrSy ||
   274       Scan.many1 (Symbol.is_blank o symbol) >> token Space;
   279       Scan.many1 (Symbol.is_blank o Symbol_Pos.symbol) >> token Space;
   275   in
   280   in
   276     (case Scan.error
   281     (case Scan.error
   277         (Scan.finite Symbol_Pos.stopper (Scan.repeat scan_token)) syms of
   282         (Scan.finite Symbol_Pos.stopper (Scan.repeat scan_token)) syms of
   278       (toks, []) => toks
   283       (toks, []) => toks
   279     | (_, ss) => error ("Inner lexical error at: " ^ Symbol_Pos.content ss ^
   284     | (_, ss) => error ("Inner lexical error at: " ^ Symbol_Pos.content ss ^
   299       | chop_idx (cs as (_ :: "\\<^isup>" :: _)) ds = idxname cs ds
   304       | chop_idx (cs as (_ :: "\\<^isup>" :: _)) ds = idxname cs ds
   300       | chop_idx (c :: cs) ds =
   305       | chop_idx (c :: cs) ds =
   301           if Symbol.is_digit c then chop_idx cs (c :: ds)
   306           if Symbol.is_digit c then chop_idx cs (c :: ds)
   302           else idxname (c :: cs) ds;
   307           else idxname (c :: cs) ds;
   303 
   308 
   304     val scan = (scan_id >> map symbol) --
   309     val scan = (scan_id >> map Symbol_Pos.symbol) --
   305       Scan.optional ($$$ "." |-- scan_nat >> (nat 0 o map symbol)) ~1;
   310       Scan.optional ($$$ "." |-- scan_nat >> (nat 0 o map Symbol_Pos.symbol)) ~1;
   306   in
   311   in
   307     scan >>
   312     scan >>
   308       (fn (cs, ~1) => chop_idx (rev cs) []
   313       (fn (cs, ~1) => chop_idx (rev cs) []
   309         | (cs, i) => (implode cs, i))
   314         | (cs, i) => (implode cs, i))
   310   end;
   315   end;
   332 
   337 
   333 fun read_var str =
   338 fun read_var str =
   334   let
   339   let
   335     val scan =
   340     val scan =
   336       $$$ "?" |-- scan_indexname --| Scan.ahead (Scan.one Symbol_Pos.is_eof) >> var ||
   341       $$$ "?" |-- scan_indexname --| Scan.ahead (Scan.one Symbol_Pos.is_eof) >> var ||
   337       Scan.many (Symbol.is_regular o symbol) >> (free o implode o map symbol);
   342       Scan.many (Symbol.is_regular o Symbol_Pos.symbol) >> (free o implode o map Symbol_Pos.symbol);
   338   in the (Scan.read Symbol_Pos.stopper scan (Symbol_Pos.explode (str, Position.none))) end;
   343   in the (Scan.read Symbol_Pos.stopper scan (Symbol_Pos.explode (str, Position.none))) end;
   339 
   344 
   340 
   345 
   341 (* read_variable *)
   346 (* read_variable *)
   342 
   347 
   376 (* read numbers *)
   381 (* read numbers *)
   377 
   382 
   378 local
   383 local
   379 
   384 
   380 fun nat cs =
   385 fun nat cs =
   381   Option.map (#1 o Library.read_int o map symbol)
   386   Option.map (#1 o Library.read_int o map Symbol_Pos.symbol)
   382     (Scan.read Symbol_Pos.stopper scan_nat cs);
   387     (Scan.read Symbol_Pos.stopper scan_nat cs);
   383 
   388 
   384 in
   389 in
   385 
   390 
   386 fun read_nat s = nat (Symbol_Pos.explode (s, Position.none));
   391 fun read_nat s = nat (Symbol_Pos.explode (s, Position.none));