more position information;
authorwenzelm
Sat, 17 Mar 2018 20:32:39 +0100
changeset 67895 cd00999d2d30
parent 67894 fee080c4045f
child 67896 00797fb82869
more position information;
src/Pure/Isar/token.scala
src/Pure/PIDE/command_span.scala
src/Pure/PIDE/document.scala
--- a/src/Pure/Isar/token.scala	Sat Mar 17 18:30:13 2018 +0100
+++ b/src/Pure/Isar/token.scala	Sat Mar 17 20:32:39 2018 +0100
@@ -209,11 +209,12 @@
     def column = 0
     def lineContents = ""
 
-    def advance(token: Token): Pos =
+    def advance(token: Token): Pos = advance(token.source)
+    def advance(source: String): Pos =
     {
       var line1 = line
       var offset1 = offset
-      for (s <- Symbol.iterator(token.source)) {
+      for (s <- Symbol.iterator(source)) {
         if (line1 > 0 && Symbol.is_newline(s)) line1 += 1
         if (offset1 > 0) offset1 += 1
       }
@@ -230,6 +231,7 @@
 
     def position(): Position.T = position(0)
     def position(token: Token): Position.T = position(advance(token).offset)
+    def position(source: String): Position.T = position(advance(source).offset)
 
     override def toString: String = Position.here(position(), delimited = false)
   }
--- a/src/Pure/PIDE/command_span.scala	Sat Mar 17 18:30:13 2018 +0100
+++ b/src/Pure/PIDE/command_span.scala	Sat Mar 17 20:32:39 2018 +0100
@@ -32,6 +32,13 @@
     def position: Position.T =
       kind match { case Command_Span(_, pos) => pos case _ => Position.none }
 
+    def keyword_pos(start: Token.Pos): Token.Pos =
+      kind match {
+        case _: Command_Span =>
+          (start /: content.iterator.takeWhile(tok => !tok.is_command))(_.advance(_))
+        case _ => start
+      }
+
     def is_begin: Boolean = content.exists(_.is_begin)
     def is_end: Boolean = content.exists(_.is_end)
 
--- a/src/Pure/PIDE/document.scala	Sat Mar 17 18:30:13 2018 +0100
+++ b/src/Pure/PIDE/document.scala	Sat Mar 17 20:32:39 2018 +0100
@@ -224,6 +224,17 @@
         }
       }
 
+      def starts_pos(commands: Iterator[Command], pos: Token.Pos = Token.Pos.start)
+        : Iterator[(Command, Token.Pos)] =
+      {
+        var p = pos
+        for (command <- commands) yield {
+          val start = p
+          p = (p /: command.span.content)(_.advance(_))
+          (command, start)
+        }
+      }
+
       private val block_size = 256
     }