Skip to content

Commit

Permalink
feat: Remove method getSemanticNode and make the Parser build a Seman…
Browse files Browse the repository at this point in the history
…ticAstNode directly
  • Loading branch information
felipebz committed Apr 24, 2024
1 parent 769f728 commit a256b94
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 64 deletions.
33 changes: 0 additions & 33 deletions zpa-core/src/main/kotlin/org/sonar/plsqlopen/PlSqlAstScanner.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object TestPlSqlVisitorRunner {

private fun createContext(plSqlFile: PlSqlFile, metadata: FormsMetadata?): PlSqlVisitorContext {
val parser = PlSqlParser.create(PlSqlConfiguration(StandardCharsets.UTF_8))
val rootTree = getSemanticNode(parser.parse(plSqlFile.contents()))
val rootTree = parser.parse(plSqlFile.contents())
return PlSqlVisitorContext(rootTree, plSqlFile, metadata)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ import com.felipebz.flr.impl.Parser
import org.sonar.plsqlopen.lexer.PlSqlLexer
import org.sonar.plsqlopen.squid.PlSqlConfiguration
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.squid.SemanticAstNode

object PlSqlParser {
fun create(conf: PlSqlConfiguration): Parser<Grammar> =
Parser.builder(PlSqlGrammar.create(conf).build())
.withLexer(PlSqlLexer.create(conf)).build()
.withLexer(PlSqlLexer.create(conf))
.withNonTerminalNodeBuilder(::SemanticAstNode)
.withTerminalNodeBuilder(::SemanticAstNode)
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.felipebz.flr.api.Grammar
import com.felipebz.flr.api.RecognitionException
import com.felipebz.flr.impl.Parser
import org.sonar.plsqlopen.FormsMetadataAwareCheck
import org.sonar.plsqlopen.getSemanticNode
import org.sonar.plsqlopen.metadata.FormsMetadata
import org.sonar.plsqlopen.metrics.ComplexityVisitor
import org.sonar.plsqlopen.metrics.FunctionComplexityVisitor
Expand Down Expand Up @@ -117,7 +116,7 @@ class AstScanner(private val checks: Collection<PlSqlVisitor>,
private fun getPlSqlVisitorContext(inputFile: PlSqlFile): PlSqlVisitorContext {
var visitorContext: PlSqlVisitorContext
try {
val root = getSemanticNode(parser.parse(inputFile.contents()))
val root = parser.parse(inputFile.contents())
visitorContext = PlSqlVisitorContext(root, inputFile, formsMetadata)
} catch (e: RecognitionException) {
visitorContext = PlSqlVisitorContext(inputFile, e, formsMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.sonar.plugins.plsqlopen.api.squid

import com.felipebz.flr.api.AstNode
import com.felipebz.flr.api.AstNodeType
import com.felipebz.flr.api.Token
import org.sonar.plsqlopen.sslr.PlSqlGrammarBuilder
import org.sonar.plsqlopen.sslr.Tree
import org.sonar.plsqlopen.sslr.TreeImpl
Expand All @@ -29,7 +31,10 @@ import org.sonar.plugins.plsqlopen.api.symbols.Symbol
import org.sonar.plugins.plsqlopen.api.symbols.datatype.PlSqlDatatype
import org.sonar.plugins.plsqlopen.api.symbols.datatype.UnknownDatatype

class SemanticAstNode(private val astNode: AstNode) : AstNode(astNode.type, astNode.name, astNode.token) {
class SemanticAstNode(type: AstNodeType, name: String, token: Token?) : AstNode(type, name, token) {
constructor(token: Token) : this(token.type, token.type.name, token)

private var internalTree: Tree? = null

var symbol: Symbol? = null
set(symbol) {
Expand All @@ -45,23 +50,27 @@ class SemanticAstNode(private val astNode: AstNode) : AstNode(astNode.type, astN
val plSqlType: PlSqlType
get() = plSqlDatatype.type

val tree: Tree by lazy {
var type = PlSqlGrammarBuilder.classForType(astNode.type)
if (type == TreeImpl::class.java) {
var node = astNode
while (type == TreeImpl::class.java && node.numberOfChildren == 1) {
node = node.firstChild
type = PlSqlGrammarBuilder.classForType(node.type)
val tree: Tree
get() {
internalTree?.let { return it }

var classType = PlSqlGrammarBuilder.classForType(super.type)
if (classType == TreeImpl::class.java) {
var node = this
while (classType == TreeImpl::class.java && node.numberOfChildren == 1) {
node = node.firstChild as SemanticAstNode
classType = PlSqlGrammarBuilder.classForType(node.type)
}
}
}

type.getDeclaredConstructor(SemanticAstNode::class.java)
.newInstance(this)
}
val instance = classType.getDeclaredConstructor(SemanticAstNode::class.java)
.newInstance(this)
internalTree = instance
return instance
}

val allTokensToString: String by lazy {
tokens.joinToString(" ") { it.originalValue }
}
val allTokensToString: String
get() = tokens.joinToString(" ") { it.originalValue }

override fun toString(): String {
return super.toString() + if (plSqlDatatype !is UnknownDatatype) {
Expand All @@ -70,9 +79,4 @@ class SemanticAstNode(private val astNode: AstNode) : AstNode(astNode.type, astN
""
}
}

init {
super.fromIndex = astNode.fromIndex
super.toIndex = astNode.toIndex
}
}
5 changes: 3 additions & 2 deletions zpa-core/src/test/kotlin/org/sonar/plsqlopen/sslr/TreeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.sonar.plsqlopen.sslr

import org.junit.jupiter.api.BeforeEach
import org.sonar.plsqlopen.getSemanticNode
import org.sonar.plsqlopen.asSemantic
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.RuleTest

Expand All @@ -32,7 +32,8 @@ abstract class TreeTest<T : Tree>(private val root: PlSqlGrammar) : RuleTest() {
}

fun parse(content: String): T {
return getSemanticNode(p.parse(content)).tree as T
@Suppress("UNCHECKED_CAST")
return p.parse(content).asSemantic().tree as T
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package org.sonar.plugins.plsqlopen.api.matchers
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.sonar.plsqlopen.getSemanticNode
import org.sonar.plsqlopen.asSemantic
import org.sonar.plugins.plsqlopen.api.PlSqlGrammar
import org.sonar.plugins.plsqlopen.api.RuleTest
import org.sonar.plugins.plsqlopen.api.squid.SemanticAstNode
Expand Down Expand Up @@ -84,7 +84,7 @@ class MethodMatcherWithTypesTest : RuleTest() {
}

private fun getAstNodeWithArguments(text: String, vararg types: PlSqlDatatype): SemanticAstNode {
val node = getSemanticNode(p.parse(text).firstChild)
val node = p.parse(text).firstChild

val arguments = node.getDescendants(PlSqlGrammar.ARGUMENT)

Expand All @@ -93,7 +93,7 @@ class MethodMatcherWithTypesTest : RuleTest() {
MethodMatcher.semantic(actualArgument).plSqlDatatype = types[i]
}

return node
return node.asSemantic()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package com.felipebz.flr.internal.toolkit
import com.felipebz.flr.api.AstNode
import com.felipebz.flr.impl.ast.AstXmlPrinter
import com.felipebz.flr.toolkit.ConfigurationModel
import org.sonar.plsqlopen.getSemanticNode
import org.sonar.plsqlopen.symbols.DefaultTypeSolver
import org.sonar.plsqlopen.symbols.SymbolVisitor
import org.sonar.plugins.plsqlopen.api.PlSqlVisitorContext
Expand All @@ -47,7 +46,7 @@ internal class SourceCodeModel(private val configurationModel: ConfigurationMode

fun setSourceCode(sourceCode: String) {
parseTime = measureNanoTime {
astNode = getSemanticNode(configurationModel.parser.parse(sourceCode))
astNode = configurationModel.parser.parse(sourceCode)
}
this.sourceCode = sourceCode
loadSymbolTable()
Expand Down

0 comments on commit a256b94

Please sign in to comment.