| author | wenzelm | 
| Sun, 25 Oct 2009 19:18:59 +0100 | |
| changeset 33167 | f02b804305d6 | 
| parent 32450 | 375db037f4d2 | 
| child 34211 | 686f828548ef | 
| 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 | ||
| 32450 | 10 | object Position | 
| 11 | {
 | |
| 31705 | 12 | type T = List[(String, String)] | 
| 27968 | 13 | |
| 31705 | 14 | private def get_string(name: String, pos: T): Option[String] = | 
| 15 | pos.find(p => p._1 == name).map(_._2) | |
| 16 | ||
| 17 | private def get_int(name: String, pos: T): Option[Int] = | |
| 18 |   {
 | |
| 19 |     get_string(name, pos) match {
 | |
| 27968 | 20 | case None => None | 
| 21 | case Some(value) => | |
| 22 |         try { Some(Integer.parseInt(value)) }
 | |
| 27993 
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
 wenzelm parents: 
27989diff
changeset | 23 |         catch { case _: NumberFormatException => None }
 | 
| 27968 | 24 | } | 
| 25 | } | |
| 26 | ||
| 31705 | 27 | def line_of(pos: T) = get_int(Markup.LINE, pos) | 
| 28 | def column_of(pos: T) = get_int(Markup.COLUMN, pos) | |
| 29 | def offset_of(pos: T) = get_int(Markup.OFFSET, pos) | |
| 30 | def end_line_of(pos: T) = get_int(Markup.END_LINE, pos) | |
| 31 | def end_column_of(pos: T) = get_int(Markup.END_COLUMN, pos) | |
| 32 | def end_offset_of(pos: T) = get_int(Markup.END_OFFSET, pos) | |
| 33 | def file_of(pos: T) = get_string(Markup.FILE, pos) | |
| 34 | def id_of(pos: T) = get_string(Markup.ID, pos) | |
| 35 | ||
| 36 | def offsets_of(pos: T): (Option[Int], Option[Int]) = | |
| 37 |   {
 | |
| 38 | val begin = offset_of(pos) | |
| 39 | val end = end_offset_of(pos) | |
| 40 | (begin, if (end.isDefined) end else begin.map(_ + 1)) | |
| 41 | } | |
| 27968 | 42 | } |