src/Pure/ProofGeneral/pgip_parser.ML
author wenzelm
Wed Jun 25 17:38:32 2008 +0200 (2008-06-25)
changeset 27353 71c4dd53d4cb
parent 24192 4eccd4bb8b64
child 27664 0e7a7fcd403b
permissions -rw-r--r--
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm@23797
     1
(*  Title:      Pure/ProofGeneral/pgip_parser.ML
wenzelm@23797
     2
    ID:         $Id$
wenzelm@23797
     3
    Author:     David Aspinall and Makarius
wenzelm@23797
     4
wenzelm@23797
     5
Parsing theory sources without execution (via keyword classification).
wenzelm@23797
     6
*)
wenzelm@23797
     7
wenzelm@23797
     8
signature PGIP_PARSER =
wenzelm@23797
     9
sig
wenzelm@23797
    10
  val pgip_parser: Position.T -> string -> PgipMarkup.pgipdocument
wenzelm@23797
    11
end
wenzelm@23797
    12
wenzelm@23797
    13
structure PgipParser: PGIP_PARSER =
wenzelm@23797
    14
struct
wenzelm@23797
    15
wenzelm@23797
    16
structure K = OuterKeyword;
wenzelm@23797
    17
structure D = PgipMarkup;
wenzelm@23797
    18
structure I = PgipIsabelle;
wenzelm@23797
    19
wenzelm@23797
    20
wenzelm@23797
    21
fun badcmd text = [D.Badcmd {text = text}];
wenzelm@23797
    22
wenzelm@23797
    23
fun thy_begin text =
wenzelm@23868
    24
  (case try (ThyHeader.read Position.none) (Source.of_string text) of
aspinall@24192
    25
    NONE => D.Opentheory {thyname = NONE, parentnames = [], text = text}
wenzelm@23797
    26
  | SOME (name, imports, _) =>
aspinall@24192
    27
       D.Opentheory {thyname = SOME name, parentnames = imports, text = text})
aspinall@24192
    28
  :: [D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjTheoryBody}];
wenzelm@23797
    29
wenzelm@23797
    30
fun thy_heading text =
wenzelm@23797
    31
  [D.Closeblock {},
wenzelm@23797
    32
   D.Doccomment {text = text},
wenzelm@23797
    33
   D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjTheoryBody}];
wenzelm@23797
    34
wenzelm@23797
    35
fun thy_decl text =
wenzelm@23797
    36
  [D.Theoryitem {name = NONE, objtype = SOME I.ObjTheoryDecl, text = text}];
wenzelm@23797
    37
wenzelm@23797
    38
fun goal text =
wenzelm@23797
    39
  [D.Opengoal {thmname = NONE, text = text},
wenzelm@23797
    40
   D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjProofBody}];
wenzelm@23797
    41
wenzelm@23797
    42
fun prf_block text =
wenzelm@23797
    43
  [D.Closeblock {},
wenzelm@23797
    44
   D.Proofstep {text = text},
wenzelm@23797
    45
   D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjProofBody}];
wenzelm@23797
    46
wenzelm@23797
    47
fun prf_open text =
wenzelm@23797
    48
 [D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjProofBody},
wenzelm@23797
    49
  D.Proofstep {text = text}];
wenzelm@23797
    50
wenzelm@23797
    51
fun proofstep text = [D.Proofstep {text = text}];
wenzelm@23797
    52
fun closegoal text = [D.Closegoal {text = text}, D.Closeblock {}];
wenzelm@23797
    53
wenzelm@23797
    54
wenzelm@23797
    55
fun command k f = Symtab.update_new (OuterKeyword.kind_of k, f);
wenzelm@23797
    56
wenzelm@23797
    57
val command_keywords = Symtab.empty
wenzelm@23797
    58
  |> command K.control          badcmd
wenzelm@23797
    59
  |> command K.diag             (fn text => [D.Spuriouscmd {text = text}])
wenzelm@23797
    60
  |> command K.thy_begin        thy_begin
wenzelm@23797
    61
  |> command K.thy_switch       badcmd
wenzelm@23797
    62
  |> command K.thy_end          (fn text => [D.Closeblock {}, D.Closetheory {text = text}])
wenzelm@23797
    63
  |> command K.thy_heading      thy_heading
wenzelm@23797
    64
  |> command K.thy_decl         thy_decl
wenzelm@23797
    65
  |> command K.thy_script       thy_decl
wenzelm@23797
    66
  |> command K.thy_goal         goal
wenzelm@23797
    67
  |> command K.qed              closegoal
wenzelm@23797
    68
  |> command K.qed_block        closegoal
wenzelm@23797
    69
  |> command K.qed_global       (fn text => [D.Giveupgoal {text = text}])
wenzelm@23797
    70
  |> command K.prf_heading      (fn text => [D.Doccomment {text = text}])
wenzelm@23797
    71
  |> command K.prf_goal         goal
wenzelm@23797
    72
  |> command K.prf_block        prf_block
wenzelm@23797
    73
  |> command K.prf_open         prf_open
wenzelm@23797
    74
  |> command K.prf_close        (fn text => [D.Proofstep {text = text}, D.Closeblock {}])
wenzelm@23797
    75
  |> command K.prf_chain        proofstep
wenzelm@23797
    76
  |> command K.prf_decl         proofstep
wenzelm@23797
    77
  |> command K.prf_asm          proofstep
wenzelm@23797
    78
  |> command K.prf_asm_goal     goal
wenzelm@23797
    79
  |> command K.prf_script       proofstep;
wenzelm@23797
    80
wenzelm@23797
    81
val _ = OuterKeyword.kinds subset_string Symtab.keys command_keywords
wenzelm@23797
    82
  orelse sys_error "Incomplete coverage of command keywords";
wenzelm@23797
    83
wenzelm@23797
    84
fun parse_command "sorry" text = [D.Postponegoal {text = text}, D.Closeblock {}]
wenzelm@23797
    85
  | parse_command name text =
wenzelm@27353
    86
      (case OuterKeyword.command_keyword name of
wenzelm@23797
    87
        NONE => [D.Unparseable {text = text}]
wenzelm@23797
    88
      | SOME k =>
wenzelm@23797
    89
          (case Symtab.lookup command_keywords (OuterKeyword.kind_of k) of
wenzelm@23797
    90
            NONE => [D.Unparseable {text = text}]
wenzelm@23797
    91
          | SOME f => f text));
wenzelm@23797
    92
wenzelm@23797
    93
fun parse_item (kind, toks) =
wenzelm@23797
    94
  let val text = implode (map (PgmlIsabelle.pgml_mode ThyEdit.present_token) toks) in
wenzelm@23797
    95
    (case kind of
wenzelm@23797
    96
      ThyEdit.Whitespace => [D.Whitespace {text = text}]
wenzelm@23797
    97
    | ThyEdit.Junk => [D.Unparseable {text = text}]
wenzelm@23797
    98
    | ThyEdit.Commandspan name => parse_command name text)
wenzelm@23797
    99
  end;
wenzelm@23797
   100
wenzelm@23797
   101
wenzelm@23797
   102
fun pgip_parser pos str =
wenzelm@23797
   103
  maps parse_item (ThyEdit.parse_items pos (Source.of_string str));
wenzelm@23797
   104
wenzelm@23797
   105
end;