author | wenzelm |
Mon, 01 Mar 2021 22:22:12 +0100 | |
changeset 73340 | 0ffcad1f6130 |
parent 72708 | 0cc96d337e8f |
child 74263 | be49c660ebbf |
permissions | -rw-r--r-- |
27968 | 1 |
/* Title: Pure/General/position.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Position properties. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
48409 | 10 |
import java.io.{File => JFile} |
11 |
||
12 |
||
32450 | 13 |
object Position |
14 |
{ |
|
43780 | 15 |
type T = Properties.T |
27968 | 16 |
|
55490 | 17 |
val none: T = Nil |
18 |
||
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
19 |
val Line = new Properties.Int(Markup.LINE) |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
20 |
val Offset = new Properties.Int(Markup.OFFSET) |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
21 |
val End_Offset = new Properties.Int(Markup.END_OFFSET) |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
22 |
val File = new Properties.String(Markup.FILE) |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
23 |
val Id = new Properties.Long(Markup.ID) |
59706
bf6ca55aae13
proper command id for inlined errors, which is important for Command.State.accumulate;
wenzelm
parents:
59671
diff
changeset
|
24 |
val Id_String = new Properties.String(Markup.ID) |
31705 | 25 |
|
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
26 |
val Def_Line = new Properties.Int(Markup.DEF_LINE) |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
27 |
val Def_Offset = new Properties.Int(Markup.DEF_OFFSET) |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
28 |
val Def_End_Offset = new Properties.Int(Markup.DEF_END_OFFSET) |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
29 |
val Def_File = new Properties.String(Markup.DEF_FILE) |
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49419
diff
changeset
|
30 |
val Def_Id = new Properties.Long(Markup.DEF_ID) |
49419
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
31 |
|
48920 | 32 |
object Line_File |
33 |
{ |
|
56464 | 34 |
def apply(line: Int, file: String): T = |
35 |
(if (line > 0) Line(line) else Nil) ::: |
|
36 |
(if (file != "") File(file) else Nil) |
|
37 |
||
48920 | 38 |
def unapply(pos: T): Option[(Int, String)] = |
39 |
(pos, pos) match { |
|
40 |
case (Line(i), File(name)) => Some((i, name)) |
|
41 |
case (_, File(name)) => Some((1, name)) |
|
42 |
case _ => None |
|
43 |
} |
|
44 |
} |
|
45 |
||
49419
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
46 |
object Def_Line_File |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
47 |
{ |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
48 |
def unapply(pos: T): Option[(Int, String)] = |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
49 |
(pos, pos) match { |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
50 |
case (Def_Line(i), Def_File(name)) => Some((i, name)) |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
51 |
case (_, Def_File(name)) => Some((1, name)) |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
52 |
case _ => None |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
53 |
} |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
54 |
} |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
55 |
|
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38568
diff
changeset
|
56 |
object Range |
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38568
diff
changeset
|
57 |
{ |
57910 | 58 |
def apply(range: Symbol.Range): T = Offset(range.start) ::: End_Offset(range.stop) |
55884
f2c0eaedd579
tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents:
55555
diff
changeset
|
59 |
def unapply(pos: T): Option[Symbol.Range] = |
48920 | 60 |
(pos, pos) match { |
61 |
case (Offset(start), End_Offset(stop)) if start <= stop => Some(Text.Range(start, stop)) |
|
62 |
case (Offset(start), _) => Some(Text.Range(start, start + 1)) |
|
63 |
case _ => None |
|
64 |
} |
|
65 |
} |
|
66 |
||
64662 | 67 |
object Item_Id |
48920 | 68 |
{ |
64667 | 69 |
def unapply(pos: T): Option[(Long, Symbol.Range)] = |
57931 | 70 |
pos match { |
64667 | 71 |
case Id(id) => |
72 |
val offset = Offset.unapply(pos) getOrElse 0 |
|
73 |
val end_offset = End_Offset.unapply(pos) getOrElse (offset + 1) |
|
74 |
Some((id, Text.Range(offset, end_offset))) |
|
38722
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38568
diff
changeset
|
75 |
case _ => None |
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38568
diff
changeset
|
76 |
} |
ba31936497c2
organized markup properties via apply/unapply patterns;
wenzelm
parents:
38568
diff
changeset
|
77 |
} |
38872 | 78 |
|
64662 | 79 |
object Item_Def_Id |
49419
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
80 |
{ |
64667 | 81 |
def unapply(pos: T): Option[(Long, Symbol.Range)] = |
57931 | 82 |
pos match { |
64667 | 83 |
case Def_Id(id) => |
84 |
val offset = Def_Offset.unapply(pos) getOrElse 0 |
|
85 |
val end_offset = Def_End_Offset.unapply(pos) getOrElse (offset + 1) |
|
86 |
Some((id, Text.Range(offset, end_offset))) |
|
49419
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
87 |
case _ => None |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
88 |
} |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
89 |
} |
e2726211f834
pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents:
48992
diff
changeset
|
90 |
|
64662 | 91 |
object Item_File |
92 |
{ |
|
64667 | 93 |
def unapply(pos: T): Option[(String, Int, Symbol.Range)] = |
64662 | 94 |
pos match { |
95 |
case Line_File(line, name) => |
|
64667 | 96 |
val offset = Offset.unapply(pos) getOrElse 0 |
97 |
val end_offset = End_Offset.unapply(pos) getOrElse (offset + 1) |
|
98 |
Some((name, line, Text.Range(offset, end_offset))) |
|
64662 | 99 |
case _ => None |
100 |
} |
|
101 |
} |
|
102 |
||
103 |
object Item_Def_File |
|
104 |
{ |
|
64667 | 105 |
def unapply(pos: T): Option[(String, Int, Symbol.Range)] = |
64662 | 106 |
pos match { |
107 |
case Def_Line_File(line, name) => |
|
64667 | 108 |
val offset = Def_Offset.unapply(pos) getOrElse 0 |
109 |
val end_offset = Def_End_Offset.unapply(pos) getOrElse (offset + 1) |
|
110 |
Some((name, line, Text.Range(offset, end_offset))) |
|
64662 | 111 |
case _ => None |
112 |
} |
|
113 |
} |
|
114 |
||
48370 | 115 |
|
56532
3da244bc02bd
tuned message, to accommodate extra brackets produced by Scala parsers;
wenzelm
parents:
56473
diff
changeset
|
116 |
/* here: user output */ |
48992 | 117 |
|
65402 | 118 |
def here(props: T, delimited: Boolean = true): String = |
64728 | 119 |
{ |
72708 | 120 |
val pos = props.filter(Markup.position_property) |
64728 | 121 |
if (pos.isEmpty) "" |
122 |
else { |
|
123 |
val s0 = |
|
124 |
(Line.unapply(pos), File.unapply(pos)) match { |
|
125 |
case (Some(i), None) => "line " + i.toString |
|
126 |
case (Some(i), Some(name)) => "line " + i.toString + " of " + quote(name) |
|
127 |
case (None, Some(name)) => "file " + quote(name) |
|
128 |
case _ => "" |
|
129 |
} |
|
68287 | 130 |
val s = if (s0 == "") s0 else if (delimited) " (" + s0 + ")" else s0 |
64728 | 131 |
Markup(Markup.POSITION, pos).markup(s) |
132 |
} |
|
133 |
} |
|
67882 | 134 |
|
135 |
||
136 |
/* JSON representation */ |
|
137 |
||
138 |
object JSON |
|
139 |
{ |
|
140 |
def apply(pos: T): isabelle.JSON.Object.T = |
|
141 |
isabelle.JSON.Object.empty ++ |
|
142 |
Line.unapply(pos).map(Line.name -> _) ++ |
|
143 |
Offset.unapply(pos).map(Offset.name -> _) ++ |
|
144 |
End_Offset.unapply(pos).map(End_Offset.name -> _) ++ |
|
145 |
File.unapply(pos).map(File.name -> _) ++ |
|
146 |
Id.unapply(pos).map(Id.name -> _) |
|
147 |
||
148 |
def unapply(json: isabelle.JSON.T): Option[T] = |
|
149 |
for { |
|
150 |
line <- isabelle.JSON.int_default(json, Line.name) |
|
151 |
offset <- isabelle.JSON.int_default(json, Offset.name) |
|
152 |
end_offset <- isabelle.JSON.int_default(json, End_Offset.name) |
|
153 |
file <- isabelle.JSON.string_default(json, File.name) |
|
154 |
id <- isabelle.JSON.long_default(json, Id.name) |
|
155 |
} |
|
156 |
yield { |
|
157 |
def defined(name: String): Boolean = isabelle.JSON.value(json, name).isDefined |
|
158 |
(if (defined(Line.name)) Line(line) else Nil) ::: |
|
159 |
(if (defined(Offset.name)) Offset(offset) else Nil) ::: |
|
160 |
(if (defined(End_Offset.name)) End_Offset(end_offset) else Nil) ::: |
|
161 |
(if (defined(File.name)) File(file) else Nil) ::: |
|
162 |
(if (defined(Id.name)) Id(id) else Nil) |
|
163 |
} |
|
164 |
} |
|
27968 | 165 |
} |