Example usage for org.antlr.v4.runtime ParserRuleContext getStart

List of usage examples for org.antlr.v4.runtime ParserRuleContext getStart

Introduction

In this page you can find the example usage for org.antlr.v4.runtime ParserRuleContext getStart.

Prototype

public Token getStart() 

Source Link

Document

Get the initial token in this context.

Usage

From source file:org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.java

License:Apache License

/**
 * Validates non negative integer value.
 *
 * @param integerValue  integer to be validated
 * @param yangConstruct yang construct for creating error message
 * @param ctx           context object of the grammar rule
 * @return valid non negative integer value
 *///from w ww .ja v  a  2  s. c  om
public static int getValidNonNegativeIntegerValue(String integerValue, YangConstructType yangConstruct,
        ParserRuleContext ctx) {

    String value = removeQuotesAndHandleConcat(integerValue);
    if (!value.matches(NON_NEGATIVE_INTEGER_PATTERN)) {
        ParserException parserException = new ParserException("YANG file error : "
                + getYangConstructType(yangConstruct) + " value " + value + " is not " + "valid.");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }

    int valueInInteger;
    try {
        valueInInteger = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        ParserException parserException = new ParserException("YANG file error : "
                + getYangConstructType(yangConstruct) + " value " + value + " is not " + "valid.");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }
    return valueInInteger;
}

From source file:org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.java

License:Apache License

/**
 * Validates integer value./*from  w  ww.j a v a  2 s  .c  o  m*/
 *
 * @param integerValue  integer to be validated
 * @param yangConstruct yang construct for creating error message
 * @param ctx           context object of the grammar rule
 * @return valid integer value
 */
public static int getValidIntegerValue(String integerValue, YangConstructType yangConstruct,
        ParserRuleContext ctx) {

    String value = removeQuotesAndHandleConcat(integerValue);
    if (!INTEGER_PATTERN.matcher(value).matches()) {
        ParserException parserException = new ParserException("YANG file error : "
                + getYangConstructType(yangConstruct) + " value " + value + " is not " + "valid.");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }

    int valueInInteger;
    try {
        valueInInteger = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        ParserException parserException = new ParserException("YANG file error : "
                + getYangConstructType(yangConstruct) + " value " + value + " is not " + "valid.");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }
    return valueInInteger;
}

From source file:org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.java

License:Apache License

/**
 * Validates boolean value.//  w  ww  .  ja  v a  2  s .co  m
 *
 * @param booleanValue  value to be validated
 * @param yangConstruct yang construct for creating error message
 * @param ctx           context object of the grammar rule
 * @return boolean value either true or false
 */
public static boolean getValidBooleanValue(String booleanValue, YangConstructType yangConstruct,
        ParserRuleContext ctx) {

    String value = removeQuotesAndHandleConcat(booleanValue);
    if (value.equals(TRUE)) {
        return true;
    } else if (value.equals(FALSE)) {
        return false;
    } else {
        ParserException parserException = new ParserException("YANG file error : "
                + getYangConstructType(yangConstruct) + " value " + value + " is not " + "valid.");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }
}

From source file:org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.java

License:Apache License

/**
 * Validates the unique syntax from the reference path.
 *
 * @param uniquePath   path of unique//  w ww .j av a 2s  . co m
 * @param prefixOfFile current file's prefix
 * @param ctx          yang construct's context to get the line number and character position
 * @return list of absolute path
 */
private static List<YangAtomicPath> validateUniqueValues(String uniquePath, String prefixOfFile,
        ParserRuleContext ctx) {
    List<YangAtomicPath> atomicPath = new LinkedList<>();
    String[] pathInUnique = uniquePath.split(SLASH_FOR_STRING);
    for (String uniqueValue : pathInUnique) {
        YangAtomicPath yangAtomicPathPath = new YangAtomicPath();
        YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(uniqueValue, YangConstructType.UNIQUE_DATA,
                ctx);
        yangAtomicPathPath.setNodeIdentifier(nodeIdentifier);
        atomicPath.add(yangAtomicPathPath);
        if (nodeIdentifier.getPrefix() != null && nodeIdentifier.getPrefix() != prefixOfFile) {
            ParserException parserException = new ParserException(
                    "YANG file error : A leaf reference, in unique," + " must refer to a leaf in the list");
            parserException.setLine(ctx.getStart().getLine());
            parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
            throw parserException;
        }
    }
    return atomicPath;
}

From source file:org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.java

License:Apache License

/**
 * Validates unique field from the list.
 *
 * @param yangList instance of YANG list
 * @param ctx      yang construct's context to get the line number and character position
 *//*from   ww  w  .  j av  a  2  s  .  c  o m*/
