src/Pure/Tools/isabelle_syntax.scala
author wenzelm
Fri, 19 Dec 2008 20:37:29 +0100
changeset 29140 e7ac5bb20aed
parent 27954 4558d93e83b7
child 29552 5b21c79785b0
permissions -rw-r--r--
removed Ids;

/*  Title:      Pure/Tools/isabelle_syntax.scala
    Author:     Makarius

Isabelle outer syntax.
*/

package isabelle

import java.util.{Properties, Enumeration}


object IsabelleSyntax {

  /* string token */

  def append_string(str: String, result: StringBuilder) = {
    result.append("\"")
    for (c <- str) {
      if (c < 32 || c == '\\' || c == '\"') {
        result.append("\\")
        if (c < 10) result.append('0')
        if (c < 100) result.append('0')
        result.append(c.asInstanceOf[Int].toString)
      }
      else result.append(c)
    }
    result.append("\"")
  }

  def encode_string(str: String) = {
    val result = new StringBuilder(str.length + 20)
    append_string(str, result)
    result.toString
  }


  /* properties */

  def append_properties(props: Properties, result: StringBuilder) = {
    result.append("(")
    val names = props.propertyNames.asInstanceOf[Enumeration[String]]
    while (names.hasMoreElements) {
      val name = names.nextElement; val value = props.getProperty(name)
      append_string(name, result); result.append(" = "); append_string(value, result)
      if (names.hasMoreElements) result.append(", ")
    }
    result.append(")")
  }

  def encode_properties(props: Properties) = {
    val result = new StringBuilder(40)
    append_properties(props, result)
    result.toString
  }

}