mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Add columns in position information
This commit is contained in:
parent
90db2b0aa3
commit
fc73bce943
@ -1,5 +1,7 @@
|
||||
package com.annimon.ownlang.exceptions;
|
||||
|
||||
import com.annimon.ownlang.parser.Pos;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author aNNiMON
|
||||
@ -10,7 +12,7 @@ public final class LexerException extends RuntimeException {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public LexerException(int row, int col, String message) {
|
||||
super("["+row+":"+col+"] " + message);
|
||||
public LexerException(Pos pos, String message) {
|
||||
super(pos.format() + " " + message);
|
||||
}
|
||||
}
|
@ -357,10 +357,10 @@ public final class Lexer {
|
||||
}
|
||||
|
||||
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) {
|
||||
return new LexerException(row, col, text);
|
||||
return new LexerException(new Pos(row, col), text);
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,9 @@
|
||||
package com.annimon.ownlang.parser;
|
||||
|
||||
public final class ParseError {
|
||||
|
||||
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;
|
||||
}
|
||||
public record ParseError(Exception exception, Pos pos) {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ParseError on line " + line + ": " + exception.getMessage();
|
||||
return "ParseError on line " + pos.row() + ": " + exception;
|
||||
}
|
||||
}
|
||||
|
@ -17,8 +17,8 @@ public final class ParseErrors implements Iterable<ParseError> {
|
||||
errors.clear();
|
||||
}
|
||||
|
||||
public void add(Exception ex, int line) {
|
||||
errors.add(new ParseError(line, ex));
|
||||
public void add(Exception ex, Pos pos) {
|
||||
errors.add(new ParseError(ex, pos));
|
||||
}
|
||||
|
||||
public boolean hasErrors() {
|
||||
|
@ -22,7 +22,7 @@ public final class Parser {
|
||||
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;
|
||||
static {
|
||||
@ -71,7 +71,7 @@ public final class Parser {
|
||||
try {
|
||||
result.add(statement());
|
||||
} catch (Exception ex) {
|
||||
parseErrors.add(ex, getErrorLine());
|
||||
parseErrors.add(ex, getErrorPos());
|
||||
recover();
|
||||
}
|
||||
}
|
||||
@ -79,10 +79,10 @@ public final class Parser {
|
||||
return result;
|
||||
}
|
||||
|
||||
private int getErrorLine() {
|
||||
if (size == 0) return 0;
|
||||
if (pos >= size) return tokens.get(size - 1).row();
|
||||
return tokens.get(pos).row();
|
||||
private Pos getErrorPos() {
|
||||
if (size == 0) return new Pos(0, 0);
|
||||
if (pos >= size) return tokens.get(size - 1).pos();
|
||||
return tokens.get(pos).pos();
|
||||
}
|
||||
|
||||
private void recover() {
|
||||
|
@ -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 + "]";
|
||||
}
|
||||
}
|
@ -3,14 +3,10 @@ package com.annimon.ownlang.parser;
|
||||
/**
|
||||
* @author aNNiMON
|
||||
*/
|
||||
public record Token(TokenType type, String text, int row, int col) {
|
||||
|
||||
public String position() {
|
||||
return "[" + row + " " + col + "]";
|
||||
}
|
||||
public record Token(TokenType type, String text, Pos pos) {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return type.name() + " " + position() + " " + text;
|
||||
return type.name() + " " + pos().format() + " " + text;
|
||||
}
|
||||
}
|
||||
|
@ -172,11 +172,11 @@ public class LexerTest {
|
||||
}
|
||||
|
||||
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) {
|
||||
return new Token(type, text, row, col);
|
||||
private static Token token(TokenType type, String text, Pos pos) {
|
||||
return new Token(type, text, pos);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,10 +7,7 @@ import com.annimon.ownlang.exceptions.StoppedException;
|
||||
import com.annimon.ownlang.lib.Functions;
|
||||
import com.annimon.ownlang.lib.UserDefinedFunction;
|
||||
import com.annimon.ownlang.lib.Variables;
|
||||
import com.annimon.ownlang.parser.Lexer;
|
||||
import com.annimon.ownlang.parser.Parser;
|
||||
import com.annimon.ownlang.parser.Token;
|
||||
import com.annimon.ownlang.parser.TokenType;
|
||||
import com.annimon.ownlang.parser.*;
|
||||
import com.annimon.ownlang.parser.ast.BlockStatement;
|
||||
import com.annimon.ownlang.parser.ast.Statement;
|
||||
import com.annimon.ownlang.parser.visitors.PrintVisitor;
|
||||
@ -36,7 +33,7 @@ public final class Repl {
|
||||
RESET = ":reset",
|
||||
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) {
|
||||
System.out.println("Welcome to OwnLang " + Version.VERSION + " REPL");
|
||||
|
Loading…
Reference in New Issue
Block a user