Search in sources :

Example 1 with Whitespace

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));
    }
}
Also used : TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Whitespace(org.ballerinalang.model.Whitespace) Stack(java.util.Stack)

Example 2 with Whitespace

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;
}
Also used : Whitespace(org.ballerinalang.model.Whitespace)

Example 3 with Whitespace

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);
}
Also used : Whitespace(org.ballerinalang.model.Whitespace)

Example 4 with Whitespace

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);
}
Also used : BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Whitespace(org.ballerinalang.model.Whitespace)

Example 5 with Whitespace

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);
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Whitespace(org.ballerinalang.model.Whitespace) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser)

Aggregations

Whitespace (org.ballerinalang.model.Whitespace)20 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)5 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)4 IdentifierNode (org.ballerinalang.model.tree.IdentifierNode)3 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)3 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)3 CaseFormat (com.google.common.base.CaseFormat)2 JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 JsonNull (com.google.gson.JsonNull)2 JsonObject (com.google.gson.JsonObject)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Paths (java.nio.file.Paths)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2