src/Pure/Syntax/printer.ML
changeset 81157 fbe44afbd659
parent 81155 1e7b60cb7694
child 81158 06461d0d46e1
equal deleted inserted replaced
81156:cf750881f1fe 81157:fbe44afbd659
   215   let
   215   let
   216     val type_mode = Config.get ctxt pretty_type_mode;
   216     val type_mode = Config.get ctxt pretty_type_mode;
   217     val curried = Config.get ctxt pretty_curried;
   217     val curried = Config.get ctxt pretty_curried;
   218     val show_brackets = Config.get ctxt show_brackets;
   218     val show_brackets = Config.get ctxt show_brackets;
   219 
   219 
   220     (*default applications: prefix / postfix*)
   220     val application =
   221     val appT =
       
   222       if type_mode then Syntax_Trans.tappl_ast_tr'
   221       if type_mode then Syntax_Trans.tappl_ast_tr'
   223       else if curried then Syntax_Trans.applC_ast_tr'
   222       else if curried then Syntax_Trans.applC_ast_tr'
   224       else Syntax_Trans.appl_ast_tr';
   223       else Syntax_Trans.appl_ast_tr';
   225 
   224 
   226     fun synT _ ([], args) = ([], args)
   225     fun syntax _ ([], args) = ([], args)
   227       | synT m (Arg p :: symbs, t :: args) =
   226       | syntax m (Arg p :: symbs, t :: args) =
   228           let val (Ts, args') = synT m (symbs, args);
   227           let val (Ts, args') = syntax m (symbs, args);
   229           in (astT (t, p) @ Ts, args') end
   228           in (main (t, p) @ Ts, args') end
   230       | synT m (TypArg p :: symbs, t :: args) =
   229       | syntax m (TypArg p :: symbs, t :: args) =
   231           let
   230           let
   232             val (Ts, args') = synT m (symbs, args);
   231             val (Ts, args') = syntax m (symbs, args);
   233           in
   232           in
   234             if type_mode then (astT (t, p) @ Ts, args')
   233             if type_mode then (main (t, p) @ Ts, args')
   235             else
   234             else
   236               let val ctxt' = ctxt
   235               let val ctxt' = ctxt
   237                 |> Config.put pretty_type_mode true
   236                 |> Config.put pretty_type_mode true
   238                 |> Config.put pretty_priority p
   237                 |> Config.put pretty_priority p
   239               in (pretty ctxt' tabs trf markup_trans markup_extern t @ Ts, args') end
   238               in (pretty ctxt' tabs trf markup_trans markup_extern t @ Ts, args') end
   240           end
   239           end
   241       | synT m (String (literal_markup, s) :: symbs, args) =
   240       | syntax m (String (literal_markup, s) :: symbs, args) =
   242           let
   241           let
   243             val (Ts, args') = synT m (symbs, args);
   242             val (Ts, args') = syntax m (symbs, args);
   244             val T = Pretty.marks_str (if null literal_markup then [] else m @ literal_markup, s);
   243             val T = Pretty.marks_str (if null literal_markup then [] else m @ literal_markup, s);
   245           in (T :: Ts, args') end
   244           in (T :: Ts, args') end
   246       | synT m (Block (block, bsymbs) :: symbs, args) =
   245       | syntax m (Block (block, bsymbs) :: symbs, args) =
   247           let
   246           let
   248             val {markup, open_block, consistent, unbreakable, indent} = block;
   247             val {markup, open_block, consistent, unbreakable, indent} = block;
   249             val (bTs, args') = synT m (bsymbs, args);
   248             val (bTs, args') = syntax m (bsymbs, args);
   250             val (Ts, args'') = synT m (symbs, args');
   249             val (Ts, args'') = syntax m (symbs, args');
   251             val prt_block =
   250             val prt_block =
   252               {markup = markup, open_block = open_block, consistent = consistent, indent = indent};
   251               {markup = markup, open_block = open_block, consistent = consistent, indent = indent};
   253             val T = Pretty.markup_blocks prt_block bTs |> unbreakable ? Pretty.unbreakable;
   252             val T = Pretty.markup_blocks prt_block bTs |> unbreakable ? Pretty.unbreakable;
   254           in (T :: Ts, args'') end
   253           in (T :: Ts, args'') end
   255       | synT m (Break i :: symbs, args) =
   254       | syntax m (Break i :: symbs, args) =
   256           let
   255           let
   257             val (Ts, args') = synT m (symbs, args);
   256             val (Ts, args') = syntax m (symbs, args);
   258             val T = if i < 0 then Pretty.fbrk else Pretty.brk i;
   257             val T = if i < 0 then Pretty.fbrk else Pretty.brk i;
   259           in (T :: Ts, args') end
   258           in (T :: Ts, args') end
   260 
   259 
   261     and parT m (pr, args, p, p') = #1 (synT m
   260     and parens m (pr, args, p, p') = #1 (syntax m
   262           (if p > p' orelse (show_brackets andalso p' <> 1000 andalso not (is_chain pr))
   261           (if p > p' orelse (show_brackets andalso p' <> 1000 andalso not (is_chain pr))
   263            then [Block (par_block, par_bg :: pr @ [par_en])] else pr, args))
   262            then [Block (par_block, par_bg :: pr @ [par_en])] else pr, args))
   264 
   263 
   265     and atomT a = Pretty.marks_str (#1 markup_extern a, #2 markup_extern a)
   264     and split_args 0 ([x], ys) = (x, ys)
   266 
   265       | split_args 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys)
   267     and splitT 0 ([x], ys) = (x, ys)
   266       | split_args n (rev_xs, y :: ys) = split_args (n - 1) (y :: rev_xs, ys)
   268       | splitT 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys)
   267 
   269       | splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys)
   268     and translation a args p =
   270 
       
   271     and transT a args p =
       
   272       (case markup_trans a args of
   269       (case markup_trans a args of
   273         SOME prt => SOME [prt]
   270         SOME prt => SOME [prt]
   274       | NONE => Option.map astT (SOME (trf a ctxt args, p) handle Match => NONE))
   271       | NONE => Option.map main (SOME (trf a ctxt args, p) handle Match => NONE))
   275 
   272 
   276     and combT c a args p =
   273     and combination c a args p =
   277       (case transT a args p of
   274       (case translation a args p of
   278         SOME res => res
   275         SOME res => res
   279       | NONE =>
   276       | NONE =>
   280           (*find matching table entry, or print as prefix / postfix*)
   277           (*find matching table entry, or print as prefix / postfix*)
   281           let
   278           let
   282             val nargs = length args;
   279             val nargs = length args;
   285                 Symtab.lookup_list tab a |> find_first (fn (_, n, _) =>
   282                 Symtab.lookup_list tab a |> find_first (fn (_, n, _) =>
   286                   nargs = n orelse nargs > n andalso not type_mode));
   283                   nargs = n orelse nargs > n andalso not type_mode));
   287           in
   284           in
   288             (case entry of
   285             (case entry of
   289               NONE =>
   286               NONE =>
   290                 if nargs = 0 then [atomT a]
   287                 if nargs = 0 then [Pretty.marks_str (#1 markup_extern a, #2 markup_extern a)]
   291                 else astT (appT (c, args), p)
   288                 else main (application (c, args), p)
   292             | SOME (pr, n, p') =>
   289             | SOME (pr, n, p') =>
   293                 if nargs = n then parT (#1 markup_extern a) (pr, args, p, p')
   290                 if nargs = n then parens (#1 markup_extern a) (pr, args, p, p')
   294                 else astT (appT (splitT n ([c], args)), p))
   291                 else main (application (split_args n ([c], args)), p))
   295           end)
   292           end)
   296 
   293 
   297     and astT (Ast.Variable x, _) = [Ast.pretty_var x]
   294     and main (Ast.Variable x, _) = [Ast.pretty_var x]
   298       | astT (c as Ast.Constant a, p) = combT c a [] p
   295       | main (c as Ast.Constant a, p) = combination c a [] p
   299       | astT (Ast.Appl ((c as Ast.Constant a) :: (args as _ :: _)), p) = combT c a args p
   296       | main (Ast.Appl ((c as Ast.Constant a) :: (args as _ :: _)), p) = combination c a args p
   300       | astT (Ast.Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
   297       | main (Ast.Appl (f :: (args as _ :: _)), p) = main (application (f, args), p)
   301       | astT (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]);
   298       | main (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]);
   302   in astT (ast0, Config.get ctxt pretty_priority) end;
   299   in main (ast0, Config.get ctxt pretty_priority) end;
   303 
   300 
   304 end;
   301 end;
   305 
   302 
   306 
   303 
   307 (* pretty_term_ast *)
   304 (* pretty_term_ast *)