Created
April 28, 2012 13:13
-
-
Save adamretter/2518994 to your computer and use it in GitHub Desktop.
Scala Parser, trying to parse regexp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package csvvalidator | |
import scala.util.parsing.combinator.syntactical.StandardTokenParsers | |
import scala.io.Source; | |
/** | |
* Adam Retter | |
*/ | |
object TestProblemParser extends StandardTokenParsers { | |
def main(args: Array[String]): Unit = { | |
//left side of first : should be an identifier e.g. anything except whitespace or eol | |
//right side of first : should be an unescaped regular expression | |
TestProblemParser.test("file_path: [A-Z][0-9]{3}:[0-9]") | |
} | |
lexical.delimiters += ("@", ":") | |
def Expr = ColumnRuleExpr+ | |
def ColumnRuleExpr = (ColumnIdentifierExpr <~ ":") ~ ColumnValidationExpr | |
def ColumnIdentifierExpr = ident; | |
def ColumnValidationExpr = elem("regexp", (p: Elem) => !p.chars.contains("""\s""")) | |
def parse(data: String) = { | |
val tokens = new lexical.Scanner(data) | |
phrase(Expr)(tokens) | |
} | |
//Simplify testing | |
def test(exprstr: String) = { | |
parse(exprstr) match { | |
case Success(tree, _) => | |
println("Tree: "+tree) | |
//val v = tree.eval(); | |
//println("Eval: "+v) | |
case e: NoSuccess => Console.err.println(e) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment