src/Pure/Isar/antiquote.ML
changeset 30573 49899f26fbd1
parent 29606 fedb8be05f24
equal deleted inserted replaced
30572:8fbc355100f2 30573:49899f26fbd1
     5 *)
     5 *)
     6 
     6 
     7 signature ANTIQUOTE =
     7 signature ANTIQUOTE =
     8 sig
     8 sig
     9   datatype antiquote =
     9   datatype antiquote =
    10     Text of string | Antiq of SymbolPos.T list * Position.T |
    10     Text of string | Antiq of Symbol_Pos.T list * Position.T |
    11     Open of Position.T | Close of Position.T
    11     Open of Position.T | Close of Position.T
    12   val is_antiq: antiquote -> bool
    12   val is_antiq: antiquote -> bool
    13   val read: SymbolPos.T list * Position.T -> antiquote list
    13   val read: Symbol_Pos.T list * Position.T -> antiquote list
    14   val read_antiq: Scan.lexicon -> (OuterLex.token list -> 'a * OuterLex.token list) ->
    14   val read_antiq: Scan.lexicon -> (OuterLex.token list -> 'a * OuterLex.token list) ->
    15     SymbolPos.T list * Position.T -> 'a
    15     Symbol_Pos.T list * Position.T -> 'a
    16 end;
    16 end;
    17 
    17 
    18 structure Antiquote: ANTIQUOTE =
    18 structure Antiquote: ANTIQUOTE =
    19 struct
    19 struct
    20 
    20 
    21 (* datatype antiquote *)
    21 (* datatype antiquote *)
    22 
    22 
    23 datatype antiquote =
    23 datatype antiquote =
    24   Text of string |
    24   Text of string |
    25   Antiq of SymbolPos.T list * Position.T |
    25   Antiq of Symbol_Pos.T list * Position.T |
    26   Open of Position.T |
    26   Open of Position.T |
    27   Close of Position.T;
    27   Close of Position.T;
    28 
    28 
    29 fun is_antiq (Text _) = false
    29 fun is_antiq (Text _) = false
    30   | is_antiq _ = true;
    30   | is_antiq _ = true;
    46   in check antiqs [] end;
    46   in check antiqs [] end;
    47 
    47 
    48 
    48 
    49 (* scan_antiquote *)
    49 (* scan_antiquote *)
    50 
    50 
    51 open BasicSymbolPos;
    51 open Basic_Symbol_Pos;
    52 structure T = OuterLex;
    52 structure T = OuterLex;
    53 
    53 
    54 local
    54 local
    55 
    55 
    56 val scan_txt =
    56 val scan_txt =
    61 val scan_ant =
    61 val scan_ant =
    62   T.scan_quoted ||
    62   T.scan_quoted ||
    63   Scan.one (fn (s, _) => s <> "}" andalso Symbol.is_regular s) >> single;
    63   Scan.one (fn (s, _) => s <> "}" andalso Symbol.is_regular s) >> single;
    64 
    64 
    65 val scan_antiq =
    65 val scan_antiq =
    66   SymbolPos.scan_pos -- ($$$ "@" |-- $$$ "{" |--
    66   Symbol_Pos.scan_pos -- ($$$ "@" |-- $$$ "{" |--
    67     T.!!! "missing closing brace of antiquotation"
    67     T.!!! "missing closing brace of antiquotation"
    68       (Scan.repeat scan_ant -- ($$$ "}" |-- SymbolPos.scan_pos)))
    68       (Scan.repeat scan_ant -- ($$$ "}" |-- Symbol_Pos.scan_pos)))
    69   >> (fn (pos1, (body, pos2)) => Antiq (flat body, Position.encode_range (pos1, pos2)));
    69   >> (fn (pos1, (body, pos2)) => Antiq (flat body, Position.encode_range (pos1, pos2)));
    70 
    70 
    71 in
    71 in
    72 
    72 
    73 val scan_antiquote =
    73 val scan_antiquote =
    74  (Scan.repeat1 scan_txt >> (Text o SymbolPos.content o flat) ||
    74  (Scan.repeat1 scan_txt >> (Text o Symbol_Pos.content o flat) ||
    75   scan_antiq ||
    75   scan_antiq ||
    76   SymbolPos.scan_pos --| $$$ "\\<lbrace>" >> Open ||
    76   Symbol_Pos.scan_pos --| $$$ "\\<lbrace>" >> Open ||
    77   SymbolPos.scan_pos --| $$$ "\\<rbrace>" >> Close);
    77   Symbol_Pos.scan_pos --| $$$ "\\<rbrace>" >> Close);
    78 
    78 
    79 end;
    79 end;
    80 
    80 
    81 
    81 
    82 (* read *)
    82 (* read *)
    84 fun report_antiq (Antiq (_, pos)) = Position.report Markup.antiq pos
    84 fun report_antiq (Antiq (_, pos)) = Position.report Markup.antiq pos
    85   | report_antiq _ = ();
    85   | report_antiq _ = ();
    86 
    86 
    87 fun read ([], _) = []
    87 fun read ([], _) = []
    88   | read (syms, pos) =
    88   | read (syms, pos) =
    89       (case Scan.read SymbolPos.stopper (Scan.repeat scan_antiquote) syms of
    89       (case Scan.read Symbol_Pos.stopper (Scan.repeat scan_antiquote) syms of
    90         SOME xs => (List.app report_antiq xs; check_nesting xs; xs)
    90         SOME xs => (List.app report_antiq xs; check_nesting xs; xs)
    91       | NONE => error ("Malformed quotation/antiquotation source" ^ Position.str_of pos));
    91       | NONE => error ("Malformed quotation/antiquotation source" ^ Position.str_of pos));
    92 
    92 
    93 
    93 
    94 (* read_antiq *)
    94 (* read_antiq *)
    95 
    95 
    96 fun read_antiq lex scan (syms, pos) =
    96 fun read_antiq lex scan (syms, pos) =
    97   let
    97   let
    98     fun err msg = cat_error msg ("Malformed antiquotation" ^ Position.str_of pos ^ ":\n" ^
    98     fun err msg = cat_error msg ("Malformed antiquotation" ^ Position.str_of pos ^ ":\n" ^
    99       "@{" ^ SymbolPos.content syms ^ "}");
    99       "@{" ^ Symbol_Pos.content syms ^ "}");
   100 
   100 
   101     val res =
   101     val res =
   102       Source.of_list syms
   102       Source.of_list syms
   103       |> T.source' {do_recover = NONE} (K (lex, Scan.empty_lexicon))
   103       |> T.source' {do_recover = NONE} (K (lex, Scan.empty_lexicon))
   104       |> T.source_proper
   104       |> T.source_proper