src/Pure/Thy/completion.scala
author wenzelm
Sat, 19 Dec 2009 11:48:11 +0100
changeset 34135 63dd95e3b393
parent 34097 9274a44358c4
child 34141 297b2149077d
permissions -rw-r--r--
indicate final state of keywords; added symbol scanner;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/completion.scala
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     3
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     4
Completion of symbols and keywords.
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     5
*/
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     6
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     7
package isabelle
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     8
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     9
import scala.util.matching.Regex
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    10
import scala.util.parsing.combinator.RegexParsers
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    11
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    12
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    13
private object Completion
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    14
{
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    15
  /** String/CharSequence utilities */
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    16
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    17
  def length_ord(s1: String, s2: String): Boolean =
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    18
    s1.length < s2.length || s1.length == s2.length && s1 <= s2
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    19
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    20
  class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    21
  {
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    22
    require(0 <= start && start <= end && end <= text.length)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    23
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    24
    def this(text: CharSequence) = this(text, 0, text.length)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    25
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    26
    def length: Int = end - start
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    27
    def charAt(i: Int): Char = text.charAt(end - i - 1)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    28
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    29
    def subSequence(i: Int, j: Int): CharSequence =
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    30
      if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    31
      else throw new IndexOutOfBoundsException
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    32
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    33
    override def toString: String =
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    34
    {
34097
wenzelm
parents: 34093
diff changeset
    35
      val buf = new StringBuilder(length)
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    36
      for (i <- 0 until length)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    37
        buf.append(charAt(i))
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    38
      buf.toString
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    39
    }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    40
  }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    41
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    42
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    43
  /** word completion **/
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    44
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    45
  val word_regex = "[a-zA-Z0-9_']+".r
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    46
  def is_word(s: CharSequence): Boolean = word_regex.pattern.matcher(s).matches
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    47
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    48
  object Parse extends RegexParsers
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    49
  {
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    50
    override val whiteSpace = "".r
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    51
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
    52
    def rev_symbol: Parser[String] = """>[A-Za-z0-9_']+<\\""".r
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
    53
    def rev_symb: Parser[String] = """[A-Za-z0-9_']{2,}<\\""".r
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    54
    def word: Parser[String] = "[a-zA-Z0-9_']{3,}".r
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    55
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    56
    def read(in: CharSequence): Option[String] =
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    57
    {
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
    58
      val rev_in = new Reverse(in)
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
    59
      parse((rev_symbol | rev_symb | word) ^^ (_.reverse), rev_in) match {
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    60
        case Success(result, _) => Some(result)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    61
        case _ => None
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    62
      }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    63
    }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    64
  }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    65
}
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    66
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    67
class Completion
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    68
{
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    69
  /* representation */
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    70
31780
d78e5cff9a9f non-public representation;
wenzelm
parents: 31765
diff changeset
    71
  protected val words_lex = Scan.Lexicon()
d78e5cff9a9f non-public representation;
wenzelm
parents: 31765
diff changeset
    72
  protected val words_map = Map[String, String]()
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    73
31780
d78e5cff9a9f non-public representation;
wenzelm
parents: 31765
diff changeset
    74
  protected val abbrevs_lex = Scan.Lexicon()
d78e5cff9a9f non-public representation;
wenzelm
parents: 31765
diff changeset
    75
  protected val abbrevs_map = Map[String, (String, String)]()
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    76
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    77
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    78
  /* adding stuff */
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    79
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    80
  def + (keyword: String): Completion =
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    81
  {
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    82
    val old = this
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    83
    new Completion {
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    84
      override val words_lex = old.words_lex + keyword
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    85
      override val words_map = old.words_map + (keyword -> keyword)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    86
      override val abbrevs_lex = old.abbrevs_lex
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    87
      override val abbrevs_map = old.abbrevs_map
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    88
    }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    89
  }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    90
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    91
  def + (symbols: Symbol.Interpretation): Completion =
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    92
  {
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    93
    val words =
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    94
      (for ((x, _) <- symbols.names) yield (x, x)).toList :::
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    95
      (for ((x, y) <- symbols.names) yield (y, x)).toList :::
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    96
      (for ((x, y) <- symbols.abbrevs if Completion.is_word(y)) yield (y, x)).toList
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    97
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    98
    val abbrs =
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    99
      (for ((x, y) <- symbols.abbrevs if !Completion.is_word(y))
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   100
        yield (y.reverse.toString, (y, x))).toList
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   101
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   102
    val old = this
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   103
    new Completion {
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   104
      override val words_lex = old.words_lex ++ words.map(_._1)
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   105
      override val words_map = old.words_map ++ words
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   106
      override val abbrevs_lex = old.abbrevs_lex ++ abbrs.map(_._1)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   107
      override val abbrevs_map = old.abbrevs_map ++ abbrs
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   108
    }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   109
  }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   110
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   111
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   112
  /* complete */
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   113
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   114
  def complete(line: CharSequence): Option[(String, List[String])] =
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   115
  {
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   116
    abbrevs_lex.parse(abbrevs_lex.keyword, new Completion.Reverse(line)) match {
34135
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 34097
diff changeset
   117
      case abbrevs_lex.Success((rev_a, _), _) =>
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   118
        val (word, c) = abbrevs_map(rev_a)
34093
3d654643cf56 filter out identical completions;
wenzelm
parents: 31780
diff changeset
   119
        if (word == c) None
3d654643cf56 filter out identical completions;
wenzelm
parents: 31780
diff changeset
   120
        else Some(word, List(c))
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   121
      case _ =>
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   122
        Completion.Parse.read(line) match {
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   123
          case Some(word) =>
34093
3d654643cf56 filter out identical completions;
wenzelm
parents: 31780
diff changeset
   124
            words_lex.completions(word).map(words_map(_)).filter(_ != word) match {
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   125
              case Nil => None
34093
3d654643cf56 filter out identical completions;
wenzelm
parents: 31780
diff changeset
   126
              case cs => Some (word, cs.sort(Completion.length_ord _))
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   127
            }
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   128
          case None => None
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   129
        }
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   130
    }
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   131
  }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   132
}