src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Aug 12 15:31:24 2014 +0200 (2014-08-12)
changeset 57911 dcb758188aa6
parent 57910 a50837b637dc
child 58503 ea22f2380871
permissions -rw-r--r--
clarified Position.Identified: do not require range from prover, default to command position;
     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   def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
    58   def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
    59 
    60   def load_command(name: String): Option[List[String]] =
    61     keywords.get(name) match {
    62       case Some((Keyword.THY_LOAD, exts)) => Some(exts)
    63       case _ => None
    64     }
    65 
    66   val load_commands: List[(String, List[String])] =
    67     (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
    68 
    69   def load_commands_in(text: String): Boolean =
    70     load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
    71 
    72   def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
    73   {
    74     val keywords1 = keywords + (name -> kind)
    75     val lexicon1 = lexicon + name
    76     val completion1 =
    77       if (Keyword.control(kind._1) || replace == Some("")) completion
    78       else completion + (name, replace getOrElse name)
    79     new Outer_Syntax(keywords1, lexicon1, completion1, language_context, true)
    80   }
    81 
    82   def + (name: String, kind: (String, List[String])): Outer_Syntax =
    83     this + (name, kind, Some(name))
    84   def + (name: String, kind: String): Outer_Syntax =
    85     this + (name, (kind, Nil), Some(name))
    86   def + (name: String, replace: Option[String]): Outer_Syntax =
    87     this + (name, (Keyword.MINOR, Nil), replace)
    88   def + (name: String): Outer_Syntax = this + (name, None)
    89 
    90   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
    91     (this /: keywords) {
    92       case (syntax, (name, Some((kind, _)), replace)) =>
    93         syntax +
    94           (Symbol.decode(name), kind, replace) +
    95           (Symbol.encode(name), kind, replace)
    96       case (syntax, (name, None, replace)) =>
    97         syntax +
    98           (Symbol.decode(name), replace) +
    99           (Symbol.encode(name), replace)
   100     }
   101 
   102   def is_command(name: String): Boolean =
   103     keyword_kind(name) match {
   104       case Some(kind) => kind != Keyword.MINOR
   105       case None => false
   106     }
   107 
   108   def heading_level(name: String): Option[Int] =
   109   {
   110     keyword_kind(name) match {
   111       case _ if name == "header" => Some(0)
   112       case Some(Keyword.THY_HEADING1) => Some(1)
   113       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
   114       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
   115       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
   116       case Some(kind) if Keyword.theory(kind) => Some(5)
   117       case _ => None
   118     }
   119   }
   120 
   121   def heading_level(command: Command): Option[Int] =
   122     heading_level(command.name)
   123 
   124 
   125   /* token language */
   126 
   127   def scan(input: CharSequence): List[Token] =
   128   {
   129     var in: Reader[Char] = new CharSequenceReader(input)
   130     Token.Parsers.parseAll(
   131         Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), in) match {
   132       case Token.Parsers.Success(tokens, _) => tokens
   133       case _ => error("Unexpected failure of tokenizing input:\n" + input.toString)
   134     }
   135   }
   136 
   137   def scan_line(input: CharSequence, context: Scan.Line_Context): (List[Token], Scan.Line_Context) =
   138   {
   139     var in: Reader[Char] = new CharSequenceReader(input)
   140     val toks = new mutable.ListBuffer[Token]
   141     var ctxt = context
   142     while (!in.atEnd) {
   143       Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
   144         case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   145         case Token.Parsers.NoSuccess(_, rest) =>
   146           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   147       }
   148     }
   149     (toks.toList, ctxt)
   150   }
   151 
   152 
   153   /* parse_spans */
   154 
   155   def parse_spans(toks: List[Token]): List[Command_Span.Span] =
   156   {
   157     val result = new mutable.ListBuffer[Command_Span.Span]
   158     val content = new mutable.ListBuffer[Token]
   159     val improper = new mutable.ListBuffer[Token]
   160 
   161     def ship(span: List[Token])
   162     {
   163       val kind =
   164         if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error)) {
   165           val name = span.head.source
   166           val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1)
   167           Command_Span.Command_Span(name, pos)
   168         }
   169         else if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   170         else Command_Span.Malformed_Span
   171       result += Command_Span.Span(kind, span)
   172     }
   173 
   174     def flush()
   175     {
   176       if (!content.isEmpty) { ship(content.toList); content.clear }
   177       if (!improper.isEmpty) { ship(improper.toList); improper.clear }
   178     }
   179 
   180     for (tok <- toks) {
   181       if (tok.is_command) { flush(); content += tok }
   182       else if (tok.is_improper) improper += tok
   183       else { content ++= improper; improper.clear; content += tok }
   184     }
   185     flush()
   186 
   187     result.toList
   188   }
   189 
   190   def parse_spans(input: CharSequence): List[Command_Span.Span] =
   191     parse_spans(scan(input))
   192 
   193 
   194   /* language context */
   195 
   196   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   197     new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
   198 
   199   def no_tokens: Outer_Syntax =
   200   {
   201     require(keywords.isEmpty && lexicon.isEmpty)
   202     new Outer_Syntax(
   203       completion = completion,
   204       language_context = language_context,
   205       has_tokens = false)
   206   }
   207 }