Example #1
0
  @NotNull
  public ASTNode parse(IElementType root, PsiBuilder builder) {
    if (ALLOW_ONLY_ONE_THREAD) {
      try {
        SEMAPHORE.acquire();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    // builder.setDebugMode(true);
    final PsiBuilder.Marker rootMarker = builder.mark();
    if (!builder.eof()) { // Empty file is not an error
      final GLSLParsing theRealParser = new GLSLParsing(builder);

      theRealParser.parseTranslationUnit();
      while (!builder.eof()) // exhaust the file if unable to parse everything
      builder.advanceLexer();
    }

    rootMarker.done(root);

    if (ALLOW_ONLY_ONE_THREAD) {
      SEMAPHORE.release();
    }
    return builder.getTreeBuilt();
  }
  /**
   * Named list operators
   *
   * @param b PerlBuilder
   * @param l Parsing level
   * @return parsing result
   */
  public static boolean isListOperator(PsiBuilder b, int l) {
    PerlTokenData prevTokenData = ((PerlBuilder) b).lookupToken(-1);

    if (prevTokenData != null && prevTokenData.getTokenType() == OPERATOR_DEREFERENCE) return false;

    IElementType tokenType = b.getTokenType();
    IElementType nextTokenType = b.lookAhead(1);

    if (CONVERTABLE_TOKENS.contains(tokenType)
        && nextTokenType != LEFT_PAREN // not function call
        && !PACKAGE_TOKENS.contains(nextTokenType) // not method Package::
        && !(nextTokenType == IDENTIFIER
            && ((PerlBuilder) b)
                .isKnownPackage(
                    ((PerlBuilder) b).lookupToken(1).getTokenText())) // not Method Package
    )
      // todo we should check current namespace here
      return !PerlSubUtil.BUILT_IN_UNARY.contains(b.getTokenText());
    else if (PACKAGE_TOKENS.contains(tokenType)
        && CONVERTABLE_TOKENS.contains(nextTokenType)
        && b.lookAhead(2) != LEFT_PAREN)
      return !PerlSubUtil.isUnary(
          b.getTokenText(), ((PerlBuilder) b).lookupToken(1).getTokenText());

    return false;
  }
Example #3
0
  /*
   * Parses Groovy-style 'in' clause
   */
  private static boolean forInClauseParse(PsiBuilder builder, GroovyParser parser) {

    PsiBuilder.Marker marker = builder.mark();

    PsiBuilder.Marker declMarker = builder.mark();

    if (ParserUtils.lookAhead(builder, mIDENT, kIN)) {
      ParserUtils.eatElement(builder, PARAMETER);
      declMarker.drop();
      ParserUtils.getToken(builder, kIN);
      if (!ShiftExpression.parse(builder, parser)) {
        builder.error(GroovyBundle.message("expression.expected"));
      }
      marker.done(FOR_IN_CLAUSE);
      return true;
    }

    if (DeclarationStart.parse(builder, parser)) {
      if (Modifiers.parse(builder, parser)) {
        TypeSpec.parse(builder);
        return singleDeclNoInitParse(builder, marker, declMarker, parser);
      }
    }

    if (TypeSpec.parse(builder)) {
      return singleDeclNoInitParse(builder, marker, declMarker, parser);
    }

    declMarker.drop();
    marker.drop();
    return false;
  }
Example #4
0
  /*
   * Parses list of control expression in for condition
   */
  private static void controlExpressionListParse(PsiBuilder builder, GroovyParser parser) {

    if (!StrictContextExpression.parse(builder, parser)) return;

    while (mCOMMA.equals(builder.getTokenType())) {

      if (ParserUtils.lookAhead(builder, mCOMMA, mNLS, mRPAREN)
          || ParserUtils.lookAhead(builder, mCOMMA, mRPAREN)) {
        ParserUtils.getToken(builder, mCOMMA);
        builder.error(GroovyBundle.message("expression.expected"));
      } else {
        ParserUtils.getToken(builder, mCOMMA);
      }
      ParserUtils.getToken(builder, mNLS);
      if (!StrictContextExpression.parse(builder, parser)) {
        ParserUtils.getToken(builder, mNLS);
        if (!mRPAREN.equals(builder.getTokenType()) && !mSEMI.equals(builder.getTokenType())) {
          builder.error(GroovyBundle.message("expression.expected"));
        }
        if (!mRPAREN.equals(builder.getTokenType())
            && !mSEMI.equals(builder.getTokenType())
            && !mCOMMA.equals(builder.getTokenType())
            && !mNLS.equals(builder.getTokenType())) {
          builder.advanceLexer();
        }
      }
    }
  }
Example #5
0
  private static boolean tradForClauseParse(PsiBuilder builder, GroovyParser parser) {

    PsiBuilder.Marker marker = builder.mark();

    if (ParserUtils.getToken(builder, mSEMI)
        || (Declaration.parse(builder, false, parser) && ParserUtils.getToken(builder, mSEMI))) {
      StrictContextExpression.parse(builder, parser);
      ParserUtils.getToken(builder, mSEMI, GroovyBundle.message("semi.expected"));
      ParserUtils.getToken(builder, mNLS);
      if (!mRPAREN.equals(builder.getTokenType())) {
        controlExpressionListParse(builder, parser);
      }
    } else {
      marker.rollbackTo();
      marker = builder.mark();
      controlExpressionListParse(builder, parser);
      ParserUtils.getToken(builder, mSEMI, GroovyBundle.message("semi.expected"));
      StrictContextExpression.parse(builder, parser);
      ParserUtils.getToken(builder, mSEMI, GroovyBundle.message("semi.expected"));
      ParserUtils.getToken(builder, mNLS);
      if (!mRPAREN.equals(builder.getTokenType())) {
        controlExpressionListParse(builder, parser);
      }
    }

    marker.done(FOR_TRADITIONAL_CLAUSE);
    return true;
  }
  public static void parse(PsiBuilder builder, GroovyParser parser) {

    PsiBuilder.Marker annArgs = builder.mark();
    if (!ParserUtils.getToken(builder, mLPAREN)) {
      annArgs.done(ANNOTATION_ARGUMENTS);
      return;
    }

    if (ParserUtils.lookAhead(builder, mIDENT, mASSIGN)) {
      if (!parseAnnotationMemberValuePairs(builder, parser)) {
        annArgs.rollbackTo();
        return;
      }
    } else {
      PsiBuilder.Marker pairMarker = builder.mark();
      if (!parseAnnotationMemberValueInitializer(builder, parser)) {
        pairMarker.drop();
      } else {
        pairMarker.done(ANNOTATION_MEMBER_VALUE_PAIR);
      }
    }

    ParserUtils.getToken(builder, mNLS);

    if (!ParserUtils.getToken(builder, mRPAREN)) {
      builder.error(GroovyBundle.message("rparen.expected"));
    }
    annArgs.done(ANNOTATION_ARGUMENTS);
  }
Example #7
0
  public static boolean parse(
      PsiBuilder builder, boolean isInClass, boolean isInAnnotation, GroovyParser parser) {
    PsiBuilder.Marker declMarker = builder.mark();
    // allows error messages
    boolean modifiersParsed = Modifiers.parse(builder, parser);

    final boolean methodStart = mLT == builder.getTokenType();
    final IElementType type =
        parseAfterModifiers(
            builder, isInClass, isInAnnotation, parser, declMarker, modifiersParsed);
    if (type == WRONGWAY) {
      if (modifiersParsed && methodStart) {
        declMarker.error(GroovyBundle.message("method.definitions.expected"));
        return false;
      }

      declMarker.rollbackTo();
      if (modifiersParsed) {
        builder.error(GroovyBundle.message("variable.definitions.expected"));
      }

      return false;
    }

    if (type != null) {
      declMarker.done(type);
    } else {
      declMarker.drop();
    }
    return true;
  }
  private void beforeConsume(PsiBuilder builder, IElementType type) {
    if (type.equals(SS_VAR)) {
      varMarker = builder.mark();
      markingVar = true;
    }
    if (type.equals(SS_COMMENT_START)) {
      commentMarker = builder.mark();
      markingComment = true;
    }
    if (type.equals(SS_THEME_VAR)) {
      themeMarker = builder.mark();
      markingTheme = true;
    }

    if (type.equals(SS_BLOCK_START) && !markingBlock) {
      nextToken = getNextToken(builder);
      blockStartTokens = STATEMENT_MAP.get(nextToken);
      blockType = BLOCK_TYPE_MAP.get(nextToken);
      if (blockStartTokens != null) {
        blockTokenText = getNextTokenText(builder);
        blockMarker = builder.mark();
        markingBlock = true;
      }
    }

    if (markingTheme && !type.equals(SS_THEME_VAR) && !type.equals(SS_STRING)) {
      markingTheme = false;
    }
  }
  @Nullable
  public PsiBuilder.Marker parseConditional(final PsiBuilder builder) {
    final PsiBuilder.Marker condition = parseExpression(builder, ExprType.CONDITIONAL_OR);
    if (condition == null) return null;

    if (builder.getTokenType() != JavaTokenType.QUEST) return condition;
    final PsiBuilder.Marker ternary = condition.precede();
    builder.advanceLexer();

    final PsiBuilder.Marker truePart = parse(builder);
    if (truePart == null) {
      error(builder, JavaErrorMessages.message("expected.expression"));
      ternary.done(JavaElementType.CONDITIONAL_EXPRESSION);
      return ternary;
    }

    if (builder.getTokenType() != JavaTokenType.COLON) {
      error(builder, JavaErrorMessages.message("expected.colon"));
      ternary.done(JavaElementType.CONDITIONAL_EXPRESSION);
      return ternary;
    }
    builder.advanceLexer();

    final PsiBuilder.Marker falsePart = parseConditional(builder);
    if (falsePart == null) {
      error(builder, JavaErrorMessages.message("expected.expression"));
      ternary.done(JavaElementType.CONDITIONAL_EXPRESSION);
      return ternary;
    }

    ternary.done(JavaElementType.CONDITIONAL_EXPRESSION);
    return ternary;
  }
 private void getNextTokenValue(PsiBuilder builder) {
   PsiBuilder.Marker rb = builder.mark();
   builder.advanceLexer();
   nextToken = builder.getTokenType();
   nextTokenText = builder.getTokenText();
   rb.rollbackTo();
 }
  @Nullable
  private PsiBuilder.Marker parseLambdaExpression(
      final PsiBuilder builder, final boolean typed, @Nullable final PsiBuilder.Marker typeList) {
    final PsiBuilder.Marker start = typeList != null ? typeList.precede() : builder.mark();

    myParser.getDeclarationParser().parseLambdaParameterList(builder, typed);

    if (!expect(builder, JavaTokenType.ARROW)) {
      start.rollbackTo();
      return null;
    }

    final PsiBuilder.Marker body;
    if (builder.getTokenType() == JavaTokenType.LBRACE) {
      body = myParser.getStatementParser().parseCodeBlock(builder);
    } else {
      body = parse(builder);
    }

    if (body == null) {
      builder.error(JavaErrorMessages.message("expected.lbrace"));
    }

    start.done(JavaElementType.LAMBDA_EXPRESSION);
    return start;
  }
  @Nullable
  private static IElementType getGtTokenType(final PsiBuilder builder) {
    final PsiBuilder.Marker sp = builder.mark();

    IElementType tokenType = builder.getTokenType();
    if (tokenType == JavaTokenType.GT) {
      builder.advanceLexer();
      if (builder.getTokenType() == JavaTokenType.GT) {
        builder.advanceLexer();
        if (builder.getTokenType() == JavaTokenType.GT) {
          builder.advanceLexer();
          if (builder.getTokenType() == JavaTokenType.EQ) {
            tokenType = JavaTokenType.GTGTGTEQ;
          } else {
            tokenType = JavaTokenType.GTGTGT;
          }
        } else if (builder.getTokenType() == JavaTokenType.EQ) {
          tokenType = JavaTokenType.GTGTEQ;
        } else {
          tokenType = JavaTokenType.GTGT;
        }
      } else if (builder.getTokenType() == JavaTokenType.EQ) {
        tokenType = JavaTokenType.GE;
      }
    }

    sp.rollbackTo();
    return tokenType;
  }
  /**
   * Statement recovery function. Should not consume token, only check;
   *
   * @param b PerlBuilder
   * @param l parsing level
   * @return parsing result
   */
  public static boolean recoverStatement(PsiBuilder b, int l) {
    assert b instanceof PerlBuilder;

    if (!((PerlBuilder) b).isRecoveringStatement()) ((PerlBuilder) b).startRecovery();

    IElementType currentTokenType = b.getTokenType();

    //		System.err.println("Checking " + b.getTokenText() + currentTokenType);

    if (currentTokenType == null // got end of file
        || ((PerlBuilder) b).getBracesLevel() == 0
            && ( // we are not in braced statement
            UNCONDITIONAL_STATEMENT_RECOVERY_TOKENS.contains(
                    currentTokenType) // got semi, package, end of regex, use, compound or suffix
                || currentTokenType == RESERVED_SUB
                    && STATEMENT_RECOVERY_SUB_SUFFIX.contains(b.lookAhead(1)) // got sub definition
            )) {
      ((PerlBuilder) b).stopRecovery();
      return false;
    }

    if (currentTokenType == LEFT_BRACE) ((PerlBuilder) b).openBrace();
    else if (currentTokenType == RIGHT_BRACE) ((PerlBuilder) b).closeBrace();

    return true;
  }
 private void passEOLsAndIndents(final PsiBuilder builder) {
   IElementType tokenType = builder.getTokenType();
   while (tokenType == myEolTokenType || tokenType == myIndentTokenType) {
     builder.advanceLexer();
     tokenType = builder.getTokenType();
   }
 }
 private void parseListLiteralExpression(final PsiBuilder builder, boolean isTargetExpression) {
   LOG.assertTrue(builder.getTokenType() == PyTokenTypes.LBRACKET);
   final PsiBuilder.Marker expr = builder.mark();
   builder.advanceLexer();
   if (builder.getTokenType() == PyTokenTypes.RBRACKET) {
     builder.advanceLexer();
     expr.done(PyElementTypes.LIST_LITERAL_EXPRESSION);
     return;
   }
   if (!parseSingleExpression(isTargetExpression)) {
     builder.error(message("PARSE.expected.expression"));
   }
   if (builder.getTokenType() == PyTokenTypes.FOR_KEYWORD) {
     parseComprehension(expr, PyTokenTypes.RBRACKET, PyElementTypes.LIST_COMP_EXPRESSION);
   } else {
     while (builder.getTokenType() != PyTokenTypes.RBRACKET) {
       if (!matchToken(PyTokenTypes.COMMA)) {
         builder.error("expected ',' or ']'");
       }
       if (atToken(PyTokenTypes.RBRACKET)) {
         break;
       }
       if (!parseSingleExpression(isTargetExpression)) {
         builder.error(message("PARSE.expected.expr.or.comma.or.bracket"));
         break;
       }
     }
     checkMatches(PyTokenTypes.RBRACKET, message("PARSE.expected.rbracket"));
     expr.done(PyElementTypes.LIST_LITERAL_EXPRESSION);
   }
 }
Example #16
0
 public static boolean parse(final PsiBuilder builder) {
   boolean result = false;
   while (!builder.eof() && SEPARATOR.contains(builder.getTokenType())) {
     builder.advanceLexer();
     result = true;
   }
   return result;
 }
 public static boolean empty_element_parsed_guard_(PsiBuilder builder, String funcName, int pos) {
   if (pos == current_position_(builder)) {
     builder.error(
         "Empty element parsed in '" + funcName + "' at offset " + builder.getCurrentOffset());
     return false;
   }
   return true;
 }
 private void parseReprExpression(PsiBuilder builder) {
   LOG.assertTrue(builder.getTokenType() == PyTokenTypes.TICK);
   final PsiBuilder.Marker expr = builder.mark();
   builder.advanceLexer();
   parseExpression();
   checkMatches(PyTokenTypes.TICK, message("PARSE.expected.tick"));
   expr.done(PyElementTypes.REPR_EXPRESSION);
 }
  private static void addVariant(PsiBuilder builder, ErrorState state, Object o) {
    builder.eof(); // skip whitespaces
    addVariantInner(state, builder.rawTokenIndex(), o);

    CompletionState completionState = state.completionState;
    if (completionState != null && state.predicateSign) {
      addCompletionVariant(builder, completionState, o);
    }
  }
 /**
  * Parsing label declaration LABEL:
  *
  * @param b PerlBuilder
  * @param l parsing level
  * @return parsing result
  */
 public static boolean parseLabelDeclaration(PsiBuilder b, int l) {
   if (CONVERTABLE_TOKENS.contains(b.getTokenType()) && b.lookAhead(1) == COLON) {
     PsiBuilder.Marker m = b.mark();
     b.advanceLexer();
     m.collapse(LABEL);
     b.advanceLexer();
     return true;
   }
   return false;
 }
 protected static boolean recoverTo(PsiBuilder b, IElementType toElement, String errorMessage) {
   // recover bad code
   PsiBuilder.Marker errorMarker = b.mark();
   while (!b.eof() && b.getTokenType() != toElement) {
     b.advanceLexer();
     ;
   }
   errorMarker.error(errorMessage);
   return b.eof();
 }
Example #22
0
 protected boolean checkMatches(
     final PsiBuilder builder, final IElementType token, final String message) {
   if (builder.getTokenType() == token) {
     builder.advanceLexer();
     return true;
   } else {
     builder.error(message);
     return false;
   }
 }
 /**
  * Checks for version token and convert if necessary
  *
  * @param b PerlBuilder
  * @param l parsing level
  * @return parsing result
  */
 public static boolean parsePerlVersion(PsiBuilder b, int l) {
   if (b.getTokenType() == NUMBER_VERSION) return consumeToken(b, NUMBER_VERSION);
   else if (VERSION_TOKENS.contains(b.getTokenType())) {
     PsiBuilder.Marker m = b.mark();
     b.advanceLexer();
     m.collapse(NUMBER_VERSION);
     return true;
   }
   return false;
 }
 /**
  * Checks and parses pragma to package token
  *
  * @param b PerlBuilder
  * @param l parsing level
  * @param sourceTokenType source token type
  * @param targetTokenType token type to collapse to
  * @return result
  */
 public static boolean checkAndCollapseToken(
     PsiBuilder b, int l, IElementType sourceTokenType, IElementType targetTokenType) {
   if (b.getTokenType() == sourceTokenType) {
     PsiBuilder.Marker m = b.mark();
     b.advanceLexer();
     m.collapse(targetTokenType);
     return true;
   }
   return false;
 }
  @Nullable
  private PsiBuilder.Marker parseArrayInitializer(final PsiBuilder builder) {
    if (builder.getTokenType() != JavaTokenType.LBRACE) return null;

    final PsiBuilder.Marker arrayInit = builder.mark();
    builder.advanceLexer();

    boolean expressionMissed = false;
    PsiBuilder.Marker lastComma = null;
    while (true) {
      if (builder.getTokenType() == JavaTokenType.RBRACE) {
        builder.advanceLexer();
        break;
      }

      if (builder.getTokenType() == null) {
        error(builder, JavaErrorMessages.message("expected.rbrace"));
        break;
      }

      if (expressionMissed && lastComma != null) {
        // before comma must be an expression
        lastComma
            .precede()
            .errorBefore(JavaErrorMessages.message("expected.expression"), lastComma);
        lastComma.drop();
        lastComma = null;
      }

      final PsiBuilder.Marker arg = parse(builder);
      if (arg == null) {
        if (builder.getTokenType() == JavaTokenType.COMMA) {
          expressionMissed = true;
          lastComma = builder.mark();
        } else {
          error(builder, JavaErrorMessages.message("expected.rbrace"));
          break;
        }
      }

      final IElementType tokenType = builder.getTokenType();
      if (tokenType == JavaTokenType.COMMA) {
        builder.advanceLexer();
      } else if (tokenType != JavaTokenType.RBRACE) {
        error(builder, JavaErrorMessages.message("expected.comma"));
      }
    }

    if (lastComma != null) {
      lastComma.drop();
    }

    arrayInit.done(JavaElementType.ARRAY_INITIALIZER_EXPRESSION);
    return arrayInit;
  }
  public static boolean convertBracedString(PsiBuilder b, int l) {

    if (CONVERTABLE_TOKENS.contains(b.getTokenType()) && b.lookAhead(1) == RIGHT_BRACE) {
      // fixme shouldn't we add string_sq here?
      PsiBuilder.Marker m = b.mark();
      b.advanceLexer();
      m.collapse(STRING_CONTENT);
      return true;
    }
    return false;
  }
  /**
   * Joining several regex tokens into one to lighten PSI tree. Temporary solution, until regex
   * parsing is implemented
   *
   * @param b PerlBuilder
   * @param l parsing level
   * @return parsing result
   */
  public static boolean joinRegexTokens(PsiBuilder b, int l) {
    if (b.getTokenType() == REGEX_TOKEN) {
      PsiBuilder.Marker m = b.mark();

      while (b.getTokenType() == REGEX_TOKEN) b.advanceLexer();

      m.collapse(REGEX_TOKEN);
      return true;
    }
    return false;
  }
  // @todo this is really raw
  public static boolean parseVariableAttributes(PsiBuilder b, int l) {

    PsiBuilder.Marker m = null;
    while (!b.eof() && !VARIABLE_ATTRIBUTE_STOP_TOKENS.contains(b.getTokenType())) {
      if (m == null) m = b.mark();
      b.advanceLexer();
    }
    if (m != null) m.collapse(VAR_ATTRIBUTE);

    return true;
  }
 /**
  * @param root The file currently being parsed
  * @param builder used to build the parse tree
  * @return parsed tree
  */
 @NotNull
 @Override
 public ASTNode parse(IElementType root, PsiBuilder builder) {
   PsiBuilder.Marker rootMarker = builder.mark();
   PsiBuilder.Marker wrapperMarker = builder.mark();
   builder.setDebugMode(true);
   // Process all tokens
   parseTree(builder);
   wrapperMarker.done(SS_STATEMENTS);
   rootMarker.done(root);
   return builder.getTreeBuilt();
 }
 public boolean parseStringLiteralExpression() {
   final PsiBuilder builder = myContext.getBuilder();
   if (PyTokenTypes.STRING_NODES.contains(builder.getTokenType())) {
     final PsiBuilder.Marker marker = builder.mark();
     while (PyTokenTypes.STRING_NODES.contains(builder.getTokenType())) {
       nextToken();
     }
     marker.done(PyElementTypes.STRING_LITERAL_EXPRESSION);
     return true;
   }
   return false;
 }