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.yang.compiler.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   w  w  w .j av a 2  s.  co 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.yang.compiler.parser.impl.parserutils.ListenerUtil.java

License:Apache License

/**
 * Returns the last node under the unique path.
 *
 * @param uniquePath atomic path list/*from  w ww .j  a va  2s  .c o  m*/
 * @param node       root node from where it starts searching
 * @param ctx        yang construct's context to get the line number and character position
 * @return last node in the list
 */
private static YangNode getNodeUnderListFromPath(List<YangAtomicPath> uniquePath, YangNode node,
        ParserRuleContext ctx) {
    Iterator<YangAtomicPath> nodesInReference = uniquePath.listIterator();
    YangNode potentialReferredNode = node.getChild();
    while (nodesInReference.hasNext()) {
        YangAtomicPath nodeInUnique = nodesInReference.next();
        YangNode referredNode = getReferredNodeFromTheUniqueNodes(nodeInUnique.getNodeIdentifier(),
                potentialReferredNode);
        if (referredNode == null) {
            ParserException parserException = new ParserException(
                    "YANG file error : The target node in unique " + "reference path is invalid");
            parserException.setLine(ctx.getStart().getLine());
            parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
            throw parserException;
        } else {
            potentialReferredNode = referredNode.getChild();
        }
    }
    return potentialReferredNode;
}

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

License:Apache License

/**
 * Checks and return valid absolute schema node id.
 *
 * @param argumentString    string from yang file
 * @param yangConstructType yang construct for creating error message
 * @param ctx               yang construct's context to get the line number and character position
 * @return target nodes list of absolute schema node id
 *///from w ww. j av a  2  s  . c  o  m
public static List<YangAtomicPath> getValidAbsoluteSchemaNodeId(String argumentString,
        YangConstructType yangConstructType, ParserRuleContext ctx) {

    List<YangAtomicPath> targetNodes = new ArrayList<>();
    YangNodeIdentifier yangNodeIdentifier;
    String tmpSchemaNodeId = removeQuotesAndHandleConcat(argumentString);

    // absolute-schema-nodeid = 1*("/" node-identifier)
    if (!tmpSchemaNodeId.startsWith(SLASH)) {
        ParserException parserException = new ParserException("YANG file error : "
                + getYangConstructType(yangConstructType) + " name " + argumentString + "is not valid");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }
    String[] tmpData = tmpSchemaNodeId.replaceFirst(CARET + SLASH, EMPTY_STRING).split(SLASH);
    for (String nodeIdentifiers : tmpData) {
        yangNodeIdentifier = getValidNodeIdentifier(nodeIdentifiers, yangConstructType, ctx);
        YangAtomicPath yangAbsPath = new YangAtomicPath();
        yangAbsPath.setNodeIdentifier(yangNodeIdentifier);
        targetNodes.add(yangAbsPath);
    }
    return targetNodes;
}

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

License:Apache License

/**
 * Throws parser exception for unsupported YANG constructs.
 *
 * @param type       construct type/*from   w  ww  .  j  av a2  s.  c  o  m*/
 * @param ctx        construct context
 * @param errorInfo  error msg
 * @param fileName   YANG file name
 * @param identifier identifier of the node
 */
public static void handleUnsupportedYangConstruct(YangConstructType type, ParserRuleContext ctx,
        String errorInfo, String fileName, String identifier) {
    StringBuilder b = new StringBuilder();
    int lineNumber = ctx.getStart().getLine();
    int charPostion = ctx.getStart().getCharPositionInLine();
    b.append(YANG_FILE_ERROR).append(QUOTES).append(getYangConstructType(type)).append(SPACE).append(identifier)
            .append(QUOTES).append(AT).append(LINE_NUMBER).append(lineNumber).append(AT)
            .append(CHARACTER_POSITION).append(charPostion).append(IN).append(FILE).append(fileName)
            .append(errorInfo);
    log.info(b.toString());
}

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

License:Apache License

/**
 * Returns date and makes it in usable format for revision.
 *
 * @param dateInString date argument string from yang file
 * @param ctx          yang construct's context to get the line number and character position
 * @return date format for revision/*  w w  w.j  av a  2 s.  c  om*/
 */
public static Date getValidDateFromString(String dateInString, ParserRuleContext ctx) {
    String dateArgument = removeQuotesAndHandleConcat(dateInString);
    if (!dateArgument.matches(DATE_PATTERN)) {
        ParserException parserException = new ParserException("YANG file error: Input date is not correct");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }

    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    sdf.setLenient(false);

    try {
        //if not valid, it will throw ParseException
        return sdf.parse(dateArgument);
    } catch (ParseException e) {
        ParserException parserException = new ParserException("YANG file error: Input date is not correct");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }
}

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

License:Apache License

/**
 * Checks and return valid prefix.//  w ww . j  av  a2  s .  c om
 *
 * @param inputString   string from yang file
 * @param yangConstruct yang construct for creating error message
 * @param ctx           yang construct's context to get the line number and character position
 * @return valid prefix
 */
public static String getValidPrefix(String inputString, YangConstructType yangConstruct,
        ParserRuleContext ctx) {
    String tmpPrefixString = removeQuotesAndHandleConcat(inputString);
    String[] tmpData = tmpPrefixString.split(Pattern.quote(COLON));
    if (tmpData.length == 2) {
        return tmpData[0];
    } else {
        ParserException parserException = new ParserException("YANG file error : "
                + getYangConstructType(yangConstruct) + " name " + inputString + " is not valid.");
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }
}

