400 |
400 |
401 (* pretty_gram *) |
401 (* pretty_gram *) |
402 |
402 |
403 fun pretty_gram (Gram {tags, prods, chains, ...}) = |
403 fun pretty_gram (Gram {tags, prods, chains, ...}) = |
404 let |
404 let |
405 fun pretty_name name = [Pretty.str (name ^ " =")]; |
405 val print_nt = the o Inttab.lookup (Inttab.make (map swap (Symtab.dest tags))); |
406 |
406 fun print_pri p = if p < 0 then "" else Symbol.make_sup ("(" ^ signed_string_of_int p ^ ")"); |
407 val nt_name = the o Inttab.lookup (Inttab.make (map swap (Symtab.dest tags))); |
407 |
408 |
408 fun pretty_symb (Terminal (Lexicon.Token (kind, s, _))) = |
409 fun pretty_symb (Terminal (Lexicon.Token (Lexicon.Literal, s, _))) = Pretty.quote (Pretty.str s) |
409 if kind = Lexicon.Literal then Pretty.quote (Pretty.keyword1 s) else Pretty.str s |
410 | pretty_symb (Terminal tok) = Pretty.str (Lexicon.str_of_token tok) |
410 | pretty_symb (Nonterminal (tag, p)) = Pretty.str (print_nt tag ^ print_pri p); |
411 | pretty_symb (Nonterminal (tag, p)) = |
|
412 Pretty.str (nt_name tag ^ "[" ^ signed_string_of_int p ^ "]"); |
|
413 |
411 |
414 fun pretty_const "" = [] |
412 fun pretty_const "" = [] |
415 | pretty_const c = [Pretty.str ("=> " ^ quote c)]; |
413 | pretty_const c = [Pretty.str ("\<^bold>\<Rightarrow> " ^ quote c)]; |
416 |
414 |
417 fun pretty_pri p = [Pretty.str ("(" ^ signed_string_of_int p ^ ")")]; |
415 fun prod_of_chain from = ([Nonterminal (from, ~1)], "", ~1); |
418 |
416 |
419 fun pretty_prod name (symbs, const, pri) = |
417 fun pretty_prod (name, tag) = |
420 Pretty.block (Pretty.breaks (pretty_name name @ |
418 (fold (union (op =) o #2) (#2 (Vector.sub (prods, tag))) [] @ |
421 map pretty_symb symbs @ pretty_const const @ pretty_pri pri)); |
419 map prod_of_chain (these (AList.lookup (op =) chains tag))) |
422 |
420 |> map (fn (symbs, const, p) => |
423 fun pretty_nt (name, tag) = |
421 Pretty.block (Pretty.breaks |
424 let |
422 (Pretty.str (name ^ print_pri p ^ " =") :: map pretty_symb symbs @ pretty_const const))); |
425 fun prod_of_chain from = ([Nonterminal (from, ~1)], "", ~1); |
423 in maps pretty_prod (sort_by #1 (Symtab.dest tags)) end; |
426 |
|
427 val nt_prods = |
|
428 fold (union (op =) o snd) (snd (Vector.sub (prods, tag))) [] @ |
|
429 map prod_of_chain (these (AList.lookup (op =) chains tag)); |
|
430 in map (pretty_prod name) nt_prods end; |
|
431 |
|
432 in maps pretty_nt (sort_by fst (Symtab.dest tags)) end; |
|
433 |
424 |
434 |
425 |
435 |
426 |
436 (** Operations on gramars **) |
427 (** Operations on gramars **) |
437 |
428 |