src/Pure/Tools/check_keywords.scala
author wenzelm
Fri, 01 Apr 2022 17:06:10 +0200
changeset 75393 87ebf5a50283
parent 73340 0ffcad1f6130
child 75394 42267c650205
permissions -rw-r--r--
clarified formatting, for the sake of scala3;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59891
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/check_keywords.scala
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     3
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     4
Check theory sources for conflicts with proposed keywords.
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     5
*/
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     6
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     7
package isabelle
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     8
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    10
object Check_Keywords {
59891
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    11
  def conflicts(
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    12
    keywords: Keyword.Keywords,
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    13
    check: Set[String],
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    14
    input: CharSequence,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    15
    start: Token.Pos
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    16
  ): List[(Token, Position.T)] = {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    17
    object Parser extends Parse.Parser {
59891
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    18
      private val conflict =
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    19
        position(token("token", tok => !(tok.is_command || tok.is_keyword) && check(tok.source)))
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    20
      private val other = token("token", _ => true)
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    21
      private val item = conflict ^^ (x => Some(x)) | other ^^ (_ => None)
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    22
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    23
      val result =
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    24
        parse_all(rep(item), Token.reader(Token.explode(keywords, input), start)) match {
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    25
          case Success(res, _) => for (Some(x) <- res) yield x
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    26
          case bad => error(bad.toString)
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    27
        }
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    28
    }
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    29
    Parser.result
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    30
  }
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    31
59895
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    32
  def check_keywords(
61276
8a4bd05c1735 clarified modules;
wenzelm
parents: 59895
diff changeset
    33
    progress: Progress,
59895
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    34
    keywords: Keyword.Keywords,
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    35
    check: Set[String],
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    36
    paths: List[Path]
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    37
  ): Unit = {
59895
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    38
    val parallel_args = paths.map(path => (File.read(path), Token.Pos.file(path.expand.implode)))
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    39
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    40
    val bad =
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    41
      Par_List.map((arg: (String, Token.Pos)) => {
67880
e59220a075de tuned signature;
wenzelm
parents: 65827
diff changeset
    42
        progress.expose_interrupt()
59895
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    43
        conflicts(keywords, check, arg._1, arg._2)
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    44
      }, parallel_args).flatten
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    45
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    46
    for ((tok, pos) <- bad) {
65827
wenzelm
parents: 61276
diff changeset
    47
      progress.echo_warning(
wenzelm
parents: 61276
diff changeset
    48
        "keyword conflict: " + tok.kind.toString + " " + quote(tok.content) + Position.here(pos))
59895
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    49
    }
a68a0fec288d clarified module;
wenzelm
parents: 59891
diff changeset
    50
  }
59891
9ce697050455 added isabelle build option -k, for fast off-line checking of theory sources;
wenzelm
parents:
diff changeset
    51
}