src/Pure/Isar/outer_syntax.scala
author wenzelm
Thu Oct 16 12:24:19 2014 +0200 (2014-10-16)
changeset 58695 91839729224e
parent 58694 983e98da2a42
child 58696 6b7445774ce3
permissions -rw-r--r--
tuned comments;
     1 /*  Title:      Pure/Isar/outer_syntax.scala
     2     Author:     Makarius
     3 
     4 Isabelle/Isar outer syntax.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.util.parsing.input.{Reader, CharSequenceReader}
    11 import scala.collection.mutable
    12 
    13 
    14 object Outer_Syntax
    15 {
    16   def quote_string(str: String): String =
    17   {
    18     val result = new StringBuilder(str.length + 10)
    19     result += '"'
    20     for (s <- Symbol.iterator(str)) {
    21       if (s.length == 1) {
    22         val c = s(0)
    23         if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') {
    24           result += '\\'
    25           if (c < 10) result += '0'
    26           if (c < 100) result += '0'
    27           result ++= (c.asInstanceOf[Int].toString)
    28         }
    29         else result += c
    30       }
    31       else result ++= s
    32     }
    33     result += '"'
    34     result.toString
    35   }
    36 
    37   val empty: Outer_Syntax = new Outer_Syntax()
    38 
    39   def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
    40 }
    41 
    42 final class Outer_Syntax private(
    43   keywords: Map[String, (String, List[String])] = Map.empty,
    44   lexicon: Scan.Lexicon = Scan.Lexicon.empty,
    45   val completion: Completion = Completion.empty,
    46   val language_context: Completion.Language_Context = Completion.Language_Context.outer,
    47   val has_tokens: Boolean = true) extends Prover.Syntax
    48 {
    49   override def toString: String =
    50     (for ((name, (kind, files)) <- keywords) yield {
    51       if (kind == Keyword.MINOR) quote(name)
    52       else
    53         quote(name) + " :: " + quote(kind) +
    54         (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
    55     }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
    56 
    57 
    58   /* keyword kind */
    59 
    60   def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
    61   def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
    62 
    63   def is_command(name: String): Boolean =
    64     keyword_kind(name) match {
    65       case Some(kind) => kind != Keyword.MINOR
    66       case None => false
    67     }
    68 
    69 
    70   /* load commands */
    71 
    72   def load_command(name: String): Option[List[String]] =
    73     keywords.get(name) match {
    74       case Some((Keyword.THY_LOAD, exts)) => Some(exts)
    75       case _ => None
    76     }
    77 
    78   val load_commands: List[(String, List[String])] =
    79     (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
    80 
    81   def load_commands_in(text: String): Boolean =
    82     load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
    83 
    84 
    85   /* add keywords */
    86 
    87   def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
    88   {
    89     val keywords1 = keywords + (name -> kind)
    90     val lexicon1 = lexicon + name
    91     val completion1 =
    92       if (Keyword.control(kind._1) || replace == Some("")) completion
    93       else completion + (name, replace getOrElse name)
    94     new Outer_Syntax(keywords1, lexicon1, completion1, language_context, true)
    95   }
    96 
    97   def + (name: String, kind: (String, List[String])): Outer_Syntax =
    98     this + (name, kind, Some(name))
    99   def + (name: String, kind: String): Outer_Syntax =
   100     this + (name, (kind, Nil), Some(name))
   101   def + (name: String, replace: Option[String]): Outer_Syntax =
   102     this + (name, (Keyword.MINOR, Nil), replace)
   103   def + (name: String): Outer_Syntax = this + (name, None)
   104 
   105   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
   106     (this /: keywords) {
   107       case (syntax, (name, Some((kind, _)), replace)) =>
   108         syntax +
   109           (Symbol.decode(name), kind, replace) +
   110           (Symbol.encode(name), kind, replace)
   111       case (syntax, (name, None, replace)) =>
   112         syntax +
   113           (Symbol.decode(name), replace) +
   114           (Symbol.encode(name), replace)
   115     }
   116 
   117 
   118   /* document headings */
   119 
   120   def heading_level(name: String): Option[Int] =
   121   {
   122     keyword_kind(name) match {
   123       case _ if name == "header" => Some(0)
   124       case Some(Keyword.THY_HEADING1) => Some(1)
   125       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
   126       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
   127       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
   128       case Some(kind) if Keyword.theory(kind) => Some(5)
   129       case _ => None
   130     }
   131   }
   132 
   133   def heading_level(command: Command): Option[Int] =
   134     heading_level(command.name)
   135 
   136 
   137   /* token language */
   138 
   139   def scan(input: CharSequence): List[Token] =
   140   {
   141     val in: Reader[Char] = new CharSequenceReader(input)
   142     Token.Parsers.parseAll(
   143         Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), in) match {
   144       case Token.Parsers.Success(tokens, _) => tokens
   145       case _ => error("Unexpected failure of tokenizing input:\n" + input.toString)
   146     }
   147   }
   148 
   149   def scan_line(input: CharSequence, context: Scan.Line_Context, depth: Int)
   150     : (List[Token], Scan.Line_Context, Int) =
   151   {
   152     var in: Reader[Char] = new CharSequenceReader(input)
   153     val toks = new mutable.ListBuffer[Token]
   154     var ctxt = context
   155     while (!in.atEnd) {
   156       Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
   157         case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   158         case Token.Parsers.NoSuccess(_, rest) =>
   159           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   160       }
   161     }
   162 
   163     val depth1 = depth // FIXME
   164     (toks.toList, ctxt, depth1)
   165   }
   166 
   167 
   168   /* parse_spans */
   169 
   170   def parse_spans(toks: List[Token]): List[Command_Span.Span] =
   171   {
   172     val result = new mutable.ListBuffer[Command_Span.Span]
   173     val content = new mutable.ListBuffer[Token]
   174     val improper = new mutable.ListBuffer[Token]
   175 
   176     def ship(span: List[Token])
   177     {
   178       val kind =
   179         if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error)) {
   180           val name = span.head.source
   181           val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1)
   182           Command_Span.Command_Span(name, pos)
   183         }
   184         else if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   185         else Command_Span.Malformed_Span
   186       result += Command_Span.Span(kind, span)
   187     }
   188 
   189     def flush()
   190     {
   191       if (!content.isEmpty) { ship(content.toList); content.clear }
   192       if (!improper.isEmpty) { ship(improper.toList); improper.clear }
   193     }
   194 
   195     for (tok <- toks) {
   196       if (tok.is_command) { flush(); content += tok }
   197       else if (tok.is_improper) improper += tok
   198       else { content ++= improper; improper.clear; content += tok }
   199     }
   200     flush()
   201 
   202     result.toList
   203   }
   204 
   205   def parse_spans(input: CharSequence): List[Command_Span.Span] =
   206     parse_spans(scan(input))
   207 
   208 
   209   /* language context */
   210 
   211   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   212     new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
   213 
   214   def no_tokens: Outer_Syntax =
   215   {
   216     require(keywords.isEmpty && lexicon.isEmpty)
   217     new Outer_Syntax(
   218       completion = completion,
   219       language_context = language_context,
   220       has_tokens = false)
   221   }
   222 }