src/Pure/General/position.scala
author wenzelm
Wed Aug 13 22:29:43 2014 +0200 (2014-08-13)
changeset 57931 4e2cbff02f23
parent 57911 dcb758188aa6
child 59671 9715eb8e9408
permissions -rw-r--r--
tuned;
wenzelm@27968
     1
/*  Title:      Pure/General/position.scala
wenzelm@27968
     2
    Author:     Makarius
wenzelm@27968
     3
wenzelm@27968
     4
Position properties.
wenzelm@27968
     5
*/
wenzelm@27968
     6
wenzelm@27968
     7
package isabelle
wenzelm@27968
     8
wenzelm@27968
     9
wenzelm@48409
    10
import java.io.{File => JFile}
wenzelm@48409
    11
wenzelm@48409
    12
wenzelm@32450
    13
object Position
wenzelm@32450
    14
{
wenzelm@43780
    15
  type T = Properties.T
wenzelm@27968
    16
wenzelm@55490
    17
  val none: T = Nil
wenzelm@55490
    18
wenzelm@50201
    19
  val Line = new Properties.Int(Markup.LINE)
wenzelm@50201
    20
  val Offset = new Properties.Int(Markup.OFFSET)
wenzelm@50201
    21
  val End_Offset = new Properties.Int(Markup.END_OFFSET)
wenzelm@50201
    22
  val File = new Properties.String(Markup.FILE)
wenzelm@50201
    23
  val Id = new Properties.Long(Markup.ID)
wenzelm@31705
    24
wenzelm@50201
    25
  val Def_Line = new Properties.Int(Markup.DEF_LINE)
wenzelm@50201
    26
  val Def_Offset = new Properties.Int(Markup.DEF_OFFSET)
wenzelm@50201
    27
  val Def_End_Offset = new Properties.Int(Markup.DEF_END_OFFSET)
wenzelm@50201
    28
  val Def_File = new Properties.String(Markup.DEF_FILE)
wenzelm@50201
    29
  val Def_Id = new Properties.Long(Markup.DEF_ID)
wenzelm@49419
    30
wenzelm@48920
    31
  object Line_File
wenzelm@48920
    32
  {
wenzelm@56464
    33
    def apply(line: Int, file: String): T =
wenzelm@56464
    34
      (if (line > 0) Line(line) else Nil) :::
wenzelm@56464
    35
      (if (file != "") File(file) else Nil)
wenzelm@56464
    36
wenzelm@48920
    37
    def unapply(pos: T): Option[(Int, String)] =
wenzelm@48920
    38
      (pos, pos) match {
wenzelm@48920
    39
        case (Line(i), File(name)) => Some((i, name))
wenzelm@48920
    40
        case (_, File(name)) => Some((1, name))
wenzelm@48920
    41
        case _ => None
wenzelm@48920
    42
      }
wenzelm@48920
    43
  }
wenzelm@48920
    44
wenzelm@49419
    45
  object Def_Line_File
wenzelm@49419
    46
  {
wenzelm@49419
    47
    def unapply(pos: T): Option[(Int, String)] =
wenzelm@49419
    48
      (pos, pos) match {
wenzelm@49419
    49
        case (Def_Line(i), Def_File(name)) => Some((i, name))
wenzelm@49419
    50
        case (_, Def_File(name)) => Some((1, name))
wenzelm@49419
    51
        case _ => None
wenzelm@49419
    52
      }
wenzelm@49419
    53
  }
wenzelm@49419
    54
wenzelm@38722
    55
  object Range
wenzelm@38722
    56
  {
wenzelm@57910
    57
    def apply(range: Symbol.Range): T = Offset(range.start) ::: End_Offset(range.stop)
wenzelm@55884
    58
    def unapply(pos: T): Option[Symbol.Range] =
wenzelm@48920
    59
      (pos, pos) match {
wenzelm@48920
    60
        case (Offset(start), End_Offset(stop)) if start <= stop => Some(Text.Range(start, stop))
wenzelm@48920
    61
        case (Offset(start), _) => Some(Text.Range(start, start + 1))
wenzelm@48920
    62
        case _ => None
wenzelm@48920
    63
      }
wenzelm@48920
    64
  }
wenzelm@48920
    65
wenzelm@57931
    66
  object Id_Offset0
wenzelm@48920
    67
  {
wenzelm@55884
    68
    def unapply(pos: T): Option[(Long, Symbol.Offset)] =
wenzelm@57931
    69
      pos match {
wenzelm@57931
    70
        case Id(id) => Some((id, Offset.unapply(pos) getOrElse 0))
wenzelm@38722
    71
        case _ => None
wenzelm@38722
    72
      }
wenzelm@38722
    73
  }
wenzelm@38872
    74
wenzelm@57931
    75
  object Def_Id_Offset0
wenzelm@49419
    76
  {
wenzelm@55884
    77
    def unapply(pos: T): Option[(Long, Symbol.Offset)] =
wenzelm@57931
    78
      pos match {
wenzelm@57931
    79
        case Def_Id(id) => Some((id, Def_Offset.unapply(pos) getOrElse 0))
wenzelm@49419
    80
        case _ => None
wenzelm@49419
    81
      }
wenzelm@49419
    82
  }
wenzelm@49419
    83
wenzelm@57911
    84
  object Identified
wenzelm@38887
    85
  {
wenzelm@57911
    86
    def unapply(pos: T): Option[(Long, Symbol.Text_Chunk.Name)] =
wenzelm@57911
    87
      pos match {
wenzelm@57911
    88
        case Id(id) =>
wenzelm@56462
    89
          val chunk_name =
wenzelm@56462
    90
            pos match {
wenzelm@56746
    91
              case File(name) => Symbol.Text_Chunk.File(name)
wenzelm@56746
    92
              case _ => Symbol.Text_Chunk.Default
wenzelm@56462
    93
            }
wenzelm@57911
    94
          Some((id, chunk_name))
wenzelm@38887
    95
        case _ => None
wenzelm@38887
    96
      }
wenzelm@38887
    97
  }
wenzelm@38887
    98
wenzelm@50201
    99
  def purge(props: T): T = props.filterNot(p => Markup.POSITION_PROPERTIES(p._1))
wenzelm@48370
   100
wenzelm@48370
   101
wenzelm@56532
   102
  /* here: user output */
wenzelm@48992
   103
wenzelm@48992
   104
  def here(pos: T): String =
wenzelm@48920
   105
    (Line.unapply(pos), File.unapply(pos)) match {
wenzelm@48370
   106
      case (Some(i), None) => " (line " + i.toString + ")"
wenzelm@48370
   107
      case (Some(i), Some(name)) => " (line " + i.toString + " of " + quote(name) + ")"
wenzelm@48370
   108
      case (None, Some(name)) => " (file " + quote(name) + ")"
wenzelm@48370
   109
      case _ => ""
wenzelm@48370
   110
    }
wenzelm@56532
   111
wenzelm@56532
   112
  def here_undelimited(pos: T): String =
wenzelm@56532
   113
    (Line.unapply(pos), File.unapply(pos)) match {
wenzelm@56532
   114
      case (Some(i), None) => "line " + i.toString
wenzelm@56532
   115
      case (Some(i), Some(name)) => "line " + i.toString + " of " + quote(name)
wenzelm@56532
   116
      case (None, Some(name)) => "file " + quote(name)
wenzelm@56532
   117
      case _ => ""
wenzelm@56532
   118
    }
wenzelm@27968
   119
}