src/Pure/ProofGeneral/parsing.ML
author aspinall
Thu, 09 Aug 2007 11:39:29 +0200
changeset 24192 4eccd4bb8b64
parent 23798 fac9ea4d58ab
permissions -rw-r--r--
PGIP change: thyname is optional in opentheory, markup even in case of header parse failure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
     1
(*  Title:      Pure/ProofGeneral/parsing.ML
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
     2
    ID:         $Id$
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
     3
    Author:     David Aspinall
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
     4
23798
fac9ea4d58ab renamed PgipParser to OldPgipParser;
wenzelm
parents: 23588
diff changeset
     5
Parsing Isabelle theory files to add PGIP markup -- OLD VERSION.
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
     6
*)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
     7
23798
fac9ea4d58ab renamed PgipParser to OldPgipParser;
wenzelm
parents: 23588
diff changeset
     8
signature OLD_PGIP_PARSER =
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
     9
sig
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    10
    val pgip_parser: PgipMarkup.pgip_parser
21971
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    11
    val init : unit -> unit		     (* clear state *)
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    12
end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    13
23798
fac9ea4d58ab renamed PgipParser to OldPgipParser;
wenzelm
parents: 23588
diff changeset
    14
structure OldPgipParser : OLD_PGIP_PARSER =
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    15
struct
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    16
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    17
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    18
(** Parsing proof scripts without execution **)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    19
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    20
structure P = OuterParse;
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    21
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    22
structure D = PgipMarkup;
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
    23
structure I = PgipIsabelle;
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
    24
