author | wenzelm |
Fri, 02 Aug 2019 11:23:09 +0200 | |
changeset 70456 | c742527a25fe |
parent 68844 | 63c9c6ceb7a3 |
permissions | -rw-r--r-- |
68839 | 1 |
(* Title: Pure/Thy/thy_element.ML |
23726 | 2 |
Author: Makarius |
3 |
||
68839 | 4 |
Theory elements: statements with optional proof. |
23726 | 5 |
*) |
6 |
||
68839 | 7 |
signature THY_ELEMENT = |
23726 | 8 |
sig |
51225 | 9 |
datatype 'a element = Element of 'a * ('a element list * 'a) option |
51321 | 10 |
val atom: 'a -> 'a element |
51225 | 11 |
val map_element: ('a -> 'b) -> 'a element -> 'b element |
12 |
val flat_element: 'a element -> 'a list |
|
51268
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents:
51267
diff
changeset
|
13 |
val last_element: 'a element -> 'a |
58923 | 14 |
val parse_elements: Keyword.keywords -> Command_Span.span list -> Command_Span.span element list |
23726 | 15 |
end; |
16 |
||
68839 | 17 |
structure Thy_Element: THY_ELEMENT = |
23726 | 18 |
struct |
19 |
||
61379
c57820ceead3
more direct HTML presentation, without print mode;
wenzelm
parents:
59809
diff
changeset
|
20 |
(* datatype element: command with optional proof *) |
43621 | 21 |
|
51225 | 22 |
datatype 'a element = Element of 'a * ('a element list * 'a) option; |
23 |
||
24 |
fun element (a, b) = Element (a, SOME b); |
|
25 |
fun atom a = Element (a, NONE); |
|
43621 | 26 |
|
51225 | 27 |
fun map_element f (Element (a, NONE)) = Element (f a, NONE) |
28 |
| map_element f (Element (a, SOME (elems, b))) = |
|
29 |
Element (f a, SOME ((map o map_element) f elems, f b)); |
|
30 |
||
31 |
fun flat_element (Element (a, NONE)) = [a] |
|
32 |
| flat_element (Element (a, SOME (elems, b))) = a :: maps flat_element elems @ [b]; |
|
43621 | 33 |
|
51268
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents:
51267
diff
changeset
|
34 |
fun last_element (Element (a, NONE)) = a |
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents:
51267
diff
changeset
|
35 |
| last_element (Element (_, SOME (_, b))) = b; |
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
wenzelm
parents:
51267
diff
changeset
|
36 |
|
28434 | 37 |
|
38 |
(* scanning spans *) |
|
39 |
||
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57903
diff
changeset
|
40 |
val eof = Command_Span.Span (Command_Span.Command_Span ("", Position.none), []); |
28434 | 41 |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
57903
diff
changeset
|
42 |
fun is_eof (Command_Span.Span (Command_Span.Command_Span ("", _), _)) = true |
28434 | 43 |
| is_eof _ = false; |
44 |
||
45 |
val not_eof = not o is_eof; |
|
46 |
||
47 |
val stopper = Scan.stopper (K eof) is_eof; |
|
48 |
||
49 |
||
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
|
50 |
(* parse *) |
28434 | 51 |
|
52 |
local |
|
53 |
||
58923 | 54 |
fun parse_element keywords = |
55 |
let |
|
68842 | 56 |
fun category pred other = |
58923 | 57 |
Scan.one |
58 |
(fn (Command_Span.Span (Command_Span.Command_Span (name, _), _)) => |
|
68842 | 59 |
pred keywords name |
60 |
| _ => other); |
|
61 |
||
68843 | 62 |
fun theory_element x = |
68844 | 63 |
(category Keyword.is_theory_goal false -- proof >> element) x |
68843 | 64 |
and proof_element x = |
68844 | 65 |
(category Keyword.is_proof_goal false -- proof >> element || |
68842 | 66 |
category Keyword.is_proof_body true >> atom) x |
68844 | 67 |
and proof x = (Scan.repeat proof_element -- category Keyword.is_qed false) x; |
68843 | 68 |
|
68844 | 69 |
val default_element = Scan.one not_eof >> atom; |
70 |
in theory_element || default_element end; |
|
28434 | 71 |
|
72 |
in |
|
73 |
||
58923 | 74 |
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
|
75 |
Source.of_list #> |
58923 | 76 |
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
|
77 |
Source.exhaust; |
28434 | 78 |
|
23726 | 79 |
end; |
28434 | 80 |
|
81 |
end; |