src/Tools/jEdit/src/token_markup.scala
author wenzelm
Thu, 16 Jun 2011 23:16:06 +0200
changeset 43416 e730cdd97dcf
parent 43414 f0770743b7ec
child 43429 095f90f8dca3
permissions -rw-r--r--
more precise imitatation of original TokenMarker: no locking, interned context etc.;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/token_markup.scala
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     3
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     4
Outer syntax token markup.
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     5
*/
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     6
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     8
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
     9
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    10
import isabelle._
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    11
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    12
import org.gjt.sp.jedit.Buffer
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    13
import org.gjt.sp.jedit.syntax.{Token => JToken, TokenMarker, TokenHandler, ParserRuleSet}
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    14
import javax.swing.text.Segment
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    15
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    16
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    17
object Token_Markup
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    18
{
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    19
  /* extended jEdit syntax styles */
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    20
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    21
  private val plain_range: Int = JToken.ID_COUNT
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    22
  private def check_range(i: Int) { require(0 <= i && i < plain_range) }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    23
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    24
  def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    25
  def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    26
  val hidden: Byte = (3 * plain_range).toByte
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    27
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    28
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    29
  /* token marker */
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    30
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    31
  private val isabelle_rules = new ParserRuleSet("isabelle", "MAIN")
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    32
43416
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    33
  private class Line_Context(val context: Scan.Context, prev: Line_Context)
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    34
    extends TokenMarker.LineContext(isabelle_rules, prev)
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    35
  {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    36
    override def hashCode: Int = context.hashCode
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    37
    override def equals(that: Any): Boolean =
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    38
      that match {
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    39
        case other: Line_Context => context == other.context
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    40
        case _ => false
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    41
      }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    42
  }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    43
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    44
  def token_marker(session: Session, buffer: Buffer): TokenMarker =
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    45
    new TokenMarker {
43416
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    46
      override def markTokens(raw_context: TokenMarker.LineContext,
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    47
          handler: TokenHandler, line: Segment): TokenMarker.LineContext =
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    48
      {
43416
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    49
        val syntax = session.current_syntax()
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    50
43416
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    51
        val context = raw_context.asInstanceOf[Line_Context]
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    52
        val ctxt = if (context == null) Scan.Finished else context.context
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    53
43416
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    54
        val (tokens, ctxt1) = syntax.scan_context(line, ctxt)
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    55
        val context1 = new Line_Context(ctxt1, context)
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    56
43416
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    57
        var offset = 0
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    58
        for (token <- tokens) {
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    59
          val style = Isabelle_Markup.token_markup(syntax, token)
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    60
          val length = token.source.length
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    61
          handler.handleToken(line, style, offset, length, context1)
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    62
          offset += length
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    63
        }
43416
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    64
        handler.handleToken(line, JToken.END, line.count, 0, context1)
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    65
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    66
        val context2 = context1.intern
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    67
        handler.setLineContext(context2)
e730cdd97dcf more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents: 43414
diff changeset
    68
        context2
43414
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    69
      }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    70
    }
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    71
}
f0770743b7ec static token markup, based on outer syntax only;
wenzelm
parents:
diff changeset
    72