Skip to content

Instantly share code, notes, and snippets.

Avatar

Jacob Appleton jacobappleton

View GitHub Profile
@jacobappleton
jacobappleton / ShuntingYardTests.scala
Created Aug 9, 2015
Tests for the Shunting Yard algorithm with parentheses
View ShuntingYardTests.scala
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.|")
}
View SimpleParenthesesTests.scala
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" +
@jacobappleton
jacobappleton / ShuntingYard.scala
Created Aug 9, 2015
A Shunting Yard algorithm implementation for a rudimentary Regex pattern language.
View ShuntingYard.scala
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)
View NestedParenthesesTests.scala
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 Aug 17, 2015
An Akka actor to handle Regex pattern parsing
View RegexWorkerActor.scala
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 = {
View PatternMatching.scala
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
@jacobappleton
jacobappleton / Routing.scala
Created Sep 8, 2015
Spray routing example
View Routing.scala
val compilerRoute : Route =
path("") {
redirect("http://www.jacobappleton.io", StatusCodes.PermanentRedirect)
} ~
pathPrefix("app") {
pathPrefix("assets") {
getFromResourceDirectory("web/assets/")
} ~ {
getFromResource("web/index.html")
}
View FirstRoute.scala
path("") {
redirect("http://www.jacobappleton.io", StatusCodes.PermanentRedirect)
}
@jacobappleton
jacobappleton / SecondRoute.scala
Created Sep 8, 2015
A resource route in Spray.IO
View SecondRoute.scala
pathPrefix("app") {
pathPrefix("assets") {
getFromResourceDirectory("web/assets/")
} ~ {
getFromResource("web/index.html")
}
}
View ThirdRoute.scala
pathPrefix("api") {
path("regex") {
post {
entity(as[String]) { pattern =>
complete {
(regexWorker ? pattern).mapTo[RegexResponse]
}
}
}
}
You can’t perform that action at this time.