Search in sources :

Example 26 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class WorkspaceUtils method prepareCompilerContext.

private static CompilerContext prepareCompilerContext(String fileName, String source) {
    CompilerContext context = new CompilerContext();
    List<Name> names = new ArrayList<>();
    names.add(new Name("."));
    // Registering custom PackageRepository to provide ballerina content without a file in file-system
    context.put(PackageRepository.class, new InMemoryPackageRepository(new PackageID(Names.ANON_ORG, names, new Name("0.0.0")), "", fileName, source.getBytes(StandardCharsets.UTF_8)));
    return context;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) PackageID(org.ballerinalang.model.elements.PackageID) Name(org.wso2.ballerinalang.compiler.util.Name) CompilerOptionName(org.ballerinalang.compiler.CompilerOptionName)

Example 27 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class CommandUtil method getStructDocumentationByPosition.

/**
 * Get the Documentation attachment for the struct definition.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the struct in the source
 * @return {@link String}   Documentation attachment for the struct
 */
static DocAttachmentInfo getStructDocumentationByPosition(BLangPackage bLangPackage, int line) {
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangStruct) {
            BLangStruct structNode = (BLangStruct) topLevelNode;
            DiagnosticPos structPos = CommonUtil.toZeroBasedPosition(structNode.getPosition());
            int structStart = structPos.getStartLine();
            if (structStart == line) {
                return getStructNodeDocumentation(structNode, line);
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 28 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class CommandUtil method getServiceDocumentationByPosition.

/**
 * Get the Documentation attachment for the service.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the service in the source
 * @return {@link String}   Documentation attachment for the service
 */
static DocAttachmentInfo getServiceDocumentationByPosition(BLangPackage bLangPackage, int line) {
    // TODO: Currently resource position is invalid and we use the annotation attachment positions.
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangService) {
            BLangService serviceNode = (BLangService) topLevelNode;
            DiagnosticPos servicePos = CommonUtil.toZeroBasedPosition(serviceNode.getPosition());
            List<BLangAnnotationAttachment> annotationAttachments = serviceNode.getAnnotationAttachments();
            if (!annotationAttachments.isEmpty()) {
                DiagnosticPos lastAttachmentPos = CommonUtil.toZeroBasedPosition(annotationAttachments.get(annotationAttachments.size() - 1).getPosition());
                if (lastAttachmentPos.getEndLine() < line && line < servicePos.getEndLine()) {
                    return getServiceNodeDocumentation(serviceNode, lastAttachmentPos.getEndLine() + 1);
                }
            } else if (servicePos.getStartLine() == line) {
                return getServiceNodeDocumentation(serviceNode, line);
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 29 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class ParserUtils method getBallerinaFileForContent.

/**
 * This method is designed to generate the Ballerina model and Diagnostic information for a given Ballerina content.
 * Ideal use case is generating Ballerina model and Diagnostic information for unsaved Ballerina files.
 *
 * @param fileName      - File name. This can be any arbitrary name as as we haven't save the file yet.
 * @param source        - Ballerina source content that needs to be parsed.
 * @param compilerPhase - This will tell up to which point(compiler phase) we should process the model
 * @return BallerinaFile - Object which contains Ballerina model and Diagnostic information
 */
public static BallerinaFile getBallerinaFileForContent(Path filePath, String fileName, String source, CompilerPhase compilerPhase) {
    CompilerContext context = prepareCompilerContext(fileName, source);
    CompilerOptions options = CompilerOptions.getInstance(context);
    options.put(COMPILER_PHASE, compilerPhase.toString());
    options.put(PRESERVE_WHITESPACE, Boolean.TRUE.toString());
    return getBallerinaFile(filePath, fileName, context);
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerOptions(org.wso2.ballerinalang.compiler.util.CompilerOptions)

Example 30 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project ballerina by ballerina-lang.

the class ParserUtils method prepareCompilerContext.

/**
 * Returns a CompilerContext for the provided fileName and Ballerina source content.
 *
 * @param fileName - File name. This can be any arbitrary name as as we haven't save the file yet.
 * @param source   - Ballerina source content that needs to be parsed.
 * @return CompilerContext
 */
private static CompilerContext prepareCompilerContext(String fileName, String source) {
    CompilerContext context = new CompilerContext();
    List<Name> names = new ArrayList<>();
    names.add(new org.wso2.ballerinalang.compiler.util.Name("."));
    // Registering custom PackageRepository to provide ballerina content without a file in file-system
    context.put(PackageRepository.class, new InMemoryPackageRepository(PackageID.DEFAULT, "", fileName, source.getBytes(StandardCharsets.UTF_8)));
    return context;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Name(org.wso2.ballerinalang.compiler.util.Name) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

Test (org.testng.annotations.Test)22 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 ArrayList (java.util.ArrayList)11 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 OMElement (org.apache.axiom.om.OMElement)8 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)8 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)8 Event (org.wso2.siddhi.core.event.Event)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Compiler (org.wso2.ballerinalang.compiler.Compiler)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)6