public static void validateUniqueInList(YangList yangList, ParserRuleContext ctx) {
    YangLeaf leaf;
    // Returns the prefix for the file where unique is present.
    String prefixOfTheFile = getRootPrefix(yangList);
    List<String> uniques = yangList.getUniqueList();
    if (uniques != null && !uniques.isEmpty()) {
        Iterator<String> uniqueList = uniques.listIterator();
        while (uniqueList.hasNext()) {
            String pathInUnique = uniqueList.next();
            List<YangAtomicPath> atomicPathInUnique = validateUniqueValues(pathInUnique, prefixOfTheFile, ctx);
            YangAtomicPath leafInPath = atomicPathInUnique.get(atomicPathInUnique.size() - 1);
            if (atomicPathInUnique.size() == 1) {
                leaf = getReferenceLeafFromUnique(yangList, leafInPath);
            } else {
                atomicPathInUnique.remove(atomicPathInUnique.size() - 1);
                YangNode holderOfLeaf = getNodeUnderListFromPath(atomicPathInUnique, yangList, ctx);
                leaf = getReferenceLeafFromUnique(holderOfLeaf, leafInPath);
            }
            if (leaf == null) {
                ParserException parserException = new ParserException("YANG file error : A leaf reference, in "
                        + "unique," + " must refer to a leaf under the list");
                parserException.setLine(ctx.getStart().getLine());
                parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
                throw parserException;
            }
        }
    }
}

From source file:org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.java

License:Apache License

/**
 * Throws parser exception for unsupported YANG constructs.
 *
 * @param type      construct type/*from  w  ww .  j ava  2 s .c  om*/
 * @param ctx       construct context
 * @param errorInfo error msg
 * @param fileName  YANG file name
 */
public static void handleUnsupportedYangConstruct(YangConstructType type, ParserRuleContext ctx,
        String errorInfo, String fileName) {
    ParserException parserException = new ParserException(
            YANG_FILE_ERROR + QUOTES + getYangConstructType(type) + QUOTES + errorInfo);
    parserException.setLine(ctx.getStart().getLine());
    parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
    //FIXME this exception should probably be thrown rather than just logged
    //throw parserException;
}

From source file:org.opencypher.tools.g4processors.BNFListener.java

License:Apache License

private String findHiddenTextBefore(ParserRuleContext ctx, boolean forHeader) {
    Token startCtx = ctx.getStart();
    int i = startCtx.getTokenIndex();
    List<Token> normalTextChannel = tokens.getHiddenTokensToLeft(i, BNFLexer.HIDDEN);
    if (normalTextChannel != null) {
        // find where the blank lines are
        // when called for a rule, is the quasi-comment part of the content of the previous rule or
        // the description of this one. Immaterial for grammar header

        List<Token> lineTokens = normalTextChannel.stream().collect(Collectors.toList());

        int precedingBlankLines = startCtx.getLine() - lineTokens.get(lineTokens.size() - 1).getLine() - 1;
        if (precedingBlankLines > 0) {
            if (forHeader) {
                // this will preserve the linefeeds
                return lineTokens.stream().map(tk -> tk.getText().replaceFirst("// ?", ""))
                        .collect(Collectors.joining("\n"));
            } // it wasn't a description (just a stray comment ?)
        } else {//from   ww w  . j  av  a 2 s .  co m
            if (forHeader) {
                // no blank line, so this is a description to the first 
                return "";
            }
            // description - go back and find any gap showing a last blank line
            int lastGoodLine = startCtx.getLine() - 1;
            int currentIndex = lineTokens.size() - 1;
            while (currentIndex >= 0 && lineTokens.get(currentIndex).getLine() == lastGoodLine) {
                currentIndex--;
                lastGoodLine--;
            }
            List<String> content = new ArrayList<>();
            for (int j = currentIndex + 1; j < lineTokens.size(); j++) {
                content.add(lineTokens.get(j).getText().replaceFirst("// ?", ""));
            }
            return content.stream().collect(Collectors.joining("\n"));
        }
    }
    return "";
}

From source file:org.opencypher.tools.g4processors.BNFListener.java

License:Apache License

private String findHiddenTextWithin(ParserRuleContext ctx) {
    List<Token> allTokens = tokens.get(ctx.getStart().getTokenIndex(), ctx.getStop().getTokenIndex());

    return allTokens.stream().filter(t -> t.getChannel() == BNFLexer.HIDDEN)
            .map(t -> t.getText().replaceFirst("// ?", "")).collect(Collectors.joining("\n"));
}

From source file:org.teavm.flavour.expr.Parser.java

License:Apache License

private <T extends Expr> T withLocation(T expr, ParserRuleContext startCtx, ParserRuleContext endCtx) {
    expr.setStart(startCtx.getStart().getStartIndex());
    expr.setEnd(endCtx.getStop().getStopIndex() + 1);
    return expr;/*from w w w .ja v  a 2  s .  c o m*/
}

From source file:org.tvl.goworks.editor.go.parser.CodeModelBuilderListener.java

License:Open Source License

@NonNull
private static String createAnonymousTypeName(@NonNull ParserRuleContext context) {
    return String.format("$%s_%d", GoParser.ruleNames[context.getRuleIndex()],
            context.getStart().getStartIndex());
}