Add columns in position information

This commit is contained in:
aNNiMON 2023-09-09 21:58:32 +03:00 committed by Victor Melnik
parent 90db2b0aa3
commit fc73bce943
9 changed files with 36 additions and 45 deletions

View File

@ -1,5 +1,7 @@
package com.annimon.ownlang.exceptions; package com.annimon.ownlang.exceptions;
import com.annimon.ownlang.parser.Pos;
/** /**
* *
* @author aNNiMON * @author aNNiMON
@ -9,8 +11,8 @@ public final class LexerException extends RuntimeException {
public LexerException(String message) { public LexerException(String message) {
super(message); super(message);
} }
public LexerException(int row, int col, String message) { public LexerException(Pos pos, String message) {
super("["+row+":"+col+"] " + message); super(pos.format() + " " + message);
} }
} }

View File

@ -357,10 +357,10 @@ public final class Lexer {
} }
private void addToken(TokenType type, String text) { private void addToken(TokenType type, String text) {
tokens.add(new Token(type, text, row, col)); tokens.add(new Token(type, text, new Pos(row, col)));
} }
private LexerException error(String text) { private LexerException error(String text) {
return new LexerException(row, col, text); return new LexerException(new Pos(row, col), text);
} }
} }

View File

@ -1,25 +1,9 @@
package com.annimon.ownlang.parser; package com.annimon.ownlang.parser;
public final class ParseError { public record ParseError(Exception exception, Pos pos) {
private final int line;
private final Exception exception;
public ParseError(int line, Exception exception) {
this.line = line;
this.exception = exception;
}
public int getLine() {
return line;
}
public Exception getException() {
return exception;
}
@Override @Override
public String toString() { public String toString() {
return "ParseError on line " + line + ": " + exception.getMessage(); return "ParseError on line " + pos.row() + ": " + exception;
} }
} }

View File

@ -17,8 +17,8 @@ public final class ParseErrors implements Iterable<ParseError> {
errors.clear(); errors.clear();
} }
public void add(Exception ex, int line) { public void add(Exception ex, Pos pos) {
errors.add(new ParseError(line, ex)); errors.add(new ParseError(ex, pos));
} }
public boolean hasErrors() { public boolean hasErrors() {

View File

@ -22,7 +22,7 @@ public final class Parser {
return program; return program;
} }
private static final Token EOF = new Token(TokenType.EOF, "", -1, -1); private static final Token EOF = new Token(TokenType.EOF, "", new Pos(-1, -1));
private static final EnumMap<TokenType, BinaryExpression.Operator> ASSIGN_OPERATORS; private static final EnumMap<TokenType, BinaryExpression.Operator> ASSIGN_OPERATORS;
static { static {
@ -71,7 +71,7 @@ public final class Parser {
try { try {
result.add(statement()); result.add(statement());
} catch (Exception ex) { } catch (Exception ex) {
parseErrors.add(ex, getErrorLine()); parseErrors.add(ex, getErrorPos());
recover(); recover();
} }
} }
@ -79,10 +79,10 @@ public final class Parser {
return result; return result;
} }
private int getErrorLine() { private Pos getErrorPos() {
if (size == 0) return 0; if (size == 0) return new Pos(0, 0);
if (pos >= size) return tokens.get(size - 1).row(); if (pos >= size) return tokens.get(size - 1).pos();
return tokens.get(pos).row(); return tokens.get(pos).pos();
} }
private void recover() { private void recover() {

View File

@ -0,0 +1,12 @@
package com.annimon.ownlang.parser;
public record Pos(int row, int col) {
public Pos normalize() {
return new Pos(Math.max(0, row - 1), Math.max(0, col - 1));
}
public String format() {
return "[" + row + ":" + col + "]";
}
}

View File

@ -3,14 +3,10 @@ package com.annimon.ownlang.parser;
/** /**
* @author aNNiMON * @author aNNiMON
*/ */
public record Token(TokenType type, String text, int row, int col) { public record Token(TokenType type, String text, Pos pos) {
public String position() {
return "[" + row + " " + col + "]";
}
@Override @Override
public String toString() { public String toString() {
return type.name() + " " + position() + " " + text; return type.name() + " " + pos().format() + " " + text;
} }
} }

View File

@ -172,11 +172,11 @@ public class LexerTest {
} }
private static Token token(TokenType type) { private static Token token(TokenType type) {
return token(type, "", 0, 0); return token(type, "", new Pos(0, 0));
} }
private static Token token(TokenType type, String text, int row, int col) { private static Token token(TokenType type, String text, Pos pos) {
return new Token(type, text, row, col); return new Token(type, text, pos);
} }
} }

View File

@ -7,10 +7,7 @@ import com.annimon.ownlang.exceptions.StoppedException;
import com.annimon.ownlang.lib.Functions; import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.UserDefinedFunction; import com.annimon.ownlang.lib.UserDefinedFunction;
import com.annimon.ownlang.lib.Variables; import com.annimon.ownlang.lib.Variables;
import com.annimon.ownlang.parser.Lexer; import com.annimon.ownlang.parser.*;
import com.annimon.ownlang.parser.Parser;
import com.annimon.ownlang.parser.Token;
import com.annimon.ownlang.parser.TokenType;
import com.annimon.ownlang.parser.ast.BlockStatement; import com.annimon.ownlang.parser.ast.BlockStatement;
import com.annimon.ownlang.parser.ast.Statement; import com.annimon.ownlang.parser.ast.Statement;
import com.annimon.ownlang.parser.visitors.PrintVisitor; import com.annimon.ownlang.parser.visitors.PrintVisitor;
@ -36,7 +33,7 @@ public final class Repl {
RESET = ":reset", RESET = ":reset",
EXIT = ":exit"; EXIT = ":exit";
private static final Token PRINTLN_TOKEN = new Token(TokenType.PRINTLN, "", 0, 0); private static final Token PRINTLN_TOKEN = new Token(TokenType.PRINTLN, "", new Pos(0, 0));
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Welcome to OwnLang " + Version.VERSION + " REPL"); System.out.println("Welcome to OwnLang " + Version.VERSION + " REPL");