src/Pure/Isar/outer_syntax.scala
author wenzelm
Sat Oct 18 22:41:36 2014 +0200 (2014-10-18)
changeset 58703 883efcc7a50d
parent 58700 4717d18cc619
child 58706 70a947611792
permissions -rw-r--r--
more folds;
     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 ((x, y), tok) =>
   172           if (tok.is_command) {
   173             if (command_kind(tok, Keyword.theory_goal)) (2, 1)
   174             else if (command_kind(tok, Keyword.theory)) (1, 0)
   175             else if (command_kind(tok, Keyword.proof_goal) || tok.source == "{") (y + 2, y + 1)
   176             else if (command_kind(tok, Keyword.qed) || tok.source == "}") (y + 1, y - 1)
   177             else if (command_kind(tok, Keyword.qed_global)) (1, 0)
   178             else (x, y)
   179           }
   180           else (x, y)
   181       }
   182 
   183     Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
   184   }
   185 
   186 
   187   /* token language */
   188 
   189   def scan(input: CharSequence): List[Token] =
   190   {
   191     val in: Reader[Char] = new CharSequenceReader(input)
   192     Token.Parsers.parseAll(
   193         Token.Parsers.rep(Token.Parsers.token(lexicon, is_command)), in) match {
   194       case Token.Parsers.Success(tokens, _) => tokens
   195       case _ => error("Unexpected failure of tokenizing input:\n" + input.toString)
   196     }
   197   }
   198 
   199   def scan_line(
   200     input: CharSequence,
   201     context: Scan.Line_Context,
   202     structure: Outer_Syntax.Line_Structure)
   203     : (List[Token], Scan.Line_Context, Outer_Syntax.Line_Structure) =
   204   {
   205     var in: Reader[Char] = new CharSequenceReader(input)
   206     val toks = new mutable.ListBuffer[Token]
   207     var ctxt = context
   208     while (!in.atEnd) {
   209       Token.Parsers.parse(Token.Parsers.token_line(lexicon, is_command, ctxt), in) match {
   210         case Token.Parsers.Success((x, c), rest) => { toks += x; ctxt = c; in = rest }
   211         case Token.Parsers.NoSuccess(_, rest) =>
   212           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   213       }
   214     }
   215     val tokens = toks.toList
   216     (tokens, ctxt, line_structure(tokens, structure))
   217   }
   218 
   219 
   220   /* parse_spans */
   221 
   222   def parse_spans(toks: List[Token]): List[Command_Span.Span] =
   223   {
   224     val result = new mutable.ListBuffer[Command_Span.Span]
   225     val content = new mutable.ListBuffer[Token]
   226     val improper = new mutable.ListBuffer[Token]
   227 
   228     def ship(span: List[Token])
   229     {
   230       val kind =
   231         if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error)) {
   232           val name = span.head.source
   233           val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1)
   234           Command_Span.Command_Span(name, pos)
   235         }
   236         else if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   237         else Command_Span.Malformed_Span
   238       result += Command_Span.Span(kind, span)
   239     }
   240 
   241     def flush()
   242     {
   243       if (!content.isEmpty) { ship(content.toList); content.clear }
   244       if (!improper.isEmpty) { ship(improper.toList); improper.clear }
   245     }
   246 
   247     for (tok <- toks) {
   248       if (tok.is_command) { flush(); content += tok }
   249       else if (tok.is_improper) improper += tok
   250       else { content ++= improper; improper.clear; content += tok }
   251     }
   252     flush()
   253 
   254     result.toList
   255   }
   256 
   257   def parse_spans(input: CharSequence): List[Command_Span.Span] =
   258     parse_spans(scan(input))
   259 
   260 
   261   /* language context */
   262 
   263   def set_language_context(context: Completion.Language_Context): Outer_Syntax =
   264     new Outer_Syntax(keywords, lexicon, completion, context, has_tokens)
   265 
   266   def no_tokens: Outer_Syntax =
   267   {
   268     require(keywords.isEmpty && lexicon.isEmpty)
   269     new Outer_Syntax(
   270       completion = completion,
   271       language_context = language_context,
   272       has_tokens = false)
   273   }
   274 }