Skip to content

Commit

Permalink
Replaced Stack by ArrayDeque in AstBuilderListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Feb 14, 2024
1 parent dca827f commit 569510f
Showing 1 changed file with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
*******************************************************************************/
package org.eclipse.acceleo.query.parser;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.EmptyStackException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;

import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.BaseErrorListener;
Expand Down Expand Up @@ -395,8 +396,8 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int
final Integer startPosition = Integer.valueOf(((EnumLitContext)parser.getContext()).start
.getStartIndex());
final Integer endPosition = Integer.valueOf(((Token)offendingSymbol).getStopIndex() + 1);
diagnosticStack.push(new BasicDiagnostic(Diagnostic.WARNING, PLUGIN_ID, 0, msg, new Object[] {
startPosition, endPosition, }));
diagnosticStack.addLast(new BasicDiagnostic(Diagnostic.WARNING, PLUGIN_ID, 0, msg,
new Object[] {startPosition, endPosition, }));
}
}

Expand Down Expand Up @@ -427,7 +428,7 @@ private void classifierTypeRuleContextError(Object offendingSymbol, String msg,
diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, PLUGIN_ID, 0, String.format(
INVALID_TYPE_LITERAL, ctx.getText()), new Object[] {errorEClassifierTypeLiteral }));
errors.add(errorEClassifierTypeLiteral);
final Expression variableExpression = lambdaVariableExpression.peek();
final Expression variableExpression = lambdaVariableExpression.getLast();
final VariableDeclaration variableDeclaration = builder.variableDeclaration(variableName,
errorEClassifierTypeLiteral, variableExpression);
setPositions(variableDeclaration, ctx.start, (Token)offendingSymbol);
Expand Down Expand Up @@ -489,7 +490,7 @@ private void typeLiteralContextError(Object offendingSymbol, String msg, Recogni
diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, PLUGIN_ID, 0, String.format(
INVALID_TYPE_LITERAL, msg), new Object[] {type }));
errors.add(type);
final Expression variableExpression = lambdaVariableExpression.peek();
final Expression variableExpression = lambdaVariableExpression.getLast();
final VariableDeclaration variableDeclaration = builder.variableDeclaration(variableName,
type, variableExpression);
setPositions(variableDeclaration, ((TypeLiteralContext)e.getCtx()).start,
Expand All @@ -498,16 +499,16 @@ private void typeLiteralContextError(Object offendingSymbol, String msg, Recogni
final ErrorExpression errorExpression = builder.errorExpression();
pushError(errorExpression, MISSING_EXPRESSION);
setPositions(errorExpression, ((TypeLiteralContext)e.getCtx()).start, (Token)offendingSymbol);
} else if (stack.isEmpty() || !(stack.peek() instanceof TypeLiteral)) {
} else if (stack.isEmpty() || !(stack.getLast() instanceof TypeLiteral)) {
errorRule = QueryParser.RULE_typeLiteral;
final ErrorTypeLiteral errorTypeLiteral = builder.errorTypeLiteral();
setPositions(errorTypeLiteral, ((TypeLiteralContext)e.getCtx()).start,
(Token)offendingSymbol);
pushError(errorTypeLiteral, String.format(INVALID_TYPE_LITERAL, msg));
} else {
diagnosticStack.push(new BasicDiagnostic(Diagnostic.WARNING, PLUGIN_ID, 0, msg, new Object[] {
((TypeLiteralContext)e.getCtx()).start.getStartIndex(), ((Token)offendingSymbol)
.getStopIndex() + 1, }));
diagnosticStack.addLast(new BasicDiagnostic(Diagnostic.WARNING, PLUGIN_ID, 0, msg,
new Object[] {((TypeLiteralContext)e.getCtx()).start.getStartIndex(),
((Token)offendingSymbol).getStopIndex() + 1, }));
}
}

