• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala PackratParsers类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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")
    }
  }
} 
开发者ID:lymr,项目名称:fun-chat,代码行数:58,代码来源:MessageParser.scala


示例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)
  }
} 
开发者ID:alpclk,项目名称:lambda-calculus-interpretter,代码行数:28,代码来源:LambdaParser.scala


示例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 {} 
开发者ID:writeonly,项目名称:resentiment,代码行数:20,代码来源:Analyzer.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala ChiSqSelector类代码示例发布时间:2022-05-23
下一篇:
Scala DateTimeFormatter类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap