Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simple parse to ast

  • Loading branch information...
commit ab4fd048e90aaa9283b0c5ee7bfe7f50665135f3 1 parent 49f3da5
@mwunsch authored
View
6 src/main/scala/com/gilt/handlebars/Handlebars.scala
@@ -4,8 +4,12 @@ import com.gilt.handlebars._
object Handlebars {
- def parse(template: String, delimiters: (String, String) = ("{{", "}}")) = {
+ def parse(template: String, delimiters: (String, String) = ("{{", "}}")): Program = {
HandlebarsGrammar(delimiters).scan(template)
}
+ def eval(context: Any, program: Program) = {
+
+ }
+
}
View
8 src/main/scala/com/gilt/handlebars/HandlebarsGrammar.scala
@@ -24,8 +24,12 @@ object HandlebarsGrammar {
class HandlebarsGrammar(delimiters: (String, String)) extends JavaTokenParsers {
- def scan(in: String) = {
- parseAll(root, in)
+ def scan(in: String): Program = {
+ parseAll(root, in) match {
+ case Success(result, _) => result
+ // case NoSuccess(msg, next) => throw an error
+ case _ => Program(List(Content("Nothing to see here")))
+ }
}
def root: Parser[Program] = rep(content | statement) ^^ {Program(_)}
View
4 src/test/scala/com/gilt/handlebars/HandlebarsGrammarSpec.scala
@@ -9,7 +9,6 @@ class HandlebarsGrammarSpec extends Specification with ParserMatchers {
val parsers = HandlebarsGrammar(("{{","}}"))
"The Handlebars root grammar" should {
-
"parse simple mustaches" in {
parsers.root("{{foo}}") must haveSuccessResult("""Mustache\(.*foo\)""")
}
@@ -81,7 +80,6 @@ class HandlebarsGrammarSpec extends Specification with ParserMatchers {
"report line numbers in failures" in {
parsers.root("hello\nmy\n{{foo}") must haveFailureMsg("^[3")
}
-
-
}
+
}
View
12 src/test/scala/com/gilt/handlebars/HandlebarsSpec.scala
@@ -0,0 +1,12 @@
+package com.gilt.handlebars
+
+import org.specs2.mutable._
+import com.gilt.handlebars._
+
+class HandlebarsSpec extends Specification {
+ "Handlebars" should {
+ "parse a template into an ast" in {
+ Handlebars.parse("{{foo}} bar") must beAnInstanceOf[Program]
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.