author | wenzelm |
Wed, 25 Mar 2015 11:39:52 +0100 | |
changeset 59809 | 87641097d0f3 |
parent 59125 | ee19c92ae8b4 |
child 61379 | c57820ceead3 |
permissions | -rw-r--r-- |
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 | 2 |
Author: Makarius |
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 | 5 |
*) |
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 | 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 | 12 |
datatype 'a element = Element of 'a * ('a element list * 'a) option |
51321 | 13 |
val atom: 'a -> 'a element |
51225 | 14 |
val map_element: ('a -> 'b) -> 'a element -> 'b element |
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 | 17 |
val parse_elements: Keyword.keywords -> Command_Span.span list -> Command_Span.span element list |
23726 | 18 |
end; |
19 |
||
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37197
diff
changeset
|
20 |
structure Thy_Syntax: THY_SYNTAX = |
23726 | 21 |
struct |
22 |
||
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57903
diff
changeset
|
23 |
(** presentation **) |
23726 | 24 |
|
25 |
local |
|
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 | 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 | 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 | 37 |
|
23803 | 38 |
in |
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 | 47 |
fold_rev Markup.enclose (Token.markups keywords tok) (Output.output (Token.unparse tok)); |
23726 | 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 | 50 |
|
51 |
||
28434 | 52 |
|
43621 | 53 |
(** specification elements: commands with optional proof **) |
54 |
||
51225 | 55 |
datatype 'a element = Element of 'a * ('a element list * 'a) option; |
56 |
||
57 |
fun element (a, b) = Element (a, SOME b); |
|
58 |
fun atom a = Element (a, NONE); |
|
43621 | 59 |
|
51225 | 60 |
fun map_element f (Element (a, NONE)) = Element (f a, NONE) |
61 |
| map_element f (Element (a, SOME (elems, b))) = |
|
62 |
Element (f a, SOME ((map o map_element) f elems, f b)); |
|
63 |
||
64 |
fun flat_element (Element (a, NONE)) = [a] |
|
65 |
| flat_element (Element (a, SOME (elems, b))) = a :: maps flat_element elems @ [b]; |
|
43621 | 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 | 70 |
|
71 |
(* scanning spans *) |
|
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 | 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 | 76 |
| is_eof _ = false; |
77 |
||
78 |
val not_eof = not o is_eof; |
|
79 |
||
80 |
val stopper = Scan.stopper (K eof) is_eof; |
|
81 |
||
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 | 84 |
|
85 |
local |
|
86 |
||
48878 | 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 | 90 |
|
58923 | 91 |
fun parse_element keywords = |
92 |
let |
|
93 |
val proof_atom = |
|
94 |
Scan.one |
|
95 |
(fn (Command_Span.Span (Command_Span.Command_Span (name, _), _)) => |
|
96 |
Keyword.is_proof_body keywords name |
|
97 |
| _ => true) >> atom; |
|
98 |
fun proof_element x = |
|
99 |
(command_with (Keyword.is_proof_goal keywords) -- proof_rest >> element || proof_atom) x |
|
100 |
and proof_rest x = |
|
101 |
(Scan.repeat proof_element -- command_with (Keyword.is_qed keywords)) x; |
|
102 |
in |
|
103 |
command_with (Keyword.is_theory_goal keywords) -- proof_rest >> element || |
|
104 |
Scan.one not_eof >> atom |
|
105 |
end; |
|
28434 | 106 |
|
107 |
in |
|
108 |
||
58923 | 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 | 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 | 113 |
|
23726 | 114 |
end; |
28434 | 115 |
|
116 |
end; |