Example #1
0
  /**
   * It is called when parser enters grammar rule (bit), it perform validations and updates the data
   * model tree.
   *
   * @param listener listener's object
   * @param ctx context object of the grammar rule
   */
  public static void processBitEntry(
      TreeWalkListener listener, GeneratedYangParser.BitStatementContext ctx) {

    // Check for stack to be non empty.
    checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), ENTRY);

    String identifier = getValidIdentifier(ctx.identifier().getText(), BIT_DATA, ctx);

    YangBit bitNode = new YangBit();
    bitNode.setBitName(identifier);
    listener.getParsedDataStack().push(bitNode);
  }
Example #2
0
  /**
   * It is called when parser exits from grammar rule (bit), it perform validations and update the
   * data model tree.
   *
   * @param listener Listener's object
   * @param ctx context object of the grammar rule
   */
  public static void processBitExit(
      TreeWalkListener listener, GeneratedYangParser.BitStatementContext ctx) {

    // Check for stack to be non empty.
    checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT);

    Parsable tmpBitNode = listener.getParsedDataStack().peek();
    if (tmpBitNode instanceof YangBit) {
      listener.getParsedDataStack().pop();

      // Check for stack to be non empty.
      checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT);

      Parsable tmpNode = listener.getParsedDataStack().peek();
      switch (tmpNode.getYangConstructType()) {
        case BITS_DATA:
          {
            YangBits yangBits = (YangBits) tmpNode;
            if (ctx.bitBodyStatement() == null
                || ctx.bitBodyStatement().positionStatement() == null) {
              int maxPosition = 0;
              boolean isPositionPresent = false;

              for (YangBit curBit : yangBits.getBitSet()) {
                if (maxPosition <= curBit.getPosition()) {
                  maxPosition = curBit.getPosition();
                  isPositionPresent = true;
                }
              }
              if (isPositionPresent) {
                maxPosition++;
              }
              ((YangBit) tmpBitNode).setPosition(maxPosition);
            }
            try {
              yangBits.addBitInfo((YangBit) tmpBitNode);
            } catch (DataModelException e) {
              ParserException parserException =
                  new ParserException(
                      constructExtendedListenerErrorMessage(
                          INVALID_CONTENT,
                          BIT_DATA,
                          ctx.identifier().getText(),
                          EXIT,
                          e.getMessage()));
              parserException.setLine(ctx.getStart().getLine());
              parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
              throw parserException;
            }
            break;
          }
        default:
          throw new ParserException(
              constructListenerErrorMessage(
                  INVALID_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT));
      }
    } else {
      throw new ParserException(
          constructListenerErrorMessage(
              MISSING_CURRENT_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT));
    }
  }