structure T = PgipTypes;
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    25
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    26
(* Notes.
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    27
   This is quite tricky, because 1) we need to put back whitespace which
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    28
   was removed during parsing so we can provide markup around commands;
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    29
   2) parsing is intertwined with execution in Isar so we have to repeat
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    30
   the parsing to extract interesting parts of commands.  The trace of
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    31
   tokens parsed which is now recorded in each transition (added by
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    32
   Markus June '04) helps do this, but the mechanism is still a bad mess.
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    33
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    34
   If we had proper parse trees it would be easy, although having a
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    35
   fixed type of trees might be hard with Isar's extensible parser.
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    36
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    37
   Probably the best solution is to produce the meta-information at
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    38
   the same time as the parse, for each command, e.g. by recording a
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    39
   list of (name,objtype) pairs which record the bindings created by
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    40
   a command.  This would require changing the interfaces in
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    41
   outer_syntax.ML (or providing new ones),
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    42
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    43
    datatype metainfo = Binding of string * string  (* name, objtype *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    44
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    45
    val command_withmetainfo: string -> string -> string ->
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    46
      (token list ->
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    47
       ((Toplevel.transition -> Toplevel.transition) * metainfo list) *
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    48
       token list) -> parser
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    49
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    50
   We'd also like to render terms as they appear in output, but this
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    51
   will be difficult because inner syntax is extensible and we don't
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    52
   have the correct syntax to hand when just doing outer parsing
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    53
   without execution.  A reasonable approximation could
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    54
   perhaps be obtained by using the syntax of the current context.
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    55
   However, this would mean more mess trying to pick out terms,
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    56
   so at this stage a more serious change to the Isar functions
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    57
   seems necessary.
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    58
*)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    59
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    60
local
21971
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    61
    val keywords_classification_table = ref (NONE: string Symtab.table option)
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    62
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    63
in
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    64
    fun get_keywords_classification_table () =
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    65
        case (!keywords_classification_table) of
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    66
          SOME t => t
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    67
        | NONE => (let
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    68
                     val tab = fold (fn (c, _, k, _) => Symtab.update (c, k))
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    69
                                    (OuterSyntax.dest_parsers ()) Symtab.empty;
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    70
                   in (keywords_classification_table := SOME tab; tab) end)
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    71
  
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    72
    (* To allow dynamic extensions to language during interactive use
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    73
       we need a hook in outer_syntax.ML to reset our table.  But this is
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    74
       pretty obscure; initialisation at startup should suffice. *)
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    75
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    76
    fun init () = (keywords_classification_table := NONE)
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    77
end
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    78
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    79
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
    80
local
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    81
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    82
    fun whitespace str = D.Whitespace { text=str }
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    83
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    84
    (* an extra token is needed to terminate the command *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    85
    val sync = OuterSyntax.scan "\\<^sync>";
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    86
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    87
    fun nameparse elt objtyp nameP toks = 
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    88
        (fst (nameP (toks@sync)))
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    89
        handle _ => (error ("Error occurred in name parser for " ^ elt ^
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
    90
                            "(objtype: " ^ (T.name_of_objtype objtyp) ^ ")");
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
    91
                     "")
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    92
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    93
    fun parse_warning msg = warning ("script->PGIP markup parser: " ^ msg)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    94
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    95
    fun xmls_of_thy_begin (name,toks,str) = (* see isar_syn.ML/theoryP *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    96
        let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    97
            val ((thyname,imports,files),_) = ThyHeader.args (toks@sync)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
    98
        in
24192
4eccd4bb8b64 PGIP change: thyname is optional in opentheory, markup even in case of header parse failure
aspinall
parents: 23798
diff changeset
    99
            [D.Opentheory { thyname=SOME thyname, 
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   100
			    parentnames = imports,
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   101
			    text = str },
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   102
	     D.Openblock { metavarid=NONE,name=NONE,objtype=SOME I.ObjTheoryBody }]
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   103
        end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   104
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   105
    fun xmls_of_thy_decl (name,toks,str) = (* see isar_syn.ML/thy_decl cases  *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   106
        let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   107
            (* NB: PGIP only deals with single name bindings at the moment;
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   108
               potentially we could markup grouped definitions several times but
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   109
               that might suggest the wrong structure to the editor?
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   110
               Better alternative would be to put naming closer around text,
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   111
               but to do that we'd be much better off modifying the real parser
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   112
               instead of reconstructing it here. *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   113
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   114
            val plain_items = (* no names, unimportant names, or too difficult *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   115
                ["defaultsort","arities","judgement","finalconsts",
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   116
                 "syntax", "nonterminals", "translations",
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   117
                 "global", "local", "hide",
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   118
                 "ML_setup", "setup", "method_setup",
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   119
                 "parse_ast_translation", "parse_translation", "print_translation",
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   120
                 "typed_print_translation", "print_ast_translation", "token_translation",
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   121
                 "oracle",
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   122
                 "declare"]
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   123
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   124
            fun named_item nameP ty = 
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   125
		[D.Theoryitem {text=str,
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   126
			       name=SOME (nameparse "theoryitem" ty nameP toks),
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   127
			       objtype=SOME ty}]
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   128
            val opt_overloaded = P.opt_keyword "overloaded";
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   129
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   130
            val thmnameP = (* see isar_syn.ML/theoremsP *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   131
                let
22101
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21971
diff changeset
   132
                    val name_facts = P.and_list1 ((SpecParse.opt_thm_name "=" --| SpecParse.xthms1) >> #1)
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21971
diff changeset
   133
                    val theoremsP = P.opt_target |-- name_facts >> (fn [] => "" | x::_ => x)
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   134
                in
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   135
                    theoremsP
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   136
                end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   137
        in
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   138
            (* TODO: ideally we would like to render terms properly, just as they are
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   139
               in output. This implies using PGML for symbols and variables/atoms.
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   140
               BUT it's rather tricky without having the correct concrete syntax to hand,
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   141
               and even if we did, we'd have to mess around here a whole lot more first
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   142
               to pick out the terms from the syntax. *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   143
23588
4fc6df2c7098 Classify -- comments as ordinary comments (no undo)
aspinall
parents: 22405
diff changeset
   144
            if member (op =) plain_items name then 
4fc6df2c7098 Classify -- comments as ordinary comments (no undo)
aspinall
parents: 22405
diff changeset
   145
		[D.Theoryitem {text=str,name=NONE,objtype=NONE}]
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   146
            else case name of
23588
4fc6df2c7098 Classify -- comments as ordinary comments (no undo)
aspinall
parents: 22405
diff changeset
   147
                     "text"      => [D.Doccomment {text=str}]
4fc6df2c7098 Classify -- comments as ordinary comments (no undo)
aspinall
parents: 22405
diff changeset
   148
                   | "text_raw"  => [D.Doccomment {text=str}]
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   149
                   | "typedecl"  => named_item (P.type_args |-- P.name) T.ObjType
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   150
                   | "types"     => named_item (P.type_args |-- P.name) T.ObjType
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   151
                   | "classes"   => named_item P.name I.ObjClass
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   152
                   | "classrel"  => named_item P.name I.ObjClass
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   153
                   | "consts"    => named_item (P.const >> #1) T.ObjTerm
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   154
                   | "axioms"    => named_item (SpecParse.spec_name >> (#1 o #1)) T.ObjTheorem
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   155
                   | "defs"      => named_item (opt_overloaded |-- SpecParse.spec_name >> (#1 o #1)) T.ObjTheorem
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   156
                   | "constdefs" => named_item P.name (* FIXME: heavily simplified/wrong *) T.ObjTerm
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   157
                   | "theorems"  => named_item thmnameP I.ObjTheoremSet
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   158
                   | "lemmas"    => named_item thmnameP I.ObjTheoremSet
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   159
                   | "oracle"    => named_item P.name I.ObjOracle
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   160
		   (* FIXME: locale needs to introduce a block *)
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   161
                   | "locale"    => named_item ((P.opt_keyword "open" >> not) |-- P.name) I.ObjLocale
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   162
                   | _ => (parse_warning ("Unrecognized thy-decl command: " ^ name); 
23588
4fc6df2c7098 Classify -- comments as ordinary comments (no undo)
aspinall
parents: 22405
diff changeset
   163
			   [D.Theoryitem {text=str,name=NONE,objtype=NONE}])
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   164
        end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   165
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   166
    fun xmls_of_thy_goal (name,toks,str) =
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   167
        let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   168
            (* see isar_syn.ML/gen_theorem *)
22101
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21971
diff changeset
   169
         val statement = P.and_list1 (SpecParse.opt_thm_name ":" -- Scan.repeat1 P.propp);
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   170
         val general_statement =
22101
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21971
diff changeset
   171
            statement >> pair [] || Scan.repeat SpecParse.locale_element -- (P.$$$ "shows" |-- statement);
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   172
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   173
            val gen_theoremP =
22101
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21971
diff changeset
   174
                P.opt_target -- Scan.optional (SpecParse.opt_thm_name ":" --|
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21971
diff changeset
   175
                 Scan.ahead (SpecParse.locale_keyword || P.$$$ "shows")) ("", []) --
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   176
                                 general_statement >> (fn ((locale, a), (elems, concl)) =>
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   177
                                                         fst a) (* a : (bstring * Args.src list) *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   178
22101
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21971
diff changeset
   179
            val nameP = P.opt_target |-- (Scan.optional ((SpecParse.opt_thm_name ":") >> #1) "")
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   180
	    val thmname = nameparse "opengoal" T.ObjTheorem nameP toks
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   181
        in
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   182
            [D.Opengoal {thmname=SOME thmname, text=str},
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   183
             D.Openblock {metavarid=NONE,name=NONE,objtype=SOME I.ObjProofBody}]
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   184
        end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   185
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   186
    fun xmls_of_qed (name,str) =
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   187
        let val qedmarkup  =
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   188
                (case name of
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   189
                     "sorry" => D.Postponegoal {text=str}
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   190
                   | "oops"  => D.Giveupgoal {text=str}
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   191
                   | "done"  => D.Closegoal {text=str}
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   192
                   | "by"    => D.Closegoal {text=str}  (* nested or toplevel *)
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   193
                   | "qed"   => D.Closegoal {text=str}  (* nested or toplevel *)
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   194
                   | "."     => D.Closegoal {text=str}  (* nested or toplevel *)
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   195
                   | ".."    => D.Closegoal {text=str}  (* nested or toplevel *)
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   196
                   | other => (* default to closegoal: may be wrong for extns *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   197
                                  (parse_warning
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   198
                                       ("Unrecognized qed command: " ^ quote other);
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   199
                                       D.Closegoal {text=str}))
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   200
        in qedmarkup :: [D.Closeblock {}] end
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   201
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   202
    fun xmls_of_kind kind (name,toks,str) =
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   203
    case kind of
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   204
      "control"        => [D.Badcmd {text=str}]
21971
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
   205
    | "diag"           => [D.Spuriouscmd {text=str}]
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   206
    (* theory/files *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   207
    | "theory-begin"   => xmls_of_thy_begin (name,toks,str)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   208
    | "theory-decl"    => xmls_of_thy_decl (name,toks,str)
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   209
    | "theory-heading" => [D.Closeblock {},
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   210
			   D.Doccomment {text=str},
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   211
			   D.Openblock {metavarid=NONE,name=NONE,objtype=SOME I.ObjTheoryBody}]
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   212
    | "theory-script"  => [D.Theoryitem {name=NONE,objtype=SOME I.ObjTheoryDecl,text=str}]
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   213
    | "theory-end"     => [D.Closeblock {}, D.Closetheory {text=str}]
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   214
    (* proof control *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   215
    | "theory-goal"    => xmls_of_thy_goal (name,toks,str)
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   216
    | "proof-heading"  => [D.Doccomment {text=str}]
22374
db0b80b8371c Add closeblock/openblock structure to proof-block commands
aspinall
parents: 22160
diff changeset
   217
    | "proof-goal"     => [D.Opengoal {text=str,thmname=NONE}, 
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   218
			   D.Openblock {metavarid=NONE,name=NONE,objtype=SOME I.ObjProofBody}]
22374
db0b80b8371c Add closeblock/openblock structure to proof-block commands
aspinall
parents: 22160
diff changeset
   219
    | "proof-block"    => [D.Closeblock {}, 
db0b80b8371c Add closeblock/openblock structure to proof-block commands
aspinall
parents: 22160
diff changeset
   220
			   D.Proofstep {text=str},
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   221
			   D.Openblock {metavarid=NONE,name=NONE,objtype=SOME I.ObjProofBody}]
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   222
    | "proof-open"     => [D.Openblock {metavarid=NONE,name=NONE,objtype=SOME I.ObjProofBody}, 
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   223
			   D.Proofstep {text=str} ]
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   224
    | "proof-close"    => [D.Proofstep {text=str}, D.Closeblock {}]
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   225
    | "proof-script"   => [D.Proofstep {text=str}]
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   226
    | "proof-chain"    => [D.Proofstep {text=str}]
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   227
    | "proof-decl"     => [D.Proofstep {text=str}]
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   228
    | "proof-asm"      => [D.Proofstep {text=str}]
22405
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   229
    | "proof-asm-goal" => [D.Opengoal {text=str,thmname=NONE}, 
7eef90be0db4 Add objtypes to openblock/closeblock and theory items (can be used to control folding).
aspinall
parents: 22374
diff changeset
   230
			   D.Openblock {metavarid=NONE,name=NONE,objtype=NONE}]
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   231
    | "qed"            => xmls_of_qed (name,str)
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   232
    | "qed-block"      => xmls_of_qed (name,str)
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   233
    | "qed-global"     => xmls_of_qed (name,str)
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   234
    | other => (* default for anything else is "spuriouscmd", ***ignored for undo*** *)
21971
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
   235
      (parse_warning ("Internal inconsistency, unrecognized keyword kind: " ^ quote other ^
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
   236
		      "(treated as spuriouscmd)");
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   237
       [D.Spuriouscmd {text=str}])
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   238
in
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   239
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   240
fun xmls_of_transition (name,str,toks) =
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   241
   let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   242
     val classification = Symtab.lookup (get_keywords_classification_table ()) name
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   243
   in case classification of
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   244
          SOME k => (xmls_of_kind k (name,toks,str))
21971
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
   245
        | NONE => (* an uncategorized keyword is treated as a theory item (maybe wrong) *)
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
   246
          (parse_warning ("Uncategorized command found: " ^ name ^ " -- using theoryitem");
513e1d82640d Fix diagnostic kind (*are* spuriouscmd). Add init to get right table for logic at startup. Use theoryitem as default for unrecognised command.
aspinall
parents: 21966
diff changeset
   247
           [D.Theoryitem {name=NONE,objtype=NONE,text=str}])
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   248
   end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   249
22160
27cdecde8c2b Comments
aspinall
parents: 22101
diff changeset
   250
27cdecde8c2b Comments
aspinall
parents: 22101
diff changeset
   251
(* this would be a lot neater if we could match on toks! *)
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   252
fun markup_comment_whs [] = []
22160
27cdecde8c2b Comments
aspinall
parents: 22101
diff changeset
   253
  | markup_comment_whs (toks as t::ts) = 
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   254
    let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   255
        val (prewhs,rest) = take_prefix (OuterLex.is_kind OuterLex.Space) toks
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   256
    in
21902
8e5e2571c716 made SML/NJ happy
haftmann
parents: 21867
diff changeset
   257
        if not (null prewhs) then
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   258
            D.Whitespace {text=implode (map OuterLex.unparse prewhs)}
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   259
            :: (markup_comment_whs rest)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   260
        else
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   261
            D.Comment {text=OuterLex.unparse t} ::
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   262
            (markup_comment_whs ts)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   263
    end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   264
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   265
fun xmls_pre_cmd [] = ([],[])
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   266
  | xmls_pre_cmd toks =
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   267
    let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   268
        (* an extra token is needed to terminate the command *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   269
        val sync = OuterSyntax.scan "\\<^sync>";
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   270
        val spaceP = Scan.bulk (Scan.one (fn t =>not (OuterLex.is_proper t)) >> OuterLex.val_of) >> implode
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   271
        val text_with_whs =
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   272
            ((spaceP || Scan.succeed "") --
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   273
             (P.short_ident || P.long_ident || P.sym_ident || P.string || P.number || P.verbatim) >> op^)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   274
             -- (spaceP || Scan.succeed "") >> op^
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   275
        val (prewhs,rest1) = take_prefix (not o OuterLex.is_proper) (toks@sync)
23588
4fc6df2c7098 Classify -- comments as ordinary comments (no undo)
aspinall
parents: 22405
diff changeset
   276
        (* NB: this collapses  doclitcomment,(doclitcomment|whitespace)* to a single doclitcomment *)
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   277
        val (_,rest2) = (Scan.bulk (P.$$$ "--" |-- text_with_whs) >> implode || Scan.succeed "") rest1
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   278
        val (postwhs,rest3) = take_prefix (not o OuterLex.is_proper) rest2
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   279
    in
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   280
        ((markup_comment_whs prewhs) @
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   281
         (if (length rest2 = length rest1)  then []
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   282
          else 
23588
4fc6df2c7098 Classify -- comments as ordinary comments (no undo)
aspinall
parents: 22405
diff changeset
   283
	      (* These comments are "literate", but *not* counted for undo. So classify as ordinary comment. *)
4fc6df2c7098 Classify -- comments as ordinary comments (no undo)
aspinall
parents: 22405
diff changeset
   284
	      [D.Comment 
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   285
		   {text= implode
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   286
                          (map OuterLex.unparse (Library.take (length rest1 - length rest2, rest1)))}]
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   287
              @
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   288
              (markup_comment_whs postwhs)),
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   289
              Library.take (length rest3 - 1,rest3))
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   290
    end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   291
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   292
fun xmls_of_impropers toks =
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   293
    let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   294
        val (xmls,rest) = xmls_pre_cmd toks
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   295
	val unparsed =  
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   296
	    case rest of [] => []
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   297
		       | _ => [D.Unparseable 
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   298
			      {text= implode (map OuterLex.unparse rest)}]
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   299
    in
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   300
        xmls @ unparsed
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   301
    end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   302
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   303
end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   304
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   305
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   306
local
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   307
    (* we have to weave together the whitespace/comments with proper tokens
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   308
       to reconstruct the input. *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   309
    (* TODO: see if duplicating isar_output/parse_thy can help here *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   310
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   311
    fun match_tokens ([],us,vs) = (rev vs,us)  (* used, unused *)
21902
8e5e2571c716 made SML/NJ happy
haftmann
parents: 21867
diff changeset
   312
      | match_tokens (t::ts, w::ws, vs) =
21966
edab0ecfbd7c removed misleading OuterLex.eq_token;
wenzelm
parents: 21902
diff changeset
   313
        if (t: OuterLex.token) = w  (* FIXME !? *)
21902
8e5e2571c716 made SML/NJ happy
haftmann
parents: 21867
diff changeset
   314
          then match_tokens (ts, ws, w::vs)
8e5e2571c716 made SML/NJ happy
haftmann
parents: 21867
diff changeset
   315
          else match_tokens (t::ts, ws, w::vs)
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   316
      | match_tokens _ = error ("match_tokens: mismatched input parse")
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   317
in
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   318
    fun pgip_parser str =
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   319
    let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   320
       (* parsing:   See outer_syntax.ML/toplevel_source *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   321
       fun parse_loop ([],[],xmls) = xmls
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   322
         | parse_loop ([],itoks,xmls) = xmls @ (xmls_of_impropers itoks)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   323
         | parse_loop ((nm,toks,_)::trs,itoks,xmls) =
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   324
           let
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   325
               (* first proper token after whitespace/litcomment/whitespace is command *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   326
               val (xmls_pre_cmd,cmditoks') = xmls_pre_cmd itoks
22160
27cdecde8c2b Comments
aspinall
parents: 22101
diff changeset
   327
               val (cmdtok,itoks') = 
27cdecde8c2b Comments
aspinall
parents: 22101
diff changeset
   328
		   case cmditoks' of x::xs => (x,xs)
27cdecde8c2b Comments
aspinall
parents: 22101
diff changeset
   329
                                   | _ => error("proof_general/parse_loop impossible")
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   330
               val (utoks,itoks'') = match_tokens (toks,itoks',[])
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   331
               (* FIXME: should take trailing [w/s+] semicolon too in utoks *)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   332
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   333
               val str = implode (map OuterLex.unparse (cmdtok::utoks))
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   334
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   335
               val xmls_tr  = xmls_of_transition (nm,str,toks)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   336
           in
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   337
               parse_loop(trs,itoks'',xmls @ xmls_pre_cmd @ xmls_tr)
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   338
           end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   339
    in
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   340
        (let val toks = OuterSyntax.scan str
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   341
         in
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   342
             parse_loop (OuterSyntax.read toks, toks, [])
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   343
         end)
21867
8750fbc28d5c Add abstraction for objtypes and documents.
aspinall
parents: 21655
diff changeset
   344
           handle _ => [D.Unparseable {text=str}]
21637
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   345
    end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   346
end
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   347
a7b156c404e2 Revamped Proof General interface.
aspinall
parents:
diff changeset
   348
end