Skip to content

Instantly share code, notes, and snippets.

@Rogach
Rogach / gist:1577445
Created January 8, 2012 06:17
Trying to get simple syntax tree out of scala compiler
import scala.tools.nsc.interactive.{Global, RefinedBuildManager}
import scala.tools.nsc.Settings
import scala.tools.nsc.reporters.StoreReporter
import scala.tools.nsc.io._
import scala.tools.nsc.util._
import scala.tools.nsc.interactive.Response
import scala.tools.nsc.util.{Position, OffsetPosition}
import scala.reflect.generic.Trees
val settings = new Settings
@Rogach
Rogach / gist:1646123
Created January 20, 2012 08:24
Simple math interpreter
object Interpreter {
def main(args:Array[String]) = {
(new SimpleParser).parseString("2*3.2 + 3*(36.07/4 - 10)").println
}
import scala.util.parsing.combinator._
class SimpleParser extends JavaTokenParsers {
def expr: Parser[Double] = (
term~"+"~expr ^^ { case a~"+"~b => a + b }
| term~"-"~expr ^^ { case a~"-"~b => a - b }
@Rogach
Rogach / gist:1661713
Created January 23, 2012 08:18
Non-homogeneous tree
package socco
abstract class SoccoTree(val children:List[SoccoTree]) extends Seq[SoccoTree] with SoccoNode[SoccoTree] {
def apply(n:Int) = children(n)
def iterator = children.iterator
def length = children.length
// def add(t:SoccoTree)(implicit builder:SoccoTreeBuilder[B]) = builder.build(this.asInstanceOf[B], children :+ t)
}
trait SoccoNode[B <: SoccoTree] {
def soccoCompanion:SoccoTreeCompanion[B]
@Rogach
Rogach / Help output.txt
Created April 4, 2012 20:16
Scallop examples
test 1.2.3 (c) 2012 Mr Placeholder
Usage: test [OPTION]...
test is an awesome program, which does something funny
Options:
-Dkey=value [key=value]...
some key-value pairs
-d, --donkey
use donkey mode
-m, --monkeys <arg>
@Rogach
Rogach / gist:2308208
Created April 5, 2012 05:20
ScallopConf
object Main extends App {
object Conf extends ScallopConf(arguments) {
val apples:SOption[Int] = opt[Int]("apples")
val bananas = opt[Int]("bananas")
val file = trailArg[String]
verify
}
Conf.apples.get
Conf.bananas.option // not sure about option retreiving syntax
// maybe I should copy the code for SOption from stdlib Option?
@Rogach
Rogach / ScallopTest.scala
Created April 7, 2012 10:19 — forked from alexy/ScallopTest.scala
Holder fails, builder parses trailing args allright
import org.rogach.scallop._
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers
/**
* Created with IntelliJ IDEA.
* User: Alexy
* Date: 4/6/12
* Time: 10:34 AM
@Rogach
Rogach / example.scala
Created April 8, 2012 11:34
scallop examples 2
object Conf extends ScallopConf(List("-a","3","-b","5","tree")) {
val apples = opt[Int]("apples")
val bananas = opt[Int]("bananas")
verify
}
Conf.apples() // 3
Conf.bananas.get // Some(5)
def LogConfig(configItem: String, configValue: Any) = {
val logString = configValue match {
case configValueMap: Map[_,_] => "Configured " + configItem + " to " + configValueMap.mkString("{",", ","}") + "."
case _ => "Configured " + configItem + " to " + configValue + "."
}
info(logString)
}
def readConfig(lines:List[String]):mutable.Map[String,Map[String,String]] = {
// assuming that *all* properties have their sections
if (lines.isEmpty) Map()
else {
val sectionRgx = """\s*\[(.*)\]\s*""".r
val sectionRgx(sectName) = lines.head
val pairs = lines.tail.takeWhile(!_.startsWith("[")).map(_.split("=")).map(a => (a(0),a(1))).toMap
Map(sectName -> pairs) ++ readConfig(lines.tail.dropWhile(!_.startsWith("["))
}
}
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
object ReadXMLFile {
def main(argv: Array[String]) {
try {
val factory = SAXParserFactory.newInstance