Skip to content

Instantly share code, notes, and snippets.

@eiennohito
Created December 9, 2011 07:38
Show Gist options
  • Save eiennohito/1450635 to your computer and use it in GitHub Desktop.
Save eiennohito/1450635 to your computer and use it in GitHub Desktop.
package org.eiennohito.scot.epwing.simple.gaiji
import util.parsing.combinator.RegexParsers
/**
* @author eiennohito
* @since 09.12.11
*/
abstract class Literal {
def process(g: GaijiDict): String
}
case class StringLiteral(s: String) extends Literal {
def process(g: GaijiDict) = s
}
case class Gaiji(v: String) extends Literal {
def process(g: GaijiDict) = {
g(v) match {
case Some(va) => va
case None => "_"
}
}
}
class GaijiParser extends RegexParsers {
def word = "[^\\[]+".r ^^ { StringLiteral(_) }
def gaiji = "[GAIJI=" <~ ".[0-9a-fA-F]".r ~> "]" ^^ { Gaiji(_) }
def article = word ~ (gaiji ~ word).* ^^ {
case w ~ l => w :: l.flatMap {
case gaiji ~ word=> gaiji :: word :: Nil
}
}
def parse(s: String) = parseAll(article, s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment