Skip to content

Instantly share code, notes, and snippets.

View kmizu's full-sized avatar

Kota Mizushima kmizu

View GitHub Profile
import scala.io._
import scala.util.parsing.combinator.syntactical._
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Label
import org.objectweb.asm.Opcodes._
object Calculator extends StandardTokenParsers {
lexical.delimiters ++= List(
"(", ")","+","-","*","/", ">", "<", ">=", "<=", "=", ";"
import scala.util.parsing.combinator.{Parsers, ImplicitConversions}
import scala.util.parsing.input.CharArrayReader
import scala.util.parsing.input.CharSequenceReader
object GrassParser extends Parsers {
sealed abstract class I
type Code = List[I]
type Env = List[V]
type Ret = List[F]
case class App(m: Int, n: Int) extends I
import scala.xml._
object Attrs {
def unapplySeq(data: MetaData): Option[Seq[(String, String)]] = {
Some(data.map{(m:MetaData) => (m.key, m.value(0).text)}.toList)
}
def main(args: Array[String]) {
val foo = <foo a="1" b="2" c="3"/>
//困ったことに、XMLリテラルは属性の順番を保存してくれないので、
//ファイルから読み込むとか別の方法が必要
//XML.loadStringとかだと属性が逆順で読み込まれるっぽい
/*
* This file can be compiled only in Scala 2.8.0 or later version.
* In Scala 2.7.X, this file cannot be compiled.
*/
import scala.util.parsing.combinator._
import scala.util.matching._
trait ImplicitDisabledRegexParsers {parsers: RegexParsers =>
override def literal(s: String): Parser[String] = parsers.literal(s)
override def regex(r: Regex): Parser[String] = parsers.regex(r)
/**
* This code doen't work in Scala 2.7.X.
*/
import scala.util.parsing.combinator._
import scala.util.parsing.input._
object LeftRecursion extends PackratParsers {
type Elem = Char
lazy val any = elem("", _ != CharSequenceReader.EofCh)
lazy val P: PackratParser[Any] = A ~ not(any)
object CommentInTypeAlias {
type Callable[A] = {
/** This is a dummy comment. */
def call: A
}
}
import java.io._
object JSONLiteral {
sealed trait JSONValue
case class JSONObject(members: Map[String, JSONValue]) extends JSONValue
case class JSONString(value: String) extends JSONValue
case class JSONNumber(value: Double) extends JSONValue
case class JSONArray(members: List[JSONValue]) extends JSONValue
implicit def int2JSONNumber(value: Int) = JSONNumber(value)
implicit def double2JSONNumber(value: Double) = JSONNumber(value)
implicit def string2JSONString(value: String) = JSONString(value)
object StringDump{
class DumpableString(str: String) {
def dump = StringDump.dump(str)
}
implicit def enrichString(str: String) = new DumpableString(str)
def dump(str:String):String={
str.getBytes("UTF-8").map{
case 34 => "\\\""
case 92 => "\\"
case n if 32 <= n && n <= 126 => new String(Array[Byte](n))
object F {
class Link(f: () => Link) extends (() => Link) {
def apply(): Link = f()
}
def f(): Link = {
def n(x: Int): Link = {
println(x)
new Link(() => n(x + 1))
}
n(0)
import scala.util.parsing.combinator._
object Example extends RegexParsers {
/** String#rの型はRegexで、Regexは~なんてメソッドは当然持っていない
* (~はParserのメソッドで、レシーバのParserと引数のParserを連結した新しい
* Parserを生成するメソッド)
* だが、Regex → Parser[String]へのimplicit conversionがRegexParsersで
* 定義されているため、Regexに~を呼び出すと、Parser[String]に自動変換される。
* もちろん、引数に来たRegexもParser[String]に変換される。
* RubyでやるならRegex相当クラスを拡張して、~メソッドを直接定義してやる、などの方法が