-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve support for parsing dependencies.
- Loading branch information
1 parent
90970f8
commit e59da91
Showing
9 changed files
with
436 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
core/src/main/kotlin/cash/grammar/kotlindsl/model/gradle/DependencyContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cash.grammar.kotlindsl.model.gradle | ||
|
||
import cash.grammar.kotlindsl.model.DependencyDeclaration | ||
|
||
/** | ||
* A container for all the [Statements][com.squareup.cash.grammar.KotlinParser.StatementsContext] in | ||
* a `dependencies` block in a Gradle build script. These statements are an ordered (not sorted!) | ||
* list of "raw" statements and modeled | ||
* [DependencyDeclarations][cash.grammar.kotlindsl.model.DependencyDeclaration]. | ||
* | ||
* Rather than attempt to model everything that might possibly be found inside a build script, we | ||
* declare defeat on anything that isn't a standard dependency declaration and simply retain it | ||
* as-is. | ||
*/ | ||
public class DependencyContainer( | ||
private val statements: List<Any>, | ||
) { | ||
|
||
public fun getDependencyDeclarations(): List<DependencyDeclaration> { | ||
return statements.filterIsInstance<DependencyDeclaration>() | ||
} | ||
|
||
public fun getNonDeclarations(): List<String> { | ||
return statements.filterIsInstance<String>() | ||
} | ||
|
||
internal companion object { | ||
val EMPTY = DependencyContainer(emptyList()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
core/src/main/kotlin/cash/grammar/kotlindsl/utils/Comments.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cash.grammar.kotlindsl.utils | ||
|
||
import com.squareup.cash.grammar.KotlinLexer | ||
import org.antlr.v4.runtime.CommonTokenStream | ||
import org.antlr.v4.runtime.ParserRuleContext | ||
import org.antlr.v4.runtime.Token | ||
|
||
public class Comments( | ||
private val tokens: CommonTokenStream, | ||
private val indent: String, | ||
) { | ||
|
||
private var level = 0 | ||
|
||
public fun onEnterBlock() { | ||
level++ | ||
} | ||
|
||
public fun onExitBlock() { | ||
level-- | ||
} | ||
|
||
public fun getCommentsToLeft(before: ParserRuleContext): String? { | ||
return getCommentsToLeft(before.start) | ||
} | ||
|
||
public fun getCommentsToLeft(before: Token): String? { | ||
var index = before.tokenIndex - 1 | ||
if (index <= 0) return null | ||
|
||
var next = tokens.get(index) | ||
|
||
while (next != null && next.isWhitespace()) { | ||
next = tokens.get(--index) | ||
} | ||
|
||
if (next == null) return null | ||
|
||
val comments = ArrayDeque<String>() | ||
|
||
while (next != null) { | ||
if (next.isComment()) { | ||
comments.addFirst(next.text) | ||
} else if (next.isNotWhitespace()) { | ||
break | ||
} | ||
|
||
next = tokens.get(--index) | ||
} | ||
|
||
if (comments.isEmpty()) return null | ||
|
||
return comments.joinToString(separator = "\n") { | ||
"${indent.repeat(level)}$it" | ||
} | ||
} | ||
|
||
private fun Token.isWhitespace(): Boolean { | ||
return text.isBlank() | ||
} | ||
|
||
private fun Token.isNotWhitespace(): Boolean { | ||
return text.isNotBlank() | ||
} | ||
|
||
private fun Token.isComment(): Boolean { | ||
return type == KotlinLexer.LineComment || type == KotlinLexer.DelimitedComment | ||
} | ||
} |
Oops, something went wrong.