Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rubysrc2cpg] Decouple parser and ASTCreator #3283

Merged
merged 4 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private object Frontend {
programName("rubysrc2cpg"),
opt[Unit]("enableDependencyDownload")
.hidden()
.action((_, c) => c.withEnableDependencyDownload(false))
.action((_, c) => c.withEnableDependencyDownload(true))
.text("enable dependency download for Unix System only")
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.joern.rubysrc2cpg.astcreation

import io.joern.rubysrc2cpg.parser.{RubyLexer, RubyParser}
import org.antlr.v4.runtime.{CharStreams, CommonTokenStream, ParserRuleContext, Token}

class AntlrParser {

def parse(filename: String): RubyParser.ProgramContext = {
val charStream = CharStreams.fromFileName(filename)
val lexer = new RubyLexer(charStream)
val tokenStream = new CommonTokenStream(lexer)
val parser = new RubyParser(tokenStream)
parser.program()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import io.joern.x2cpg.datastructures.{Global, Scope}
import io.joern.x2cpg.{Ast, AstCreatorBase, AstNodeBuilder, Defines as XDefines}
import io.shiftleft.codepropertygraph.generated.*
import io.shiftleft.codepropertygraph.generated.nodes.*
import org.antlr.v4.runtime.{ParserRuleContext, Token}
import org.antlr.v4.runtime.tree.TerminalNode
import org.antlr.v4.runtime.{CharStreams, CommonTokenStream, ParserRuleContext, Token}
import org.slf4j.LoggerFactory
import overflowdb.{BatchedUpdate, NodeOrDetachedNode}

Expand All @@ -19,6 +19,7 @@ import scala.collection.immutable.Seq
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.jdk.CollectionConverters.*
import scala.util.{Failure, Success, Try}

class AstCreator(
protected val filename: String,
Expand Down Expand Up @@ -105,12 +106,19 @@ class AstCreator(
methodAliases.getOrElse(name, name)
}
override def createAst(): BatchedUpdate.DiffGraphBuilder = {
val charStream = CharStreams.fromFileName(filename)
val lexer = new RubyLexer(charStream)
val tokenStream = new CommonTokenStream(lexer)
val parser = new RubyParser(tokenStream)
val programCtx = parser.program()
Try {
new AntlrParser().parse(filename)
} match {
case Success(programCtx) =>
createAstForProgramCtx(programCtx)
case Failure(exc) =>
logger.warn(s"Could not parse file: $filename, skipping")
logger.warn(exc.getMessage)
diffGraph
}
}

private def createAstForProgramCtx(programCtx: RubyParser.ProgramContext) = {
val name = ":program"
val fullName = s"$relativeFilename:$name"
val programMethod =
Expand Down