Search in sources :

Example 96 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class IncludeItemTransfer method nativeToJava.

@Override
protected ParseTree[] nativeToJava(final TransferData transferData) {
    byte[] bytes = (byte[]) super.nativeToJava(transferData);
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
    try {
        int n = in.readInt();
        ParseTree[] items = new ParseTree[2 * n];
        String fileName;
        String hiddenBefore;
        for (int i = 0; i < n; i++) {
            hiddenBefore = in.readUTF();
            fileName = in.readUTF();
            items[2 * i] = ConfigTreeNodeUtilities.createHiddenTokenNode(hiddenBefore);
            items[2 * i + 1] = new AddedParseTree(fileName);
        }
        return items;
    } catch (IOException e) {
        ErrorReporter.logExceptionStackTrace(e);
        return new ParseTree[] {};
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 97 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class IncludeItemTransfer method javaToNative.

@Override
protected void javaToNative(final Object object, final TransferData transferData) {
    ParseTree[] items = (ParseTree[]) object;
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    byte[] bytes = null;
    try {
        out.writeInt(items.length);
        for (int i = 0; i < items.length; i++) {
            out.writeUTF(convertToString(items[2 * i]));
            out.writeUTF(convertToString(items[2 * i + 1]));
        }
        out.close();
        bytes = byteOut.toByteArray();
    } catch (IOException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
    if (bytes != null) {
        super.javaToNative(bytes, transferData);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AddedParseTree(org.eclipse.titan.common.parsers.AddedParseTree) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 98 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class IncludeSectionDragSourceListener method dragStart.

@Override
public void dragStart(final DragSourceEvent event) {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    event.doit = !selection.isEmpty() && (selection.getFirstElement() instanceof ParseTree);
}
Also used : IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 99 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project titan.EclipsePlug-ins by eclipse.

the class IncludeSectionDragSourceListener method dragSetData.

@Override
public void dragSetData(final DragSourceEvent event) {
    if (IncludeItemTransfer.getInstance().isSupportedType(event.dataType)) {
        IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
        List<ParseTree> items = new ArrayList<ParseTree>();
        if (!selection.isEmpty()) {
            for (Iterator<?> it = selection.iterator(); it.hasNext(); ) {
                Object element = it.next();
                if (element instanceof ParseTree) {
                    items.add((ParseTree) element);
                }
            }
            event.data = items.toArray(new ParseTree[items.size()]);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 100 with ParseTree

use of org.antlr.v4.runtime.tree.ParseTree in project inmemantlr by julianthome.

the class TestSimple method testOclStringParsing.

@Test
public void testOclStringParsing() throws IOException {
    try (InputStream sgrammar = getClass().getClassLoader().getResourceAsStream("inmemantlr/DeepOcl.g4")) {
        sgrammarcontent = FileUtils.getStringFromStream(sgrammar);
    }
    String toParse = "context Dependent inv inv54: (self.birthyear " + ">=2012 and self.allowances->size()=1) or (self.birthyear < " + "2012 and self.birthyear >= 1996)";
    GenericParser gp = new GenericParser(sgrammarcontent);
    DefaultTreeListener t = new DefaultTreeListener();
    gp.setListener(t);
    boolean compile;
    try {
        gp.compile();
        compile = true;
    } catch (CompilationException e) {
        compile = false;
        LOGGER.debug(e.getMessage());
    }
    Assertions.assertTrue(compile);
    try {
        ParserRuleContext ctx = gp.parse(toParse);
        LOGGER.info("ctx {}", ctx.getChild(0).getText());
    } catch (IllegalWorkflowException | ParsingException e) {
        Assertions.assertTrue(false);
    }
    ParseTree a = t.getParseTree();
    Assertions.assertTrue(a.getNodes().size() > 1);
    LOGGER.debug(a.toDot());
}
Also used : CompilationException(org.snt.inmemantlr.exceptions.CompilationException) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) InputStream(java.io.InputStream) ParsingException(org.snt.inmemantlr.exceptions.ParsingException) IllegalWorkflowException(org.snt.inmemantlr.exceptions.IllegalWorkflowException) DefaultTreeListener(org.snt.inmemantlr.listener.DefaultTreeListener) ParseTree(org.snt.inmemantlr.tree.ParseTree) GenericParser(org.snt.inmemantlr.GenericParser) Test(org.junit.jupiter.api.Test)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)311 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)104 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)60 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)49 AddedParseTree (org.eclipse.titan.common.parsers.AddedParseTree)46 CharStream (org.antlr.v4.runtime.CharStream)43 Test (org.junit.Test)43 CommonToken (org.antlr.v4.runtime.CommonToken)35 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)35 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 File (java.io.File)26 ArrayList (java.util.ArrayList)22 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)22 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 Grammar (org.antlr.v4.tool.Grammar)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 IOException (java.io.IOException)19 LexerGrammar (org.antlr.v4.tool.LexerGrammar)16