src/Pure/Isar/outer_syntax.scala
changeset 59924 801b979ec0c2
parent 59735 24bee1b11fce
child 59939 7d46aa03696e
equal deleted inserted replaced
59923:b21c82422d65 59924:801b979ec0c2
   147 
   147 
   148   /** parsing **/
   148   /** parsing **/
   149 
   149 
   150   /* line-oriented structure */
   150   /* line-oriented structure */
   151 
   151 
   152   def line_structure(tokens: List[Token], struct: Outer_Syntax.Line_Structure)
   152   def line_structure(tokens: List[Token], structure: Outer_Syntax.Line_Structure)
   153     : Outer_Syntax.Line_Structure =
   153     : Outer_Syntax.Line_Structure =
   154   {
   154   {
   155     val improper1 = tokens.forall(_.is_improper)
   155     val improper1 = tokens.forall(_.is_improper)
   156     val command1 = tokens.exists(_.is_command)
   156     val command1 = tokens.exists(_.is_command)
   157 
   157 
   158     val depth1 =
   158     val depth1 =
   159       if (tokens.exists(tok => tok.is_command_kind(keywords, Keyword.theory))) 0
   159       if (tokens.exists(tok => tok.is_command_kind(keywords, Keyword.theory))) 0
   160       else if (command1) struct.after_span_depth
   160       else if (command1) structure.after_span_depth
   161       else struct.span_depth
   161       else structure.span_depth
   162 
   162 
   163     val (span_depth1, after_span_depth1) =
   163     val (span_depth1, after_span_depth1) =
   164       ((struct.span_depth, struct.after_span_depth) /: tokens) {
   164       ((structure.span_depth, structure.after_span_depth) /: tokens) {
   165         case ((x, y), tok) =>
   165         case ((x, y), tok) =>
   166           if (tok.is_command) {
   166           if (tok.is_command) {
   167             if (tok.is_command_kind(keywords, Keyword.theory_goal))
   167             if (tok.is_command_kind(keywords, Keyword.theory_goal))
   168               (2, 1)
   168               (2, 1)
   169             else if (tok.is_command_kind(keywords, Keyword.theory))
   169             else if (tok.is_command_kind(keywords, Keyword.theory))
   192     val improper = new mutable.ListBuffer[Token]
   192     val improper = new mutable.ListBuffer[Token]
   193 
   193 
   194     def ship(span: List[Token])
   194     def ship(span: List[Token])
   195     {
   195     {
   196       val kind =
   196       val kind =
   197         if (span.nonEmpty && span.head.is_command && !span.exists(_.is_error)) {
   197         if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   198           val name = span.head.source
   198         else if (span.exists(_.is_error)) Command_Span.Malformed_Span
   199           val pos = Position.Range(Text.Range(0, Symbol.iterator(name).length) + 1)
   199         else
   200           Command_Span.Command_Span(name, pos)
   200           span.find(_.is_command) match {
   201         }
   201             case None => Command_Span.Malformed_Span
   202         else if (span.forall(_.is_improper)) Command_Span.Ignored_Span
   202             case Some(cmd) =>
   203         else Command_Span.Malformed_Span
   203               val name = cmd.source
       
   204               val offset =
       
   205                 (0 /: span.takeWhile(_ != cmd)) {
       
   206                   case (i, tok) => i + Symbol.iterator(tok.source).length }
       
   207               val end_offset = offset + Symbol.iterator(name).length
       
   208               val pos = Position.Range(Text.Range(offset, end_offset) + 1)
       
   209               Command_Span.Command_Span(name, pos)
       
   210           }
   204       result += Command_Span.Span(kind, span)
   211       result += Command_Span.Span(kind, span)
   205     }
   212     }
   206 
   213 
   207     def flush()
   214     def flush()
   208     {
   215     {
   209       if (content.nonEmpty) { ship(content.toList); content.clear }
   216       if (content.nonEmpty) { ship(content.toList); content.clear }
   210       if (improper.nonEmpty) { ship(improper.toList); improper.clear }
   217       if (improper.nonEmpty) { ship(improper.toList); improper.clear }
   211     }
   218     }
   212 
   219 
   213     for (tok <- toks) {
   220     for (tok <- toks) {
   214       if (tok.is_command) { flush(); content += tok }
   221       if (tok.is_improper) improper += tok
   215       else if (tok.is_improper) improper += tok
   222       else if (tok.is_private ||
       
   223         tok.is_command && (!content.exists(_.is_private) || content.exists(_.is_command)))
       
   224       { flush(); content += tok }
   216       else { content ++= improper; improper.clear; content += tok }
   225       else { content ++= improper; improper.clear; content += tok }
   217     }
   226     }
   218     flush()
   227     flush()
   219 
   228 
   220     result.toList
   229     result.toList