src/Pure/PIDE/command_span.scala
author wenzelm
Fri, 27 Nov 2020 16:40:31 +0100
changeset 72744 0017eb17ac1c
parent 72743 bc82fc605424
child 72748 04d5f6d769a7
permissions -rw-r--r--
unused (see 7634d33c1a79);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/PIDE/command_span.scala
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     3
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     4
Syntactic representation of command spans.
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     5
*/
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     6
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     7
package isabelle
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     8
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     9
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    10
import scala.collection.mutable
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    11
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    12
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    13
object Command_Span
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    14
{
72743
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    15
  /* loaded files */
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    16
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    17
  type Loaded_Files = (List[String], Int)
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    18
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    19
  val no_loaded_files: Loaded_Files = (Nil, -1)
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    20
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    21
  def loaded_files(exts: List[String], tokens: List[(Token, Int)]): Loaded_Files =
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    22
    tokens.collectFirst({ case (t, i) if t.is_embedded => (t.content, i) }) match {
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    23
      case Some((file, i)) =>
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    24
        if (exts.isEmpty) (List(file), i)
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    25
        else (exts.map(ext => file + "." + ext), i)
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    26
      case None => no_loaded_files
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    27
    }
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    28
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    29
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    30
  /* span kind */
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    31
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    32
  sealed abstract class Kind {
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    33
    override def toString: String =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    34
      this match {
65717
wenzelm
parents: 65335
diff changeset
    35
        case Command_Span(name, _) => proper_string(name) getOrElse "<command>"
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    36
        case Ignored_Span => "<ignored>"
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    37
        case Malformed_Span => "<malformed>"
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
    38
        case Theory_Span => "<theory>"
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    39
      }
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    40
  }
57910
a50837b637dc maintain Command_Range position as in ML;
wenzelm
parents: 57905
diff changeset
    41
  case class Command_Span(name: String, pos: Position.T) extends Kind
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    42
  case object Ignored_Span extends Kind
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    43
  case object Malformed_Span extends Kind
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
    44
  case object Theory_Span extends Kind
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    45
72743
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    46
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    47
  /* span */
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    48
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    49
  sealed case class Span(kind: Kind, content: List[Token])
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    50
  {
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
    51
    def is_theory: Boolean = kind == Theory_Span
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
    52
59735
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    53
    def name: String =
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    54
      kind match { case Command_Span(name, _) => name case _ => "" }
58802
3cc68ec558b0 find command span in buffer;
wenzelm
parents: 57910
diff changeset
    55
59735
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    56
    def position: Position.T =
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    57
      kind match { case Command_Span(_, pos) => pos case _ => Position.none }
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    58
67895
cd00999d2d30 more position information;
wenzelm
parents: 65717
diff changeset
    59
    def keyword_pos(start: Token.Pos): Token.Pos =
cd00999d2d30 more position information;
wenzelm
parents: 65717
diff changeset
    60
      kind match {
cd00999d2d30 more position information;
wenzelm
parents: 65717
diff changeset
    61
        case _: Command_Span =>
cd00999d2d30 more position information;
wenzelm
parents: 65717
diff changeset
    62
          (start /: content.iterator.takeWhile(tok => !tok.is_command))(_.advance(_))
cd00999d2d30 more position information;
wenzelm
parents: 65717
diff changeset
    63
        case _ => start
cd00999d2d30 more position information;
wenzelm
parents: 65717
diff changeset
    64
      }
cd00999d2d30 more position information;
wenzelm
parents: 65717
diff changeset
    65
68845
3b2daa7bf9f4 support Thy_Element in Scala, following ML version;
wenzelm
parents: 68840
diff changeset
    66
    def is_kind(keywords: Keyword.Keywords, pred: String => Boolean, other: Boolean): Boolean =
68840
51ab4c78235b tuned signature;
wenzelm
parents: 67895
diff changeset
    67
      keywords.kinds.get(name) match {
51ab4c78235b tuned signature;
wenzelm
parents: 67895
diff changeset
    68
        case Some(k) => pred(k)
68845
3b2daa7bf9f4 support Thy_Element in Scala, following ML version;
wenzelm
parents: 68840
diff changeset
    69
        case None => other
68840
51ab4c78235b tuned signature;
wenzelm
parents: 67895
diff changeset
    70
      }
51ab4c78235b tuned signature;
wenzelm
parents: 67895
diff changeset
    71
63606
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 59735
diff changeset
    72
    def is_begin: Boolean = content.exists(_.is_begin)
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 59735
diff changeset
    73
    def is_end: Boolean = content.exists(_.is_end)
fc3a23763617 support for context block structure in Sidekick;
wenzelm
parents: 59735
diff changeset
    74
59735
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    75
    def length: Int = (0 /: content)(_ + _.source.length)
59705
740a0ca7e09b clarified span position;
wenzelm
parents: 59689
diff changeset
    76
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    77
    def compact_source: (String, Span) =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    78
    {
59735
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    79
      val source = Token.implode(content)
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    80
      val content1 = new mutable.ListBuffer[Token]
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    81
      var i = 0
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    82
      for (Token(kind, s) <- content) {
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    83
        val n = s.length
59735
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    84
        val s1 = source.substring(i, i + n)
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    85
        content1 += Token(kind, s1)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    86
        i += n
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    87
      }
59735
24bee1b11fce misc tuning and simplification;
wenzelm
parents: 59705
diff changeset
    88
      (source, Span(kind, content1.toList))
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    89
    }
72742
bda424c5819f clarified modules;
wenzelm
parents: 72692
diff changeset
    90
72743
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    91
    def clean_arguments: List[(Token, Int)] =
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    92
    {
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    93
      if (name.nonEmpty) {
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    94
        def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    95
          toks match {
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    96
            case (t1, i1) :: (t2, i2) :: rest =>
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    97
              if (t1.is_keyword && t1.source == "%" && t2.is_name) clean(rest)
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    98
              else (t1, i1) :: clean((t2, i2) :: rest)
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
    99
            case _ => toks
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   100
          }
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   101
        clean(content.zipWithIndex.filter({ case (t, _) => t.is_proper }))
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   102
          .dropWhile({ case (t, _) => !t.is_command })
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   103
          .dropWhile({ case (t, _) => t.is_command })
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   104
      }
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   105
      else Nil
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   106
    }
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   107
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   108
    def loaded_files(syntax: Outer_Syntax): Loaded_Files =
72742
bda424c5819f clarified modules;
wenzelm
parents: 72692
diff changeset
   109
      syntax.load_command(name) match {
72743
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   110
        case Some(exts) => isabelle.Command_Span.loaded_files(exts, clean_arguments)
bc82fc605424 clarified signature;
wenzelm
parents: 72742
diff changeset
   111
        case None => no_loaded_files
72742
bda424c5819f clarified modules;
wenzelm
parents: 72692
diff changeset
   112
      }
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
   113
  }
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
   114
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
   115
  val empty: Span = Span(Ignored_Span, Nil)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
   116
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
   117
  def unparsed(source: String, theory: Boolean): Span =
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
   118
  {
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
   119
    val kind = if (theory) Theory_Span else Malformed_Span
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
   120
    Span(kind, List(Token(Token.Kind.UNPARSED, source)))
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 68845
diff changeset
   121
  }
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
   122
}