Skip to content

Commit

Permalink
[object-access] Improve location and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Oct 24, 2023
1 parent 79d71c3 commit 6cf5568
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,9 @@ else if (!mExpression.getType().canBeCallable()) {
if (count == mParameters.size()) {
var staticMethod = methods.get(count);
if (staticMethod.level == AccessLevel.PRIVATE && compiler.getCurrentClass() != clazz) {
compiler.addError(new AnalyzeError(oa.getFieldToken(), AnalyzeErrorLevel.ERROR, Error.PRIVATE_STATIC_METHOD, new String[] { clazz.getName(), oa.getField() }));
compiler.addError(new AnalyzeError(oa.getLastToken(), AnalyzeErrorLevel.ERROR, Error.PRIVATE_STATIC_METHOD, new String[] { clazz.getName(), oa.getField() }));
} else if (staticMethod.level == AccessLevel.PROTECTED && (compiler.getCurrentClass() == null || !compiler.getCurrentClass().descendsFrom(clazz))) {
compiler.addError(new AnalyzeError(oa.getFieldToken(), AnalyzeErrorLevel.ERROR, Error.PROTECTED_STATIC_METHOD, new String[] { clazz.getName(), oa.getField() }));
compiler.addError(new AnalyzeError(oa.getLastToken(), AnalyzeErrorLevel.ERROR, Error.PROTECTED_STATIC_METHOD, new String[] { clazz.getName(), oa.getField() }));
}
is_static_method = true;
method = staticMethod;
Expand All @@ -562,13 +562,13 @@ else if (!mExpression.getType().canBeCallable()) {
var field = clazz.getStaticField(oa.getField());
if (field != null) {
if (field.level == AccessLevel.PRIVATE && compiler.getCurrentClass() != clazz) {
compiler.addError(new AnalyzeError(oa.getFieldToken(), AnalyzeErrorLevel.ERROR, Error.PRIVATE_STATIC_FIELD, new String[] { clazz.getName(), oa.getField() }));
compiler.addError(new AnalyzeError(oa.getLastToken(), AnalyzeErrorLevel.ERROR, Error.PRIVATE_STATIC_FIELD, new String[] { clazz.getName(), oa.getField() }));
} else if (field.level == AccessLevel.PROTECTED && (compiler.getCurrentClass() == null || !compiler.getCurrentClass().descendsFrom(clazz))) {
compiler.addError(new AnalyzeError(oa.getFieldToken(), AnalyzeErrorLevel.ERROR, Error.PROTECTED_STATIC_FIELD, new String[] { clazz.getName(), oa.getField() }));
compiler.addError(new AnalyzeError(oa.getLastToken(), AnalyzeErrorLevel.ERROR, Error.PROTECTED_STATIC_FIELD, new String[] { clazz.getName(), oa.getField() }));
}
is_static_method = true;
} else {
compiler.addError(new AnalyzeError(oa.getFieldToken(), AnalyzeErrorLevel.ERROR, Error.CLASS_STATIC_MEMBER_DOES_NOT_EXIST, new String[] { clazz.getName(), oa.getField() }));
compiler.addError(new AnalyzeError(oa.getLastToken(), AnalyzeErrorLevel.ERROR, Error.CLASS_STATIC_MEMBER_DOES_NOT_EXIST, new String[] { clazz.getName(), oa.getField() }));
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/leekscript/compiler/expression/LeekObjectAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void analyze(WordCompiler compiler) throws LeekCompilerException {
}

public String getField() {
return field.getWord();
return field == null ? "" : field.getWord();
}

public Token getFieldToken() {
Expand Down Expand Up @@ -492,7 +492,7 @@ public Hover hover(Token token) {
}
}
var clazz = object.getType().getClassDeclaration();
if (clazz != null) {
if (clazz != null && field != null) {
var member = clazz.getMember(field.getWord());
if (member != null) {
return new Hover(member.getType(), getLocation(), member.getLocation());
Expand All @@ -515,4 +515,15 @@ public Complete complete(Token token) {
public LeekVariable getVariable() {
return this.variable;
}

public Token getDot() {
return this.dot;
}

public Token getLastToken() {
if (this.field != null) {
return this.field;
}
return this.dot;
}
}

0 comments on commit 6cf5568

Please sign in to comment.