| author | wenzelm | 
| Sat, 27 May 2017 12:52:36 +0200 | |
| changeset 65944 | 79e4d94aa9ad | 
| parent 61379 | c57820ceead3 | 
| permissions | -rw-r--r-- | 
| 29315 
b074c05f00ad
renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
 wenzelm parents: 
28454diff
changeset | 1 | (* Title: Pure/Thy/thy_syntax.ML | 
| 23726 | 2 | Author: Makarius | 
| 3 | ||
| 61379 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
59809diff
changeset | 4 | Theory syntax elements. | 
| 23726 | 5 | *) | 
| 6 | ||
| 29315 
b074c05f00ad
renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
 wenzelm parents: 
28454diff
changeset | 7 | signature THY_SYNTAX = | 
| 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: 
51267diff
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 | ||
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
37197diff
changeset | 17 | structure Thy_Syntax: THY_SYNTAX = | 
| 23726 | 18 | struct | 
| 19 | ||
| 61379 
c57820ceead3
more direct HTML presentation, without print mode;
 wenzelm parents: 
59809diff
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: 
51267diff
changeset | 34 | fun last_element (Element (a, NONE)) = a | 
| 
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
 wenzelm parents: 
51267diff
changeset | 35 | | last_element (Element (_, SOME (_, b))) = b; | 
| 
fcc4b89a600d
simplified Outer_Syntax.read_span: internalized Toplevel.is_ignored;
 wenzelm parents: 
51267diff
changeset | 36 | |
| 28434 | 37 | |
| 38 | (* scanning spans *) | |
| 39 | ||
| 57905 
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
 wenzelm parents: 
57903diff
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: 
57903diff
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: 
45666diff
changeset | 50 | (* parse *) | 
| 28434 | 51 | |
| 52 | local | |
| 53 | ||
| 48878 | 54 | fun command_with pred = | 
| 57905 
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
 wenzelm parents: 
57903diff
changeset | 55 | Scan.one | 
| 
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
 wenzelm parents: 
57903diff
changeset | 56 | (fn (Command_Span.Span (Command_Span.Command_Span (name, _), _)) => pred name | _ => false); | 
| 28434 | 57 | |
| 58923 | 58 | fun parse_element keywords = | 
| 59 | let | |
| 60 | val proof_atom = | |
| 61 | Scan.one | |
| 62 | (fn (Command_Span.Span (Command_Span.Command_Span (name, _), _)) => | |
| 63 | Keyword.is_proof_body keywords name | |
| 64 | | _ => true) >> atom; | |
| 65 | fun proof_element x = | |
| 66 | (command_with (Keyword.is_proof_goal keywords) -- proof_rest >> element || proof_atom) x | |
| 67 | and proof_rest x = | |
| 68 | (Scan.repeat proof_element -- command_with (Keyword.is_qed keywords)) x; | |
| 69 | in | |
| 70 | command_with (Keyword.is_theory_goal keywords) -- proof_rest >> element || | |
| 71 | Scan.one not_eof >> atom | |
| 72 | end; | |
| 28434 | 73 | |
| 74 | in | |
| 75 | ||
| 58923 | 76 | 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: 
45666diff
changeset | 77 | Source.of_list #> | 
| 58923 | 78 | 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: 
45666diff
changeset | 79 | Source.exhaust; | 
| 28434 | 80 | |
| 23726 | 81 | end; | 
| 28434 | 82 | |
| 83 | end; |