当前位置: 首页>>代码示例>>Scala>>正文


Scala ParserRuleContext类代码示例

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


在下文中一共展示了ParserRuleContext类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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))
    }
  }
} 
开发者ID:definiti,项目名称:definiti-core,代码行数:62,代码来源:ParserUtils.scala

示例2: 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)
  }
} 
开发者ID:definiti,项目名称:definiti-core,代码行数:52,代码来源:AntlrGenerator.scala


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