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

Scala JavaTokenParsers类代码示例

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

本文整理汇总了Scala中scala.util.parsing.combinator.JavaTokenParsers的典型用法代码示例。如果您正苦于以下问题:Scala JavaTokenParsers类的具体用法?Scala JavaTokenParsers怎么用?Scala JavaTokenParsers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了JavaTokenParsers类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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: CalculatorParser

//设置package包名称以及导入依赖的类
package org.glavo.parser

import scala.util.parsing.combinator.JavaTokenParsers


class CalculatorParser extends JavaTokenParsers {
    def number: Parser[Node] =
        """-?[0-9]+(\.[0-9]+)?""".r ^^ { it => Number(BigDecimal(it)) } |
            value

    def expr: Parser[Node] =
        term ~ (("+" | "-") ~ term).* ^^ {
            case t ~ list => list.foldLeft(t) { (n, t) =>
                t match {
                    case ("+" ~ nn) => new +(n, nn)
                    case ("-" ~ nn) => new -(n, nn)
                }
            }
        }


    def term: Parser[Node] =
        "(" ~> expr <~ ")" |
            number ~ (("*" | "/") ~ number).* ^^ {
                case t ~ list => list.foldLeft(t) { (n, t) =>
                    t match {
                        case ("*" ~ nn) => *(n, nn)
                        case ("/" ~ nn) => /(n, nn)
                    }
                }
            }

    def value: Parser[Node] =
        ident ^^ Value

    def statement: Parser[Node] =
        let

    def let: Parser[Node] =
        "let".r ~ ident ~ "=" ~ expr ^^ {
            case _ ~ name ~ _ ~ value => Let(name, value)
        }


    def grammar: Parser[Node] =
        statement | expr
} 
开发者ID:Glavo,项目名称:Calculator,代码行数:48,代码来源:CalculatorParser.scala


示例3: timestamp

//设置package包名称以及导入依赖的类
package com.fyber.challenge.rollingregression.io

import com.fyber.challenge.rollingregression.datatypes._
import scala.util.parsing.combinator.JavaTokenParsers


trait MeasurementParser extends JavaTokenParsers {

  private def timestamp: Parser1653282172 =
    wholeNumber ^^ ( n => Timestamp(n.toInt) )
  private def priceRatio: Parser[PriceRatio] =
    floatingPointNumber ^^ ( p => PriceRatio(p.toDouble) )
  private  def measurement: Parser[Measurement] =
   timestamp ~ priceRatio ^^ {
     case time ~ ratio => Measurement(time, ratio)
   }

  protected def parseMeasurement(input: String): Measurement =
    parse(measurement, input) match {
      case Success(result, _) => result
      case NoSuccess(cause, _) => throw new RuntimeException(cause)
    }

} 
开发者ID:EhsanYousefi,项目名称:rolling-regression,代码行数:25,代码来源:MeasurementParser.scala


示例4: MathParserStage1

//设置package包名称以及导入依赖的类
package de.parser.math

import de.parser.math._
import scala.util.parsing.combinator.JavaTokenParsers

object MathParserStage1 extends JavaTokenParsers {

  def parse(in: String): ParseResult[MathExp] = parseAll(expr, in)
      
  def expr: Parser[MathExp] = term ~ rep("+" ~ term | "-" ~ term) ^^ { case s ~ ts => (s /: ts){ case (a,t) => 
          t match {
            case "+" ~ b => Add(a,b)
            case "-" ~ b => Sub(a,b)
          }
        }}
  
  def term: Parser[MathExp] = factor ~ rep("*" ~ factor | "/" ~ factor) ^^ { case s ~ fs => (s /: fs){ case (a,f) => 
          f match {
            case "*" ~ b => Times(a,b)
            case "/" ~ b => Div(a,b)
          }
        }}
  
  def factor: Parser[MathExp] = ( 
       floatingPointNumber ^^ ( x => Number(x.toDouble) ) 
     | "(" ~> expr <~ ")"
  )
  
} 
开发者ID:saig0,项目名称:parser-playground,代码行数:30,代码来源:MathParserStage1.scala


示例5: 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


示例6: MathExpScanner

//设置package包名称以及导入依赖的类
package io.github.facaiy.math.expression.compiler.scanner

import scala.util.parsing.combinator.JavaTokenParsers

import io.github.facaiy.math.expression._


object MathExpScanner extends JavaTokenParsers {
  def add: Parser[Operator] = ADD.toString ^^ (_ => ADD)
  def minus: Parser[Operator] = MINUS.toString ^^ (_ => MINUS)
  def multiply: Parser[Operator] = MULTIPLY.toString ^^ (_ => MULTIPLY)
  def divide: Parser[Operator] = DIVIDE.toString ^^ (_ => DIVIDE)
  def power: Parser[Operator] = POWER.toString ^^ (_ => POWER)

  def comma: Parser[Delimiter] = COMMA.toString ^^ (_ => COMMA)
  def leftParenthesis: Parser[Delimiter] = LEFT_PARENTHESIS.toString ^^ (_ => LEFT_PARENTHESIS)
  def rightParenthesis: Parser[Delimiter] = RIGHT_PARENTHESIS.toString ^^ (_ => RIGHT_PARENTHESIS)

  def number: Parser[NUMBER] = floatingPointNumber ^^ (x => NUMBER(x.toDouble))

  def variable: Parser[VAR_NAME] = "$" ~ ident ^^ {
    case _ ~ n => VAR_NAME(n)
  }

  def function: Parser[FUNC_NAME] = ident ^^ (n => FUNC_NAME(n))

  def tokens: Parser[List[MathExpToken]] = {
    phrase(rep1(add | power | multiply | divide |
                comma | leftParenthesis | rightParenthesis |
                number |
                minus |                   // ???????
                variable | function))
  }

  def apply(expression: String): Either[MathExpScannerError, List[MathExpToken]] =
    parse(tokens, expression) match {
      case NoSuccess(msg, next) => Left(MathExpScannerError(msg))
      case Success(result, next) => Right(result)
    }
} 
开发者ID:facaiy,项目名称:math-expression-parser,代码行数:41,代码来源:MathExpScanner.scala



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala Hex类代码示例发布时间:2022-05-23
下一篇:
Scala FromConfig类代码示例发布时间: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