Skip to content

Commit

Permalink
fix: improve error messages during parse errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
autonomousapps committed Oct 10, 2024
1 parent 6de0651 commit fcb24cb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,23 @@ public data class DependencyDeclaration(
FILE,
FILES,
FILE_TREE,
GRADLE_DISTRIBUTION,
MODULE,
PROJECT,
;

public fun or(identifier: Identifier): Type {
return if (identifier.path in GRADLE_DISTRIBUTIONS) {
GRADLE_DISTRIBUTION
} else {
// In this case, might just be a user-supplied function that returns a dependency declaration
this
}
}

private companion object {
/** Well-known dependencies available directly from the local Gradle distribution. */
val GRADLE_DISTRIBUTIONS = listOf("gradleApi()", "gradleTestKit()", "localGroovy()")
}
}
}
14 changes: 6 additions & 8 deletions core/src/main/kotlin/cash/grammar/kotlindsl/utils/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import org.antlr.v4.runtime.misc.Interval
public object Context {

/**
* Returns the "full text", from [input], represented by [this][ParserRuleContext]. The full text
* includes tokens that are sent to hidden channels by the lexer. cf.
* [ParserRuleContext.text][ParserRuleContext.getText], which only considers tokens which have
* been added to the parse tree (i.e., not comments or whitespace).
* Returns the "full text", from [input], represented by [this][ParserRuleContext]. The full text includes tokens that
* are sent to hidden channels by the lexer. cf. [ParserRuleContext.text][ParserRuleContext.getText], which only
* considers tokens which have been added to the parse tree (i.e., not comments or whitespace).
*
* Returns null if `this` has a null [ParserRuleContext.start] or [ParserRuleContext.stop], which
* can happen when, e.g., `this` is a
* [ScriptContext][com.squareup.cash.grammar.KotlinParser.ScriptContext]. (I don't
* fully understand why those tokens might be null.)
* Returns null if `this` has a null [ParserRuleContext.start] or [ParserRuleContext.stop], which can happen when,
* e.g., `this` is a [ScriptContext][com.squareup.cash.grammar.KotlinParser.ScriptContext]. (I don't fully understand
* why those tokens might be null.)
*/
public fun ParserRuleContext.fullText(input: CharStream): String? {
val a = start?.startIndex ?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,16 @@ public class DependencyExtractor(
}

val precedingComment = comments.getCommentsToLeft(declaration)
val fullText = declaration.fullText(input)
?: error("Could not determine 'full text' of dependency declaration. Failed to parse expression:\n ${declaration.text}")

return DependencyDeclaration(
configuration = configuration,
identifier = identifier!!,
identifier = identifier
?: error("Could not determine dependency identifier. Failed to parse expression:\n `$fullText`"),
capability = capability,
type = type,
fullText = declaration.fullText(input)!!,
type = type.or(identifier),
fullText = fullText,
precedingComment = precedingComment,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal class DependencyExtractorTest {
configuration = "api",
identifier = "gradleApi()",
capability = Capability.DEFAULT,
type = Type.MODULE, // TODO: new type?
type = Type.GRADLE_DISTRIBUTION,
),
)
}
Expand Down

0 comments on commit fcb24cb

Please sign in to comment.