本文整理汇总了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")
}
}
}
示例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
}
示例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: Parser[Timestamp] =
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)
}
}
示例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 <~ ")"
)
}
示例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 {}
示例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)
}
}