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

net.intelie.pipes.generated.ParseExceptionMessage Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes.generated;

import net.intelie.pipes.PipeException;
import net.intelie.pipes.ast.SourceLocation;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ParseExceptionMessage {
    public static String make(String message, Token currentToken,
                              int[][] expectedTokenSequences,
                              String[] tokenImage) {
        if (currentToken == null || expectedTokenSequences == null || tokenImage == null)
            return message;

        try {
            return "Unexpected " + makeFound(tokenImage, expectedTokenSequences, currentToken.next)
                    + ". " + makeExpectedStr(tokenImage, expectedTokenSequences);
        } catch (Throwable e) {
            return message;
        }
    }

    private static String makeExpectedStr(String[] tokenImage, int[][] expectedTokenSequences) {
        Set expected = makeExpectedSet(tokenImage, expectedTokenSequences);
        List expectedList = new ArrayList<>(expected);
        String expectedStr = expectedList.size() > 1 ? "Was expecting one of: " : "Was expecting: ";
        for (int i = 0; i < Math.min(expectedList.size(), 3); i++) {
            if (i > 0)
                expectedStr += ", ";
            expectedStr += expectedList.get(i);
        }
        if (expectedList.size() > 3)
            expectedStr += "... (" + (expectedList.size() - 3) + " more)";
        else
            expectedStr += ".";
        return expectedStr;
    }

    private static String makeFound(String[] tokenImage, int[][] expectedTokenSequences, Token foundToken) {
        int maxSize = findMaxSize(expectedTokenSequences);
        String found = "";
        for (int i = 0; i < maxSize; i++) {
            if (i != 0) found += " ";
            if (foundToken.kind == 0) {
                found += tokenImage[0];
                break;
            }
            found += "\"" + foundToken.image + "\"";
            foundToken = foundToken.next;
        }
        return found;
    }

    private static Set makeExpectedSet(String[] tokenImage, int[][] expectedTokenSequences) {
        Set expected = new LinkedHashSet();
        for (int[] sequence : expectedTokenSequences) {
            String s = "";
            for (int j = 0; j < sequence.length; j++) {
                if (j > 0) s += " ";
                s += tokenImage[sequence[j]];
            }
            expected.add(s);
        }
        return expected;
    }

    private static int findMaxSize(int[][] expectedTokenSequences) {
        int maxSize = 0;
        for (int[] sequence : expectedTokenSequences) {
            if (maxSize < sequence.length) {
                maxSize = sequence.length;
            }
        }
        return maxSize;
    }

    public static PipeException handle(Throwable e, SourceLocation location) {
        if (e instanceof ParseException && ((ParseException) e).currentToken != null)
            return new PipeException(PipesTokenManager.toFullLocation((PipesTokenManager.Token)
                    ((ParseException) e).currentToken.next),
                    PipeException.Level.SYNTACTIC,
                    ParseExceptionMessage.make(e.getMessage(), ((ParseException) e).currentToken, ((ParseException) e).expectedTokenSequences, ((ParseException) e).tokenImage));

        return PipeException.handle(e).maybeAddSource(location);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy