-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c85ba4b
commit 853bb25
Showing
9 changed files
with
99 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
3 changes: 2 additions & 1 deletion
3
cli/src/main/scala/phony/cli/template/parser/TemplateAST.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package phony.cli.template.parser | ||
|
||
sealed trait TemplateAST | ||
case class Text(value: String) extends TemplateAST | ||
case class Text(value: String) extends TemplateAST | ||
case class FunctionCall(func: String, args: Seq[String] = Nil) extends TemplateAST | ||
case class Loop(count: Int, templates: List[TemplateAST]) extends TemplateAST |
63 changes: 39 additions & 24 deletions
63
cli/src/main/scala/phony/cli/template/parser/TemplateExpressionParser.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,52 @@ | ||
package phony.cli.template.parser | ||
|
||
import phony.cli | ||
import phony.cli.template.tokenizer.{Expression, PlainText, TemplateToken} | ||
import phony.cli.util.BaseRegexParser | ||
import phony.cli.template.tokenizer._ | ||
|
||
object TemplateExpressionParser extends BaseRegexParser { | ||
import scala.util.parsing.combinator.Parsers | ||
import scala.util.parsing.input.{NoPosition, Position, Reader} | ||
|
||
def functionName: Parser[String] = | ||
ident ~ rep(ident | ".") ^^ (n => n._1 + n._2.mkString("")) | ||
object TemplateExpressionParser extends Parsers { | ||
override type Elem = TemplateToken | ||
|
||
def paramString: Parser[String] = """\"[0-9a-zA-Z\-"#\?]+\"""".r | ||
class TemplateTokenReader(tokens: Seq[TemplateToken]) extends Reader[TemplateToken] { | ||
override def first: TemplateToken = tokens.head | ||
override def atEnd: Boolean = tokens.isEmpty | ||
override def pos: Position = NoPosition | ||
override def rest: Reader[TemplateToken] = new TemplateTokenReader(tokens.tail) | ||
} | ||
|
||
def functionWithoutArgs: Parser[FunctionCall] = | ||
functionName ^^ (n => FunctionCall(n)) | ||
def functionCall: Parser[FunctionCall] = accept( | ||
"functionCall", { | ||
case f: FunctionCallToken => FunctionCall(f.func, f.args) | ||
} | ||
) | ||
|
||
def functionWithArgs: Parser[FunctionCall] = | ||
functionName ~ "(" ~ repsep(paramString | decimalNumber, ",") ~ ")" ^^ { | ||
case func ~ _ ~ args ~ _ => FunctionCall(func, args) | ||
def plainText: Parser[Text] = accept( | ||
"text", { | ||
case t: PlainTextToken => Text(t.value) | ||
} | ||
) | ||
|
||
def program: Parser[TemplateAST] = | ||
phrase(functionWithArgs | functionWithoutArgs) | ||
def loopStart: Parser[Int] = accept( | ||
"loopStart", { | ||
case t: LoopStartToken => t.count | ||
} | ||
) | ||
|
||
def apply(tokens: Seq[TemplateToken]): Either[Throwable, List[TemplateAST]] = { | ||
val parts = tokens.map { | ||
case Expression(expr) => | ||
parse(program, expr) match { | ||
case Success(result, _) => Right(result) | ||
case NoSuccess(msg, _) => Left(new Exception(s"Error: $msg")) | ||
} | ||
case PlainText(v) => Right(Text(v)) | ||
}.toList | ||
def loop: Parser[Loop] = | ||
loopStart ~ rep(plainText | functionCall) <~ LoopEndToken ^^ { | ||
case c ~ tokens => Loop(c, tokens) | ||
} | ||
|
||
cli.util.sequence(parts) | ||
def program: Parser[List[TemplateAST]] = | ||
rep(loop | functionCall | plainText) | ||
|
||
def apply(tokens: List[TemplateToken]): Either[Throwable, List[TemplateAST]] = { | ||
val reader = new TemplateTokenReader(tokens) | ||
|
||
program(reader) match { | ||
case Success(result, _) => Right(result) | ||
case NoSuccess(msg, _) => Left(new RuntimeException(msg)) | ||
} | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
cli/src/main/scala/phony/cli/template/tokenizer/TemplateToken.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package phony.cli.template.tokenizer | ||
|
||
sealed trait TemplateToken | ||
case class Expression(expr: String) extends TemplateToken | ||
case class PlainText(value: String) extends TemplateToken | ||
case class FunctionCallToken(func: String, args: Seq[String] = Nil) extends TemplateToken | ||
case class PlainTextToken(value: String) extends TemplateToken | ||
case class LoopStartToken(count: Int) extends TemplateToken | ||
case object LoopEndToken extends TemplateToken |
This file contains 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
This file contains 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
This file contains 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