Skip to content

Commit aaa39ee

Browse files
committed
Add parameter error recovery
1 parent 06b7b1e commit aaa39ee

File tree

11 files changed

+43
-17
lines changed

11 files changed

+43
-17
lines changed

src/main/gen/io/runescript/plugin/lang/parser/RsParser.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,12 +991,13 @@ public static boolean ParExpression(PsiBuilder b, int l) {
991991
public static boolean Parameter(PsiBuilder b, int l) {
992992
if (!recursion_guard_(b, l, "Parameter")) return false;
993993
if (!nextTokenIs(b, "<parameter>", ARRAY_TYPE_LITERAL, TYPE_LITERAL)) return false;
994-
boolean r;
994+
boolean r, p;
995995
Marker m = enter_section_(b, l, _NONE_, PARAMETER, "<parameter>");
996996
r = Parameter_0(b, l + 1);
997+
p = r; // pin = 1
997998
r = r && LocalVariableExpression(b, l + 1);
998-
exit_section_(b, l, m, r, false, null);
999-
return r;
999+
exit_section_(b, l, m, r, p, null);
1000+
return r || p;
10001001
}
10011002

10021003
// TypeName | ARRAY_TYPE_LITERAL
@@ -1062,6 +1063,26 @@ private static boolean ParameterList_1_0_1_0(PsiBuilder b, int l) {
10621063
return r;
10631064
}
10641065

1066+
/* ********************************************************** */
1067+
// !(COMMA | RPAREN)
1068+
static boolean Parameter_Recover(PsiBuilder b, int l) {
1069+
if (!recursion_guard_(b, l, "Parameter_Recover")) return false;
1070+
boolean r;
1071+
Marker m = enter_section_(b, l, _NOT_);
1072+
r = !Parameter_Recover_0(b, l + 1);
1073+
exit_section_(b, l, m, r, false, null);
1074+
return r;
1075+
}
1076+
1077+
// COMMA | RPAREN
1078+
private static boolean Parameter_Recover_0(PsiBuilder b, int l) {
1079+
if (!recursion_guard_(b, l, "Parameter_Recover_0")) return false;
1080+
boolean r;
1081+
r = consumeToken(b, COMMA);
1082+
if (!r) r = consumeToken(b, RPAREN);
1083+
return r;
1084+
}
1085+
10651086
/* ********************************************************** */
10661087
// ('(' LogicalOrWrapper ')') | Expression
10671088
public static boolean RelationalValueExpression(PsiBuilder b, int l) {

src/main/gen/io/runescript/plugin/lang/psi/RsParameter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public interface RsParameter extends PsiElement, StubBasedPsiElement<RsParameterStub> {
1111

12-
@NotNull
12+
@Nullable
1313
RsLocalVariableExpression getLocalVariableExpression();
1414

1515
@Nullable

src/main/gen/io/runescript/plugin/lang/psi/impl/RsParameterImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public void accept(@NotNull PsiElementVisitor visitor) {
3939
}
4040

4141
@Override
42-
@NotNull
42+
@Nullable
4343
public RsLocalVariableExpression getLocalVariableExpression() {
44-
return notNullChild(PsiTreeUtil.getStubChildOfType(this, RsLocalVariableExpression.class));
44+
return PsiTreeUtil.getStubChildOfType(this, RsLocalVariableExpression.class);
4545
}
4646

4747
@Override

src/main/grammars/RuneScript.bnf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ ParameterList ::= '(' (Parameter (',' Parameter)*)? ')' {
8080
Parameter ::= (TypeName | ARRAY_TYPE_LITERAL) LocalVariableExpression {
8181
stubClass="io.runescript.plugin.lang.stubs.RsParameterStub"
8282
elementTypeFactory = "io.runescript.plugin.lang.stubs.StubElementTypeFactory.create"
83+
pin=1
84+
recoverWhile=Parameter_Recover
8385
}
8486

87+
private Parameter_Recover ::= !(COMMA | RPAREN)
88+
8589
ReturnList ::= '(' (TypeName (',' TypeName)*)? ')' {
8690
stubClass="io.runescript.plugin.lang.stubs.RsReturnListStub"
8791
elementTypeFactory = "io.runescript.plugin.lang.stubs.StubElementTypeFactory.create"

src/main/kotlin/io/runescript/plugin/ide/codeInsight/RsGosubParameterInfoProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ data class CallInfo(
132132

133133
fun of(parameters: List<RsParameter>) = CallInfo(parameters.map {
134134
val typeName = it.arrayTypeLiteral?.text ?: it.typeName?.text ?: "unknown"
135-
val parameterName = it.localVariableExpression.name!!
135+
val parameterName = it.localVariableExpression?.name ?: return@map CallParameterInfo(typeName, "<unknown-parameter>")
136136
CallParameterInfo(typeName, parameterName)
137137
})
138138
}

src/main/kotlin/io/runescript/plugin/ide/codeInsight/RsInlayParameterHintsProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class RsInlayParameterHintsProvider : InlayParameterHintsProvider {
8080
is RsGosubExpression, is RsCommandExpression -> {
8181
val reference = element.reference?.resolve() ?: return null
8282
reference as RsScript
83-
reference.parameterList?.parameterList?.map { it.localVariableExpression.name!! }
83+
reference.parameterList?.parameterList?.map { it.localVariableExpression?.name ?: "<unknown-parameter>" }
8484
}
8585

8686
else -> return null

src/main/kotlin/io/runescript/plugin/ide/doc/RsDocReference.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class RsDocReference(element: RsDocName) : PsiPolyVariantReferenceBase<RsDocName
4949
return owner
5050
?.parameterList
5151
?.parameterList
52-
?.map { it.localVariableExpression }
52+
?.mapNotNull { it.localVariableExpression }
5353
?.filter { it.nameLiteral.text == elementName }
5454
?.toTypedArray()
5555
?: emptyArray()

src/main/kotlin/io/runescript/plugin/ide/doc/RsDocumentationProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class RsDocumentationProvider : AbstractDocumentationProvider() {
107107
}
108108
appendKeyword((it.typeName ?: it.arrayTypeLiteral)!!.text)
109109
appendHighlighted(
110-
" ${it.localVariableExpression.text}",
110+
" ${it.localVariableExpression?.text ?: "<unknown-parameter>" }",
111111
RsSyntaxHighlighterColors.LOCAL_VARIABLE
112112
)
113113
}
@@ -143,7 +143,7 @@ class RsDocumentationProvider : AbstractDocumentationProvider() {
143143
override fun getDocumentationElementForLink(
144144
psiManager: PsiManager,
145145
link: String,
146-
context: PsiElement
146+
context: PsiElement,
147147
): PsiElement? {
148148
val parts = link.split("/")
149149
if (parts.size < 2) {

src/main/kotlin/io/runescript/plugin/ide/inspections/RuneScriptDuplicateLocalVariableInspection.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class RuneScriptDuplicateLocalVariableInspection : LocalInspectionTool() {
2121
return object : RsVisitor() {
2222

2323
override fun visitParameter(o: RsParameter) {
24-
val name = o.localVariableExpression.name ?: return
24+
val name = o.localVariableExpression?.name ?: return
2525
var prev = o.prevSibling
2626
while (prev != null) {
2727
if (prev is RsParameter) {
28-
val prevName = prev.localVariableExpression.name
28+
val prevName = prev.localVariableExpression?.name
2929
if (name == prevName) {
3030
holder.registerProblem(
31-
o.localVariableExpression,
31+
o.localVariableExpression!!,
3232
RsBundle.message("inspection.error.duplicate.parameter", name),
3333
ProblemHighlightType.GENERIC_ERROR
3434
)

src/main/kotlin/io/runescript/plugin/lang/psi/mixin/RsParameterListMixin.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ abstract class RsParameterListMixin : StubBasedPsiElementBase<RsParameterListStu
1818

1919
override fun processDeclarations(processor: PsiScopeProcessor, state: ResolveState, lastParent: PsiElement?, place: PsiElement): Boolean {
2020
parameterList.forEach {
21-
if (lastParent != it && !processor.execute(it.localVariableExpression, state)) {
21+
val expr = it.localVariableExpression
22+
if (lastParent != it && expr != null && !processor.execute(expr, state)) {
2223
return false
2324
}
2425
}

0 commit comments

Comments
 (0)