本文整理汇总了Scala中scala.util.parsing.combinator.syntactical.StandardTokenParsers类的典型用法代码示例。如果您正苦于以下问题:Scala StandardTokenParsers类的具体用法?Scala StandardTokenParsers怎么用?Scala StandardTokenParsers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StandardTokenParsers类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: JSON
//设置package包名称以及导入依赖的类
package examples.parsing
import scala.util.parsing.combinator.syntactical.StandardTokenParsers
object JSON extends StandardTokenParsers {
lexical.delimiters += ("{", "}", "[", "]", ":", ",")
lexical.reserved += ("null", "true", "false")
def obj : Parser[Any] = "{" ~ repsep(member, ",") ~ "}"
def arr : Parser[Any] = "[" ~ repsep(value, ",") ~ "]"
def member: Parser[Any] = ident ~ ":" ~ value
def value : Parser[Any] = ident | numericLit | obj | arr |
"null" | "true" | "false"
def main(args: Array[String]) {
val input = args.mkString(" ")
val tokens = new lexical.Scanner(input)
println(input)
println(phrase(value)(tokens))
}
}
object JSON1 extends StandardTokenParsers {
lexical.delimiters += ("{", "}", "[", "]", ":", ",")
lexical.reserved += ("null", "true", "false")
def obj: Parser[Map[String, Any]] =
"{" ~> repsep(member, ",") <~ "}" ^^ (Map() ++ _)
def arr: Parser[List[Any]] =
"[" ~> repsep(value, ",") <~ "]"
def member: Parser[(String, Any)] =
ident ~ ":" ~ value ^^ { case name ~ ":" ~ value => (name -> value) }
def value: Parser[Any] =
ident | numericLit ^^ (_.toInt) | obj | arr |
"null" ^^^ null | "true" ^^^ true | "false" ^^^ false
def main(args: Array[String]) {
val input = args.mkString(" ")
val tokens = new lexical.Scanner(input)
println(input)
println(phrase(value)(tokens))
}
}
示例2: miniML
//设置package包名称以及导入依赖的类
package examples.parsing
import scala.util.parsing.combinator.syntactical.StandardTokenParsers
import scala.util.parsing.combinator.syntactical.StandardTokenParsers
object miniML extends StandardTokenParsers {
lexical.delimiters += ("(", ")", ".", "=")
lexical.reserved += ("lambda", "let", "in")
def expr: Parser[Any] = (
"let" ~ ident ~ "=" ~ expr ~ "in" ~ expr
| "lambda" ~ ident ~ "." ~ expr
| simpleExpr ~ rep(expr)
)
def simpleExpr: Parser[Any] = (
ident
| "(" ~ expr ~ ")"
)
def main(args: Array[String]) {
val input = args.mkString(" ")
val tokens = new lexical.Scanner(input)
println(input)
println(phrase(expr)(tokens))
}
}
object miniML1 extends StandardTokenParsers {
class Expr
case class Let(x: String, expr: Expr, body: Expr) extends Expr
case class Lambda(x: String, expr: Expr) extends Expr
case class Apply(fun: Expr, arg: Expr) extends Expr
case class Var(x: String) extends Expr
lexical.delimiters += ("(", ")", ".", "=")
lexical.reserved += ("lambda", "let", "in")
def expr: Parser[Expr] = (
"let" ~ ident ~ "=" ~ expr ~ "in" ~ expr ^^ { case "let" ~ x ~ "=" ~ e ~ "in" ~ b => Let(x, e, b) }
| "lambda" ~ ident ~ "." ~ expr ^^ { case "lambda" ~ x ~ "." ~ e => Lambda(x, e) }
| simpleExpr ~ rep(expr) ^^ { case f ~ as => (f /: as) (Apply) }
)
def simpleExpr: Parser[Expr] = (
ident ^^ { Var }
| "(" ~> expr <~ ")"
)
def main(args: Array[String]) {
val input = args.mkString(" ")
val tokens = new lexical.Scanner(input)
println(input)
println(phrase(expr)(tokens))
}
}
示例3: listParsers
//设置package包名称以及导入依赖的类
package examples.parsing
import scala.util.parsing.combinator.syntactical.StandardTokenParsers
object listParsers extends StandardTokenParsers {
lexical.delimiters ++= List("(", ")", ",")
def expr: Parser[Any] = "(" ~ exprs ~ ")" | ident | numericLit
def exprs: Parser[Any] = expr ~ rep ("," ~ expr)
def main(args: Array[String]) {
val input = args.mkString(" ")
val tokens = new lexical.Scanner(input)
println(input)
println(phrase(expr)(tokens))
}
}
object listParsers1 extends StandardTokenParsers {
lexical.delimiters ++= List("(", ")", ",")
def expr: Parser[Any] = "(" ~> exprs <~ ")" | ident | numericLit
def exprs: Parser[List[Any]] = expr ~ rep ("," ~> expr) ^^ { case x ~ y => x :: y }
def main(args: Array[String]) {
val input = args.mkString(" ")
val tokens = new lexical.Scanner(input)
println(input)
println(phrase(expr)(tokens))
}
}
示例4: Exercise08
//设置package包名称以及导入依赖的类
package forimpatient.chapter19
import scala.collection.mutable.Map
import scala.util.parsing.combinator.syntactical.StandardTokenParsers
object Exercise08 extends App {
println("Chapter 19 Exercise 08")
val parser = new ExprParser
val result = parser.parseAll(parser.program, "a = 3 - 4 - 5; out = b; b = a + 1; out = b; out = 5 * 2 ^ 3")
class ExprParser extends StandardTokenParsers {
lexical.delimiters += ("+", "-", "*", "(", ")", ";", "%", "/", "^", "=")
val number = numericLit
val varName = ident
val variables = Map[String, Int]()
def parseAll[T](p: Parser[T], in: String): ParseResult[T] = phrase(p)(new lexical.Scanner(in))
def program: Parser[Any] = rep(statement)
def expr: Parser[Int] = term ~ rep(("+" | "-") ~ term ^^ {
case "+" ~ t => t
case "-" ~ t => -t
}) ^^ {case t ~ r => t + r.sum}
def term: Parser[Int] = pow ~ rep(("*" | "/" | "%") ~ pow) ^^ {
case f ~ r => r.foldLeft(f)((n, pair) => pair._1 match {
case "*" => n * pair._2;
case "/" => n / pair._2
case "%" => n % pair._2
})
}
def pow: Parser[Int] = rep(factor <~ "^") ~ factor ^^ {
case r ~ f => r.foldRight(f)(math.pow(_, _).toInt)
}
def factor: Parser[Int] = number ^^ (_.toInt)| "(" ~> expr <~ ")" | variable
def variable: Parser[Int] = varName ^^ {
v => variables.getOrElse(v, 0)
}
def statement: Parser[Any] = varName ~ ("=" ~> expr <~ opt(";")) ^^ {
case "out" ~ e => println(e)
case v ~ e => variables(v) = e
} | expr
}
}