src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Jan 05 15:44:06 2010 +0100 (2010-01-05)
changeset 34264 b5025782a4ed
parent 34166 446a33b874b3
child 36947 285b39022372
permissions -rw-r--r--
tuned message;
wenzelm@34166
     1
/*  Title:      Pure/Isar/outer_syntax.scala
wenzelm@34166
     2
    Author:     Makarius
wenzelm@34166
     3
wenzelm@34166
     4
Isabelle/Isar outer syntax.
wenzelm@34166
     5
*/
wenzelm@34166
     6
wenzelm@34166
     7
package isabelle
wenzelm@34166
     8
wenzelm@34166
     9
wenzelm@34166
    10
import scala.util.parsing.input.{Reader, CharSequenceReader}
wenzelm@34166
    11
wenzelm@34166
    12
wenzelm@34166
    13
class Outer_Syntax(symbols: Symbol.Interpretation)
wenzelm@34166
    14
{
wenzelm@34166
    15
  protected val keywords: Map[String, String] = Map((";" -> Outer_Keyword.DIAG))
wenzelm@34166
    16
  protected val lexicon: Scan.Lexicon = Scan.Lexicon.empty
wenzelm@34166
    17
  lazy val completion: Completion = new Completion + symbols  // FIXME !?
wenzelm@34166
    18
wenzelm@34166
    19
  def + (name: String, kind: String): Outer_Syntax =
wenzelm@34166
    20
  {
wenzelm@34166
    21
    val new_keywords = keywords + (name -> kind)
wenzelm@34166
    22
    val new_lexicon = lexicon + name
wenzelm@34166
    23
    val new_completion = completion + name
wenzelm@34166
    24
    new Outer_Syntax(symbols) {
wenzelm@34166
    25
      override val lexicon = new_lexicon
wenzelm@34166
    26
      override val keywords = new_keywords
wenzelm@34166
    27
      override lazy val completion = new_completion
wenzelm@34166
    28
    }
wenzelm@34166
    29
  }
wenzelm@34166
    30
wenzelm@34166
    31
  def + (name: String): Outer_Syntax = this + (name, Outer_Keyword.MINOR)
wenzelm@34166
    32
wenzelm@34166
    33
  def is_command(name: String): Boolean =
wenzelm@34166
    34
    keywords.get(name) match {
wenzelm@34166
    35
      case Some(kind) => kind != Outer_Keyword.MINOR
wenzelm@34166
    36
      case None => false
wenzelm@34166
    37
    }
wenzelm@34166
    38
wenzelm@34166
    39
wenzelm@34166
    40
  /* tokenize */
wenzelm@34166
    41
wenzelm@34166
    42
  def scan(input: Reader[Char]): List[Outer_Lex.Token] =
wenzelm@34166
    43
  {
wenzelm@34166
    44
    import lexicon._
wenzelm@34166
    45
wenzelm@34166
    46
    parseAll(rep(token(symbols, is_command)), input) match {
wenzelm@34166
    47
      case Success(tokens, _) => tokens
wenzelm@34264
    48
      case _ => error("Unexpected failure of tokenizing input:\n" + input.source.toString)
wenzelm@34166
    49
    }
wenzelm@34166
    50
  }
wenzelm@34166
    51
wenzelm@34166
    52
  def scan(input: CharSequence): List[Outer_Lex.Token] =
wenzelm@34166
    53
    scan(new CharSequenceReader(input))
wenzelm@34166
    54
}