src/Pure/Syntax/syntax_phases.ML
changeset 44736 c2a3f1c84179
parent 44735 66862d02678c
child 45412 7797f5351ec4
equal deleted inserted replaced
44735:66862d02678c 44736:c2a3f1c84179
     8 signature SYNTAX_PHASES =
     8 signature SYNTAX_PHASES =
     9 sig
     9 sig
    10   val term_sorts: term -> (indexname * sort) list
    10   val term_sorts: term -> (indexname * sort) list
    11   val typ_of_term: (indexname -> sort) -> term -> typ
    11   val typ_of_term: (indexname -> sort) -> term -> typ
    12   val decode_term: Proof.context ->
    12   val decode_term: Proof.context ->
    13     Position.reports * term Exn.result -> Position.reports * term Exn.result
    13     Position.report list * term Exn.result -> Position.report list * term Exn.result
    14   val parse_ast_pattern: Proof.context -> string * string -> Ast.ast
    14   val parse_ast_pattern: Proof.context -> string * string -> Ast.ast
    15   val term_of_typ: Proof.context -> typ -> term
    15   val term_of_typ: Proof.context -> typ -> term
    16 end
    16 end
    17 
    17 
    18 structure Syntax_Phases: SYNTAX_PHASES =
    18 structure Syntax_Phases: SYNTAX_PHASES =
   124 
   124 
   125 (* parsetree_to_ast *)
   125 (* parsetree_to_ast *)
   126 
   126 
   127 fun parsetree_to_ast ctxt constrain_pos trf parsetree =
   127 fun parsetree_to_ast ctxt constrain_pos trf parsetree =
   128   let
   128   let
   129     val reports = Unsynchronized.ref ([]: Position.reports);
   129     val reports = Unsynchronized.ref ([]: Position.report list);
   130     fun report pos = Position.store_reports reports [pos];
   130     fun report pos = Position.store_reports reports [pos];
   131 
   131 
   132     fun trans a args =
   132     fun trans a args =
   133       (case trf a of
   133       (case trf a of
   134         NONE => Ast.mk_appl (Ast.Constant a) args
   134         NONE => Ast.mk_appl (Ast.Constant a) args
   194   in term_of end;
   194   in term_of end;
   195 
   195 
   196 
   196 
   197 (* decode_term -- transform parse tree into raw term *)
   197 (* decode_term -- transform parse tree into raw term *)
   198 
   198 
   199 fun decode_term _ (result as (_: Position.reports, Exn.Exn _)) = result
   199 fun decode_term _ (result as (_: Position.report list, Exn.Exn _)) = result
   200   | decode_term ctxt (reports0, Exn.Res tm) =
   200   | decode_term ctxt (reports0, Exn.Res tm) =
   201       let
   201       let
   202         fun get_const a =
   202         fun get_const a =
   203           ((true, #1 (Term.dest_Const (Proof_Context.read_const_proper ctxt false a)))
   203           ((true, #1 (Term.dest_Const (Proof_Context.read_const_proper ctxt false a)))
   204             handle ERROR _ => (false, Consts.intern (Proof_Context.consts_of ctxt) a));
   204             handle ERROR _ => (false, Consts.intern (Proof_Context.consts_of ctxt) a));
   260 fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos;
   260 fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos;
   261 
   261 
   262 fun proper_results results = map_filter (fn (y, Exn.Res x) => SOME (y, x) | _ => NONE) results;
   262 fun proper_results results = map_filter (fn (y, Exn.Res x) => SOME (y, x) | _ => NONE) results;
   263 fun failed_results results = map_filter (fn (y, Exn.Exn e) => SOME (y, e) | _ => NONE) results;
   263 fun failed_results results = map_filter (fn (y, Exn.Exn e) => SOME (y, e) | _ => NONE) results;
   264 
   264 
   265 fun report ctxt = List.app (fn (pos, m) => Context_Position.report ctxt pos m);
       
   266 
       
   267 fun report_result ctxt pos results =
   265 fun report_result ctxt pos results =
   268   (case (proper_results results, failed_results results) of
   266   (case (proper_results results, failed_results results) of
   269     ([], (reports, exn) :: _) => (report ctxt reports; reraise exn)
   267     ([], (reports, exn) :: _) => (Context_Position.reports ctxt reports; reraise exn)
   270   | ([(reports, x)], _) => (report ctxt reports; x)
   268   | ([(reports, x)], _) => (Context_Position.reports ctxt reports; x)
   271   | _ => error (ambiguity_msg pos));
   269   | _ => error (ambiguity_msg pos));
   272 
   270 
   273 
   271 
   274 (* parse raw asts *)
   272 (* parse raw asts *)
   275 
   273 
   277   let
   275   let
   278     val syn = Proof_Context.syn_of ctxt;
   276     val syn = Proof_Context.syn_of ctxt;
   279     val ast_tr = Syntax.parse_ast_translation syn;
   277     val ast_tr = Syntax.parse_ast_translation syn;
   280 
   278 
   281     val toks = Syntax.tokenize syn raw syms;
   279     val toks = Syntax.tokenize syn raw syms;
   282     val _ = List.app (Lexicon.report_token ctxt) toks;
   280     val _ = Context_Position.reports ctxt (map Lexicon.report_of_token toks);
   283 
   281 
   284     val pts = Syntax.parse ctxt syn root (filter Lexicon.is_proper toks)
   282     val pts = Syntax.parse ctxt syn root (filter Lexicon.is_proper toks)
   285       handle ERROR msg =>
   283       handle ERROR msg =>
   286         error (msg ^
   284         error (msg ^
   287           implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks));
   285           implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks));