src/Pure/ProofGeneral/pgip_parser.ML
author wenzelm
Thu, 27 May 2010 18:10:37 +0200
changeset 37146 f652333bbf8e
parent 36950 75b8f26f2f07
child 37216 3165bc303f66
permissions -rw-r--r--
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ProofGeneral/pgip_parser.ML
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     2
    Author:     David Aspinall and Makarius
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     3
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     4
Parsing theory sources without execution (via keyword classification).
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     5
*)
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     6
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     7
signature PGIP_PARSER =
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     8
sig
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
     9
  val pgip_parser: Position.T -> string -> PgipMarkup.pgipdocument
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    10
end
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    11
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    12
structure PgipParser: PGIP_PARSER =
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    13
struct
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    14
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    15
structure D = PgipMarkup;
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    16
structure I = PgipIsabelle;
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    17
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    18
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    19
fun badcmd text = [D.Badcmd {text = text}];
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    20
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    21
fun thy_begin text =
32466
a393b7e2a2f8 modernized Thy_Header;
wenzelm
parents: 29606
diff changeset
    22
  (case try (Thy_Header.read Position.none) (Source.of_string text) of
24192
4eccd4bb8b64 PGIP change: thyname is optional in opentheory, markup even in case of header parse failure
aspinall
parents: 23868
diff changeset
    23
    NONE => D.Opentheory {thyname = NONE, parentnames = [], text = text}
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    24
  | SOME (name, imports, _) =>
24192
4eccd4bb8b64 PGIP change: thyname is optional in opentheory, markup even in case of header parse failure
aspinall
parents: 23868
diff changeset
    25
       D.Opentheory {thyname = SOME name, parentnames = imports, text = text})
4eccd4bb8b64 PGIP change: thyname is optional in opentheory, markup even in case of header parse failure
aspinall
parents: 23868
diff changeset
    26
  :: [D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjTheoryBody}];
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    27
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    28
fun thy_heading text =
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    29
  [D.Closeblock {},
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    30
   D.Doccomment {text = text},
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    31
   D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjTheoryBody}];
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    32
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    33
fun thy_decl text =
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    34
  [D.Theoryitem {name = NONE, objtype = SOME I.ObjTheoryDecl, text = text}];
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    35
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    36
fun goal text =
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    37
  [D.Opengoal {thmname = NONE, text = text},
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    38
   D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjProofBody}];
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    39
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    40
fun prf_block text =
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    41
  [D.Closeblock {},
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    42
   D.Proofstep {text = text},
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    43
   D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjProofBody}];
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    44
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    45
fun prf_open text =
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    46
 [D.Openblock {metavarid = NONE, name = NONE, objtype = SOME I.ObjProofBody},
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    47
  D.Proofstep {text = text}];
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    48
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    49
fun proofstep text = [D.Proofstep {text = text}];
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    50
fun closegoal text = [D.Closegoal {text = text}, D.Closeblock {}];
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    51
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    52
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    53
fun command k f = Symtab.update_new (Keyword.kind_of k, f);
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    54
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    55
val command_keywords = Symtab.empty
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    56
  |> command Keyword.control          badcmd
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    57
  |> command Keyword.diag             (fn text => [D.Spuriouscmd {text = text}])
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    58
  |> command Keyword.thy_begin        thy_begin
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    59
  |> command Keyword.thy_switch       badcmd
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    60
  |> command Keyword.thy_end          (fn text => [D.Closeblock {}, D.Closetheory {text = text}])
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    61
  |> command Keyword.thy_heading      thy_heading
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    62
  |> command Keyword.thy_decl         thy_decl
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    63
  |> command Keyword.thy_script       thy_decl
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    64
  |> command Keyword.thy_goal         goal
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    65
  |> command Keyword.thy_schematic_goal goal
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    66
  |> command Keyword.qed              closegoal
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    67
  |> command Keyword.qed_block        closegoal
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    68
  |> command Keyword.qed_global       (fn text => [D.Giveupgoal {text = text}])
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    69
  |> command Keyword.prf_heading      (fn text => [D.Doccomment {text = text}])
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    70
  |> command Keyword.prf_goal         goal
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    71
  |> command Keyword.prf_block        prf_block
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    72
  |> command Keyword.prf_open         prf_open
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    73
  |> command Keyword.prf_close        (fn text => [D.Proofstep {text = text}, D.Closeblock {}])
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    74
  |> command Keyword.prf_chain        proofstep
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    75
  |> command Keyword.prf_decl         proofstep
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    76
  |> command Keyword.prf_asm          proofstep
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    77
  |> command Keyword.prf_asm_goal     goal
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    78
  |> command Keyword.prf_script       proofstep;
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    79
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    80
val _ = subset (op =) (Keyword.kinds, Symtab.keys command_keywords)
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    81
  orelse sys_error "Incomplete coverage of command keywords";
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    82
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    83
fun parse_command "sorry" text = [D.Postponegoal {text = text}, D.Closeblock {}]
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    84
  | parse_command name text =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    85
      (case Keyword.command_keyword name of
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    86
        NONE => [D.Unparseable {text = text}]
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    87
      | SOME k =>
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
    88
          (case Symtab.lookup command_keywords (Keyword.kind_of k) of
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    89
            NONE => [D.Unparseable {text = text}]
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    90
          | SOME f => f text));
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    91
27841
55b028593335 adapted ThyEdit operations;
wenzelm
parents: 27664
diff changeset
    92
fun parse_span span =
55b028593335 adapted ThyEdit operations;
wenzelm
parents: 27664
diff changeset
    93
  let
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 28035
diff changeset
    94
    val kind = ThySyntax.span_kind span;
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 28035
diff changeset
    95
    val toks = ThySyntax.span_content span;
37146
f652333bbf8e renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents: 36950
diff changeset
    96
    val text = implode (map (Print_Mode.setmp [] ThySyntax.present_token) toks);
27841
55b028593335 adapted ThyEdit operations;
wenzelm
parents: 27664
diff changeset
    97
  in
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
    98
    (case kind of
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 28035
diff changeset
    99
      ThySyntax.Command name => parse_command name text
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 28035
diff changeset
   100
    | ThySyntax.Ignored => [D.Whitespace {text = text}]
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 28035
diff changeset
   101
    | ThySyntax.Malformed => [D.Unparseable {text = text}])
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
   102
  end;
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
   103
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
   104
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
   105
fun pgip_parser pos str =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36315
diff changeset
   106
  maps parse_span (ThySyntax.parse_spans (Keyword.get_lexicons ()) pos str);
23797
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
   107
f4dbbffbfe06 Parsing theory sources without execution (via keyword classification).
wenzelm
parents:
diff changeset
   108
end;