src/Pure/Isar/outer_syntax.scala
changeset 58700 4717d18cc619
parent 58697 5bc1d6c4a499
child 58703 883efcc7a50d
equal deleted inserted replaced
58699:e46afcceb781 58700:4717d18cc619
    41 
    41 
    42   /* line-oriented structure */
    42   /* line-oriented structure */
    43 
    43 
    44   object Line_Structure
    44   object Line_Structure
    45   {
    45   {
    46     val init = Line_Structure(0, 0)
    46     val init = Line_Structure()
    47   }
    47   }
    48 
    48 
    49   sealed case class Line_Structure(depth_before: Int, depth: Int)
    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)
    50 }
    55 }
    51 
    56 
    52 final class Outer_Syntax private(
    57 final class Outer_Syntax private(
    53   keywords: Map[String, (String, List[String])] = Map.empty,
    58   keywords: Map[String, (String, List[String])] = Map.empty,
    54   lexicon: Scan.Lexicon = Scan.Lexicon.empty,
    59   lexicon: Scan.Lexicon = Scan.Lexicon.empty,
   148     heading_level(command.name)
   153     heading_level(command.name)
   149 
   154 
   150 
   155 
   151   /* line-oriented structure */
   156   /* line-oriented structure */
   152 
   157 
   153   def line_structure(tokens: List[Token], depth: Int): Outer_Syntax.Line_Structure =
   158   def line_structure(tokens: List[Token], struct: Outer_Syntax.Line_Structure)
   154   {
   159     : Outer_Syntax.Line_Structure =
       
   160   {
       
   161     val improper1 = tokens.forall(_.is_improper)
       
   162     val command1 = tokens.exists(_.is_command)
       
   163 
   155     val depth1 =
   164     val depth1 =
   156       if (tokens.exists(tok => command_kind(tok, Keyword.theory))) 0
   165       if (tokens.exists(tok => command_kind(tok, Keyword.theory))) 0
   157       else depth
   166       else if (command1) struct.after_span_depth
   158     val depth2 =
   167       else struct.span_depth
   159       (depth /: tokens) { case (d, tok) =>
   168 
   160         if (command_kind(tok, Keyword.theory_goal)) 1
   169     val (span_depth1, after_span_depth1) =
   161         else if (command_kind(tok, Keyword.theory)) 0
   170       ((struct.span_depth, struct.after_span_depth) /: tokens) {
   162         else if (command_kind(tok, Keyword.proof_goal)) d + 1
   171         case ((d0, d), tok) =>
   163         else if (command_kind(tok, Keyword.qed)) d - 1
   172           if (command_kind(tok, Keyword.theory_goal)) (2, 1)
   164         else if (command_kind(tok, Keyword.qed_global)) 0
   173           else if (command_kind(tok, Keyword.theory)) (1, 0)
   165         else d
   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)
   166       }
   178       }
   167     Outer_Syntax.Line_Structure(depth1, depth2)
   179 
       
   180     Outer_Syntax.Line_Structure(improper1, command1, depth1, span_depth1, after_span_depth1)
   168   }
   181   }
   169 
   182 
   170 
   183 
   171   /* token language */
   184   /* token language */
   172 
   185 
   195         case Token.Parsers.NoSuccess(_, rest) =>
   208         case Token.Parsers.NoSuccess(_, rest) =>
   196           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   209           error("Unexpected failure of tokenizing input:\n" + rest.source.toString)
   197       }
   210       }
   198     }
   211     }
   199     val tokens = toks.toList
   212     val tokens = toks.toList
   200     (tokens, ctxt, line_structure(tokens, structure.depth))
   213     (tokens, ctxt, line_structure(tokens, structure))
   201   }
   214   }
   202 
   215 
   203 
   216 
   204   /* parse_spans */
   217   /* parse_spans */
   205 
   218