Source located VariableDoesNotExistsException

This commit is contained in:
aNNiMON 2023-10-15 23:09:36 +03:00 committed by Victor Melnik
parent 6a35f6e66a
commit bcce978341
3 changed files with 22 additions and 5 deletions

View File

@ -1,11 +1,13 @@
package com.annimon.ownlang.exceptions; package com.annimon.ownlang.exceptions;
import com.annimon.ownlang.util.Range;
public final class VariableDoesNotExistsException extends OwnLangRuntimeException { public final class VariableDoesNotExistsException extends OwnLangRuntimeException {
private final String variable; private final String variable;
public VariableDoesNotExistsException(String variable) { public VariableDoesNotExistsException(String variable, Range range) {
super("Variable " + variable + " does not exists"); super("Variable " + variable + " does not exists", range);
this.variable = variable; this.variable = variable;
} }

View File

@ -853,12 +853,15 @@ public final class Parser {
private Node qualifiedName() { private Node qualifiedName() {
// var || var.key[index].key2 // var || var.key[index].key2
final var startTokenIndex = index;
final Token current = get(0); final Token current = get(0);
if (!match(TokenType.WORD)) return null; if (!match(TokenType.WORD)) return null;
final List<Node> indices = variableSuffix(); final List<Node> indices = variableSuffix();
if (indices == null || indices.isEmpty()) { if (indices == null || indices.isEmpty()) {
return new VariableExpression(current.text()); final var variable = new VariableExpression(current.text());
variable.setRange(getRange(startTokenIndex, index - 1));
return variable;
} }
return new ContainerAccessExpression(current.text(), indices); return new ContainerAccessExpression(current.text(), indices);
} }

View File

@ -3,19 +3,31 @@ package com.annimon.ownlang.parser.ast;
import com.annimon.ownlang.exceptions.VariableDoesNotExistsException; import com.annimon.ownlang.exceptions.VariableDoesNotExistsException;
import com.annimon.ownlang.lib.ScopeHandler; import com.annimon.ownlang.lib.ScopeHandler;
import com.annimon.ownlang.lib.Value; import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.util.Range;
import com.annimon.ownlang.util.SourceLocation;
/** /**
* *
* @author aNNiMON * @author aNNiMON
*/ */
public final class VariableExpression extends InterruptableNode implements Accessible { public final class VariableExpression extends InterruptableNode implements Accessible, SourceLocation {
public final String name; public final String name;
private Range range;
public VariableExpression(String name) { public VariableExpression(String name) {
this.name = name; this.name = name;
} }
public void setRange(Range range) {
this.range = range;
}
@Override
public Range getRange() {
return range;
}
@Override @Override
public Value eval() { public Value eval() {
super.interruptionCheck(); super.interruptionCheck();
@ -25,7 +37,7 @@ public final class VariableExpression extends InterruptableNode implements Acces
@Override @Override
public Value get() { public Value get() {
if (!ScopeHandler.isVariableOrConstantExists(name)) { if (!ScopeHandler.isVariableOrConstantExists(name)) {
throw new VariableDoesNotExistsException(name); throw new VariableDoesNotExistsException(name, getRange());
} }
return ScopeHandler.getVariableOrConstant(name); return ScopeHandler.getVariableOrConstant(name);
} }