src/Pure/General/position.scala
author wenzelm
Tue, 10 Mar 2015 20:12:30 +0100
changeset 59671 9715eb8e9408
parent 57931 4e2cbff02f23
child 59706 bf6ca55aae13
permissions -rw-r--r--
more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27968
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/position.scala
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     3
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     4
Position properties.
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     5
*/
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     6
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     7
package isabelle
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     8
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
     9
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48370
diff changeset
    10
import java.io.{File => JFile}
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48370
diff changeset
    11
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48370
diff changeset
    12
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31705
diff changeset
    13
object Position
375db037f4d2 misc tuning;
wenzelm
parents: 31705
diff changeset
    14
{
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents: 43710
diff changeset
    15
  type T = Properties.T
27968
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
    16
55490
9b0fb0e2c9f5 updated thy_info.dependencies;
wenzelm
parents: 55429
diff changeset
    17
  val none: T = Nil
9b0fb0e2c9f5 updated thy_info.dependencies;
wenzelm
parents: 55429
diff changeset
    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)
31705
0c83e3e75fcf replaced java Properties by pure property lists;
wenzelm
parents: 29140
diff changeset
    24
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49419
diff changeset
    25
  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
    26
  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
    27
  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
    28
  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
    29
  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
    30
48920
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    31
  object Line_File
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    32
  {
56464
555f4be59be6 more precise token positions;
wenzelm
parents: 56462
diff changeset
    33
    def apply(line: Int, file: String): T =
555f4be59be6 more precise token positions;
wenzelm
parents: 56462
diff changeset
    34
      (if (line > 0) Line(line) else Nil) :::
555f4be59be6 more precise token positions;
wenzelm
parents: 56462
diff changeset
    35
      (if (file != "") File(file) else Nil)
555f4be59be6 more precise token positions;
wenzelm
parents: 56462
diff changeset
    36
48920
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    37
    def unapply(pos: T): Option[(Int, String)] =
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    38
      (pos, pos) match {
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    39
        case (Line(i), File(name)) => Some((i, name))
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    40
        case (_, File(name)) => Some((1, name))
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    41
        case _ => None
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    42
      }
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    43
  }
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    44
49419
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    45
  object Def_Line_File
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    46
  {
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    47
    def unapply(pos: T): Option[(Int, String)] =
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    48
      (pos, pos) match {
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    49
        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
    50
        case (_, Def_File(name)) => Some((1, name))
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    51
        case _ => None
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    52
      }
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
38722
ba31936497c2 organized markup properties via apply/unapply patterns;
wenzelm
parents: 38568
diff changeset
    55
  object Range
ba31936497c2 organized markup properties via apply/unapply patterns;
wenzelm
parents: 38568
diff changeset
    56
  {
57910
a50837b637dc maintain Command_Range position as in ML;
wenzelm
parents: 56746
diff changeset
    57
    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
    58
    def unapply(pos: T): Option[Symbol.Range] =
48920
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    59
      (pos, pos) match {
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    60
        case (Offset(start), End_Offset(stop)) if start <= stop => Some(Text.Range(start, stop))
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    61
        case (Offset(start), _) => Some(Text.Range(start, start + 1))
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    62
        case _ => None
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    63
      }
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    64
  }
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    65
57931
wenzelm
parents: 57911
diff changeset
    66
  object Id_Offset0
48920
9f84d872feba tuned signature;
wenzelm
parents: 48548
diff changeset
    67
  {
55884
f2c0eaedd579 tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents: 55555
diff changeset
    68
    def unapply(pos: T): Option[(Long, Symbol.Offset)] =
57931
wenzelm
parents: 57911
diff changeset
    69
      pos match {
wenzelm
parents: 57911
diff changeset
    70
        case Id(id) => Some((id, Offset.unapply(pos) getOrElse 0))
38722
ba31936497c2 organized markup properties via apply/unapply patterns;
wenzelm
parents: 38568
diff changeset
    71
        case _ => None
ba31936497c2 organized markup properties via apply/unapply patterns;
wenzelm
parents: 38568
diff changeset
    72
      }
ba31936497c2 organized markup properties via apply/unapply patterns;
wenzelm
parents: 38568
diff changeset
    73
  }
38872
26c505765024 Command.results: ordered by serial number;
wenzelm
parents: 38722
diff changeset
    74
57931
wenzelm
parents: 57911
diff changeset
    75
  object Def_Id_Offset0
49419
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    76
  {
55884
f2c0eaedd579 tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents: 55555
diff changeset
    77
    def unapply(pos: T): Option[(Long, Symbol.Offset)] =
57931
wenzelm
parents: 57911
diff changeset
    78
      pos match {
wenzelm
parents: 57911
diff changeset
    79
        case Def_Id(id) => Some((id, Def_Offset.unapply(pos) getOrElse 0))
49419
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    80
        case _ => None
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    81
      }
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    82
  }
e2726211f834 pass base_snapshot to enable hyperlinks into other nodes;
wenzelm
parents: 48992
diff changeset
    83
57911
dcb758188aa6 clarified Position.Identified: do not require range from prover, default to command position;
wenzelm
parents: 57910
diff changeset
    84
  object Identified
38887
1261481ef5e5 Command.State: add reported positions to markup tree, according main message position or Markup.binding/entity/report occurrences in body;
wenzelm
parents: 38872
diff changeset
    85
  {
57911
dcb758188aa6 clarified Position.Identified: do not require range from prover, default to command position;
wenzelm
parents: 57910
diff changeset
    86
    def unapply(pos: T): Option[(Long, Symbol.Text_Chunk.Name)] =
dcb758188aa6 clarified Position.Identified: do not require range from prover, default to command position;
wenzelm
parents: 57910
diff changeset
    87
      pos match {
dcb758188aa6 clarified Position.Identified: do not require range from prover, default to command position;
wenzelm
parents: 57910
diff changeset
    88
        case Id(id) =>
56462
b64b0cb845fe more explicit Command.Chunk types, less ooddities;
wenzelm
parents: 55884
diff changeset
    89
          val chunk_name =
b64b0cb845fe more explicit Command.Chunk types, less ooddities;
wenzelm
parents: 55884
diff changeset
    90
            pos match {
56746
d37a5d09a277 tuned signature;
wenzelm
parents: 56532
diff changeset
    91
              case File(name) => Symbol.Text_Chunk.File(name)
d37a5d09a277 tuned signature;
wenzelm
parents: 56532
diff changeset
    92
              case _ => Symbol.Text_Chunk.Default
56462
b64b0cb845fe more explicit Command.Chunk types, less ooddities;
wenzelm
parents: 55884
diff changeset
    93
            }
57911
dcb758188aa6 clarified Position.Identified: do not require range from prover, default to command position;
wenzelm
parents: 57910
diff changeset
    94
          Some((id, chunk_name))
38887
1261481ef5e5 Command.State: add reported positions to markup tree, according main message position or Markup.binding/entity/report occurrences in body;
wenzelm
parents: 38872
diff changeset
    95
        case _ => None
1261481ef5e5 Command.State: add reported positions to markup tree, according main message position or Markup.binding/entity/report occurrences in body;
wenzelm
parents: 38872
diff changeset
    96
      }
1261481ef5e5 Command.State: add reported positions to markup tree, according main message position or Markup.binding/entity/report occurrences in body;
wenzelm
parents: 38872
diff changeset
    97
  }
1261481ef5e5 Command.State: add reported positions to markup tree, according main message position or Markup.binding/entity/report occurrences in body;
wenzelm
parents: 38872
diff changeset
    98
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49419
diff changeset
    99
  def purge(props: T): T = props.filterNot(p => Markup.POSITION_PROPERTIES(p._1))
48370
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 45666
diff changeset
   100
d0fa3efec93b require explicit initialization of options;
wenzelm
parents: 45666
diff changeset
   101
56532
3da244bc02bd tuned message, to accommodate extra brackets produced by Scala parsers;
wenzelm
parents: 56473
diff changeset
   102
  /* here: user output */
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48920
diff changeset
   103
59671
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   104
  def yxml_markup(pos: T, str: String): String =
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   105
    YXML.string_of_tree(XML.Elem(Markup(Markup.POSITION, pos), List(XML.Text(str))))
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   106
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48920
diff changeset
   107
  def here(pos: T): String =
59671
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   108
    yxml_markup(pos,
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   109
      (Line.unapply(pos), File.unapply(pos)) match {
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   110
        case (Some(i), None) => " (line " + i.toString + ")"
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   111
        case (Some(i), Some(name)) => " (line " + i.toString + " of " + quote(name) + ")"
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   112
        case (None, Some(name)) => " (file " + quote(name) + ")"
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   113
        case _ => ""
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   114
      })
56532
3da244bc02bd tuned message, to accommodate extra brackets produced by Scala parsers;
wenzelm
parents: 56473
diff changeset
   115
3da244bc02bd tuned message, to accommodate extra brackets produced by Scala parsers;
wenzelm
parents: 56473
diff changeset
   116
  def here_undelimited(pos: T): String =
59671
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   117
    yxml_markup(pos,
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   118
      (Line.unapply(pos), File.unapply(pos)) match {
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   119
        case (Some(i), None) => "line " + i.toString
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   120
        case (Some(i), Some(name)) => "line " + i.toString + " of " + quote(name)
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   121
        case (None, Some(name)) => "file " + quote(name)
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   122
        case _ => ""
9715eb8e9408 more precise position information in Isabelle/Scala, with YXML markup as in Isabelle/ML;
wenzelm
parents: 57931
diff changeset
   123
      })
27968
85b5f024d94b Position properties.
wenzelm
parents:
diff changeset
   124
}