Skip to content

Commit

Permalink
Use strings in place of regex and literal() wraps where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmay committed Feb 5, 2013
1 parent b35b265 commit af060d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
26 changes: 13 additions & 13 deletions src/main/scala/org/moe/parser/Expressions.scala
Expand Up @@ -8,8 +8,8 @@ import org.moe.ast._
trait Expressions extends Literals with JavaTokenParsers {

private lazy val array_index_rule = sigil ~
(namespacedIdentifier <~ literal("[")) ~
(expression <~ literal("]"))
(namespacedIdentifier <~ "[") ~
(expression <~ "]")

def expression: Parser[AST] = (
arrayIndex
Expand All @@ -23,37 +23,37 @@ trait Expressions extends Literals with JavaTokenParsers {
| expressionParens
)

def expressionParens: Parser[AST] = literal("(") ~> expression <~ literal(")")
def expressionParens: Parser[AST] = "(" ~> expression <~ ")"

// List stuff
def list: Parser[List[AST]] = (",?".r ~> repsep(expression, ",") <~ ",?".r)
def array: Parser[ArrayLiteralNode] = literal("(") ~> list <~ literal(")") ^^ ArrayLiteralNode
def list: Parser[List[AST]] = (literal(",").? ~> repsep(expression, ",") <~ literal(",").?)
def array: Parser[ArrayLiteralNode] = "(" ~> list <~ ")" ^^ ArrayLiteralNode
def arrayRef: Parser[ArrayRefLiteralNode] =
literal("[") ~> list <~ literal("]") ^^ ArrayRefLiteralNode
"[" ~> list <~ "]" ^^ ArrayRefLiteralNode

// Hash stuff
def barehashKey: Parser[StringLiteralNode] =
"""[0-9\w_]*""".r ^^ StringLiteralNode
def hashKey: Parser[AST] = scalar | barehashKey
def pair: Parser[PairLiteralNode] =
(hashKey <~ literal("=>")) ~ expression ^^ { case k ~ v => PairLiteralNode(k, v) }
(hashKey <~ "=>") ~ expression ^^ { case k ~ v => PairLiteralNode(k, v) }
def hashContent: Parser[List[PairLiteralNode]] =
repsep(pair, ",")
def hash: Parser[HashLiteralNode] =
literal("(") ~> hashContent <~ literal(")") ^^ HashLiteralNode
"(" ~> hashContent <~ ")" ^^ HashLiteralNode
def hashRef: Parser[HashRefLiteralNode] =
literal("{") ~> hashContent <~ literal("}") ^^ HashRefLiteralNode
"{" ~> hashContent <~ "}" ^^ HashRefLiteralNode

// Variable stuff
def sigil = """[$@%]""".r
def varname = sigil ~ namespacedIdentifier ^^ { case a ~ b => a + b }
def variable = varname ^^ VariableAccessNode

def simpleScalar = literal("$") ~> namespacedIdentifier ^^ {i: String => VariableAccessNode("$" + i) }
def simpleArray = literal("@") ~> namespacedIdentifier ^^ {i: String => VariableAccessNode("@" + i) }
def simpleHash = literal("%") ~> namespacedIdentifier ^^ {i: String => VariableAccessNode("%" + i) }
def simpleScalar = "$" ~> namespacedIdentifier ^^ {i: String => VariableAccessNode("$" + i) }
def simpleArray = "@" ~> namespacedIdentifier ^^ {i: String => VariableAccessNode("@" + i) }
def simpleHash = "%" ~> namespacedIdentifier ^^ {i: String => VariableAccessNode("%" + i) }

def declaration = "my".r ~> varname ~ ("=".r ~> expression).? ^^ {
def declaration = "my" ~> varname ~ ("=" ~> expression).? ^^ {
case v ~ expr => VariableDeclarationNode(v, expr.getOrElse(UndefLiteralNode()))
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/org/moe/parser/Literals.scala
Expand Up @@ -45,8 +45,8 @@ trait Literals extends Base {
def singleQuoteStringContents: Parser[StringLiteralNode] =
"""([^'\p{Cntrl}\\]|\\[\\'"bfnrt]|\\x\{[a-fA-F0-9]{4}\})*""".r ^^ StringLiteralNode

def doubleQuoteString: Parser[StringLiteralNode] = "\"".r ~> doubleQuoteStringContents <~ "\"".r
def singleQuoteString: Parser[StringLiteralNode] = "'".r ~> singleQuoteStringContents <~ "'".r
def doubleQuoteString: Parser[StringLiteralNode] = "\"" ~> doubleQuoteStringContents <~ "\""
def singleQuoteString: Parser[StringLiteralNode] = "'" ~> singleQuoteStringContents <~ "'"

def string: Parser[StringLiteralNode] = doubleQuoteString | singleQuoteString

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/moe/parser/Statements.scala
Expand Up @@ -22,7 +22,7 @@ trait Statements extends Expressions {
def blockContent: Parser[StatementsNode] =
statements <~ statementDelim.?
def block: Parser[StatementsNode] =
"""\{""".r ~> blockContent <~ """\}""".r
"{" ~> blockContent <~ "}"

def doBlock: Parser[StatementsNode] = "do".r ~> block
def scopeBlock: Parser[ScopeNode] = block ^^ { ScopeNode(_) }
Expand Down

0 comments on commit af060d3

Please sign in to comment.