src/Pure/PIDE/command.scala
author wenzelm
Sun Mar 10 14:19:30 2019 +0100 (4 months ago ago)
changeset 70070 be04e9a053a7
parent 70068 b9985133805d
child 70072 def3ec9cdb7e
permissions -rw-r--r--
markup and document markers for some meta data from "Dublin Core Metadata Element Set";
     1 /*  Title:      Pure/PIDE/command.scala
     2     Author:     Fabian Immler, TU Munich
     3     Author:     Makarius
     4 
     5 Prover commands with accumulated results from execution.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import scala.collection.mutable
    12 import scala.collection.immutable.SortedMap
    13 
    14 
    15 object Command
    16 {
    17   type Edit = (Option[Command], Option[Command])
    18 
    19   type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, Symbol.Text_Chunk)])]
    20   type Blobs_Info = (List[Blob], Int)
    21   val no_blobs: Blobs_Info = (Nil, -1)
    22 
    23 
    24   /** accumulated results from prover **/
    25 
    26   /* results */
    27 
    28   object Results
    29   {
    30     type Entry = (Long, XML.Tree)
    31     val empty: Results = new Results(SortedMap.empty)
    32     def make(args: TraversableOnce[Results.Entry]): Results = (empty /: args)(_ + _)
    33     def merge(args: TraversableOnce[Results]): Results = (empty /: args)(_ ++ _)
    34 
    35 
    36     /* XML data representation */
    37 
    38     val encode: XML.Encode.T[Results] = (results: Results) =>
    39     { import XML.Encode._; list(pair(long, tree))(results.rep.toList) }
    40 
    41     val decode: XML.Decode.T[Results] = (body: XML.Body) =>
    42     { import XML.Decode._; make(list(pair(long, tree))(body)) }
    43   }
    44 
    45   final class Results private(private val rep: SortedMap[Long, XML.Tree])
    46   {
    47     def is_empty: Boolean = rep.isEmpty
    48     def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
    49     def get(serial: Long): Option[XML.Tree] = rep.get(serial)
    50     def iterator: Iterator[Results.Entry] = rep.iterator
    51 
    52     def + (entry: Results.Entry): Results =
    53       if (defined(entry._1)) this
    54       else new Results(rep + entry)
    55 
    56     def ++ (other: Results): Results =
    57       if (this eq other) this
    58       else if (rep.isEmpty) other
    59       else (this /: other.iterator)(_ + _)
    60 
    61     override def hashCode: Int = rep.hashCode
    62     override def equals(that: Any): Boolean =
    63       that match {
    64         case other: Results => rep == other.rep
    65         case _ => false
    66       }
    67     override def toString: String = iterator.mkString("Results(", ", ", ")")
    68   }
    69 
    70 
    71   /* exports */
    72 
    73   object Exports
    74   {
    75     type Entry = (Long, Export.Entry)
    76     val empty: Exports = new Exports(SortedMap.empty)
    77     def merge(args: TraversableOnce[Exports]): Exports = (empty /: args)(_ ++ _)
    78   }
    79 
    80   final class Exports private(private val rep: SortedMap[Long, Export.Entry])
    81   {
    82     def is_empty: Boolean = rep.isEmpty
    83     def iterator: Iterator[Exports.Entry] = rep.iterator
    84 
    85     def + (entry: Exports.Entry): Exports =
    86       if (rep.isDefinedAt(entry._1)) this
    87       else new Exports(rep + entry)
    88 
    89     def ++ (other: Exports): Exports =
    90       if (this eq other) this
    91       else if (rep.isEmpty) other
    92       else (this /: other.iterator)(_ + _)
    93 
    94     override def hashCode: Int = rep.hashCode
    95     override def equals(that: Any): Boolean =
    96       that match {
    97         case other: Exports => rep == other.rep
    98         case _ => false
    99       }
   100     override def toString: String = iterator.mkString("Exports(", ", ", ")")
   101   }
   102 
   103 
   104   /* markups */
   105 
   106   object Markup_Index
   107   {
   108     val markup: Markup_Index = Markup_Index(false, Symbol.Text_Chunk.Default)
   109   }
   110 
   111   sealed case class Markup_Index(status: Boolean, chunk_name: Symbol.Text_Chunk.Name)
   112 
   113   object Markups
   114   {
   115     val empty: Markups = new Markups(Map.empty)
   116     def init(markup: Markup_Tree): Markups = new Markups(Map(Markup_Index.markup -> markup))
   117     def merge(args: TraversableOnce[Markups]): Markups = (empty /: args)(_ ++ _)
   118 
   119 
   120     /* XML data representation */
   121 
   122     def encode(source_length: Int): XML.Encode.T[Markups] = (markups: Markups) =>
   123     {
   124       import XML.Encode._
   125 
   126       val markup_index: T[Markup_Index] = (index: Markup_Index) =>
   127         pair(bool, Symbol.Text_Chunk.encode_name)(index.status, index.chunk_name)
   128 
   129       val markup_tree: T[Markup_Tree] =
   130         _.to_XML(Text.Range(0, source_length), Symbol.spaces(source_length), Markup.Elements.full)
   131 
   132       list(pair(markup_index, markup_tree))(markups.rep.toList)
   133     }
   134 
   135     val decode: XML.Decode.T[Markups] = (body: XML.Body) =>
   136     {
   137       import XML.Decode._
   138 
   139       val markup_index: T[Markup_Index] = (body: XML.Body) =>
   140       {
   141         val (status, chunk_name) = pair(bool, Symbol.Text_Chunk.decode_name)(body)
   142         Markup_Index(status, chunk_name)
   143       }
   144 
   145       (Markups.empty /: list(pair(markup_index, Markup_Tree.from_XML(_)))(body))(_ + _)
   146     }
   147   }
   148 
   149   final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
   150   {
   151     def is_empty: Boolean = rep.isEmpty
   152 
   153     def apply(index: Markup_Index): Markup_Tree =
   154       rep.getOrElse(index, Markup_Tree.empty)
   155 
   156     def add(index: Markup_Index, markup: Text.Markup): Markups =
   157       new Markups(rep + (index -> (this(index) + markup)))
   158 
   159     def + (entry: (Markup_Index, Markup_Tree)): Markups =
   160     {
   161       val (index, tree) = entry
   162       new Markups(rep + (index -> (this(index).merge(tree, Text.Range.full, Markup.Elements.full))))
   163     }
   164 
   165     def ++ (other: Markups): Markups =
   166       if (this eq other) this
   167       else if (rep.isEmpty) other
   168       else (this /: other.rep.iterator)(_ + _)
   169 
   170     def redirection_iterator: Iterator[Document_ID.Generic] =
   171       for (Markup_Index(_, Symbol.Text_Chunk.Id(id)) <- rep.keysIterator)
   172         yield id
   173 
   174     def redirect(other_id: Document_ID.Generic): Markups =
   175     {
   176       val rep1 =
   177         (for {
   178           (Markup_Index(status, Symbol.Text_Chunk.Id(id)), markup) <- rep.iterator
   179           if other_id == id
   180         } yield (Markup_Index(status, Symbol.Text_Chunk.Default), markup)).toMap
   181       if (rep1.isEmpty) Markups.empty else new Markups(rep1)
   182     }
   183 
   184     override def hashCode: Int = rep.hashCode
   185     override def equals(that: Any): Boolean =
   186       that match {
   187         case other: Markups => rep == other.rep
   188         case _ => false
   189       }
   190     override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
   191   }
   192 
   193 
   194   /* state */
   195 
   196   object State
   197   {
   198     def get_result(states: List[State], serial: Long): Option[XML.Tree] =
   199       states.find(st => st.results.defined(serial)).map(st => st.results.get(serial).get)
   200 
   201     def get_result_proper(states: List[State], props: Properties.T): Option[Results.Entry] =
   202       for {
   203         serial <- Markup.Serial.unapply(props)
   204         tree @ XML.Elem(_, body) <- get_result(states, serial)
   205         if body.nonEmpty
   206       } yield (serial -> tree)
   207 
   208     def merge_results(states: List[State]): Results =
   209       Results.merge(states.map(_.results))
   210 
   211     def merge_exports(states: List[State]): Exports =
   212       Exports.merge(states.map(_.exports))
   213 
   214     def merge_markups(states: List[State]): Markups =
   215       Markups.merge(states.map(_.markups))
   216 
   217     def merge_markup(states: List[State], index: Markup_Index,
   218         range: Text.Range, elements: Markup.Elements): Markup_Tree =
   219       Markup_Tree.merge(states.map(_.markup(index)), range, elements)
   220 
   221     def merge(command: Command, states: List[State]): State =
   222       State(command, states.flatMap(_.status), merge_results(states),
   223         merge_exports(states), merge_markups(states))
   224 
   225 
   226     /* XML data representation */
   227 
   228     val encode: XML.Encode.T[State] = (st: State) =>
   229     {
   230       import XML.Encode._
   231 
   232       val command = st.command
   233       val blobs_names = command.blobs_names.map(_.node)
   234       val blobs_index = command.blobs_index
   235       require(command.blobs_ok)
   236 
   237       pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.encode,
   238           pair(list(Markup.encode), pair(Results.encode, Markups.encode(command.source.length)))))))(
   239         (command.id, (command.node_name.node, ((blobs_names, blobs_index), (command.span,
   240           (st.status, (st.results, st.markups)))))))
   241     }
   242 
   243     def decode(node_name: String => Document.Node.Name): XML.Decode.T[State] = (body: XML.Body) =>
   244     {
   245       import XML.Decode._
   246       val (id, (node, ((blobs_names, blobs_index), (span, (status, (results, markups)))))) =
   247         pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.decode,
   248           pair(list(Markup.decode), pair(Results.decode, Markups.decode))))))(body)
   249 
   250       val blobs_info: Blobs_Info =
   251         (blobs_names.map(name => Exn.Res((node_name(name), None)): Blob), blobs_index)
   252       val command = Command(id, node_name(node), blobs_info, span)
   253       State(command, status, results, Exports.empty, markups)
   254     }
   255   }
   256 
   257   sealed case class State(
   258     command: Command,
   259     status: List[Markup] = Nil,
   260     results: Results = Results.empty,
   261     exports: Exports = Exports.empty,
   262     markups: Markups = Markups.empty)
   263   {
   264     def initialized: Boolean = status.exists(markup => markup.name == Markup.INITIALIZED)
   265     def consolidated: Boolean = status.exists(markup => markup.name == Markup.CONSOLIDATED)
   266 
   267     lazy val maybe_consolidated: Boolean =
   268     {
   269       var touched = false
   270       var forks = 0
   271       var runs = 0
   272       for (markup <- status) {
   273         markup.name match {
   274           case Markup.FORKED => touched = true; forks += 1
   275           case Markup.JOINED => forks -= 1
   276           case Markup.RUNNING => touched = true; runs += 1
   277           case Markup.FINISHED => runs -= 1
   278           case _ =>
   279         }
   280       }
   281       touched && forks == 0 && runs == 0
   282     }
   283 
   284     lazy val document_status: Document_Status.Command_Status =
   285     {
   286       val warnings =
   287         if (results.iterator.exists(p => Protocol.is_warning(p._2) || Protocol.is_legacy(p._2)))
   288           List(Markup(Markup.WARNING, Nil))
   289         else Nil
   290       val errors =
   291         if (results.iterator.exists(p => Protocol.is_error(p._2)))
   292           List(Markup(Markup.ERROR, Nil))
   293         else Nil
   294       Document_Status.Command_Status.make((warnings ::: errors ::: status).iterator)
   295     }
   296 
   297     def markup(index: Markup_Index): Markup_Tree = markups(index)
   298 
   299     def redirect(other_command: Command): Option[State] =
   300     {
   301       val markups1 = markups.redirect(other_command.id)
   302       if (markups1.is_empty) None
   303       else Some(new State(other_command, markups = markups1))
   304     }
   305 
   306     private def add_status(st: Markup): State =
   307       copy(status = st :: status)
   308 
   309     private def add_result(entry: Results.Entry): State =
   310       copy(results = results + entry)
   311 
   312     def add_export(entry: Exports.Entry): Option[State] =
   313       if (command.node_name.theory == entry._2.theory_name) Some(copy(exports = exports + entry))
   314       else None
   315 
   316     private def add_markup(
   317       status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
   318     {
   319       val markups1 =
   320         if (status || Document_Status.Command_Status.liberal_elements(m.info.name))
   321           markups.add(Markup_Index(true, chunk_name), m)
   322         else markups
   323       copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
   324     }
   325 
   326     def accumulate(
   327         self_id: Document_ID.Generic => Boolean,
   328         other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
   329         message: XML.Elem,
   330         xml_cache: XML.Cache): State =
   331       message match {
   332         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
   333           (this /: msgs)((state, msg) =>
   334             msg match {
   335               case elem @ XML.Elem(markup, Nil) =>
   336                 state.
   337                   add_status(markup).
   338                   add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.core_range, elem))
   339               case _ =>
   340                 Output.warning("Ignored status message: " + msg)
   341                 state
   342             })
   343 
   344         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
   345           (this /: msgs)((state, msg) =>
   346             {
   347               def bad(): Unit = Output.warning("Ignored report message: " + msg)
   348 
   349               msg match {
   350                 case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
   351 
   352                   val target =
   353                     if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
   354                       Some((chunk_name, command.chunks(chunk_name)))
   355                     else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
   356                     else None
   357 
   358                   (target, atts) match {
   359                     case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
   360                       target_chunk.incorporate(symbol_range) match {
   361                         case Some(range) =>
   362                           val props = Position.purge(atts)
   363                           val elem = xml_cache.elem(XML.Elem(Markup(name, props), args))
   364                           state.add_markup(false, target_name, Text.Info(range, elem))
   365                         case None => bad(); state
   366                       }
   367                     case _ =>
   368                       // silently ignore excessive reports
   369                       state
   370                   }
   371 
   372                 case XML.Elem(Markup(name, atts), args)
   373                 if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   374                   val range = command.core_range
   375                   val props = Position.purge(atts)
   376                   val elem = xml_cache.elem(XML.Elem(Markup(name, props), args))
   377                   state.add_markup(false, Symbol.Text_Chunk.Default, Text.Info(range, elem))
   378 
   379                 case _ => bad(); state
   380               }
   381             })
   382         case XML.Elem(Markup(name, props), body) =>
   383           props match {
   384             case Markup.Serial(i) =>
   385               val markup_message =
   386                 xml_cache.elem(XML.Elem(Markup(Markup.message(name), props), body))
   387               val message_markup =
   388                 xml_cache.elem(XML.elem(Markup(name, props.filter(p => p._1 == Markup.SERIAL))))
   389 
   390               var st = add_result(i -> markup_message)
   391               if (Protocol.is_inlined(message)) {
   392                 for {
   393                   (chunk_name, chunk) <- command.chunks.iterator
   394                   range <- Protocol_Message.positions(
   395                     self_id, command.span.position, chunk_name, chunk, message)
   396                 } st = st.add_markup(false, chunk_name, Text.Info(range, message_markup))
   397               }
   398               st
   399 
   400             case _ =>
   401               Output.warning("Ignored message without serial number: " + message)
   402               this
   403           }
   404       }
   405   }
   406 
   407 
   408 
   409   /** static content **/
   410 
   411   /* make commands */
   412 
   413   def apply(
   414     id: Document_ID.Command,
   415     node_name: Document.Node.Name,
   416     blobs_info: Blobs_Info,
   417     span: Command_Span.Span): Command =
   418   {
   419     val (source, span1) = span.compact_source
   420     new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
   421   }
   422 
   423   val empty: Command =
   424     Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
   425 
   426   def unparsed(
   427     id: Document_ID.Command,
   428     source: String,
   429     results: Results,
   430     markup: Markup_Tree): Command =
   431   {
   432     val (source1, span1) = Command_Span.unparsed(source).compact_source
   433     new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
   434   }
   435 
   436   def text(source: String): Command =
   437     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   438 
   439   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   440   {
   441     val text = XML.content(body)
   442     val markup = Markup_Tree.from_XML(body)
   443     unparsed(id, text, results, markup)
   444   }
   445 
   446 
   447   /* perspective */
   448 
   449   object Perspective
   450   {
   451     val empty: Perspective = Perspective(Nil)
   452   }
   453 
   454   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   455   {
   456     def is_empty: Boolean = commands.isEmpty
   457 
   458     def same(that: Perspective): Boolean =
   459     {
   460       val cmds1 = this.commands
   461       val cmds2 = that.commands
   462       require(!cmds1.exists(_.is_undefined))
   463       require(!cmds2.exists(_.is_undefined))
   464       cmds1.length == cmds2.length &&
   465         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   466     }
   467   }
   468 
   469 
   470   /* blobs: inlined errors and auxiliary files */
   471 
   472   private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
   473   {
   474     def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
   475       toks match {
   476         case (t1, i1) :: (t2, i2) :: rest =>
   477           if (t1.is_keyword && t1.source == "%" && t2.is_name) clean(rest)
   478           else if (t1.is_keyword && Symbol.is_marker(t1.source) && t2.is_embedded) clean(rest)
   479           else (t1, i1) :: clean((t2, i2) :: rest)
   480         case _ => toks
   481       }
   482     clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
   483   }
   484 
   485   private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
   486     if (tokens.exists({ case (t, _) => t.is_command })) {
   487       tokens.dropWhile({ case (t, _) => !t.is_command }).
   488         collectFirst({ case (t, i) if t.is_embedded => (t.content, i) })
   489     }
   490     else None
   491 
   492   def span_files(syntax: Outer_Syntax, span: Command_Span.Span): (List[String], Int) =
   493     syntax.load_command(span.name) match {
   494       case Some(exts) =>
   495         find_file(clean_tokens(span.content)) match {
   496           case Some((file, i)) =>
   497             if (exts.isEmpty) (List(file), i)
   498             else (exts.map(ext => file + "." + ext), i)
   499           case None => (Nil, -1)
   500         }
   501       case None => (Nil, -1)
   502     }
   503 
   504   def blobs_info(
   505     resources: Resources,
   506     syntax: Outer_Syntax,
   507     get_blob: Document.Node.Name => Option[Document.Blob],
   508     can_import: Document.Node.Name => Boolean,
   509     node_name: Document.Node.Name,
   510     span: Command_Span.Span): Blobs_Info =
   511   {
   512     span.name match {
   513       // inlined errors
   514       case Thy_Header.THEORY =>
   515         val reader = Scan.char_reader(Token.implode(span.content))
   516         val imports = resources.check_thy_reader(node_name, reader).imports
   517         val raw_imports =
   518           try {
   519             val imports1 = Thy_Header.read(reader, Token.Pos.none).imports
   520             if (imports.length == imports1.length) imports1.map(_._1) else error("")
   521           }
   522           catch { case exn: Throwable => List.fill(imports.length)("") }
   523 
   524         val errors =
   525           for { ((import_name, pos), s) <- imports zip raw_imports if !can_import(import_name) }
   526           yield {
   527             val completion =
   528               if (Thy_Header.is_base_name(s)) {
   529                 val completed = Completion.completed(import_name.theory_base_name)
   530                 val qualifier = resources.session_base.theory_qualifier(node_name)
   531                 val dir = node_name.master_dir
   532                 for {
   533                   known_name <- resources.session_base.known.theory_names
   534                   if completed(known_name.theory_base_name)
   535                 }
   536                 yield {
   537                   resources.standard_import(
   538                     resources.session_base, qualifier, dir, known_name.theory)
   539                 }
   540               }.sorted
   541               else Nil
   542             val msg =
   543               "Bad theory import " +
   544                 Markup.Path(import_name.node).markup(quote(import_name.toString)) +
   545                 Position.here(pos) + Completion.report_theories(pos, completion)
   546             Exn.Exn(ERROR(msg)): Command.Blob
   547           }
   548         (errors, -1)
   549 
   550       // auxiliary files
   551       case _ =>
   552         val (files, index) = span_files(syntax, span)
   553         val blobs =
   554           files.map(file =>
   555             (Exn.capture {
   556               val name = Document.Node.Name(resources.append(node_name, Path.explode(file)))
   557               val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
   558               (name, blob)
   559             }).user_error)
   560         (blobs, index)
   561     }
   562   }
   563 }
   564 
   565 
   566 final class Command private(
   567     val id: Document_ID.Command,
   568     val node_name: Document.Node.Name,
   569     val blobs_info: Command.Blobs_Info,
   570     val span: Command_Span.Span,
   571     val source: String,
   572     val init_results: Command.Results,
   573     val init_markup: Markup_Tree)
   574 {
   575   override def toString: String = id + "/" + span.kind.toString
   576 
   577 
   578   /* classification */
   579 
   580   def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
   581   def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
   582 
   583   def is_undefined: Boolean = id == Document_ID.none
   584   val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
   585   val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
   586 
   587   def potentially_initialized: Boolean = span.name == Thy_Header.THEORY
   588 
   589 
   590   /* blobs */
   591 
   592   def blobs: List[Command.Blob] = blobs_info._1
   593   def blobs_index: Int = blobs_info._2
   594 
   595   def blobs_ok: Boolean =
   596     blobs.forall({ case Exn.Res(_) => true case _ => false })
   597 
   598   def blobs_names: List[Document.Node.Name] =
   599     for (Exn.Res((name, _)) <- blobs) yield name
   600 
   601   def blobs_undefined: List[Document.Node.Name] =
   602     for (Exn.Res((name, None)) <- blobs) yield name
   603 
   604   def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
   605     for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
   606 
   607   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   608     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   609 
   610 
   611   /* source chunks */
   612 
   613   val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
   614 
   615   val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
   616     ((Symbol.Text_Chunk.Default -> chunk) ::
   617       (for (Exn.Res((name, Some((_, file)))) <- blobs)
   618         yield Symbol.Text_Chunk.File(name.node) -> file)).toMap
   619 
   620   def length: Int = source.length
   621   def range: Text.Range = chunk.range
   622 
   623   val core_range: Text.Range =
   624     Text.Range(0,
   625       (length /: span.content.reverse.iterator.takeWhile(_.is_ignored))(_ - _.source.length))
   626 
   627   def source(range: Text.Range): String = range.substring(source)
   628 
   629 
   630   /* accumulated results */
   631 
   632   val init_state: Command.State =
   633     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   634 
   635   val empty_state: Command.State = Command.State(this)
   636 }