本文整理汇总了Scala中org.antlr.v4.runtime.Token类的典型用法代码示例。如果您正苦于以下问题:Scala Token类的具体用法?Scala Token怎么用?Scala Token使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Token类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ParserUtils
//设置package包名称以及导入依赖的类
package definiti.core.utils
import definiti.core.{ParameterDefinition, Position, Range, TypeReference}
import org.antlr.v4.runtime.tree.TerminalNode
import org.antlr.v4.runtime.{ParserRuleContext, Token}
import scala.collection.mutable.ListBuffer
private[core] object ParserUtils {
def extractStringContent(string: String): String = {
var temporaryResult = string
if (temporaryResult.startsWith("\"")) {
temporaryResult = temporaryResult.substring(1)
}
if (temporaryResult.endsWith("\"")) {
temporaryResult = temporaryResult.substring(0, temporaryResult.length - 1)
}
temporaryResult
}
def extractDocComment(string: String): String = {
var temporaryResult = string
if (temporaryResult.startsWith("")) {
temporaryResult = temporaryResult.substring(0, temporaryResult.length - 2)
}
temporaryResult
}
def getRangeFromContext(context: ParserRuleContext): Range = {
def position(token: Token): Position = {
if (token == null) {
Position(0, 0)
} else {
Position(token.getLine, token.getCharPositionInLine)
}
}
Range(position(context.getStart), position(context.getStop))
}
def getRangeFromTerminalNode(terminalNode: TerminalNode): Range = {
val symbol = terminalNode.getSymbol
Range(
Position(symbol.getLine, symbol.getStartIndex),
Position(symbol.getLine, symbol.getStopIndex)
)
}
def getRangeFromToken(token: Token): Range = {
Range(
Position(token.getLine, token.getStartIndex),
Position(token.getLine, token.getStopIndex)
)
}
def appendIfDefined[A, B](element: A, buffer: ListBuffer[B], transformer: A => B): Unit = {
if (element != null) {
buffer.append(transformer(element))
}
}
}
示例2: VerificationContextMock
//设置package包名称以及导入依赖的类
package definiti.core.mock.antlr
import java.util
import definiti.core.parser.antlr.DefinitiParser.{VerifyingContext, _}
import definiti.core.utils.CollectionUtils._
import org.antlr.v4.runtime.Token
import org.antlr.v4.runtime.tree.TerminalNode
case class VerificationContextMock(
verificationNameToken: Token,
verificationMessageToken: Token,
functionContext: FunctionContext,
identifier: TerminalNode,
string: TerminalNode,
docComment: TerminalNode
) extends VerificationContext(null, 0) {
this.verificationName = verificationNameToken
this.verificationMessage = verificationMessageToken
override def function(): FunctionContext = functionContext
override def IDENTIFIER(): TerminalNode = identifier
override def STRING(): TerminalNode = string
override def DOC_COMMENT(): TerminalNode = docComment
}
case class VerifyingListContextMock(
verifyingContexts: Seq[VerifyingContext]
) extends VerifyingListContext(null, 0) {
override def verifying(): util.List[VerifyingContext] = javaList(verifyingContexts)
override def verifying(i: Int): VerifyingContext = verifyingContexts(i)
}
case class VerifyingContextMock(
verificationNameToken: Token,
messageToken: Option[Token]
) extends VerifyingContext(null, 0) {
this.verificationName = verificationNameToken
this.message = messageToken.orNull
override def IDENTIFIER(): TerminalNode = TerminalNodeMock(verificationNameToken)
override def STRING(): TerminalNode = messageToken.map(TerminalNodeMock(_)).orNull
}
示例3: FunctionContextMock
//设置package包名称以及导入依赖的类
package definiti.core.mock.antlr
import java.util.{List => JList}
import definiti.core.parser.antlr.DefinitiParser._
import definiti.core.utils.CollectionUtils._
import org.antlr.v4.runtime.Token
import org.antlr.v4.runtime.tree.TerminalNode
case class FunctionContextMock(
parameterListDefinitionContext: ParameterListDefinitionContext,
chainedExpressionContext: ChainedExpressionContext,
genericTypeListContext: GenericTypeListContext
) extends FunctionContext(null, 0) {
override def parameterListDefinition(): ParameterListDefinitionContext = parameterListDefinitionContext
override def chainedExpression(): ChainedExpressionContext = chainedExpressionContext
override def genericTypeList(): GenericTypeListContext = genericTypeListContext
}
case class ParameterListDefinitionContextMock(
parameterDefinitionContexts: Seq[ParameterDefinitionContext]
) extends ParameterListDefinitionContext(null, 0) {
override def parameterDefinition(): JList[ParameterDefinitionContext] = javaList(parameterDefinitionContexts)
override def parameterDefinition(i: Int): ParameterDefinitionContext = parameterDefinitionContexts(i)
}
case class ParameterDefinitionContextMock(
parameterNameToken: Token,
parameterTypeToken: Token,
genericTypeListContext: GenericTypeListContext
) extends ParameterDefinitionContext(null, 0) {
this.parameterName = parameterNameToken
this.parameterType = parameterTypeToken
override def IDENTIFIER(): JList[TerminalNode] = javaList(Seq(TerminalNodeMock(parameterNameToken), TerminalNodeMock(parameterTypeToken)))
override def IDENTIFIER(i: Int): TerminalNode = IDENTIFIER().get(i)
override def genericTypeList(): GenericTypeListContext = genericTypeListContext
}
示例4: AntlrGenerator
//设置package包名称以及导入依赖的类
package definiti.core.generators.antlr
import definiti.core.generators.Generators
import definiti.core.mock.antlr.{TerminalNodeMock, TokenMock}
import org.antlr.v4.runtime.tree.TerminalNode
import org.antlr.v4.runtime.{ParserRuleContext, Token}
import org.scalacheck.Gen
object AntlrGenerator {
lazy val anyBooleanNode: Gen[TerminalNode] = genNode(anyBooleanToken)
lazy val anyBooleanToken: Gen[Token] = genToken(Generators.anyBooleanText)
lazy val anyNumberNode: Gen[TerminalNode] = genNode(anyNumberToken)
lazy val anyNumberToken: Gen[Token] = genToken(Generators.numberAsString)
lazy val anyStringNode: Gen[TerminalNode] = genNode(anyStringToken)
lazy val anyStringToken: Gen[Token] = genToken(Generators.anyString.map('"' + _ + '"'))
lazy val anyIdentifierNode: Gen[TerminalNode] = genNode(anyIdentifierToken)
lazy val anyIdentifierToken: Gen[Token] = genToken(Generators.anyIdentifier)
lazy val anyBinaryOperatorNode: Gen[TerminalNode] = genNode(anyBinaryOperatorToken)
lazy val anyBinaryOperatorToken: Gen[Token] = genToken(Gen.oneOf("*", "/", "%", "+", "-", "==", "!=", "<", "<=", ">", ">=", "&&", "||"))
lazy val anyNotOperatorNode: Gen[TerminalNode] = genNode(anyNotOperatorToken)
lazy val anyNotOperatorToken: Gen[Token] = genToken(Gen.const("!"))
lazy val anyDocCommentNode: Gen[TerminalNode] = genNode(anyDocCommentToken)
lazy val anyDocCommentToken: Gen[Token] = genToken(Generators.anyString.map(""))
def genContext[A <: ParserRuleContext](gen: Gen[A]): Gen[A] = for {
element <- gen
startToken <- anyIdentifierToken
stopToken <- anyIdentifierToken
} yield {
element.start = startToken
element.stop = stopToken
element
}
private def genNode(genToken: Gen[Token]): Gen[TerminalNode] = genToken.map(TerminalNodeMock(_))
private def genToken(genText: Gen[String]): Gen[Token] = for {
text <- genText
line <- Gen.posNum[Int]
startIndex <- Gen.posNum[Int]
stopIndex <- Gen.posNum[Int]
} yield {
TokenMock(text, line, startIndex, stopIndex)
}
}