Skip to content

Commit

Permalink
Introduce ParseException helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
hajduakos committed Sep 8, 2020
1 parent dfab6ec commit f83f9cc
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import hu.bme.mit.theta.core.decl.Decl;
import hu.bme.mit.theta.core.decl.ParamDecl;
import hu.bme.mit.theta.core.dsl.DeclSymbol;
import hu.bme.mit.theta.core.dsl.ParseException;
import hu.bme.mit.theta.core.type.Expr;
import hu.bme.mit.theta.core.type.Type;
import hu.bme.mit.theta.core.type.abstracttype.*;
Expand Down Expand Up @@ -266,7 +267,7 @@ public Expr<?> visitEqualityExpr(final EqualityExprContext ctx) {
case NEQ:
return Neq(leftOp, rightOp);
default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}

} else {
Expand All @@ -290,7 +291,7 @@ public Expr<?> visitRelationExpr(final RelationExprContext ctx) {
case GEQ:
return Geq(leftOp, rightOp);
default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}

} else {
Expand All @@ -310,7 +311,7 @@ public Expr<?> visitBitwiseOrExpr(final BitwiseOrExprContext ctx) {
case BITWISE_OR:
return BvExprs.Or(List.of(leftOp, rightOp));
default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}

} else {
Expand All @@ -328,7 +329,7 @@ public Expr<?> visitBitwiseXorExpr(final BitwiseXorExprContext ctx) {
case BITWISE_XOR:
return BvExprs.Xor(List.of(leftOp, rightOp));
default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}

} else {
Expand All @@ -346,7 +347,7 @@ public Expr<?> visitBitwiseAndExpr(final BitwiseAndExprContext ctx) {
case BITWISE_AND:
return BvExprs.And(List.of(leftOp, rightOp));
default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}

} else {
Expand All @@ -372,7 +373,7 @@ public Expr<?> visitBitwiseShiftExpr(final BitwiseShiftExprContext ctx) {
case BITWISE_ROTATE_RIGHT:
return BvExprs.RotateRight(leftOp, rightOp);
default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}

} else {
Expand All @@ -391,14 +392,14 @@ public Expr<?> visitAdditiveExpr(final AdditiveExprContext ctx) {
final Expr<?> opsHead = ops.get(0);
final List<? extends Expr<?>> opsTail = ops.subList(1, ops.size());

return createAdditiveExpr(opsHead, opsTail, ctx.opers);
return createAdditiveExpr(opsHead, opsTail, ctx.opers, ctx);
} else {
return visitChildren(ctx);
}
}

