src/Pure/Thy/thy_syntax.ML
author wenzelm
Wed, 25 Mar 2015 11:39:52 +0100
changeset 59809 87641097d0f3
parent 59125 ee19c92ae8b4
child 61379 c57820ceead3
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 28454
diff changeset
     1
(*  Title:      Pure/Thy/thy_syntax.ML
23726
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
     3
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 28454
diff changeset
     4
Superficial theory syntax: tokens and spans.
23726
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
     5
*)
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
     6
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 28454
diff changeset
     7
signature THY_SYNTAX =
23726
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
     8
sig
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59064
diff changeset
     9
  val reports_of_tokens: Keyword.keywords -> Token.T list -> bool * Position.report_text list
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59064
diff changeset
    10
  val present_token: Keyword.keywords -> Token.T -> Output.output
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59064
diff changeset
    11
  val present_span: Keyword.keywords -> Command_Span.span -> Output.output
51225
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    12
  datatype 'a element = Element of 'a * ('a element list * 'a) option
51321
75682dfff630 more Thy_Syntax.element operations;
wenzelm
parents: 51268
diff changeset
    13
  val atom: 'a -> 'a element
51225
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    14
  val map_element: ('a -> 'b) -> 'a element -> 'b element
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    15
  val flat_element: 'a element -> 'a list
51268
fcc4b89a600d simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents: 51267
diff changeset
    16
  val last_element: 'a element -> 'a
58923
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    17
  val parse_elements: Keyword.keywords -> Command_Span.span list -> Command_Span.span element list
23726
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
    18
end;
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
    19
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37197
diff changeset
    20
structure Thy_Syntax: THY_SYNTAX =
23726
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
    21
struct
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
    22
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57903
diff changeset
    23
(** presentation **)
23726
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
    24
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
    25
local
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
    26
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59064
diff changeset
    27
fun reports_of_token keywords tok =
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 46811
diff changeset
    28
  let
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 46811
diff changeset
    29
    val malformed_symbols =
59809
87641097d0f3 tuned signature;
wenzelm
parents: 59125
diff changeset
    30
      Input.source_explode (Token.input_of tok)
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 46811
diff changeset
    31
      |> map_filter (fn (sym, pos) =>
48756
1c843142758e sneak message into "bad" markup as property -- to be displayed after YXML parsing;
wenzelm
parents: 48752
diff changeset
    32
          if Symbol.is_malformed sym
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 48878
diff changeset
    33
          then SOME ((pos, Markup.bad), "Malformed symbolic character") else NONE);
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 46811
diff changeset
    34
    val is_malformed = Token.is_error tok orelse not (null malformed_symbols);
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59123
diff changeset
    35
    val reports = Token.reports keywords tok @ Token.completion_report tok @ malformed_symbols;
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 46811
diff changeset
    36
  in (is_malformed, reports) end;
40528
d5e9f7608341 report malformed symbols;
wenzelm
parents: 40523
diff changeset
    37
23803
11bf7af10ec8 tuned signature;
wenzelm
parents: 23790
diff changeset
    38
in
11bf7af10ec8 tuned signature;
wenzelm
parents: 23790
diff changeset
    39
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59064
diff changeset
    40
fun reports_of_tokens keywords toks =
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59064
diff changeset
    41
  let val results = map (reports_of_token keywords) toks
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 46811
diff changeset
    42
  in (exists fst results, maps snd results) end;
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 46811
diff changeset
    43
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57903
diff changeset
    44
end;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57903
diff changeset
    45
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59064
diff changeset
    46
fun present_token keywords tok =
59125
ee19c92ae8b4 more explicit markup for improper commands;
wenzelm
parents: 59123
diff changeset
    47
  fold_rev Markup.enclose (Token.markups keywords tok) (Output.output (Token.unparse tok));
23726
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
    48
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59064
diff changeset
    49
fun present_span keywords = implode o map (present_token keywords) o Command_Span.content;
54451
459cf6ee254e tuned signature;
wenzelm
parents: 53843
diff changeset
    50
459cf6ee254e tuned signature;
wenzelm
parents: 53843
diff changeset
    51
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    52
43621
e8858190cccd clarified Thy_Syntax.element;
wenzelm
parents: 43430
diff changeset
    53
