Skip to content
Snippets Groups Projects
Commit 2533b452 authored by Dominic Daniel Krämer's avatar Dominic Daniel Krämer
Browse files

advanced the parser.java (added basics for the BinaryOperation)

parent fd8638be
No related branches found
No related tags found
1 merge request!7Dominicsbranch
......@@ -4,11 +4,25 @@ import java.util.Optional;
public class Parser {
private abstract interface Expression {
private abstract class Expression {
}
private abstract class Value implements Expression {
private class BinaryOperation extends Expression {
Expression leftExpression;
char operator;
Expression rightExpression;
}
private class Variable extends Expression {
String variableName;
public Variable(String i) {
this.variableName = i;
}
}
private abstract class Value extends Expression {
}
......@@ -21,12 +35,10 @@ public class Parser {
private class Decimal extends Value {
public Number beforeDot;
public char dot;
public Number afterDot;
public Decimal(Number i1, Number i2) {
this.beforeDot = i1;
dot = '.';
this.afterDot = i2;
}
}
......@@ -55,15 +67,17 @@ public class Parser {
throw new ParserException("empty token list");
}
return parseValue(ts).orElseGet(() -> null);
return null;
//return parseValue(ts).orElseGet(() -> parseVariable(ts).orElseGet(() -> null));
}
//checks if a String only contains an allowed operator with parseCharacter(...)
private void parseOperator(String operator) throws ParserException {
private Boolean parseOperator(String operator) throws ParserException {
if(operator.length()>1) {
throw new ParserException("RuntimeException: invalid length for an operator: " + operator);
}
parseCharacter(operator, "+-*/^");
return true;
}
//called by methods parseOperator(...), parseDigit(...), parseDigitWithoutZero(...)
......@@ -80,9 +94,77 @@ public class Parser {
return true;
}
private Optional<BinaryOperation> parseBinaryOperation(List<Lexer.Token> ts) throws ParserException {
if(ts.get(0).getType()!=Lexer.TokenType.SPECIAL) {
throw new ParserException("SyntaxError: expected bracket, got " + ts.get(0).getData() + " instead");
}
String lbracket = ts.remove(0).getData();
parseCharacter(lbracket, "(");
int i;
boolean found = false;
//check for bracket
/*
for(i=0; i<ts.size(); i++) {
if(ts.get(0).getType() == Lexer.TokenType.SPECIAL) {
int rightBrackets = 0;
int leftBrackets = 0;
if(parseCharacter(ts.get(0).getData(),"(")) {
leftBrackets += 1;
}
else if(parseCharacter(ts.get(0).getData(),")")) {
rightBrackets += 1;
if(rightBrackets>leftBrackets) {
found = true;
break;
}
}
}
}
if(!found) {
throw new ParserException("SyntaxError: no matching bracket was found");
}
*/
List<Lexer.Token> leftList = new LinkedList<>();
//check for operator
for(i=0; i<ts.size(); i++) {
leftList.add(ts.remove(i));
if(parseOperator(ts.get(i).getData())) {
break;
}
}
ts.remove(0);
if(ts.size()==0) {
throw new ParserException("SyntaxError: could not find a matching operator");
}
Expression leftExpression = parseExpression(leftList);
Expression rightExpression = parseExpression(ts);
return null;
}
private Optional<Variable> parseVariable(List<Lexer.Token> ts) throws ParserException {
if (ts.isEmpty()) {
throw new ParserException("RuntimeException: empty token list");
}
if (ts.get(0).getType() != Lexer.TokenType.VARIABLE) {
return Optional.empty();
}
String data = ts.remove(0).getData();
if (data.isEmpty()) {
throw new ParserException("RuntimException: empty token");
}
parseCharacter(data,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
return Optional.of(new Variable(data));
}
//called by method parseException(...)
//parses a list of tokens into a value
private Optional<Expression> parseValue(List<Lexer.Token> ts) throws ParserException {
private Optional<Value> parseValue(List<Lexer.Token> ts) throws ParserException {
if (ts.isEmpty()) {
throw new ParserException("RuntimeException: empty token list");
}
......@@ -112,7 +194,7 @@ public class Parser {
//called by method parseValue(...)
//parses a decimal of a list of tokens & a string
private Expression parseDecimal(List<Lexer.Token> ts, String data) throws ParserException {
private Decimal parseDecimal(List<Lexer.Token> ts, String data) throws ParserException {
if(ts.size()<1) {
throw new ParserException("RuntimeException: empty token list");
}
......@@ -131,7 +213,7 @@ public class Parser {
//called by method parseValue(...)
//parses a String into a number
private Expression parseNumber(String data) throws ParserException{
private Number parseNumber(String data) throws ParserException{
if (data.isEmpty()) {
throw new ParserException("RuntimeException: empty token");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment