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 6d827a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
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)!!,
fullText = fullText,
precedingComment = precedingComment,
)
}
Expand Down

0 comments on commit 6d827a3

Please sign in to comment.