Skip to content

Commit

Permalink
False positive warning "Function ... is never used" #214 : adjust gra…
Browse files Browse the repository at this point in the history
…mmar to introduce string mixin
  • Loading branch information
xonixx committed Apr 15, 2024
1 parent 692f2fc commit 47072d4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/main/java/intellij_awk/Awk.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ non_unary_expr ::= (
| NOT expr
| simple_get (LT expr)?
| NUMBER
| STRING
| str
| ERE
| INCR lvalue
| DECR lvalue
Expand Down Expand Up @@ -391,6 +391,12 @@ function_call_name ::= FUNC_NAME
implements="intellij_awk.psi.AwkNamedElement"
}

str ::= STRING // we need this as PSI to enable GAWK's function references `f="fname"; @f()`
{
mixin="intellij_awk.psi.AwkStringMixin"
implements="intellij_awk.psi.AwkNamedElement"
}

private non_unary_expr1 ::= [
( POW expr
| MUL expr
Expand Down Expand Up @@ -449,7 +455,7 @@ private non_unary_print_expr ::= (
| LPAREN expr_lst RPAREN IN gawk_var_name
| simple_get
| NUMBER
| STRING
| str
| ERE
| INCR lvalue
| DECR lvalue
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/intellij_awk/AwkCompletionContributorBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
abstract class AwkCompletionContributorBase extends CompletionContributor {
static final PsiElementPattern.Capture<PsiElement> INSIDE_STRING = psiElement(AwkTypes.STRING);
private static final ElementPattern<PsiElement> notInsideERE =
not(or(psiElement(AwkTypes.ERE), psiElement(AwkTypes.TYPED_ERE)));
not(or(psiElement(AwkTypes.ERE), psiElement(AwkTypes.TYPED_ERE)));

private static final ObjectPattern.Capture<PsiElement> notInsideStringEre =
not(INSIDE_STRING).and(notInsideERE);
not(INSIDE_STRING).and(notInsideERE);

static ElementPattern<? extends PsiElement> notInsideStringERE(
ElementPattern<? extends PsiElement> pattern) {
ElementPattern<? extends PsiElement> pattern) {
return and(notInsideStringEre, pattern);
}

static ElementPattern<? extends PsiElement> notInsideERE(
ElementPattern<? extends PsiElement> pattern) {
ElementPattern<? extends PsiElement> pattern) {
return and(notInsideERE, pattern);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void invoke(@NotNull Project project, Editor editor, PsiFile file)
}
}
if (name == null) {
PsiElement string = nonUnaryExpr.getString();
PsiElement string = nonUnaryExpr.getStr();
if (string != null && string.getText().equals(nonUnaryExpr.getText())) {
String text = string.getText();
name = text.substring(1, text.length() - 1);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/intellij_awk/AwkParserDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AwkParserDefinition implements ParserDefinition {
new IStubFileElementType<>(AwkLanguage.INSTANCE) {
@Override
public int getStubVersion() {
return 15;
return 16;
}

@Override
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/intellij_awk/psi/AwkStringMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package intellij_awk.psi;

import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import intellij_awk.AwkReferenceFunction;
import javax.swing.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class AwkStringMixin extends AwkNamedElementImpl {
public AwkStringMixin(@NotNull ASTNode node) {
super(node);
}

public PsiElement setName(String newName) {
return replaceNameNode(AwkElementFactory.createFunctionCallName(getProject(), newName));
}

@Override
public PsiReference getReference() {
// return new AwkReferenceFunction(this, getNameTextRange());
return new AwkReferenceFunction(this, TextRange.from(1, getTextLength()-2));
// return null;
}

@Override
public @Nullable Icon getIcon(int flags) {
return null;
}
}
3 changes: 2 additions & 1 deletion src/test/testData/parser/BuiltInFuncSpace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ AWK File
PsiElement(AwkTokenType.()('(')
AwkGawkFuncCallListImpl(GAWK_FUNC_CALL_LIST)
AwkNonUnaryExprImpl(NON_UNARY_EXPR)
PsiElement(AwkTokenType.STRING)('"abc"')
AwkStrImpl(STR)
PsiElement(AwkTokenType.STRING)('"abc"')
PsiElement(AwkTokenType.,)(',')
AwkNonUnaryExprImpl(NON_UNARY_EXPR)
PsiElement(AwkTokenType.NUMBER)('2')
Expand Down
3 changes: 2 additions & 1 deletion src/test/testData/parser/UserFuncSpace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ AWK File
AwkNonUnaryExprImpl(NON_UNARY_EXPR)
PsiElement(AwkTokenType.()('(')
AwkNonUnaryExprImpl(NON_UNARY_EXPR)
PsiElement(AwkTokenType.STRING)('"A"')
AwkStrImpl(STR)
PsiElement(AwkTokenType.STRING)('"A"')
PsiElement(AwkTokenType.))(')')
PsiWhiteSpace(' ')
PsiElement(AwkTokenType.})('}')

0 comments on commit 47072d4

Please sign in to comment.