(** specification elements: commands with optional proof **)
e8858190cccd clarified Thy_Syntax.element;
wenzelm
parents: 43430
diff changeset
    54
51225
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    55
datatype 'a element = Element of 'a * ('a element list * 'a) option;
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    56
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    57
fun element (a, b) = Element (a, SOME b);
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    58
fun atom a = Element (a, NONE);
43621
e8858190cccd clarified Thy_Syntax.element;
wenzelm
parents: 43430
diff changeset
    59
51225
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    60
fun map_element f (Element (a, NONE)) = Element (f a, NONE)
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    61
  | map_element f (Element (a, SOME (elems, b))) =
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    62
      Element (f a, SOME ((map o map_element) f elems, f b));
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    63
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    64
fun flat_element (Element (a, NONE)) = [a]
3fe0d8d55975 support nested Thy_Syntax.element;
wenzelm
parents: 50238
diff changeset
    65
  | flat_element (Element (a, SOME (elems, b))) = a :: maps flat_element elems @ [b];
43621
e8858190cccd clarified Thy_Syntax.element;
wenzelm
parents: 43430
diff changeset
    66
51268
fcc4b89a600d simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents: 51267
diff changeset
    67
fun last_element (Element (a, NONE)) = a
fcc4b89a600d simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents: 51267
diff changeset
    68
  | last_element (Element (_, SOME (_, b))) = b;
fcc4b89a600d simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents: 51267
diff changeset
    69
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    70
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    71
(* scanning spans *)
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    72
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57903
diff changeset
    73
val eof = Command_Span.Span (Command_Span.Command_Span ("", Position.none), []);
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    74
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57903
diff changeset
    75
fun is_eof (Command_Span.Span (Command_Span.Command_Span ("", _), _)) = true
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    76
  | is_eof _ = false;
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    77
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    78
val not_eof = not o is_eof;
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    79
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    80
val stopper = Scan.stopper (K eof) is_eof;
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    81
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    82
46811
03a2dc9e0624 clarified command span: include trailing whitespace/comments and thus reduce number of ignored spans with associated transactions and states (factor 2);
wenzelm
parents: 45666
diff changeset
    83
(* parse *)
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    84
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    85
local
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    86
48878
5e850e6fa3c3 tuned errors;
wenzelm
parents: 48867
diff changeset
    87
fun command_with pred =
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57903
diff changeset
    88
  Scan.one
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57903
diff changeset
    89
    (fn (Command_Span.Span (Command_Span.Command_Span (name, _), _)) => pred name | _ => false);
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
    90
58923
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    91
fun parse_element keywords =
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    92
  let
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    93
    val proof_atom =
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    94
      Scan.one
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    95
        (fn (Command_Span.Span (Command_Span.Command_Span (name, _), _)) =>
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    96
              Keyword.is_proof_body keywords name
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    97
          | _ => true) >> atom;
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    98
    fun proof_element x =
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
    99
      (command_with (Keyword.is_proof_goal keywords) -- proof_rest >> element || proof_atom) x
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
   100
    and proof_rest x =
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
   101
      (Scan.repeat proof_element -- command_with (Keyword.is_qed keywords)) x;
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
   102
  in
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
   103
    command_with (Keyword.is_theory_goal keywords) -- proof_rest >> element ||
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
   104
    Scan.one not_eof >> atom
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
   105
  end;
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
   106
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
   107
in
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
   108
58923
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
   109
fun parse_elements keywords =
46811
03a2dc9e0624 clarified command span: include trailing whitespace/comments and thus reduce number of ignored spans with associated transactions and states (factor 2);
wenzelm
parents: 45666
diff changeset
   110
  Source.of_list #>
58923
cb9b69cca999 more explicit Keyword.keywords;
wenzelm
parents: 58864
diff changeset
   111
  Source.source stopper (Scan.bulk (parse_element keywords)) #>
46811
03a2dc9e0624 clarified command span: include trailing whitespace/comments and thus reduce number of ignored spans with associated transactions and states (factor 2);
wenzelm
parents: 45666
diff changeset
   112
  Source.exhaust;
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
   113
23726
2ebecff57b17 Basic editing of theory sources.
wenzelm
parents:
diff changeset
   114
end;
28434
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
   115
56f0951f4d26 added unit_source: commands with proof;
wenzelm
parents: 27863
diff changeset
   116
end;