src/Pure/Isar/line_structure.scala
author wenzelm
Thu, 06 Jun 2024 12:42:42 +0200
changeset 80267 ea908185a597
parent 75393 87ebf5a50283
permissions -rw-r--r--
more operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63603
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Isar/line_structure.scala
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     3
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     4
Line-oriented document structure, e.g. for fold handling.
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     6
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     8
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    10
object Line_Structure {
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 68730
diff changeset
    11
  val init: Line_Structure = Line_Structure()
63603
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    12
}
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    13
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    14
sealed case class Line_Structure(
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    15
  improper: Boolean = true,
66177
7fd83f20e3e9 more information;
wenzelm
parents: 63603
diff changeset
    16
  blank: Boolean = true,
63603
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    17
  command: Boolean = false,
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    18
  depth: Int = 0,
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    19
  span_depth: Int = 0,
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    20
  after_span_depth: Int = 0,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    21
  element_depth: Int = 0
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    22
) {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    23
  def update(keywords: Keyword.Keywords, tokens: List[Token]): Line_Structure = {
68730
0bc491938780 tuned signature;
wenzelm
parents: 66177
diff changeset
    24
    val improper1 = tokens.forall(tok => !tok.is_proper)
66177
7fd83f20e3e9 more information;
wenzelm
parents: 63603
diff changeset
    25
    val blank1 = tokens.forall(_.is_space)
63603
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    26
    val command1 = tokens.exists(_.is_begin_or_command)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    27
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    28
    val command_depth =
73345
8204f7b53007 tuned --- fewer warnings;
wenzelm
parents: 71601
diff changeset
    29
      tokens.iterator.filter(_.is_proper).nextOption() match {
63603
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    30
        case Some(tok) =>
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    31
          if (keywords.is_command(tok, Keyword.close_structure))
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    32
            Some(after_span_depth - 1)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    33
          else None
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    34
        case None => None
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    35
      }
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    36
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    37
    val depth1 =
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    38
      if (tokens.exists(tok =>
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    39
            keywords.is_before_command(tok) ||
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    40
            !tok.is_end && keywords.is_command(tok, Keyword.theory))) element_depth
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    41
      else if (command_depth.isDefined) command_depth.get
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    42
      else if (command1) after_span_depth
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    43
      else span_depth
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    44
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    45
    val (span_depth1, after_span_depth1, element_depth1) =
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73345
diff changeset
    46
      tokens.foldLeft((span_depth, after_span_depth, element_depth)) {
63603
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    47
        case (depths @ (x, y, z), tok) =>
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    48
          if (tok.is_begin) (z + 2, z + 1, z + 1)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    49
          else if (tok.is_end) (z + 1, z - 1, z - 1)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    50
          else if (tok.is_command) {
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    51
            val depth0 = element_depth
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    52
            if (keywords.is_command(tok, Keyword.theory_goal)) (depth0 + 2, depth0 + 1, z)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    53
            else if (keywords.is_command(tok, Keyword.theory)) (depth0 + 1, depth0, z)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    54
            else if (keywords.is_command(tok, Keyword.proof_open)) (y + 2, y + 1, z)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    55
            else if (keywords.is_command(tok, Set(Keyword.PRF_BLOCK))) (y + 2, y + 1, z)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    56
            else if (keywords.is_command(tok, Set(Keyword.QED_BLOCK))) (y - 1, y - 2, z)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    57
            else if (keywords.is_command(tok, Set(Keyword.PRF_CLOSE))) (y, y - 1, z)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    58
            else if (keywords.is_command(tok, Keyword.proof_close)) (y + 1, y - 1, z)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    59
            else if (keywords.is_command(tok, Keyword.qed_global)) (depth0 + 1, depth0, z)
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    60
            else depths
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    61
          }
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    62
          else depths
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    63
      }
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    64
66177
7fd83f20e3e9 more information;
wenzelm
parents: 63603
diff changeset
    65
    Line_Structure(
7fd83f20e3e9 more information;
wenzelm
parents: 63603
diff changeset
    66
      improper1, blank1, command1, depth1, span_depth1, after_span_depth1, element_depth1)
63603
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    67
  }
9d9ea2c6bc38 clarified modules;
wenzelm
parents:
diff changeset
    68
}