Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSS Parser
import util.parsing.combinator.JavaTokenParsers
case class Rule(val selectors : List[String], val properties : List[Property])
case class Property(name: String, value: String)
object CssParser extends JavaTokenParsers {
def rules = rule+
private def rule = selectors ~ properties ^^ {
case s ~ p => Rule(s,p)
} ;
private def selectors = repsep(selector, ",");
private def selector = """[A-Za-z0-9-_~:#=">$\*\.\[\]\|\s\^\+\(\)]+""".r;
private def properties = "{" ~> repsep(property, ";") <~ "}";
private def property = propertyName ~ propertyValue ^^ {
case n ~ v => Property(n,v);
};
private def propertyName = """([\w\d-_]+)""".r <~ ":";
private def propertyValue = """[^;}]+""".r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment