当前位置: 首页>>代码示例>>Scala>>正文


Scala StandardTokenParsers类代码示例

本文整理汇总了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))
  }
} 
开发者ID:chenc10,项目名称:spark-SSR-ICDCS17,代码行数:46,代码来源:JSON.scala

示例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))
  }
} 
开发者ID:chenc10,项目名称:spark-SSR-ICDCS17,代码行数:56,代码来源:MiniML.scala

示例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))
  }
} 
开发者ID:chenc10,项目名称:spark-SSR-ICDCS17,代码行数:33,代码来源:ListParsers.scala

示例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
  }
} 
开发者ID:Kiryna,项目名称:Scala-for-the-Impatient,代码行数:54,代码来源:Exercise08.scala


注:本文中的scala.util.parsing.combinator.syntactical.StandardTokenParsers类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。