618 |
618 |
619 val basic_nonterms = |
619 val basic_nonterms = |
620 (Lexicon.terminals @ [Syn_Ext.logic, "type", "types", "sort", "classes", |
620 (Lexicon.terminals @ [Syn_Ext.logic, "type", "types", "sort", "classes", |
621 Syn_Ext.args, Syn_Ext.cargs, "pttrn", "pttrns", "idt", "idts", "aprop", |
621 Syn_Ext.args, Syn_Ext.cargs, "pttrn", "pttrns", "idt", "idts", "aprop", |
622 "asms", Syn_Ext.any, Syn_Ext.sprop, "num_const", "float_const", |
622 "asms", Syn_Ext.any, Syn_Ext.sprop, "num_const", "float_const", |
623 "index", "struct"]); |
623 "index", "struct", "id_position", "longid_position"]); |
624 |
624 |
625 |
625 |
626 |
626 |
627 (** print syntax **) |
627 (** print syntax **) |
628 |
628 |
706 val ambiguity_limit = |
706 val ambiguity_limit = |
707 Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10)); |
707 Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10)); |
708 |
708 |
709 fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos; |
709 fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos; |
710 |
710 |
711 fun read_asts ctxt (Syntax (tabs, _)) xids root (syms, pos) = |
711 fun read_asts ctxt (Syntax (tabs, _)) raw root (syms, pos) = |
712 let |
712 let |
713 val {lexicon, gram, parse_ast_trtab, ...} = tabs; |
713 val {lexicon, gram, parse_ast_trtab, ...} = tabs; |
714 val toks = Lexicon.tokenize lexicon xids syms; |
714 val toks = Lexicon.tokenize lexicon raw syms; |
715 val _ = List.app (Lexicon.report_token ctxt) toks; |
715 val _ = List.app (Lexicon.report_token ctxt) toks; |
716 |
716 |
717 val pts = Parser.parse ctxt gram root (filter Lexicon.is_proper toks) |
717 val pts = Parser.parse ctxt gram root (filter Lexicon.is_proper toks) |
718 handle ERROR msg => |
718 handle ERROR msg => |
719 error (msg ^ |
719 error (msg ^ |
720 implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks)); |
720 implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks)); |
721 val len = length pts; |
721 val len = length pts; |
722 |
722 |
723 val limit = Config.get ctxt ambiguity_limit; |
723 val limit = Config.get ctxt ambiguity_limit; |
724 fun show_pt pt = |
724 fun show_pt pt = |
725 Pretty.string_of (Ast.pretty_ast (Syn_Trans.parsetree_to_ast ctxt (K NONE) pt)); |
725 Pretty.string_of (Ast.pretty_ast (Syn_Trans.parsetree_to_ast ctxt false (K NONE) pt)); |
726 val _ = |
726 val _ = |
727 if len <= Config.get ctxt ambiguity_level then () |
727 if len <= Config.get ctxt ambiguity_level then () |
728 else if not (Config.get ctxt ambiguity_enabled) then error (ambiguity_msg pos) |
728 else if not (Config.get ctxt ambiguity_enabled) then error (ambiguity_msg pos) |
729 else |
729 else |
730 (Context_Position.if_visible ctxt warning (cat_lines |
730 (Context_Position.if_visible ctxt warning (cat_lines |
731 (("Ambiguous input" ^ Position.str_of pos ^ |
731 (("Ambiguous input" ^ Position.str_of pos ^ |
732 "\nproduces " ^ string_of_int len ^ " parse trees" ^ |
732 "\nproduces " ^ string_of_int len ^ " parse trees" ^ |
733 (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
733 (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
734 map show_pt (take limit pts)))); |
734 map show_pt (take limit pts)))); |
735 in |
735 in |
736 some_results (Syn_Trans.parsetree_to_ast ctxt (lookup_tr parse_ast_trtab)) pts |
736 some_results (Syn_Trans.parsetree_to_ast ctxt false (lookup_tr parse_ast_trtab)) pts |
737 end; |
737 end; |
738 |
738 |
739 |
739 |
740 (* read *) |
740 (* read *) |
741 |
741 |
827 |
827 |
828 fun is_const (Syntax ({consts, ...}, _)) c = member (op =) consts c; |
828 fun is_const (Syntax ({consts, ...}, _)) c = member (op =) consts c; |
829 |
829 |
830 local |
830 local |
831 |
831 |
832 fun check_rule (rule as (lhs, rhs)) = |
832 fun check_rule rule = |
833 (case Ast.rule_error rule of |
833 (case Ast.rule_error rule of |
834 SOME msg => |
834 SOME msg => |
835 error ("Error in syntax translation rule: " ^ msg ^ "\n" ^ |
835 error ("Error in syntax translation rule: " ^ msg ^ "\n" ^ |
836 Ast.str_of_ast lhs ^ " -> " ^ Ast.str_of_ast rhs) |
836 Pretty.string_of (Ast.pretty_rule rule)) |
837 | NONE => rule); |
837 | NONE => rule); |
838 |
838 |
839 fun read_pattern ctxt syn (root, str) = |
839 fun read_pattern ctxt syn (root, str) = |
840 let |
840 let |
841 fun constify (ast as Ast.Constant _) = ast |
841 fun constify (ast as Ast.Constant _) = ast |