private Expr<?> createAdditiveExpr(final Expr<?> opsHead, final List<? extends Expr<?>> opsTail,
final List<? extends Token> opers) {
final List<? extends Token> opers, final AdditiveExprContext ctx) {
checkArgument(opsTail.size() == opers.size());

if (opsTail.isEmpty()) {
Expand All @@ -410,13 +411,14 @@ private Expr<?> createAdditiveExpr(final Expr<?> opsHead, final List<? extends E
final Token operHead = opers.get(0);
final List<? extends Token> opersTail = opers.subList(1, opers.size());

final Expr<?> subExpr = createAdditiveSubExpr(opsHead, newOpsHead, operHead);
final Expr<?> subExpr = createAdditiveSubExpr(opsHead, newOpsHead, operHead, ctx);

return createAdditiveExpr(subExpr, newOpsTail, opersTail);
return createAdditiveExpr(subExpr, newOpsTail, opersTail, ctx);
}
}

private Expr<?> createAdditiveSubExpr(final Expr<?> leftOp, final Expr<?> rightOp, final Token oper) {
private Expr<?> createAdditiveSubExpr(final Expr<?> leftOp, final Expr<?> rightOp, final Token oper,
final AdditiveExprContext ctx) {
switch (oper.getType()) {

case PLUS:
Expand All @@ -426,7 +428,7 @@ private Expr<?> createAdditiveSubExpr(final Expr<?> leftOp, final Expr<?> rightO
return createSubExpr(leftOp, rightOp);

default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator '" + oper.getText() + "'");
}
}

Expand Down Expand Up @@ -456,15 +458,15 @@ public Expr<?> visitMultiplicativeExpr(final MultiplicativeExprContext ctx) {
final Expr<?> opsHead = ops.get(0);
final List<? extends Expr<?>> opsTail = ops.subList(1, ops.size());

return createMutliplicativeExpr(opsHead, opsTail, ctx.opers);
return createMutliplicativeExpr(opsHead, opsTail, ctx.opers, ctx);
} else {
return visitChildren(ctx);
}

}

private Expr<?> createMutliplicativeExpr(final Expr<?> opsHead, final List<? extends Expr<?>> opsTail,
final List<? extends Token> opers) {
final List<? extends Token> opers, final MultiplicativeExprContext ctx) {
checkArgument(opsTail.size() == opers.size());

if (opsTail.isEmpty()) {
Expand All @@ -476,13 +478,14 @@ private Expr<?> createMutliplicativeExpr(final Expr<?> opsHead, final List<? ext
final Token operHead = opers.get(0);
final List<? extends Token> opersTail = opers.subList(1, opers.size());

final Expr<?> subExpr = createMultiplicativeSubExpr(opsHead, newOpsHead, operHead);
final Expr<?> subExpr = createMultiplicativeSubExpr(opsHead, newOpsHead, operHead, ctx);

return createMutliplicativeExpr(subExpr, newOpsTail, opersTail);
return createMutliplicativeExpr(subExpr, newOpsTail, opersTail, ctx);
}
}

private Expr<?> createMultiplicativeSubExpr(final Expr<?> leftOp, final Expr<?> rightOp, final Token oper) {
private Expr<?> createMultiplicativeSubExpr(final Expr<?> leftOp, final Expr<?> rightOp, final Token oper,
final MultiplicativeExprContext ctx) {
switch (oper.getType()) {

case MUL:
Expand All @@ -498,7 +501,7 @@ private Expr<?> createMultiplicativeSubExpr(final Expr<?> leftOp, final Expr<?>
return createRemExpr(leftOp, rightOp);

default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator '" + oper.getText() + "'");
}
}

Expand Down Expand Up @@ -539,7 +542,7 @@ public Expr<?> visitUnaryExpr(final UnaryExprContext ctx) {
return Neg(op);

default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}
} else {
return visitChildren(ctx);
Expand Down Expand Up @@ -588,7 +591,7 @@ private Expr<?> createAccessSubExpr(final Expr<?> op, final AccessContext access
} else if (access.prime != null) {
return createPrimeExpr(op);
} else {
throw new AssertionError();
throw new ParseException(access, "Unknown expression");
}
}

Expand Down Expand Up @@ -723,7 +726,7 @@ else if(sizeAndContent[1].startsWith("x")) {
isSigned = false;
}
else {
throw new IllegalArgumentException("Invalid bitvector literal");
throw new ParseException(ctx, "Invalid bitvector literal");
}

checkArgument(value.length <= size, "The value of the literal cannot be represented on the given amount of bits");
Expand Down Expand Up @@ -801,8 +804,7 @@ private boolean[] decodeHexadecimalBvContent(String lit) {
public RefExpr<?> visitIdExpr(final IdExprContext ctx) {
Optional<? extends Symbol> optSymbol = currentScope.resolve(ctx.id.getText());
if (optSymbol.isEmpty()) {
throw new NoSuchElementException("Identifier '" + ctx.id.getText() + "' at line " +
ctx.start.getLine() + " cannot be resolved");
throw new ParseException(ctx, "Identifier '" + ctx.id.getText() + "' cannot be resolved");
}
final Symbol symbol = optSymbol.get();
final Decl<?> decl = (Decl<?>) env.eval(symbol);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2017 Budapest University of Technology and Economics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hu.bme.mit.theta.core.dsl;

import org.antlr.v4.runtime.ParserRuleContext;

public class ParseException extends RuntimeException {
public ParseException(ParserRuleContext ctx, String message){
this(ctx, message, null);
}

public ParseException(ParserRuleContext ctx, String message, Throwable cause) {
super("Line " + ctx.getStart().getLine() + " col " + ctx.getStart().getCharPositionInLine() + ": " + message, cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Optional;
import java.util.stream.Stream;

import hu.bme.mit.theta.core.dsl.ParseException;
import org.antlr.v4.runtime.Token;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -268,7 +269,7 @@ public Expr<?> visitEqualityExpr(final EqualityExprContext ctx) {
case StsDslParser.NEQ:
return Neq(leftOp, rightOp);
default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}

} else {
Expand All @@ -292,7 +293,7 @@ public Expr<?> visitRelationExpr(final RelationExprContext ctx) {
case StsDslParser.GEQ:
return Geq(leftOp, rightOp);
default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator");
}

} else {
Expand All @@ -311,14 +312,14 @@ public Expr<?> visitAdditiveExpr(final AdditiveExprContext ctx) {
final Expr<?> opsHead = ops.get(0);
final List<? extends Expr<?>> opsTail = ops.subList(1, ops.size());

return createAdditiveExpr(opsHead, opsTail, ctx.opers);
return createAdditiveExpr(opsHead, opsTail, ctx.opers, ctx);
} else {
return visitChildren(ctx);
}
}

private Expr<?> createAdditiveExpr(final Expr<?> opsHead, final List<? extends Expr<?>> opsTail,
final List<? extends Token> opers) {
final List<? extends Token> opers, final AdditiveExprContext ctx) {
checkArgument(opsTail.size() == opers.size());

if (opsTail.isEmpty()) {
Expand All @@ -330,13 +331,14 @@ private Expr<?> createAdditiveExpr(final Expr<?> opsHead, final List<? extends E
final Token operHead = opers.get(0);
final List<? extends Token> opersTail = opers.subList(1, opers.size());

final Expr<?> subExpr = createAdditiveSubExpr(opsHead, newOpsHead, operHead);
final Expr<?> subExpr = createAdditiveSubExpr(opsHead, newOpsHead, operHead, ctx);

return createAdditiveExpr(subExpr, newOpsTail, opersTail);
return createAdditiveExpr(subExpr, newOpsTail, opersTail, ctx);
}
}

private Expr<?> createAdditiveSubExpr(final Expr<?> leftOp, final Expr<?> rightOp, final Token oper) {
private Expr<?> createAdditiveSubExpr(final Expr<?> leftOp, final Expr<?> rightOp, final Token oper,
final AdditiveExprContext ctx) {
switch (oper.getType()) {

case StsDslParser.PLUS:
Expand All @@ -346,7 +348,7 @@ private Expr<?> createAdditiveSubExpr(final Expr<?> leftOp, final Expr<?> rightO
return createSubExpr(leftOp, rightOp);

default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator '" + oper.getText() + "'");
}
}

Expand Down Expand Up @@ -375,15 +377,15 @@ public Expr<?> visitMultiplicativeExpr(final MultiplicativeExprContext ctx) {
final Expr<?> opsHead = ops.get(0);
final List<? extends Expr<?>> opsTail = ops.subList(1, ops.size());

return createMutliplicativeExpr(opsHead, opsTail, ctx.opers);
return createMutliplicativeExpr(opsHead, opsTail, ctx.opers, ctx);
} else {
return visitChildren(ctx);
}

}

private Expr<?> createMutliplicativeExpr(final Expr<?> opsHead, final List<? extends Expr<?>> opsTail,
final List<? extends Token> opers) {
final List<? extends Token> opers, final MultiplicativeExprContext ctx) {
checkArgument(opsTail.size() == opers.size());

if (opsTail.isEmpty()) {
Expand All @@ -395,13 +397,14 @@ private Expr<?> createMutliplicativeExpr(final Expr<?> opsHead, final List<? ext
final Token operHead = opers.get(0);
final List<? extends Token> opersTail = opers.subList(1, opers.size());

final Expr<?> subExpr = createMultiplicativeSubExpr(opsHead, newOpsHead, operHead);
final Expr<?> subExpr = createMultiplicativeSubExpr(opsHead, newOpsHead, operHead, ctx);

return createMutliplicativeExpr(subExpr, newOpsTail, opersTail);
return createMutliplicativeExpr(subExpr, newOpsTail, opersTail, ctx);
}
}

private Expr<?> createMultiplicativeSubExpr(final Expr<?> leftOp, final Expr<?> rightOp, final Token oper) {
private Expr<?> createMultiplicativeSubExpr(final Expr<?> leftOp, final Expr<?> rightOp, final Token oper,
final MultiplicativeExprContext ctx) {
switch (oper.getType()) {

case StsDslParser.MUL:
Expand All @@ -417,7 +420,7 @@ private Expr<?> createMultiplicativeSubExpr(final Expr<?> leftOp, final Expr<?>
return createRemExpr(leftOp, rightOp);

default:
throw new AssertionError();
throw new ParseException(ctx, "Unknown operator '" + oper.getText() + "'");
}
}

Expand Down Expand Up @@ -489,18 +492,16 @@ private Expr<?> createAccessSubExpr(final Expr<?> op, final AccessContext access
} else if (access.prime != null) {
return createPrimeExpr(op);
} else {
throw new AssertionError();
throw new ParseException(access, "Unknown expression");
}
}

private Expr<?> createFuncAppExpr(final Expr<?> op, final FuncAccessContext ctx) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("TODO: auto-generated method stub");
throw new ParseException(ctx, "Unsupported expression");
}

private Expr<?> createArrayReadExpr(final Expr<?> op, final ArrayAccessContext ctx) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("TODO: auto-generated method stub");
throw new ParseException(ctx, "Unsupported expression");
}

private Expr<?> createPrimeExpr(final Expr<?> op) {
Expand Down Expand Up @@ -536,7 +537,9 @@ public RatLitExpr visitRatLitExpr(final RatLitExprContext ctx) {
public Expr<?> visitIdExpr(final IdExprContext ctx) {
final Optional<? extends Symbol> optSymbol = currentScope.resolve(ctx.id.getText());

if (optSymbol.isEmpty()) throw new NoSuchElementException("Identifier '" + ctx.id.getText() + "' not found");
if (optSymbol.isEmpty()) {
throw new ParseException(ctx, "Identifier '" + ctx.id.getText() + "' cannot be resolved");
}
final Symbol symbol = optSymbol.get();

checkArgument(symbol instanceof DeclSymbol);
Expand Down
Loading

0 comments on commit f83f9cc

Please sign in to comment.