Search in sources :

Example 11 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project ballerina by ballerina-lang.

the class BallerinaParserErrorStrategy method reportInputMismatch.

@Override
public void reportInputMismatch(Parser parser, InputMismatchException e) {
    setContextException(parser);
    Token offendingToken = e.getOffendingToken();
    String mismatchedToken = getTokenErrorDisplay(offendingToken);
    String expectedToken = e.getExpectedTokens().toString(parser.getVocabulary());
    DiagnosticPos pos = getPosition(offendingToken);
    dlog.error(pos, DiagnosticCode.MISMATCHED_INPUT, mismatchedToken, expectedToken);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) Token(org.antlr.v4.runtime.Token)

Example 12 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project ballerina by ballerina-lang.

the class ServiceContextItemSorter method sortItems.

@Override
public void sortItems(TextDocumentServiceContext ctx, List<CompletionItem> completionItems) {
    BLangNode previousNode = ctx.get(CompletionKeys.PREVIOUS_NODE_KEY);
    /*
        Remove the statement type completion type. When the going through the parser
        rule contexts such as typeNameContext, we add the statements as well.
        Sorters are responsible for to the next level of such filtering.
         */
    this.removeCompletionsByType(new ArrayList<>(Collections.singletonList(ItemResolverConstants.STATEMENT_TYPE)), completionItems);
    if (previousNode == null) {
        this.populateWhenCursorBeforeOrAfterEp(completionItems);
    } else if (previousNode instanceof BLangVariableDef) {
        // BType bLangType = ((BLangVariableDef) previousNode).var.type;
        // if (bLangType instanceof BEndpointType) {
        // this.populateWhenCursorBeforeOrAfterEp(completionItems);
        // } else {
        this.setPriorities(completionItems);
        CompletionItem resItem = this.getResourceSnippet();
        resItem.setSortText(Priority.PRIORITY160.toString());
        completionItems.add(resItem);
    // }
    } else if (previousNode instanceof BLangResource) {
        completionItems.clear();
        completionItems.add(this.getResourceSnippet());
    }
}
Also used : BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionItem(org.eclipse.lsp4j.CompletionItem) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)

Example 13 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project carbon-business-process by wso2.

the class ArchiveBasedHumanTaskDeploymentUnitBuilder method readInTheWSDLFile.

/**
 * Read the WSDL file given the input stream for the WSDL source
 *
 * @param in           WSDL input stream
 * @param entryName    ZIP file entry name
 * @param fromRegistry whether the wsdl is read from registry
 * @return WSDL Definition
 * @throws javax.wsdl.WSDLException at parser error
 */
public static Definition readInTheWSDLFile(InputStream in, String entryName, boolean fromRegistry) throws WSDLException {
    WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
    // switch off the verbose mode for all usecases
    reader.setFeature(HumanTaskConstants.JAVAX_WSDL_VERBOSE_MODE_KEY, false);
    reader.setFeature("javax.wsdl.importDocuments", true);
    Definition def;
    Document doc;
    try {
        doc = XMLUtils.newDocument(in);
    } catch (ParserConfigurationException e) {
        throw new WSDLException(WSDLException.PARSER_ERROR, "Parser Configuration Error", e);
    } catch (SAXException e) {
        throw new WSDLException(WSDLException.PARSER_ERROR, "Parser SAX Error", e);
    } catch (IOException e) {
        throw new WSDLException(WSDLException.INVALID_WSDL, "IO Error", e);
    }
    // Log when and from where the WSDL is loaded.
    if (log.isDebugEnabled()) {
        log.debug("Reading 1.1 WSDL with base uri = " + entryName);
        log.debug("  the document base uri = " + entryName);
    }
    if (fromRegistry) {
        throw new UnsupportedOperationException("This operation is not currently " + "supported in this version of WSO2 BPS.");
    } else {
        def = reader.readWSDL(entryName, doc.getDocumentElement());
    }
    def.setDocumentBaseURI(entryName);
    return def;
}
Also used : WSDLException(javax.wsdl.WSDLException) Definition(javax.wsdl.Definition) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HumanInteractionsDocument(org.wso2.carbon.humantask.HumanInteractionsDocument) HTDeploymentConfigDocument(org.wso2.carbon.humantask.core.deployment.config.HTDeploymentConfigDocument) Document(org.w3c.dom.Document) WSDLReader(javax.wsdl.xml.WSDLReader) SAXException(org.xml.sax.SAXException)

Example 14 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project carbon-apimgt by wso2.

the class JSONAnalyzer method analyze.

/**
 * @param payload json payload
 * @throws APIMThreatAnalyzerException if defined limits for json payload exceeds
 */
@Override
public void analyze(String payload, String apiContext) throws APIMThreatAnalyzerException {
    try (JsonParser parser = factory.createParser(new StringReader(payload))) {
        int currentDepth = 0;
        int currentFieldCount = 0;
        JsonToken token;
        while ((token = parser.nextToken()) != null) {
            switch(token) {
                case START_OBJECT:
                    currentDepth += 1;
                    try {
                        analyzeDepth(maxJsonDepth, currentDepth, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case END_OBJECT:
                    currentDepth -= 1;
                    break;
                case FIELD_NAME:
                    currentFieldCount += 1;
                    String name = parser.getCurrentName();
                    try {
                        analyzeField(name, maxFieldCount, currentFieldCount, maxFieldLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case VALUE_STRING:
                    String value = parser.getText();
                    try {
                        analyzeString(value, maxStringLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case START_ARRAY:
                    try {
                        analyzeArray(parser, maxArrayElementCount, maxStringLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
            }
        }
    } catch (JsonParseException e) {
        logger.error(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload parsing failed", e);
        throw new APIMThreatAnalyzerException(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload parsing failed", e);
    } catch (IOException e) {
        logger.error(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload build failed", e);
        throw new APIMThreatAnalyzerException(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload build failed", e);
    }
}
Also used : StringReader(java.io.StringReader) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) APIMThreatAnalyzerException(org.wso2.carbon.apimgt.ballerina.threatprotection.APIMThreatAnalyzerException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 15 with Parser

use of org.wso2.ballerinalang.compiler.parser.Parser in project siddhi by wso2.

the class SiddhiCompiler method parse.

public static SiddhiApp parse(String source) {
    ANTLRInputStream input = new ANTLRInputStream(source);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    // parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.parse();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (SiddhiApp) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) SiddhiQLBaseVisitorImpl(org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)10 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)10 ParseTree (org.antlr.v4.runtime.tree.ParseTree)9 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)9 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)7 Token (org.antlr.v4.runtime.Token)4 JsonObject (com.google.gson.JsonObject)3 JsonParser (com.google.gson.JsonParser)3 Response (feign.Response)3 ArrayList (java.util.ArrayList)3 BallerinaParser (org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser)3 IdentityProviderException (org.wso2.carbon.apimgt.core.exception.IdentityProviderException)3 JsonArray (com.google.gson.JsonArray)2 IOException (java.io.IOException)2 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 CompletionItem (org.eclipse.lsp4j.CompletionItem)2 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)2 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1