| author | wenzelm |
| Fri, 27 Nov 2020 11:41:43 +0100 | |
| changeset 72740 | 082200ee003d |
| parent 72692 | 22aeec526ffd |
| child 72742 | bda424c5819f |
| permissions | -rw-r--r-- |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/PIDE/command_span.scala |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
3 |
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
4 |
Syntactic representation of command spans. |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
6 |
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
8 |
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
9 |
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
10 |
import scala.collection.mutable |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
11 |
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
12 |
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
13 |
object Command_Span |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
14 |
{
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
15 |
sealed abstract class Kind {
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
16 |
override def toString: String = |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
17 |
this match {
|
| 65717 | 18 |
case Command_Span(name, _) => proper_string(name) getOrElse "<command>" |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
19 |
case Ignored_Span => "<ignored>" |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
20 |
case Malformed_Span => "<malformed>" |
|
72692
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
21 |
case Theory_Span => "<theory>" |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
22 |
} |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
23 |
} |
| 57910 | 24 |
case class Command_Span(name: String, pos: Position.T) extends Kind |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
25 |
case object Ignored_Span extends Kind |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
26 |
case object Malformed_Span extends Kind |
|
72692
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
27 |
case object Theory_Span extends Kind |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
28 |
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
29 |
sealed case class Span(kind: Kind, content: List[Token]) |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
30 |
{
|
|
72692
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
31 |
def is_theory: Boolean = kind == Theory_Span |
|
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
32 |
|
| 59735 | 33 |
def name: String = |
34 |
kind match { case Command_Span(name, _) => name case _ => "" }
|
|
| 58802 | 35 |
|
| 59735 | 36 |
def position: Position.T = |
37 |
kind match { case Command_Span(_, pos) => pos case _ => Position.none }
|
|
38 |
||
| 67895 | 39 |
def keyword_pos(start: Token.Pos): Token.Pos = |
40 |
kind match {
|
|
41 |
case _: Command_Span => |
|
42 |
(start /: content.iterator.takeWhile(tok => !tok.is_command))(_.advance(_)) |
|
43 |
case _ => start |
|
44 |
} |
|
45 |
||
|
68845
3b2daa7bf9f4
support Thy_Element in Scala, following ML version;
wenzelm
parents:
68840
diff
changeset
|
46 |
def is_kind(keywords: Keyword.Keywords, pred: String => Boolean, other: Boolean): Boolean = |
| 68840 | 47 |
keywords.kinds.get(name) match {
|
48 |
case Some(k) => pred(k) |
|
|
68845
3b2daa7bf9f4
support Thy_Element in Scala, following ML version;
wenzelm
parents:
68840
diff
changeset
|
49 |
case None => other |
| 68840 | 50 |
} |
51 |
||
| 63606 | 52 |
def is_begin: Boolean = content.exists(_.is_begin) |
53 |
def is_end: Boolean = content.exists(_.is_end) |
|
54 |
||
| 59735 | 55 |
def length: Int = (0 /: content)(_ + _.source.length) |
| 59705 | 56 |
|
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
57 |
def compact_source: (String, Span) = |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
58 |
{
|
| 59735 | 59 |
val source = Token.implode(content) |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
60 |
val content1 = new mutable.ListBuffer[Token] |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
61 |
var i = 0 |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
62 |
for (Token(kind, s) <- content) {
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
63 |
val n = s.length |
| 59735 | 64 |
val s1 = source.substring(i, i + n) |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
65 |
content1 += Token(kind, s1) |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
66 |
i += n |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
67 |
} |
| 59735 | 68 |
(source, Span(kind, content1.toList)) |
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
69 |
} |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
70 |
} |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
71 |
|
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
72 |
val empty: Span = Span(Ignored_Span, Nil) |
|
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
73 |
|
|
72692
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
74 |
def unparsed(source: String, theory: Boolean): Span = |
|
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
75 |
{
|
|
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
76 |
val kind = if (theory) Theory_Span else Malformed_Span |
|
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
77 |
Span(kind, List(Token(Token.Kind.UNPARSED, source))) |
|
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
68845
diff
changeset
|
78 |
} |
| 65335 | 79 |
|
80 |
||
81 |
/* XML data representation */ |
|
82 |
||
83 |
def encode: XML.Encode.T[Span] = (span: Span) => |
|
84 |
{
|
|
85 |
import XML.Encode._ |
|
86 |
val kind: T[Kind] = |
|
87 |
variant(List( |
|
88 |
{ case Command_Span(a, b) => (List(a), properties(b)) },
|
|
89 |
{ case Ignored_Span => (Nil, Nil) },
|
|
90 |
{ case Malformed_Span => (Nil, Nil) }))
|
|
91 |
pair(kind, list(Token.encode))((span.kind, span.content)) |
|
92 |
} |
|
93 |
||
94 |
def decode: XML.Decode.T[Span] = (body: XML.Body) => |
|
95 |
{
|
|
96 |
import XML.Decode._ |
|
97 |
val kind: T[Kind] = |
|
98 |
variant(List( |
|
99 |
{ case (List(a), b) => Command_Span(a, properties(b)) },
|
|
100 |
{ case (Nil, Nil) => Ignored_Span },
|
|
101 |
{ case (Nil, Nil) => Malformed_Span }))
|
|
102 |
val (k, toks) = pair(kind, list(Token.decode))(body) |
|
103 |
Span(k, toks) |
|
104 |
} |
|
|
57905
c0c5652e796e
separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff
changeset
|
105 |
} |