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