src/Pure/Thy/completion.scala
author wenzelm
Sun, 20 Dec 2009 15:44:07 +0100
changeset 34141 297b2149077d
parent 34135 63dd95e3b393
child 35004 b89a31957950
permissions -rw-r--r--
simiplified result of keyword parser (again); sort completions by plain string order; moved Reverse to library.scala;

/*  Title:      Pure/Thy/completion.scala
    Author:     Makarius

Completion of symbols and keywords.
*/

package isabelle

import scala.util.matching.Regex
import scala.util.parsing.combinator.RegexParsers


private object Completion
{
  /** word completion **/

  val word_regex = "[a-zA-Z0-9_']+".r
  def is_word(s: CharSequence): Boolean = word_regex.pattern.matcher(s).matches

  object Parse extends RegexParsers
  {
    override val whiteSpace = "".r

    def rev_symbol: Parser[String] = """>[A-Za-z0-9_']+<\\""".r
    def rev_symb: Parser[String] = """[A-Za-z0-9_']{2,}<\\""".r
    def word: Parser[String] = "[a-zA-Z0-9_']{3,}".r

    def read(in: CharSequence): Option[String] =
    {
      val rev_in = new Library.Reverse(in)
      parse((rev_symbol | rev_symb | word) ^^ (_.reverse), rev_in) match {
        case Success(result, _) => Some(result)
        case _ => None
      }
    }
  }
}

class Completion
{
  /* representation */

  protected val words_lex = Scan.Lexicon()
  protected val words_map = Map[String, String]()

  protected val abbrevs_lex = Scan.Lexicon()
  protected val abbrevs_map = Map[String, (String, String)]()


  /* adding stuff */

  def + (keyword: String): Completion =
  {
    val old = this
    new Completion {
      override val words_lex = old.words_lex + keyword
      override val words_map = old.words_map + (keyword -> keyword)
      override val abbrevs_lex = old.abbrevs_lex
      override val abbrevs_map = old.abbrevs_map
    }
  }

  def + (symbols: Symbol.Interpretation): Completion =
  {
    val words =
      (for ((x, _) <- symbols.names) yield (x, x)).toList :::
      (for ((x, y) <- symbols.names) yield (y, x)).toList :::
      (for ((x, y) <- symbols.abbrevs if Completion.is_word(y)) yield (y, x)).toList

    val abbrs =
      (for ((x, y) <- symbols.abbrevs if !Completion.is_word(y))
        yield (y.reverse.toString, (y, x))).toList

    val old = this
    new Completion {
      override val words_lex = old.words_lex ++ words.map(_._1)
      override val words_map = old.words_map ++ words
      override val abbrevs_lex = old.abbrevs_lex ++ abbrs.map(_._1)
      override val abbrevs_map = old.abbrevs_map ++ abbrs
    }
  }


  /* complete */

  def complete(line: CharSequence): Option[(String, List[String])] =
  {
    abbrevs_lex.parse(abbrevs_lex.keyword, new Library.Reverse(line)) match {
      case abbrevs_lex.Success(rev_a, _) =>
        val (word, c) = abbrevs_map(rev_a)
        if (word == c) None
        else Some(word, List(c))
      case _ =>
        Completion.Parse.read(line) match {
          case Some(word) =>
            words_lex.completions(word).map(words_map(_)).filter(_ != word) match {
              case Nil => None
              case cs => Some(word, cs.sort(_ < _))
            }
          case None => None
        }
    }
  }
}