src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Aug 02 17:35:18 2016 +0200 (2016-08-02)
changeset 63579 73939a9b70a3
parent 63528 0f39f59317c1
child 63584 68751fe1c036
permissions -rw-r--r--
support 'abbrevs' within theory header;
simplified 'keywords': no abbreviations here;
     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.collection.mutable
    11 import scala.annotation.tailrec
    12 
    13 
    14 object Outer_Syntax
    15 {
    16   /* syntax */
    17 
    18   val empty: Outer_Syntax = new Outer_Syntax()
    19 
    20   def init(): Outer_Syntax = new Outer_Syntax(completion = Completion.init())
    21 
    22 
    23   /* string literals */
    24 
    25   def quote_string(str: String): String =
    26   {
    27     val result = new StringBuilder(str.length + 10)
    28     result += '"'
    29     for (s <- Symbol.iterator(str)) {
    30       if (s.length == 1) {
    31         val c = s(0)
    32         if (c < 32 && c != YXML.X && c != YXML.Y || c == '\\' || c == '"') {
    33           result += '\\'
    34           if (c < 10) result += '0'
    35           if (c < 100) result += '0'
    36           result ++= c.asInstanceOf[Int].toString
    37         }
    38         else result += c
    39       }
    40       else result ++= s
    41     }
    42     result += '"'
    43     result.toString
    44   }
    45 
    46 
    47   /* line-oriented structure */
    48 
    49   object Line_Structure
    50   {
    51     val init = Line_Structure()
    52   }
    53 
    54   sealed case class Line_Structure(
    55     improper: Boolean = true,
    56     command: Boolean = false,
    57     depth: Int = 0,
    58     span_depth: Int = 0,
    59     after_span_depth: Int = 0)
    60 
    61 
    62   /* overall document structure */
    63 
    64   sealed abstract class Document { def length: Int }
    65   case class Document_Block(name: String, text: String, body: List[Document]) extends Document
    66   {
    67     val length: Int = (0 /: body)(_ + _.length)
    68   }
    69   case class Document_Atom(command: Command) extends Document
    70   {
    71     def length: Int = command.length
    72   }
    73 }
    74 
    75 final class Outer_Syntax private(
    76   val keywords: Keyword.Keywords = Keyword.Keywords.empty,
    77   val completion: Completion = Completion.empty,
    78   val language_context: Completion.Language_Context = Completion.Language_Context.outer,
    79   val has_tokens: Boolean = true) extends Prover.Syntax
    80 {
    81   /** syntax content **/
    82 
    83   override def toString: String = keywords.toString
    84 
    85 
    86   /* add keywords */
    87 
    88   def + (name: String, kind: String = "", tags: List[String] = Nil): Outer_Syntax =
    89   {
    90     val keywords1 = keywords + (name, kind, tags)
    91     val completion1 =
    92       completion.add_keyword(name).add_abbrevs(
    93         if (Keyword.theory_block.contains(kind))
    94           List((name, name + "\nbegin\n\u0007\nend"), (name, name))
    95         else List((name, name)))
    96     new Outer_Syntax(keywords1, completion1, language_context, true)
    97   }
    98 
    99   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
   100     (this /: keywords) {
   101       case (syntax, (name, ((kind, tags), _))) =>
   102         syntax + (Symbol.decode(name), kind, tags) + (Symbol.encode(name), kind, tags)
   103     }
   104 
   105   def add_abbrevs(abbrevs: Thy_Header.Abbrevs): Outer_Syntax =
   106     if (abbrevs.isEmpty) this
   107     else {
   108       val completion1 =
   109         completion.add_abbrevs(
   110           (for ((a, b) <- abbrevs) yield {
   111             val a1 = Symbol.decode(a)
   112             val a2 = Symbol.encode(a)
   113             val b1 = Symbol.decode(b)
   114             List((a1, b1), (a2, b1))
   115           }).flatten)
   116       new Outer_Syntax(keywords, completion1, language_context, has_tokens)
   117     }
   118 
   119 
   120   /* merge */
   121 
   122   def ++ (other: Prover.Syntax): Prover.Syntax =
   123     if (this eq other) this
   124     else {
   125       val keywords1 = keywords ++ other.asInstanceOf[Outer_Syntax].keywords
   126       val completion1 = completion ++ other.asInstanceOf[Outer_Syntax].completion
   127       if ((keywords eq keywords1) && (completion eq completion1)) this
   128       else new Outer_Syntax(keywords1, completion1, language_context, has_tokens)
   129     }
   130 
   131 
   132   /* load commands */
   133 
   134   def load_command(name: String): Option[List[String]] = keywords.load_commands.get(name)
   135   def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
   136 
   137 
   138   /* language context */
   139 
   140   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   141     new Outer_Syntax(keywords, completion, context, has_tokens)
   142 
   143   def no_tokens: Outer_Syntax =
   144   {
   145     require(keywords.is_empty)
   146     new Outer_Syntax(
   147       completion = completion,
   148       language_context = language_context,
   149       has_tokens = false)
   150   }
   151 
   152 
   153 
   154   /** parsing **/
   155 
   156   /* line-oriented structure */
   157 
   158   private val close_structure =
   159     Set(Keyword.NEXT_BLOCK, Keyword.QED_BLOCK, Keyword.PRF_CLOSE)
   160 
   161   def line_structure(tokens: List[Token], structure: Outer_Syntax.Line_Structure)
   162     : Outer_Syntax.Line_Structure =
   163   {
   164     val improper1 = tokens.forall(_.is_improper)
   165     val command1 = tokens.exists(_.is_command)
   166 
   167     val command_depth =
   168       tokens.iterator.filter(_.is_proper).toStream.headOption match {
   169         case Some(tok) =>
   170           if (keywords.is_command(tok, close_structure))
   171             Some(structure.after_span_depth - 1)
   172           else None
   173         case None => None
   174       }
   175 
   176     val depth1 =
   177       if (tokens.exists(tok =>
   178             keywords.is_before_command(tok) || keywords.is_command(tok, Keyword.theory))) 0
   179       else if (command_depth.isDefined) command_depth.get
   180       else if (command1) structure.after_span_depth
   181       else structure.span_depth
   182 
   183     val (span_depth1, after_span_depth1) =
   184       ((structure.span_depth, structure.after_span_depth) /: tokens) {
   185         case ((x, y), tok) =>
   186           if (tok.is_command) {
   187             if (keywords.is_command(tok, Keyword.theory_goal)) (2, 1)
   188             else if (keywords.is_command(tok, Keyword.theory)) (1, 0)
   189             else if (keywords.is_command(tok, Keyword.proof_open)) (y + 2, y + 1)
   190             else if (keywords.is_command(tok, Set(Keyword.PRF_BLOCK))) (y + 2, y + 1)
   191             else if (keywords.is_command(tok, Set(Keyword.QED_BLOCK))) (y - 1, y - 2)
   192             else if (keywords.is_command(tok, Set(Keyword.PRF_CLOSE))) (y, y - 1)
   193             else if (keywords.is_command(tok, Keyword.proof_close)) (y + 1, y - 1)
   194             else if (keywords.is_command(tok, Keyword.qed_global)) (1, 0)
   195             else (x, y)
   196           }
   197           else (x, y)
   198       }
   199 
   200     Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
   201   }
   202 
   203 
   204   /* command spans */
   205 
   206   def parse_spans(toks: List[Token]): List[Command_Span.Span] =
   207   {
   208     val result = new mutable.ListBuffer[Command_Span.Span]
   209     val content = new mutable.ListBuffer[Token]
   210     val improper = new mutable.ListBuffer[Token]
   211 
   212     def ship(span: List[Token])
   213     {
   214       val kind =
   215         if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   216         else if (span.exists(_.is_error)) Command_Span.Malformed_Span
   217         else
   218           span.find(_.is_command) match {
   219             case None => Command_Span.Malformed_Span
   220             case Some(cmd) =>
   221               val name = cmd.source
   222               val offset =
   223                 (0 /: span.takeWhile(_ != cmd)) {
   224                   case (i, tok) => i + Symbol.iterator(tok.source).length }
   225               val end_offset = offset + Symbol.iterator(name).length
   226               val pos = Position.Range(Text.Range(offset, end_offset) + 1)
   227               Command_Span.Command_Span(name, pos)
   228           }
   229       result += Command_Span.Span(kind, span)
   230     }
   231 
   232     def flush()
   233     {
   234       if (content.nonEmpty) { ship(content.toList); content.clear }
   235       if (improper.nonEmpty) { ship(improper.toList); improper.clear }
   236     }
   237 
   238     for (tok <- toks) {
   239       if (tok.is_improper) improper += tok
   240       else if (keywords.is_before_command(tok) ||
   241         tok.is_command &&
   242           (!content.exists(keywords.is_before_command(_)) || content.exists(_.is_command)))
   243       { flush(); content += tok }
   244       else { content ++= improper; improper.clear; content += tok }
   245     }
   246     flush()
   247 
   248     result.toList
   249   }
   250 
   251   def parse_spans(input: CharSequence): List[Command_Span.Span] =
   252     parse_spans(Token.explode(keywords, input))
   253 
   254 
   255   /* overall document structure */
   256 
   257   def heading_level(command: Command): Option[Int] =
   258   {
   259     val name = command.span.name
   260     name match {
   261       case Thy_Header.CHAPTER => Some(0)
   262       case Thy_Header.SECTION => Some(1)
   263       case Thy_Header.SUBSECTION => Some(2)
   264       case Thy_Header.SUBSUBSECTION => Some(3)
   265       case Thy_Header.PARAGRAPH => Some(4)
   266       case Thy_Header.SUBPARAGRAPH => Some(5)
   267       case _ =>
   268         keywords.kinds.get(name) match {
   269           case Some(kind) if Keyword.theory(kind) && !Keyword.theory_end(kind) => Some(6)
   270           case _ => None
   271         }
   272     }
   273   }
   274 
   275   def parse_document(node_name: Document.Node.Name, text: CharSequence):
   276     List[Outer_Syntax.Document] =
   277   {
   278     /* stack operations */
   279 
   280     def buffer(): mutable.ListBuffer[Outer_Syntax.Document] =
   281       new mutable.ListBuffer[Outer_Syntax.Document]
   282 
   283     var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] =
   284       List((0, Command.empty, buffer()))
   285 
   286     @tailrec def close(level: Int => Boolean)
   287     {
   288       stack match {
   289         case (lev, command, body) :: (_, _, body2) :: rest if level(lev) =>
   290           body2 += Outer_Syntax.Document_Block(command.span.name, command.source, body.toList)
   291           stack = stack.tail
   292           close(level)
   293         case _ =>
   294       }
   295     }
   296 
   297     def result(): List[Outer_Syntax.Document] =
   298     {
   299       close(_ => true)
   300       stack.head._3.toList
   301     }
   302 
   303     def add(command: Command)
   304     {
   305       heading_level(command) match {
   306         case Some(i) =>
   307           close(_ > i)
   308           stack = (i + 1, command, buffer()) :: stack
   309         case None =>
   310       }
   311       stack.head._3 += Outer_Syntax.Document_Atom(command)
   312     }
   313 
   314 
   315     /* result structure */
   316 
   317     val spans = parse_spans(text)
   318     spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span)))
   319     result()
   320   }
   321 }