Skip to content

Instantly share code, notes, and snippets.

View jacobappleton's full-sized avatar

Jacob Appleton jacobappleton

View GitHub Profile
@jacobappleton
jacobappleton / ShuntingYard.scala
Created August 9, 2015 21:11
A Shunting Yard algorithm implementation for a rudimentary Regex pattern language.
package io.jacobappleton.compilers.regex
class ShuntingYard(val operatorPrecedence: Map[Char, Int]) {
def toPostfix(s: String): String = toPostfix(s, "", "")
def isOperator(x: Char) = operatorPrecedence.contains(x)
def hasLowerPrecedence(a: Char, b: Char) = operatorPrecedence(a) <= operatorPrecedence(b)
@jacobappleton
jacobappleton / ShuntingYardTests.scala
Created August 9, 2015 21:18
Tests for the Shunting Yard algorithm with parentheses
package io.jacobappleton.compilers.regex
import org.scalatest.FlatSpec
import scala.language.postfixOps
class ShuntingYardTests extends FlatSpec {
it should "when given the Regex a.b|c.d the output should be ab.cd.|" in {
val parser = new ShuntingYard(Map('|' -> 0, '.' -> 1, '*' -> 2))
assert(parser.toPostfix("a.b|c.d") == "ab.cd.|")
}
it should "handle single parentheses" in {
val nonParensNfa = new Regex("a.b|c.d").toNFA()
val parensNfa = new Regex("a.(b|c).d").toNFA()
val nonParensPrinter = new NFAPrinter(nonParensNfa)
val parensPrinter = new NFAPrinter(parensNfa)
val expectedTableNonParens =
"| | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | ε |\n" +
"|--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |\n" +
it should "handle nested parentheses" in {
val nfa = new Regex("j.a.((c|x)|(k|y)).o.b").toNFA()
val printer = new NFAPrinter(nfa)
val expectedTable =
"| | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | ε |\n" +
"|--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |--- |\n" +
"| 0 | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } |\n" +
"| 1 | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | { 2 } | { 0 } | { 0 } | { 0 } | { 0 } | { 0 } | {
@jacobappleton
jacobappleton / RegexWorkerActor.scala
Created August 17, 2015 04:10
An Akka actor to handle Regex pattern parsing
package io.jacobappleton.compilers.server.workers
import akka.actor.{Actor, ActorLogging}
import io.jacobappleton.compilers.automata.DFAPrinter
import io.jacobappleton.compilers.regex.Regex
import io.jacobappleton.compilers.server.workers.RegexWorkerActor.RegexResponse
import spray.json.DefaultJsonProtocol
class RegexWorkerActor extends Actor with ActorLogging {
def receive = {
@jacobappleton
jacobappleton / PatternMatching.scala
Created August 17, 2015 04:17
Pattern matching example
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
@jacobappleton
jacobappleton / Routing.scala
Created September 8, 2015 04:07
Spray routing example
val compilerRoute : Route =
path("") {
redirect("http://www.jacobappleton.io", StatusCodes.PermanentRedirect)
} ~
pathPrefix("app") {
pathPrefix("assets") {
getFromResourceDirectory("web/assets/")
} ~ {
getFromResource("web/index.html")
}
@jacobappleton
jacobappleton / FirstRoute.scala
Created September 8, 2015 04:13
Simple Spray.IO route.
path("") {
redirect("http://www.jacobappleton.io", StatusCodes.PermanentRedirect)
}
@jacobappleton
jacobappleton / SecondRoute.scala
Created September 8, 2015 04:35
A resource route in Spray.IO
pathPrefix("app") {
pathPrefix("assets") {
getFromResourceDirectory("web/assets/")
} ~ {
getFromResource("web/index.html")
}
}
pathPrefix("api") {
path("regex") {
post {
entity(as[String]) { pattern =>
complete {
(regexWorker ? pattern).mapTo[RegexResponse]
}
}
}
}