From source file:org.onosproject.yang.compiler.parser.impl.parserutils.ListenerValidation.java

License:Apache License

/**
 * Checks if a rule occurrences is exactly 1.
 *
 * @param childContext child's context//from w w  w.  ja va 2 s. c  om
 * @param yangChildConstruct child construct for whom cardinality is to be
 *                           validated
 * @param yangParentConstruct parent construct
 * @param parentName parent name
 * @param parentContext parents's context
 * @throws ParserException exception if cardinality check fails
 */
public static void validateCardinalityEqualsOne(List<?> childContext, YangConstructType yangChildConstruct,
        YangConstructType yangParentConstruct, String parentName, ParserRuleContext parentContext)
        throws ParserException {

    if (childContext.isEmpty()) {
        ParserException parserException = new ParserException(
                "YANG file error: Missing \"" + getYangConstructType(yangChildConstruct) + "\" in \""
                        + getYangConstructType(yangParentConstruct) + " " + parentName + "\".");
        parserException.setLine(parentContext.getStart().getLine());
        parserException.setCharPosition(parentContext.getStart().getCharPositionInLine());
        throw parserException;
    } else if (!childContext.isEmpty() && childContext.size() != 1) {
        Iterator<?> childcontext = childContext.iterator();
        ParserException parserException = new ParserException("YANG file error: \""
                + getYangConstructType(yangChildConstruct) + "\" is present more than once in \""
                + getYangConstructType(yangParentConstruct) + " " + parentName + "\".");
        parserException.setLine(((ParserRuleContext) childcontext.next()).getStart().getLine());
        parserException
                .setCharPosition(((ParserRuleContext) childcontext.next()).getStart().getCharPositionInLine());
        throw parserException;
    }
}

From source file:org.onosproject.yang.compiler.parser.impl.parserutils.ListenerValidation.java

License:Apache License

/**
 * Checks if a rule occurrences is minimum 1.
 *
 * @param childContext child's context/*from  www.  ja  v a 2 s . c o  m*/
 * @param yangChildConstruct child construct for whom cardinality is to be
 *                           validated
 * @param yangParentConstruct parent construct
 * @param parentName parent name
 * @param parentContext parents's context
 * @throws ParserException exception if cardinality check fails
 */
public static void validateCardinalityNonZero(List<?> childContext, YangConstructType yangChildConstruct,
        YangConstructType yangParentConstruct, String parentName, ParserRuleContext parentContext)
        throws ParserException {

    if (childContext.isEmpty()) {
        ParserException parserException = new ParserException(
                "YANG file error: Missing \"" + getYangConstructType(yangChildConstruct) + "\" in \""
                        + getYangConstructType(yangParentConstruct) + " " + parentName + "\".");

        parserException.setLine(parentContext.getStart().getLine());
        parserException.setCharPosition(parentContext.getStart().getCharPositionInLine());
        throw parserException;
    }
}

From source file:org.onosproject.yang.compiler.parser.impl.parserutils.ListenerValidation.java

License:Apache License

/**
 * Checks if a either of one construct occurrence.
 *
 * @param child1Context       first optional child's context
 * @param yangChild1Construct first child construct for whom cardinality is
 *                            to be validated
 * @param child2Context       second optional child's context
 * @param yangChild2Construct second child construct for whom cardinality is
 *                            to be validated
 * @param yangParentConstruct parent construct
 * @param parentName          parent name
 * @param parentContext       parents's context
 * @throws ParserException exception if cardinality check fails
 *//*from  w  w  w  .  jav a2s.c o  m*/
public static void validateCardinalityEitherOne(List<?> child1Context, YangConstructType yangChild1Construct,
        List<?> child2Context, YangConstructType yangChild2Construct, YangConstructType yangParentConstruct,
        String parentName, ParserRuleContext parentContext) throws ParserException {

    if (child1Context.isEmpty() && child2Context.isEmpty()) {
        ParserException parserException = new ParserException(
                "YANG file error: Either \"" + getYangConstructType(yangChild1Construct) + "\" or \""
                        + getYangConstructType(yangChild2Construct) + "\" should be present in \""
                        + getYangConstructType(yangParentConstruct) + " " + parentName + "\".");
        parserException.setLine(parentContext.getStart().getLine());
        parserException.setCharPosition(parentContext.getStart().getCharPositionInLine());
        throw parserException;
    }
}

From source file:org.onosproject.yang.compiler.parser.impl.parserutils.ListenerValidation.java

License:Apache License

/**
 * Checks if a either of one construct occurrence.
 *
 * @param ctx1       first optional child's context
 * @param type1      first child construct for whom cardinality is
 *                   to be validated/*from w w  w.  j a v a  2  s  . com*/
 * @param ctx2       second optional child's context
 * @param type2      second child construct for whom cardinality is
 *                   to be validated
 * @param type       parent construct
 * @param parentName parent name
 * @param ctx        parents's context
 * @throws ParserException exception if cardinality check fails
 */
public static void validateCardinalityMutuallyExclusive(List<?> ctx1, YangConstructType type1, List<?> ctx2,
        YangConstructType type2, YangConstructType type, String parentName, ParserRuleContext ctx)
        throws ParserException {

    if (!ctx1.isEmpty() && !ctx2.isEmpty()) {
        String error = "YANG file error: Either " + getYangConstructType(type1) + " or "
                + getYangConstructType(type2) + " should be present in " + getYangConstructType(type) + " "
                + parentName + ".";
        ParserException parserException = new ParserException(error);
        parserException.setLine(ctx.getStart().getLine());
        parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
        throw parserException;
    }
}