本文整理汇总了Scala中scala.util.parsing.combinator.PackratParsers类的典型用法代码示例。如果您正苦于以下问题:Scala PackratParsers类的具体用法?Scala PackratParsers怎么用?Scala PackratParsers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PackratParsers类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MessageConstants
//设置package包名称以及导入依赖的类
package messages.parser
import com.typesafe.scalalogging.StrictLogging
import messages.parser.MessageConstants._
import messages.parser.error._
import utils.StringUtils._
import scala.util.Try
import scala.util.parsing.combinator.{JavaTokenParsers, PackratParsers}
object MessageConstants {
val SendOperator = "send"
val MessageOperator = "message"
val AttachmentOperator = "attachment"
val SubjectOperator = "to"
val CommaOperator = ","
val SemicolonOperator = ";"
}
class MessageParser extends JavaTokenParsers with PackratParsers with StrictLogging {
private val delimiterOperator: PackratParser[String] = literal(CommaOperator) | literal(SemicolonOperator)
private val subjectOperator: PackratParser[String] = literal(SubjectOperator)
private val contentOperator: PackratParser[String] = literal(MessageOperator) | literal(AttachmentOperator)
private val sendOperator: PackratParser[String] = literal(SendOperator)
private val entity: PackratParser[Entity] = stringLiteral ^^ { e => Entity(e.stripMargins("\"")) }
private val contentEntity: PackratParser[Content] = (contentOperator ~ entity) ^^ {
case op ~ en => Content(en, op)
}
private val contentEntities: PackratParser[Seq[Content]] = repsep(contentEntity, delimiterOperator)
private val subjectEntities: PackratParser[Seq[Entity]] = repsep(entity, delimiterOperator)
private val toEntity: PackratParser[To] = (subjectOperator ~> subjectEntities) ^^ { subjects => To(subjects) }
private val sendOperation: PackratParser[Operation] = (sendOperator ~> toEntity ~ contentEntities) ^^ {
case subjects ~ content => Send(content, subjects)
}
def parse(text: String): Try[Operation] = Try {
val parsedResult = parseAll(sendOperation, text)
parsedResult match {
case Success(r, _) => r
case Failure(cause, _) =>
logger.error("ParsingError: Failed parsing message.", cause)
throw new MessageParsingFailure(s"Failed parsing message, Cause:= $cause")
case Error(cause, _) =>
logger.error("ParsingError: An Error occurred while parsing message", cause)
throw new MessageParsingError(s"An Error occurred while parsing message, Cause:= $cause")
}
}
}
示例2: LambdaParser
//设置package包名称以及导入依赖的类
package lambda
import scala.util.parsing.combinator.PackratParsers
import scala.util.parsing.combinator.lexical.StdLexical
import scala.util.parsing.combinator.syntactical.StdTokenParsers
class LambdaParser extends StdTokenParsers with PackratParsers {
type Tokens = StdLexical
val lexical = new LambdaLexer
// = Grammer for lambda calculus =
type P[+A] = PackratParser[A]
lazy val lam_expr : P[Term] = application | non_application
lazy val non_application : P[Term] = lambda | let | variable | parens
lazy val lambda: P[Term] = ("\\" | "?") ~> rep1(ident) ~ "." ~ lam_expr ^^ { case i ~ "." ~ e => (i :\ e)((x,y) => Fun(x, y)) }
lazy val let : P[Let] = "let" ~ ident ~ "=" ~ lam_expr ~ "in" ~ lam_expr ^^ { case "let" ~ i ~ "=" ~ l_e ~ "in" ~ r_e => Let(i, l_e, r_e) }
lazy val parens : P[Term] = "(" ~> lam_expr <~ ")"
lazy val application : P[Term] = lam_expr ~ rep1(lam_expr) ^^ { case l_e ~ r_e => (l_e /: r_e)(FApp(_, _)) }
lazy val variable : P[Var] = ident ^^ { Var(_) }
def parse(t: String): ParseResult[Term] = {
val tokens = new lexical.Scanner(t)
phrase(lam_expr)(tokens)
}
}
示例3: parse
//设置package包名称以及导入依赖的类
package io.github.writeonly.resentiment.teapot.phases.analyzers
import io.github.writeonly.resentiment.teapot.core.Command
import scala.util.parsing.combinator.{JavaTokenParsers, PackratParsers, Parsers}
trait Analyzer extends Parsers {
def parse(code : String) : ParseResult[Command]
def apply(code : String) : Command = parse(code) match {
case Success(r, n) => r
// case NoSuccess(msg, n) => throw new IllegalArgumentException(msg + "|" + n)
case f:Failure => throw new IllegalArgumentException(f.toString())
case e:Error => throw new IllegalArgumentException(e.toString())
}
}
trait AnalyzerLL extends Analyzer with JavaTokenParsers {}
trait AnalyzerLR extends AnalyzerLL with PackratParsers {}