src/Pure/Thy/completion.scala
author wenzelm
Thu, 07 Jul 2011 13:48:30 +0200
changeset 44569 5130dfe1b7be
parent 44337 7f65a68f8b26
child 46773 793bf5fa5fbf
permissions -rw-r--r--
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
tuned implicit build/init messages;
     1 /*  Title:      Pure/Thy/completion.scala
     2     Author:     Makarius
     3 
     4 Completion of symbols and keywords.
     5 */
     6 
     7 package isabelle
     8 
     9 import scala.util.matching.Regex
    10 import scala.util.parsing.combinator.RegexParsers
    11 
    12 
    13 private object Completion
    14 {
    15   /** word completion **/
    16 
    17   val word_regex = "[a-zA-Z0-9_']+".r
    18   def is_word(s: CharSequence): Boolean = word_regex.pattern.matcher(s).matches
    19 
    20   object Parse extends RegexParsers
    21   {
    22     override val whiteSpace = "".r
    23 
    24     def reverse_symbol: Parser[String] = """>[A-Za-z0-9_']+\^?<\\""".r
    25     def reverse_symb: Parser[String] = """[A-Za-z0-9_']{2,}\^?<\\""".r
    26     def word: Parser[String] = "[a-zA-Z0-9_']{2,}".r
    27 
    28     def read(in: CharSequence): Option[String] =
    29     {
    30       val reverse_in = new Library.Reverse(in)
    31       parse((reverse_symbol | reverse_symb | word) ^^ (_.reverse), reverse_in) match {
    32         case Success(result, _) => Some(result)
    33         case _ => None
    34       }
    35     }
    36   }
    37 }
    38 
    39 class Completion
    40 {
    41   /* representation */
    42 
    43   protected val words_lex = Scan.Lexicon()
    44   protected val words_map = Map[String, String]()
    45 
    46   protected val abbrevs_lex = Scan.Lexicon()
    47   protected val abbrevs_map = Map[String, (String, String)]()
    48 
    49 
    50   /* adding stuff */
    51 
    52   def + (keyword: String, replace: String): Completion =
    53   {
    54     val old = this
    55     new Completion {
    56       override val words_lex = old.words_lex + keyword
    57       override val words_map = old.words_map + (keyword -> replace)
    58       override val abbrevs_lex = old.abbrevs_lex
    59       override val abbrevs_map = old.abbrevs_map
    60     }
    61   }
    62 
    63   def + (keyword: String): Completion = this + (keyword, keyword)
    64 
    65   def add_symbols: Completion =
    66   {
    67     val words =
    68       (for ((x, _) <- Symbol.names) yield (x, x)).toList :::
    69       (for ((x, y) <- Symbol.names) yield (y, x)).toList :::
    70       (for ((x, y) <- Symbol.abbrevs if Completion.is_word(y)) yield (y, x)).toList
    71 
    72     val abbrs =
    73       (for ((x, y) <- Symbol.abbrevs if !Completion.is_word(y))
    74         yield (y.reverse.toString, (y, x))).toList
    75 
    76     val old = this
    77     new Completion {
    78       override val words_lex = old.words_lex ++ words.map(_._1)
    79       override val words_map = old.words_map ++ words
    80       override val abbrevs_lex = old.abbrevs_lex ++ abbrs.map(_._1)
    81       override val abbrevs_map = old.abbrevs_map ++ abbrs
    82     }
    83   }
    84 
    85 
    86   /* complete */
    87 
    88   def complete(line: CharSequence): Option[(String, List[String])] =
    89   {
    90     abbrevs_lex.parse(abbrevs_lex.keyword, new Library.Reverse(line)) match {
    91       case abbrevs_lex.Success(reverse_a, _) =>
    92         val (word, c) = abbrevs_map(reverse_a)
    93         Some(word, List(c))
    94       case _ =>
    95         Completion.Parse.read(line) match {
    96           case Some(word) =>
    97             words_lex.completions(word).map(words_map(_)) match {
    98               case Nil => None
    99               case cs => Some(word, cs.sortWith(_ < _))
   100             }
   101           case None => None
   102         }
   103     }
   104   }
   105 }