Expand Down Expand Up @@ -568,15 +569,15 @@ private void variableDefinitionContextError(Object offendingSymbol, RecognitionE
} else {
type = null;
}
final Expression variableExpression = lambdaVariableExpression.peek();
final Expression variableExpression = lambdaVariableExpression.getLast();
final ErrorVariableDeclaration errorVariableDeclaration = builder.errorVariableDeclaration(
variableName, type, variableExpression);
setIdentifierPositions(errorVariableDeclaration, (Token)e.getCtx().getChild(0).getPayload());
setPositions(errorVariableDeclaration, ((VariableDefinitionContext)e.getCtx()).start,
(Token)offendingSymbol);
pushError(errorVariableDeclaration, "incomplete variable definition");
} else {
final Expression variableExpression = lambdaVariableExpression.peek();
final Expression variableExpression = lambdaVariableExpression.getLast();
errorRule = QueryParser.RULE_variableDefinition;
final ErrorVariableDeclaration errorVariableDeclaration = builder.errorVariableDeclaration(
null, null, variableExpression);
Expand Down Expand Up @@ -744,7 +745,7 @@ private void noRecognitionException(Recognizer<?, ?> recognizer, Object offendin
final Integer startPosition = Integer.valueOf(((ParserRuleContext)parser.getContext()).start
.getStartIndex());
final Integer endPosition = Integer.valueOf(((Token)offendingSymbol).getStopIndex() + 1);
diagnosticStack.push(new BasicDiagnostic(Diagnostic.WARNING, PLUGIN_ID, 0, msg, new Object[] {
diagnosticStack.addLast(new BasicDiagnostic(Diagnostic.WARNING, PLUGIN_ID, 0, msg, new Object[] {
startPosition, endPosition, }));
}
}
Expand All @@ -762,7 +763,7 @@ private void noRecognitionException(Recognizer<?, ?> recognizer, Object offendin
/**
* The evaluation stack used to hold temporary results.
*/
private Stack<Object> stack = new Stack<Object>();
private Deque<Object> stack = new ArrayDeque<Object>();

/**
* The last rule index if any error. see {@link QueryParser}.
Expand All @@ -782,7 +783,7 @@ private void noRecognitionException(Recognizer<?, ?> recognizer, Object offendin
/**
* Temporary lexing warnings waiting for their {@link Expression}.
*/
private Stack<Diagnostic> diagnosticStack = new Stack<Diagnostic>();
private Deque<Diagnostic> diagnosticStack = new ArrayDeque<Diagnostic>();

/** Aggregated status of the parsing. */
private final List<Diagnostic> diagnostics = new ArrayList<Diagnostic>();
Expand All @@ -800,7 +801,7 @@ private void noRecognitionException(Recognizer<?, ?> recognizer, Object offendin
/**
* The {@link Lambda} {@link VariableDeclaration#getExpression() variable declaration expression}.
*/
private Stack<Expression> lambdaVariableExpression = new Stack<>();
private Deque<Expression> lambdaVariableExpression = new ArrayDeque<>();

/**
* Creates a new {@link AstBuilderListener}.
Expand Down Expand Up @@ -1031,10 +1032,10 @@ private Expression popExpression() {
final Expression expression = (Expression)pop();

if (!diagnosticStack.isEmpty()) {
final List<?> data = diagnosticStack.peek().getData();
final List<?> data = diagnosticStack.getLast().getData();
if (data.get(0).equals(positions.getStartPositions(expression)) && data.get(1).equals(
positions.getEndPositions(expression))) {
final Diagnostic tmpDiagnostic = diagnosticStack.pop();
final Diagnostic tmpDiagnostic = diagnosticStack.removeLast();
diagnostics.add(new BasicDiagnostic(tmpDiagnostic.getSeverity(), tmpDiagnostic
.getSource(), tmpDiagnostic.getCode(), tmpDiagnostic.getMessage(), new Object[] {
expression }));
Expand Down Expand Up @@ -1100,7 +1101,7 @@ private TypeLiteral popTypeLiteral() {
* @return the current {@link Call} at the top of the stack
*/
private Call peekCall() {
return (Call)stack.peek();
return (Call)stack.getLast();
}

/**
Expand All @@ -1110,7 +1111,7 @@ private Call peekCall() {
* the pushed object.
*/
private void push(Object obj) {
this.stack.push(obj);
this.stack.addLast(obj);
}

/**
Expand All @@ -1119,7 +1120,7 @@ private void push(Object obj) {
* @return the element at the top of the stack if any
*/
protected Object pop() {
final Object element = stack.pop();
final Object element = stack.removeLast();
return element;
}

Expand All @@ -1141,7 +1142,7 @@ private void pushError(Error error, String msg) {
* Pops the last {@link ErrorExpression}.
*/
private void popErrorExpression() {
if (!stack.isEmpty() && stack.peek() instanceof ErrorExpression) {
if (!stack.isEmpty() && stack.getLast() instanceof ErrorExpression) {
final ErrorExpression error = (ErrorExpression)pop();
errors.remove(error);
positions.remove(error);
Expand Down Expand Up @@ -1410,12 +1411,12 @@ public void exitServiceCall(ServiceCallContext ctx) {

@Override
public void enterArguments(ArgumentsContext ctx) {
lambdaVariableExpression.push((Expression)stack.peek());
lambdaVariableExpression.addLast((Expression)stack.getLast());
}

@Override
public void exitArguments(ArgumentsContext ctx) {
lambdaVariableExpression.pop();
lambdaVariableExpression.removeLast();
}

/**
Expand Down Expand Up @@ -1513,12 +1514,12 @@ public void exitVariableDefinition(VariableDefinitionContext ctx) {
final Token stop;
if (ctx.getChildCount() == 4) {
final TypeLiteral typeLiteral = popTypeLiteral();
final Expression variableExpression = lambdaVariableExpression.peek();
final Expression variableExpression = lambdaVariableExpression.getLast();
variableDeclaration = builder.variableDeclaration(ctx.getChild(0).getText(), typeLiteral,
variableExpression);
stop = ((ParserRuleContext)ctx.getChild(2)).stop;
} else {
final Expression variableExpression = lambdaVariableExpression.peek();
final Expression variableExpression = lambdaVariableExpression.getLast();
variableDeclaration = builder.variableDeclaration(ctx.getChild(0).getText(),
variableExpression);
stop = ((TerminalNode)ctx.getChild(0)).getSymbol();
Expand Down Expand Up @@ -1740,7 +1741,7 @@ public void exitLetExpr(LetExprContext ctx) {
final int column = ctx.stop.getCharPositionInLine() + ctx.stop.getText().length();
setPositions(body, position, line, column);
final List<Binding> bindingList = new ArrayList<Binding>();
while (!stack.isEmpty() && stack.peek() instanceof Binding) {
while (!stack.isEmpty() && stack.getLast() instanceof Binding) {
bindingList.add(popBinding());
}
bindings = bindingList.toArray(new Binding[bindingList.size()]);
Expand Down

0 comments on commit 569510f

Please sign in to comment.