src/Pure/Isar/outer_syntax.scala
author wenzelm
Tue Jul 12 13:26:39 2016 +0200 (2016-07-12)
changeset 63458 723f9c673c1c
parent 63441 4c3fa4dba79f
child 63459 8d68204d97d7
permissions -rw-r--r--
closing 'qed' or '}' is outside of fold;
     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, replace: Option[String] = None)
    89     : Outer_Syntax =
    90   {
    91     val keywords1 = keywords + (name, kind, tags)
    92     val completion1 =
    93       if (replace == Some("")) completion
    94       else completion + (name, replace getOrElse name)
    95     new Outer_Syntax(keywords1, completion1, language_context, true)
    96   }
    97 
    98   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
    99     (this /: keywords) {
   100       case (syntax, (name, ((kind, tags), _), replace)) =>
   101         syntax +
   102           (Symbol.decode(name), kind, tags, replace) +
   103           (Symbol.encode(name), kind, tags, replace)
   104     }
   105 
   106 
   107   /* merge */
   108 
   109   def ++ (other: Prover.Syntax): Prover.Syntax =
   110     if (this eq other) this
   111     else {
   112       val keywords1 = keywords ++ other.asInstanceOf[Outer_Syntax].keywords
   113       val completion1 = completion ++ other.asInstanceOf[Outer_Syntax].completion
   114       if ((keywords eq keywords1) && (completion eq completion1)) this
   115       else new Outer_Syntax(keywords1, completion1, language_context, has_tokens)
   116     }
   117 
   118 
   119   /* load commands */
   120 
   121   def load_command(name: String): Option[List[String]] = keywords.load_commands.get(name)
   122   def load_commands_in(text: String): Boolean = keywords.load_commands_in(text)
   123 
   124 
   125   /* language context */
   126 
   127   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   128     new Outer_Syntax(keywords, completion, context, has_tokens)
   129 
   130   def no_tokens: Outer_Syntax =
   131   {
   132     require(keywords.is_empty)
   133     new Outer_Syntax(
   134       completion = completion,
   135       language_context = language_context,
   136       has_tokens = false)
   137   }
   138 
   139 
   140 
   141   /** parsing **/
   142 
   143   /* line-oriented structure */
   144 
   145   def line_structure(tokens: List[Token], structure: Outer_Syntax.Line_Structure)
   146     : Outer_Syntax.Line_Structure =
   147   {
   148     val improper1 = tokens.forall(_.is_improper)
   149     val command1 = tokens.exists(_.is_command)
   150 
   151     val command_depth =
   152       tokens.iterator.filter(_.is_proper).toStream.headOption match {
   153         case Some(tok) =>
   154           if (keywords.is_command(tok, Keyword.QED_BLOCK == _))
   155             Some(structure.after_span_depth - 2)
   156           else if (keywords.is_command(tok, Keyword.PRF_CLOSE == _))
   157             Some(structure.after_span_depth - 1)
   158           else None
   159         case None => None
   160       }
   161 
   162     val depth1 =
   163       if (tokens.exists(keywords.is_command(_, Keyword.theory))) 0
   164       else if (command_depth.isDefined) command_depth.get
   165       else if (command1) structure.after_span_depth
   166       else structure.span_depth
   167 
   168     val (span_depth1, after_span_depth1) =
   169       ((structure.span_depth, structure.after_span_depth) /: tokens) {
   170         case ((x, y), tok) =>
   171           if (tok.is_command) {
   172             if (keywords.is_command(tok, Keyword.theory_goal)) (2, 1)
   173             else if (keywords.is_command(tok, Keyword.theory)) (1, 0)
   174             else if (keywords.is_command(tok, Keyword.proof_open)) (y + 2, y + 1)
   175             else if (keywords.is_command(tok, Keyword.PRF_BLOCK == _)) (y + 2, y + 1)
   176             else if (keywords.is_command(tok, Keyword.QED_BLOCK == _)) (y - 1, y - 2)
   177             else if (keywords.is_command(tok, Keyword.PRF_CLOSE == _)) (y, y - 1)
   178             else if (keywords.is_command(tok, Keyword.proof_close)) (y + 1, y - 1)
   179             else if (keywords.is_command(tok, Keyword.qed_global)) (1, 0)
   180             else (x, y)
   181           }
   182           else (x, y)
   183       }
   184 
   185     Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
   186   }
   187 
   188 
   189   /* command spans */
   190 
   191   def parse_spans(toks: List[Token]): List[Command_Span.Span] =
   192   {
   193     val result = new mutable.ListBuffer[Command_Span.Span]
   194     val content = new mutable.ListBuffer[Token]
   195     val improper = new mutable.ListBuffer[Token]
   196 
   197     def ship(span: List[Token])
   198     {
   199       val kind =
   200         if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   201         else if (span.exists(_.is_error)) Command_Span.Malformed_Span
   202         else
   203           span.find(_.is_command) match {
   204             case None => Command_Span.Malformed_Span
   205             case Some(cmd) =>
   206               val name = cmd.source
   207               val offset =
   208                 (0 /: span.takeWhile(_ != cmd)) {
   209                   case (i, tok) => i + Symbol.iterator(tok.source).length }
   210               val end_offset = offset + Symbol.iterator(name).length
   211               val pos = Position.Range(Text.Range(offset, end_offset) + 1)
   212               Command_Span.Command_Span(name, pos)
   213           }
   214       result += Command_Span.Span(kind, span)
   215     }
   216 
   217     def flush()
   218     {
   219       if (content.nonEmpty) { ship(content.toList); content.clear }
   220       if (improper.nonEmpty) { ship(improper.toList); improper.clear }
   221     }
   222 
   223     for (tok <- toks) {
   224       if (tok.is_improper) improper += tok
   225       else if (keywords.is_before_command(tok) ||
   226         tok.is_command &&
   227           (!content.exists(keywords.is_before_command(_)) || content.exists(_.is_command)))
   228       { flush(); content += tok }
   229       else { content ++= improper; improper.clear; content += tok }
   230     }
   231     flush()
   232 
   233     result.toList
   234   }
   235 
   236   def parse_spans(input: CharSequence): List[Command_Span.Span] =
   237     parse_spans(Token.explode(keywords, input))
   238 
   239 
   240   /* overall document structure */
   241 
   242   def heading_level(command: Command): Option[Int] =
   243   {
   244     val name = command.span.name
   245     name match {
   246       case Thy_Header.CHAPTER => Some(0)
   247       case Thy_Header.SECTION => Some(1)
   248       case Thy_Header.SUBSECTION => Some(2)
   249       case Thy_Header.SUBSUBSECTION => Some(3)
   250       case Thy_Header.PARAGRAPH => Some(4)
   251       case Thy_Header.SUBPARAGRAPH => Some(5)
   252       case _ =>
   253         keywords.kinds.get(name) match {
   254           case Some(kind) if Keyword.theory(kind) && !Keyword.theory_end(kind) => Some(6)
   255           case _ => None
   256         }
   257     }
   258   }
   259 
   260   def parse_document(node_name: Document.Node.Name, text: CharSequence):
   261     List[Outer_Syntax.Document] =
   262   {
   263     /* stack operations */
   264 
   265     def buffer(): mutable.ListBuffer[Outer_Syntax.Document] =
   266       new mutable.ListBuffer[Outer_Syntax.Document]
   267 
   268     var stack: List[(Int, Command, mutable.ListBuffer[Outer_Syntax.Document])] =
   269       List((0, Command.empty, buffer()))
   270 
   271     @tailrec def close(level: Int => Boolean)
   272     {
   273       stack match {
   274         case (lev, command, body) :: (_, _, body2) :: rest if level(lev) =>
   275           body2 += Outer_Syntax.Document_Block(command.span.name, command.source, body.toList)
   276           stack = stack.tail
   277           close(level)
   278         case _ =>
   279       }
   280     }
   281 
   282     def result(): List[Outer_Syntax.Document] =
   283     {
   284       close(_ => true)
   285       stack.head._3.toList
   286     }
   287 
   288     def add(command: Command)
   289     {
   290       heading_level(command) match {
   291         case Some(i) =>
   292           close(_ > i)
   293           stack = (i + 1, command, buffer()) :: stack
   294         case None =>
   295       }
   296       stack.head._3 += Outer_Syntax.Document_Atom(command)
   297     }
   298 
   299 
   300     /* result structure */
   301 
   302     val spans = parse_spans(text)
   303     spans.foreach(span => add(Command(Document_ID.none, node_name, Command.no_blobs, span)))
   304     result()
   305   }
   306 }