src/Tools/jEdit/src/structure_matching.scala
author wenzelm
Wed, 22 Oct 2014 16:44:57 +0200
changeset 58762 4fedc5d4b2fe
parent 58756 eb5d0c58564d
child 58763 1b943a82d5ed
permissions -rw-r--r--
restricted scanning;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/structure_matching.scala
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     3
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     4
Structure matcher for Isabelle/Isar outer syntax.
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     5
*/
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     6
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     8
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     9
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    10
import isabelle._
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    11
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    12
import org.gjt.sp.jedit.textarea.{TextArea, StructureMatcher}
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    13
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    14
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    15
object Structure_Matching
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    16
{
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    17
  object Isabelle_Matcher extends StructureMatcher
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    18
  {
58754
wenzelm
parents: 58752
diff changeset
    19
    def find_block(
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    20
      open: Token => Boolean,
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    21
      close: Token => Boolean,
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    22
      reset: Token => Boolean,
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    23
      restrict: Token => Boolean,
58754
wenzelm
parents: 58752
diff changeset
    24
      it: Iterator[Text.Info[Token]]): Option[(Text.Range, Text.Range)] =
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    25
    {
58754
wenzelm
parents: 58752
diff changeset
    26
      val range1 = it.next.range
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    27
      it.takeWhile(info => !info.info.is_command || restrict(info.info)).
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    28
        scanLeft((range1, 1))(
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    29
          { case ((r, d), Text.Info(range, tok)) =>
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    30
              if (open(tok)) (range, d + 1)
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    31
              else if (close(tok)) (range, d - 1)
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    32
              else if (reset(tok)) (range, 0)
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    33
              else (r, d) }
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    34
        ).collectFirst({ case (range2, 0) => (range1, range2) })
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    35
    }
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    36
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
    37
    def find_pair(text_area: TextArea): Option[(Text.Range, Text.Range)] =
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    38
    {
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    39
      val buffer = text_area.getBuffer
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    40
      val caret_line = text_area.getCaretLine
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
    41
      val caret = text_area.getCaretPosition
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    42
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    43
      PIDE.session.recent_syntax match {
58750
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    44
        case syntax: Outer_Syntax
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    45
        if syntax != Outer_Syntax.empty =>
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    46
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    47
          val limit = PIDE.options.value.int("jedit_structure_limit") max 0
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    48
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    49
          def iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] =
58756
eb5d0c58564d ignore improper tokens to avoid ambiguity of Range.touches (assuming that relevant tokens are separated properly);
wenzelm
parents: 58755
diff changeset
    50
            Token_Markup.line_token_iterator(syntax, buffer, line, line + lim).
eb5d0c58564d ignore improper tokens to avoid ambiguity of Range.touches (assuming that relevant tokens are separated properly);
wenzelm
parents: 58755
diff changeset
    51
              filter(_.info.is_proper)
58750
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    52
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    53
          def rev_iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] =
58756
eb5d0c58564d ignore improper tokens to avoid ambiguity of Range.touches (assuming that relevant tokens are separated properly);
wenzelm
parents: 58755
diff changeset
    54
            Token_Markup.line_token_reverse_iterator(syntax, buffer, line, line - lim).
eb5d0c58564d ignore improper tokens to avoid ambiguity of Range.touches (assuming that relevant tokens are separated properly);
wenzelm
parents: 58755
diff changeset
    55
              filter(_.info.is_proper)
58750
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    56
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    57
          def caret_iterator(): Iterator[Text.Info[Token]] =
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    58
            iterator(caret_line).dropWhile(info => !info.range.touches(caret))
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    59
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    60
          def rev_caret_iterator(): Iterator[Text.Info[Token]] =
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    61
            rev_iterator(caret_line).dropWhile(info => !info.range.touches(caret))
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    62
58756
eb5d0c58564d ignore improper tokens to avoid ambiguity of Range.touches (assuming that relevant tokens are separated properly);
wenzelm
parents: 58755
diff changeset
    63
          iterator(caret_line, 1).find(info => info.range.touches(caret))
eb5d0c58564d ignore improper tokens to avoid ambiguity of Range.touches (assuming that relevant tokens are separated properly);
wenzelm
parents: 58755
diff changeset
    64
          match {
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    65
            case Some(Text.Info(range1, tok)) if syntax.command_kind(tok, Keyword.theory_goal) =>
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    66
              find_block(
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    67
                syntax.command_kind(_, Keyword.proof_goal),
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    68
                syntax.command_kind(_, Keyword.qed),
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    69
                syntax.command_kind(_, Keyword.qed_global),
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    70
                t =>
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    71
                  syntax.command_kind(t, Keyword.diag) ||
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    72
                  syntax.command_kind(t, Keyword.proof),
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    73
                caret_iterator())
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    74
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    75
            case Some(Text.Info(range1, tok)) if syntax.command_kind(tok, Keyword.proof_goal) =>
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    76
              find_block(
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    77
                syntax.command_kind(_, Keyword.proof_goal),
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    78
                syntax.command_kind(_, Keyword.qed),
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    79
                _ => false,
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    80
                t =>
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    81
                  syntax.command_kind(t, Keyword.diag) ||
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
    82
                  syntax.command_kind(t, Keyword.proof),
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    83
                caret_iterator())
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    84
58750
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    85
            case Some(Text.Info(range1, tok)) if syntax.command_kind(tok, Keyword.qed_global) =>
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    86
              rev_caret_iterator().find(info => syntax.command_kind(info.info, Keyword.theory))
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
    87
              match {
58750
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    88
                case Some(Text.Info(range2, tok))
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
    89
                if syntax.command_kind(tok, Keyword.theory_goal) => Some((range1, range2))
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
    90
                case _ => None
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
    91
              }
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
    92
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    93
            case Some(Text.Info(range1, tok)) if syntax.command_kind(tok, Keyword.qed) =>
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    94
              find_block(
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    95
                syntax.command_kind(_, Keyword.qed),
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    96
                t =>
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    97
                  syntax.command_kind(t, Keyword.proof_goal) ||
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    98
                  syntax.command_kind(t, Keyword.theory_goal),
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
    99
                _ => false,
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   100
                t =>
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   101
                  syntax.command_kind(t, Keyword.diag) ||
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   102
                  syntax.command_kind(t, Keyword.proof) ||
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   103
                  syntax.command_kind(t, Keyword.theory_goal),
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   104
                rev_caret_iterator())
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   105
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   106
            case Some(Text.Info(range1, tok)) if tok.is_begin =>
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   107
              find_block(_.is_begin, _.is_end, _ => false, _ => true, caret_iterator())
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   108
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   109
            case Some(Text.Info(range1, tok)) if tok.is_end =>
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   110
              find_block(_.is_end, _.is_begin, _ => false, _ => true, rev_caret_iterator())
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   111
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   112
            case _ => None
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   113
          }
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   114
        case _ => None
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   115
      }
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   116
    }
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   117
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   118
    def getMatch(text_area: TextArea): StructureMatcher.Match =
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   119
      find_pair(text_area) match {
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   120
        case Some((_, range)) =>
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   121
          val line = text_area.getBuffer.getLineOfOffset(range.start)
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   122
          new StructureMatcher.Match(Structure_Matching.Isabelle_Matcher,
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   123
            line, range.start, line, range.stop)
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   124
        case None => null
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   125
      }
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   126
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   127
    def selectMatch(text_area: TextArea)
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   128
    {
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   129
      // FIXME
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   130
    }
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   131
  }
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   132
}
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   133