src/Pure/Syntax/parser.ML
changeset 67552 679253fef277
parent 67551 4a087b9a29c5
child 67556 5f86e2a9c59c
equal deleted inserted replaced
67551:4a087b9a29c5 67552:679253fef277
   399 fun pretty_gram (Gram {tags, prods, chains, ...}) =
   399 fun pretty_gram (Gram {tags, prods, chains, ...}) =
   400   let
   400   let
   401     val print_nt = tags_name tags;
   401     val print_nt = tags_name tags;
   402     fun print_pri p = if p < 0 then "" else Symbol.make_sup ("(" ^ string_of_int p ^ ")");
   402     fun print_pri p = if p < 0 then "" else Symbol.make_sup ("(" ^ string_of_int p ^ ")");
   403 
   403 
   404     fun pretty_symb (Terminal (Lexicon.Token (kind, s, _))) =
   404     fun pretty_symb (Terminal tok) =
   405           if kind = Lexicon.Literal then Pretty.quote (Pretty.keyword1 s) else Pretty.str s
   405           if Lexicon.is_literal tok
       
   406           then Pretty.quote (Pretty.keyword1 (Lexicon.str_of_token tok))
       
   407           else Pretty.str (Lexicon.str_of_token tok)
   406       | pretty_symb (Nonterminal (tag, p)) = Pretty.str (print_nt tag ^ print_pri p);
   408       | pretty_symb (Nonterminal (tag, p)) = Pretty.str (print_nt tag ^ print_pri p);
   407 
   409 
   408     fun pretty_const "" = []
   410     fun pretty_const "" = []
   409       | pretty_const c = [Pretty.str ("\<^bold>\<Rightarrow> " ^ quote c)];
   411       | pretty_const c = [Pretty.str ("\<^bold>\<Rightarrow> " ^ quote c)];
   410 
   412 
   444         (*Convert symbols to the form used by the parser;
   446         (*Convert symbols to the form used by the parser;
   445           delimiters and predefined terms are stored as terminals,
   447           delimiters and predefined terms are stored as terminals,
   446           nonterminals are converted to integer tags*)
   448           nonterminals are converted to integer tags*)
   447         fun symb_of [] nt_count tags result = (nt_count, tags, rev result)
   449         fun symb_of [] nt_count tags result = (nt_count, tags, rev result)
   448           | symb_of (Syntax_Ext.Delim s :: ss) nt_count tags result =
   450           | symb_of (Syntax_Ext.Delim s :: ss) nt_count tags result =
   449               symb_of ss nt_count tags
   451               symb_of ss nt_count tags (Terminal (Lexicon.literal s) :: result)
   450                 (Terminal (Lexicon.Token (Lexicon.Literal, s, Position.no_range)) :: result)
       
   451           | symb_of (Syntax_Ext.Argument (s, p) :: ss) nt_count tags result =
   452           | symb_of (Syntax_Ext.Argument (s, p) :: ss) nt_count tags result =
   452               let
   453               let
   453                 val (nt_count', tags', new_symb) =
   454                 val (nt_count', tags', new_symb) =
   454                   (case Lexicon.predef_term s of
   455                   (case Lexicon.predef_term s of
   455                     NONE =>
   456                     NONE =>
   720 
   721 
   721 fun guess_infix_lr (Gram gram) c = (*based on educated guess*)
   722 fun guess_infix_lr (Gram gram) c = (*based on educated guess*)
   722   let
   723   let
   723     fun freeze a = map_range (curry Vector.sub a) (Vector.length a);
   724     fun freeze a = map_range (curry Vector.sub a) (Vector.length a);
   724     val prods = maps (prods_content o #2) (freeze (#prods gram));
   725     val prods = maps (prods_content o #2) (freeze (#prods gram));
   725     fun guess (SOME ([Nonterminal (_, k),
   726     fun guess (SOME ([Nonterminal (_, k), Terminal tok, Nonterminal (_, l)], _, j)) =
   726             Terminal (Lexicon.Token (Lexicon.Literal, s, _)), Nonterminal (_, l)], _, j)) =
   727           if Lexicon.is_literal tok then
   727           if k = j andalso l = j + 1 then SOME (s, true, false, j)
   728             if k = j andalso l = j + 1 then SOME (Lexicon.str_of_token tok, true, false, j)
   728           else if k = j + 1 then if l = j then SOME (s, false, true, j)
   729             else if k = j + 1 then if l = j then SOME (Lexicon.str_of_token tok, false, true, j)
   729             else if l = j + 1 then SOME (s, false, false, j)
   730               else if l = j + 1 then SOME (Lexicon.str_of_token tok, false, false, j)
       
   731               else NONE
   730             else NONE
   732             else NONE
   731           else NONE
   733           else NONE
   732       | guess _ = NONE;
   734       | guess _ = NONE;
   733   in guess (find_first (fn (_, s, _) => s = c) prods) end;
   735   in guess (find_first (fn (_, s, _) => s = c) prods) end;
   734 
   736