use of org.ballerinalang.model.Whitespace in project ballerina by ballerina-lang.
the class BLangWSPreservingParserListener method exitEveryRule.
@Override
public void exitEveryRule(ParserRuleContext parserRuleContext) {
if (getWSWasCalledOn == parserRuleContext) {
return;
}
int rangeEndTokenIndex;
if (parserRuleContext.stop == null) {
ParseTree child = parserRuleContext.getChild(1);
if (child instanceof TerminalNode) {
// This is needed to handle A + B + C case of BinaryAddSubExpression
rangeEndTokenIndex = ((TerminalNode) child).getSymbol().getTokenIndex();
} else {
rangesOfRuleContext.pop();
return;
}
} else {
rangeEndTokenIndex = parserRuleContext.stop.getTokenIndex() + 1;
}
closeLastRange(rangeEndTokenIndex);
Stack<TokenRange> tokenRanges = new Stack<>();
// TODO: check why rangesOfRuleContext become empty before closing the last range.
if (!rangesOfRuleContext.isEmpty()) {
tokenRanges = rangesOfRuleContext.pop();
}
Stack<Whitespace> ws = new Stack<>();
for (TokenRange range : tokenRanges) {
addWSFromRange(ws, range);
}
wsSinceLastNode.addAll(ws);
if (!rangesOfRuleContext.isEmpty()) {
rangesOfRuleContext.peek().add(new TokenRange(rangeEndTokenIndex));
}
}
use of org.ballerinalang.model.Whitespace in project ballerina by ballerina-lang.
the class BLangWSPreservingParserListener method getWS.
@Override
protected Set<Whitespace> getWS(ParserRuleContext parserRuleContext) {
exitEveryRule(parserRuleContext);
this.getWSWasCalledOn = parserRuleContext;
SortedSet<Whitespace> wsForThisNode = wsSinceLastNode;
// TODO: this logic may be simplified, need a bigger sample set to check. check when unit tests are impl.
for (Iterator<Whitespace> iterator = wsForThisNode.iterator(); iterator.hasNext(); ) {
Whitespace ws = iterator.next();
if (usedTokens.contains(ws)) {
iterator.remove();
} else {
usedTokens.add(ws);
}
}
wsSinceLastNode = new TreeSet<>();
return wsForThisNode;
}
use of org.ballerinalang.model.Whitespace in project ballerina by ballerina-lang.
the class BLangWSPreservingParserListener method pushWS.
private void pushWS(Stack<Whitespace> whitespaceStack, Token previousNonWS, String wsString) {
boolean isStatic = isAllUpper(BallerinaLexer.VOCABULARY.getSymbolicName(previousNonWS.getType()));
Whitespace wsToken = new Whitespace(previousNonWS.getTokenIndex(), wsString, previousNonWS.getText(), isStatic);
whitespaceStack.push(wsToken);
}
use of org.ballerinalang.model.Whitespace in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addVariableDefStatement.
public void addVariableDefStatement(DiagnosticPos pos, Set<Whitespace> ws, String identifier, boolean exprAvailable, boolean endpoint, boolean safeAssignment) {
BLangVariable var = (BLangVariable) TreeBuilder.createVariableNode();
BLangVariableDef varDefNode = (BLangVariableDef) TreeBuilder.createVariableDefinitionNode();
// TODO : Remove endpoint logic from here.
Set<Whitespace> wsOfSemiColon = null;
if (endpoint) {
var.addWS(endpointVarWs);
var.addWS(endpointKeywordWs);
endpointVarWs = null;
endpointKeywordWs = null;
} else {
wsOfSemiColon = removeNthFromLast(ws, 0);
}
var.pos = pos;
var.addWS(ws);
var.setName(this.createIdentifier(identifier));
var.setTypeNode(this.typeNodeStack.pop());
var.safeAssignment = safeAssignment;
if (exprAvailable) {
var.setInitialExpression(this.exprNodeStack.pop());
}
varDefNode.pos = pos;
varDefNode.setVariable(var);
varDefNode.addWS(wsOfSemiColon);
addStmtToCurrentBlock(varDefNode);
}
use of org.ballerinalang.model.Whitespace in project ballerina by ballerina-lang.
the class BLangParserListener method exitSimpleLiteral.
/**
* {@inheritDoc}
*/
@Override
public void exitSimpleLiteral(BallerinaParser.SimpleLiteralContext ctx) {
if (ctx.exception != null) {
return;
}
TerminalNode node;
DiagnosticPos pos = getCurrentPos(ctx);
Set<Whitespace> ws = getWS(ctx);
Long longObject;
BallerinaParser.IntegerLiteralContext integerLiteralContext = ctx.integerLiteral();
if (integerLiteralContext != null && (longObject = getIntegerLiteral(ctx, ctx.integerLiteral())) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.INT, longObject);
} else if ((node = ctx.FloatingPointLiteral()) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.FLOAT, Double.parseDouble(getNodeValue(ctx, node)));
} else if ((node = ctx.BooleanLiteral()) != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.BOOLEAN, Boolean.parseBoolean(node.getText()));
} else if ((node = ctx.QuotedStringLiteral()) != null) {
String text = node.getText();
text = text.substring(1, text.length() - 1);
text = StringEscapeUtils.unescapeJava(text);
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.STRING, text);
} else if (ctx.NullLiteral() != null) {
this.pkgBuilder.addLiteralValue(pos, ws, TypeTags.NULL, null);
}
}
Aggregations