All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.virtdata.parser.VirtDataDSL Maven / Gradle / Ivy

Go to download

Statistical sampling library for use in virtualdataset libraries, based on apache commons math 4

There is a newer version: 2.12.15
Show newest version
package io.virtdata.parser;

import io.virtdata.ast.VirtDataAST;
import io.virtdata.ast.VirtDataFlow;
import io.virtdata.generated.VirtDataLexer;
import io.virtdata.generated.VirtDataParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


public class VirtDataDSL {
    private final static Logger logger  = LoggerFactory.getLogger(VirtDataDSL.class);

    public static VirtDataDSL.ParseResult parse(String input) {

        try {
            CodePointCharStream cstream = CharStreams.fromString(input);
            VirtDataLexer lexer = new VirtDataLexer(cstream);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            VirtDataParser parser = new VirtDataParser(tokens);
            VirtDataBuilder astListener = new VirtDataBuilder();
            parser.addParseListener(astListener);

            VirtDataParser.VirtdataFlowContext virtdataFlowContext = parser.virtdataFlow();
            logger.trace("parse tree: " + virtdataFlowContext.toStringTree(parser));

            if (astListener.hasErrors()) {
                System.out.println(astListener.getErrorNodes());
            }

            VirtDataAST ast = astListener.getModel();
            List flows = ast.getFlows();
            if (flows.size() > 1) {
                throw new RuntimeException("Only one flow expected here.");
            }

            if (astListener.hasErrors()) {
                throw new RuntimeException("Error parsing input '" + input + "'");
            }

            return new ParseResult(flows.get(0));

        } catch (Exception e) {
            logger.warn("Error while parsing flow:" + e.getMessage());
            return new ParseResult(e);
        }
    }

    public static class ParseResult {
        public Throwable throwable;
        public VirtDataFlow flow;
        public ParseResult(VirtDataFlow flow) {
            this.flow = flow;
        }
        public ParseResult(Throwable throwable) {
            this.throwable = throwable;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy