src/Pure/Isar/outer_syntax.scala
author wenzelm
Sat Oct 18 20:56:16 2014 +0200 (2014-10-18)
changeset 58700 4717d18cc619
parent 58697 5bc1d6c4a499
child 58703 883efcc7a50d
permissions -rw-r--r--
clarified Line_Structure wrt. command span;
     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   /* line-oriented structure */
    43 
    44   object Line_Structure
    45   {
    46     val init = Line_Structure()
    47   }
    48 
    49   sealed case class Line_Structure(
    50     improper: Boolean = true,
    51     command: Boolean = false,
    52     depth: Int = 0,
    53     span_depth: Int = 0,
    54     after_span_depth: Int = 0)
    55 }
    56 
    57 final class Outer_Syntax private(
    58   keywords: Map[String, (String, List[String])] = Map.empty,
    59   lexicon: Scan.Lexicon = Scan.Lexicon.empty,
    60   val completion: Completion = Completion.empty,
    61   val language_context: Completion.Language_Context = Completion.Language_Context.outer,
    62   val has_tokens: Boolean = true) extends Prover.Syntax
    63 {
    64   override def toString: String =
    65     (for ((name, (kind, files)) <- keywords) yield {
    66       if (kind == Keyword.MINOR) quote(name)
    67       else
    68         quote(name) + " :: " + quote(kind) +
    69         (if (files.isEmpty) "" else " (" + commas_quote(files) + ")")
    70     }).toList.sorted.mkString("keywords\n  ", " and\n  ", "")
    71 
    72 
    73   /* keyword kind */
    74 
    75   def keyword_kind_files(name: String): Option[(String, List[String])] = keywords.get(name)
    76   def keyword_kind(name: String): Option[String] = keyword_kind_files(name).map(_._1)
    77 
    78   def is_command(name: String): Boolean =
    79     keyword_kind(name) match {
    80       case Some(kind) => kind != Keyword.MINOR
    81       case None => false
    82     }
    83 
    84   def command_kind(token: Token, pred: String => Boolean): Boolean =
    85     token.is_command && is_command(token.source) &&
    86       pred(keyword_kind(token.source).get)
    87 
    88 
    89   /* load commands */
    90 
    91   def load_command(name: String): Option[List[String]] =
    92     keywords.get(name) match {
    93       case Some((Keyword.THY_LOAD, exts)) => Some(exts)
    94       case _ => None
    95     }
    96 
    97   val load_commands: List[(String, List[String])] =
    98     (for ((name, (Keyword.THY_LOAD, files)) <- keywords.iterator) yield (name, files)).toList
    99 
   100   def load_commands_in(text: String): Boolean =
   101     load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
   102 
   103 
   104   /* add keywords */
   105 
   106   def + (name: String, kind: (String, List[String]), replace: Option[String]): Outer_Syntax =
   107   {
   108     val keywords1 = keywords + (name -> kind)
   109     val lexicon1 = lexicon + name
   110     val completion1 =
   111       if (Keyword.control(kind._1) || replace == Some("")) completion
   112       else completion + (name, replace getOrElse name)
   113     new Outer_Syntax(keywords1, lexicon1, completion1, language_context, true)
   114   }
   115 
   116   def + (name: String, kind: (String, List[String])): Outer_Syntax =
   117     this + (name, kind, Some(name))
   118   def + (name: String, kind: String): Outer_Syntax =
   119     this + (name, (kind, Nil), Some(name))
   120   def + (name: String, replace: Option[String]): Outer_Syntax =
   121     this + (name, (Keyword.MINOR, Nil), replace)
   122   def + (name: String): Outer_Syntax = this + (name, None)
   123 
   124   def add_keywords(keywords: Thy_Header.Keywords): Outer_Syntax =
   125     (this /: keywords) {
   126       case (syntax, (name, Some((kind, _)), replace)) =>
   127         syntax +
   128           (Symbol.decode(name), kind, replace) +
   129           (Symbol.encode(name), kind, replace)
   130       case (syntax, (name, None, replace)) =>
   131         syntax +
   132           (Symbol.decode(name), replace) +
   133           (Symbol.encode(name), replace)
   134     }
   135 
   136 
   137   /* document headings */
   138 
   139   def heading_level(name: String): Option[Int] =
   140   {
   141     keyword_kind(name) match {
   142       case _ if name == "header" => Some(0)
   143       case Some(Keyword.THY_HEADING1) => Some(1)
   144       case Some(Keyword.THY_HEADING2) | Some(Keyword.PRF_HEADING2) => Some(2)
   145       case Some(Keyword.THY_HEADING3) | Some(Keyword.PRF_HEADING3) => Some(3)
   146       case Some(Keyword.THY_HEADING4) | Some(Keyword.PRF_HEADING4) => Some(4)
   147       case Some(kind) if Keyword.theory(kind) => Some(5)
   148       case _ => None
   149     }
   150   }
   151 
   152   def heading_level(command: Command): Option[Int] =
   153     heading_level(command.name)
   154 
   155 
   156   /* line-oriented structure */
   157 
   158   def line_structure(tokens: List[Token], struct: Outer_Syntax.Line_Structure)
   159     : Outer_Syntax.Line_Structure =
   160   {
   161     val improper1 = tokens.forall(_.is_improper)
   162     val command1 = tokens.exists(_.is_command)
   163 
   164     val depth1 =
   165       if (tokens.exists(tok => command_kind(tok, Keyword.theory))) 0
   166       else if (command1) struct.after_span_depth
   167       else struct.span_depth
   168 
   169     val (span_depth1, after_span_depth1) =
   170       ((struct.span_depth, struct.after_span_depth) /: tokens) {
   171         case ((d0, d), tok) =>
   172           if (command_kind(tok, Keyword.theory_goal)) (2, 1)
   173           else if (command_kind(tok, Keyword.theory)) (1, 0)
   174           else if (command_kind(tok, Keyword.proof_goal)) (d + 2, d + 1)
   175           else if (command_kind(tok, Keyword.qed)) (d + 1, d - 1)
   176           else if (command_kind(tok, Keyword.qed_global)) (1, 0)
   177           else (d0, d)
   178       }
   179 
   180     Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
   181   }
   182 
   183 
   184   /* token language */
   185 
   186   def scan(input: CharSequence): List[Token] =
   187   {
   188     val in: Reader[Char] = new CharSequenceReader(input)
   189     Token.Parsers.parseAll(
   190         Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), in) match {
   191       case Token.Parsers.Success(tokens, _) => tokens
   192       case _ => error("Unexpected failure of tokenizing input:\n" + input.toString)
   193     }
   194   }
   195 
   196   def scan_line(
   197     input: CharSequence,
   198     context: Scan.Line_Context,
   199     structure: Outer_Syntax.Line_Structure)
   200     : (List[Token], Scan.Line_Context, Outer_Syntax.Line_Structure) =
   201   {
   202     var in: Reader[Char] = new CharSequenceReader(input)
   203     val toks = new mutable.ListBuffer[Token]
   204     var ctxt = context
   205     while (!in.atEnd) {
   206       Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
   207         case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   208         case Token.Parsers.NoSuccess(_, rest) =>
   209           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   210       }
   211     }
   212     val tokens = toks.toList
   213     (tokens, ctxt, line_structure(tokens, structure))
   214   }
   215 
   216 
   217   /* parse_spans */
   218 
   219   def parse_spans(toks: List[Token]): List[Command_Span.Span] =
   220   {
   221     val result = new mutable.ListBuffer[Command_Span.Span]
   222     val content = new mutable.ListBuffer[Token]
   223     val improper = new mutable.ListBuffer[Token]
   224 
   225     def ship(span: List[Token])
   226     {
   227       val kind =
   228         if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error)) {
   229           val name = span.head.source
   230           val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1)
   231           Command_Span.Command_Span(name, pos)
   232         }
   233         else if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   234         else Command_Span.Malformed_Span
   235       result += Command_Span.Span(kind, span)
   236     }
   237 
   238     def flush()
   239     {
   240       if (!content.isEmpty) { ship(content.toList); content.clear }
   241       if (!improper.isEmpty) { ship(improper.toList); improper.clear }
   242     }
   243 
   244     for (tok <- toks) {
   245       if (tok.is_command) { flush(); content += tok }
   246       else if (tok.is_improper) improper += tok
   247       else { content ++= improper; improper.clear; content += tok }
   248     }
   249     flush()
   250 
   251     result.toList
   252   }
   253 
   254   def parse_spans(input: CharSequence): List[Command_Span.Span] =
   255     parse_spans(scan(input))
   256 
   257 
   258   /* language context */
   259 
   260   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   261     new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
   262 
   263   def no_tokens: Outer_Syntax =
   264   {
   265     require(keywords.isEmpty && lexicon.isEmpty)
   266     new Outer_Syntax(
   267       completion = completion,
   268       language_context = language_context,
   269       has_tokens = false)
   270   }
   271 }