Exemplo n.º 1
0
 /**
  * Tries to calculate given line's indent column assuming that there might be a comment at the
  * given indent offset (see {@link #getCommentPrefix(IElementType)}).
  *
  * @param line target line
  * @param indentOffset start indent offset to use for the given line
  * @param lineEndOffset given line's end offset
  * @param fallbackColumn column to return if it's not possible to apply comment-specific indent
  *     calculation rules
  * @return given line's indent column to use
  */
 private int calcIndent(int line, int indentOffset, int lineEndOffset, int fallbackColumn) {
   final HighlighterIterator it = myEditor.getHighlighter().createIterator(indentOffset);
   IElementType tokenType = it.getTokenType();
   Language language = tokenType.getLanguage();
   TokenSet comments = myComments.get(language);
   if (comments == null) {
     ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
     if (definition != null) {
       comments = definition.getCommentTokens();
     }
     if (comments == null) {
       return fallbackColumn;
     } else {
       myComments.put(language, comments);
     }
   }
   if (comments.contains(tokenType) && indentOffset == it.getStart()) {
     String prefix = COMMENT_PREFIXES.get(tokenType);
     if (prefix == null) {
       prefix = getCommentPrefix(tokenType);
     }
     if (!NO_COMMENT_INFO_MARKER.equals(prefix)) {
       final int indentInsideCommentOffset =
           CharArrayUtil.shiftForward(
               myChars, indentOffset + prefix.length(), lineEndOffset, " \t");
       if (indentInsideCommentOffset < lineEndOffset) {
         int indent = myEditor.calcColumnNumber(indentInsideCommentOffset, line);
         indentAfterUncomment.put(line, indent - prefix.length());
         return indent;
       }
     }
   }
   return fallbackColumn;
 }
Exemplo n.º 2
0
  /*
   * SimpleName{,}
   */
  private void parseFunctionLiteralShorthandParameterList() {
    PsiBuilder.Marker parameterList = mark();

    while (!eof()) {
      PsiBuilder.Marker parameter = mark();

      //            int parameterNamePos = matchTokenStreamPredicate(new LastBefore(new
      // At(IDENTIFIER), new AtOffset(doubleArrowPos)));
      //            createTruncatedBuilder(parameterNamePos).parseModifierList(MODIFIER_LIST,
      // false);

      if (at(COLON)) {
        error("Expecting parameter name");
      } else {
        expect(IDENTIFIER, "Expecting parameter name", TokenSet.create(ARROW));
      }

      if (at(COLON)) {
        advance(); // COLON
        myJetParsing.parseTypeRef(TokenSet.create(ARROW, COMMA));
      }
      parameter.done(VALUE_PARAMETER);

      if (at(ARROW)) {
        break;
      } else if (at(COMMA)) {
        advance(); // COMMA
      } else {
        error("Expecting '->' or ','");
        break;
      }
    }

    parameterList.done(VALUE_PARAMETER_LIST);
  }
Exemplo n.º 3
0
  /*
   * typeConstraint
   *   : attributes SimpleName ":" type
   *   : attributes "class" "object" SimpleName ":" type
   *   ;
   */
  private void parseTypeConstraint() {
    PsiBuilder.Marker constraint = mark();

    parseAnnotations(false);

    if (at(CLASS_KEYWORD)) {
      advance(); // CLASS_KEYWORD

      expect(OBJECT_KEYWORD, "Expecting 'object'", TYPE_REF_FIRST);
    }

    PsiBuilder.Marker reference = mark();
    if (expect(
        IDENTIFIER,
        "Expecting type parameter name",
        TokenSet.orSet(TokenSet.create(COLON, COMMA), TYPE_REF_FIRST))) {
      reference.done(REFERENCE_EXPRESSION);
    } else {
      reference.drop();
    }

    expect(COLON, "Expecting ':' before the upper bound", TYPE_REF_FIRST);

    parseTypeRef();

    constraint.done(TYPE_CONSTRAINT);
  }
Exemplo n.º 4
0
  /*
   * object
   *   : "object" SimpleName? ":" delegationSpecifier{","}? classBody?
   *   ;
   */
  void parseObject(boolean named, boolean optionalBody) {
    assert _at(OBJECT_KEYWORD);

    advance(); // OBJECT_KEYWORD

    if (named) {
      PsiBuilder.Marker propertyDeclaration = mark();
      if (!parseIdeTemplate()) {
        expect(IDENTIFIER, "Expecting object name", TokenSet.create(LBRACE));
      }
      propertyDeclaration.done(OBJECT_DECLARATION_NAME);
    } else {
      if (at(IDENTIFIER)) {
        error("An object expression cannot bind a name");
      }
    }

    if (optionalBody) {
      if (at(COLON)) {
        advance(); // COLON
        parseDelegationSpecifierList();
      }
      if (at(LBRACE)) {
        parseClassBody();
      }
    } else {
      if (at(LBRACE)) {
        parseClassBody();
      } else {
        expect(COLON, "Expecting ':'", TokenSet.create(IDENTIFIER, PACKAGE_KEYWORD));
        parseDelegationSpecifierList();
        parseClassBody();
      }
    }
  }
/** Created by IntelliJ IDEA. User: Jon S Akhtar Date: 4/3/11 Time: 12:27 AM */
public class MoonParsingLexerMergingAdapter extends MergingLexerAdapterBase
    implements MoonTokenTypes {

  static final TokenSet tokensToMerge =
      TokenSet.create(LONGSTRING_BEGIN, LONGSTRING, LONGSTRING_END, NL_BEFORE_LONGSTRING);
  static final TokenSet tokensToMerge2 =
      TokenSet.create(LONGCOMMENT_BEGIN, LONGCOMMENT, LONGCOMMENT_END, NL_BEFORE_LONGSTRING);

  static final TokenSet allMergables = TokenSet.orSet(tokensToMerge, tokensToMerge2);

  public MoonParsingLexerMergingAdapter(Lexer original) {
    super(
        original,
        new MergeFunction() {

          @Override
          public IElementType merge(IElementType type, Lexer originalLexer) {
            if (!allMergables.contains(type)) {
              return type;
            }

            TokenSet merging = tokensToMerge.contains(type) ? tokensToMerge : tokensToMerge2;

            while (true) {
              final IElementType tokenType = originalLexer.getTokenType();
              if (!merging.contains(tokenType)) break;
              originalLexer.advance();
            }

            return merging == tokensToMerge ? LONGSTRING : LONGCOMMENT;
          }
        });
  }
}
Exemplo n.º 6
0
 @Inject
 public XtendTokenSetProvider(final TokenTypeProvider tokenTypeProvider) {
   IElementType _iElementType =
       tokenTypeProvider.getIElementType(PsiInternalXtendParser.RULE_SL_COMMENT);
   TokenSet _create = TokenSet.create(_iElementType);
   this.slCommentTokens = _create;
   IElementType _iElementType_1 =
       tokenTypeProvider.getIElementType(PsiInternalXtendParser.RULE_RICH_TEXT);
   IElementType _iElementType_2 =
       tokenTypeProvider.getIElementType(PsiInternalXtendParser.RULE_RICH_TEXT_START);
   IElementType _iElementType_3 =
       tokenTypeProvider.getIElementType(PsiInternalXtendParser.RULE_RICH_TEXT_END);
   IElementType _iElementType_4 =
       tokenTypeProvider.getIElementType(PsiInternalXtendParser.RULE_RICH_TEXT_INBETWEEN);
   IElementType _iElementType_5 =
       tokenTypeProvider.getIElementType(PsiInternalXtendParser.RULE_COMMENT_RICH_TEXT_END);
   IElementType _iElementType_6 =
       tokenTypeProvider.getIElementType(PsiInternalXtendParser.RULE_COMMENT_RICH_TEXT_INBETWEEN);
   TokenSet _create_1 =
       TokenSet.create(
           _iElementType_1,
           _iElementType_2,
           _iElementType_3,
           _iElementType_4,
           _iElementType_5,
           _iElementType_6);
   this.richStringLiteralTokens = _create_1;
 }
/** Created by Michal on 18.11.2015. */
public class VaadinDesignParserDefinition implements ParserDefinition {

  public static final TokenSet WHITE_SPACES = TokenSet.create(TokenType.WHITE_SPACE);
  public static final TokenSet STRING_LITERALS = TokenSet.create(VDTypes.ATTR_VALUE);
  //    public static final TokenSet COMMENTS = TokenSet.create(SimpleTypes.COMMENT);

  public static final IFileElementType FILE =
      new IFileElementType(
          Language.<VaadinDeclarativeLanguage>findInstance(VaadinDeclarativeLanguage.class));

  @NotNull
  @Override
  public Lexer createLexer(Project project) {
    return new FlexAdapter(new VaadinDesignLexer(null));
  }

  @Override
  public PsiParser createParser(Project project) {
    return new VaadinDesignParser();
  }

  @Override
  public IFileElementType getFileNodeType() {
    return FILE;
  }

  @NotNull
  @Override
  public TokenSet getWhitespaceTokens() {
    return WHITE_SPACES;
  }

  @NotNull
  @Override
  public TokenSet getCommentTokens() {
    return TokenSet.EMPTY;
  }

  @NotNull
  @Override
  public TokenSet getStringLiteralElements() {
    return STRING_LITERALS;
  }

  @NotNull
  @Override
  public PsiElement createElement(ASTNode node) {
    return VDTypes.Factory.createElement(node);
  }

  @Override
  public PsiFile createFile(FileViewProvider viewProvider) {
    return new VaadinDesignFile(viewProvider);
  }

  @Override
  public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
    return SpaceRequirements.MAY;
  }
}
Exemplo n.º 8
0
  /**
   * 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;
  }
Exemplo n.º 9
0
  /**
   * 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;
  }
  public void parseSliceEnd(PsiBuilder.Marker exprStart, PsiBuilder.Marker sliceItemStart) {
    myBuilder.advanceLexer();
    if (atToken(PyTokenTypes.RBRACKET)) {
      PsiBuilder.Marker sliceMarker = myBuilder.mark();
      sliceMarker.done(PyElementTypes.EMPTY_EXPRESSION);
      sliceItemStart.done(PyElementTypes.SLICE_ITEM);
      nextToken();
      exprStart.done(PyElementTypes.SLICE_EXPRESSION);
      return;
    } else {
      if (atToken(PyTokenTypes.COLON)) {
        PsiBuilder.Marker sliceMarker = myBuilder.mark();
        sliceMarker.done(PyElementTypes.EMPTY_EXPRESSION);
      } else {
        parseSingleExpression(false);
      }
      if (!BRACKET_COLON_COMMA.contains(myBuilder.getTokenType())) {
        myBuilder.error(message("PARSE.expected.colon.or.rbracket"));
      }
      if (matchToken(PyTokenTypes.COLON)) {
        parseSingleExpression(false);
      }

      sliceItemStart.done(PyElementTypes.SLICE_ITEM);
      if (!BRACKET_OR_COMMA.contains(myBuilder.getTokenType())) {
        myBuilder.error("']' or ',' expected");
      }
    }

    parseSliceListTail(exprStart, null);
  }
 boolean typeExtends(IElementType child, IElementType parent) {
   if (child == parent) return true;
   if (extendsSets != null) {
     for (TokenSet set : extendsSets) {
       if (set.contains(child) && set.contains(parent)) return true;
     }
   }
   return altExtendsChecker != null && altExtendsChecker.process(child, parent);
 }
  @Nullable
  private PsiBuilder.Marker parseUnary(final PsiBuilder builder) {
    final IElementType tokenType = builder.getTokenType();

    if (PREFIX_OPS.contains(tokenType)) {
      final PsiBuilder.Marker unary = builder.mark();
      builder.advanceLexer();

      final PsiBuilder.Marker operand = parseUnary(builder);
      if (operand == null) {
        error(builder, JavaErrorMessages.message("expected.expression"));
      }

      unary.done(JavaElementType.PREFIX_EXPRESSION);
      return unary;
    } else if (tokenType == JavaTokenType.LPARENTH) {
      final PsiBuilder.Marker typeCast = builder.mark();
      builder.advanceLexer();

      ReferenceParser.TypeInfo typeInfo =
          myParser
              .getReferenceParser()
              .parseTypeInfo(
                  builder,
                  ReferenceParser.EAT_LAST_DOT
                      | ReferenceParser.WILDCARD
                      | ReferenceParser.CONJUNCTIONS
                      | ReferenceParser.INCOMPLETE_ANNO);
      if (typeInfo == null || !expect(builder, JavaTokenType.RPARENTH)) {
        typeCast.rollbackTo();
        return parsePostfix(builder);
      }

      if (PREF_ARITHMETIC_OPS.contains(builder.getTokenType()) && !typeInfo.isPrimitive) {
        typeCast.rollbackTo();
        return parsePostfix(builder);
      }

      final PsiBuilder.Marker expr = parseUnary(builder);
      if (expr == null) {
        if (!typeInfo
            .isParameterized) { // cannot parse correct parenthesized expression after correct
                                // parameterized type
          typeCast.rollbackTo();
          return parsePostfix(builder);
        } else {
          error(builder, JavaErrorMessages.message("expected.expression"));
        }
      }

      typeCast.done(JavaElementType.TYPE_CAST_EXPRESSION);
      return typeCast;
    } else {
      return parsePostfix(builder);
    }
  }
Exemplo n.º 13
0
  static {
    keys1 = new HashMap<IElementType, TextAttributesKey>();
    keys2 = new HashMap<IElementType, TextAttributesKey>();

    fillMap(keys1, JetTokens.KEYWORDS, JetHighlightingColors.KEYWORD);

    keys1.put(JetTokens.AS_SAFE, JetHighlightingColors.KEYWORD);
    keys1.put(JetTokens.LABEL_IDENTIFIER, JetHighlightingColors.LABEL);
    keys1.put(JetTokens.INTEGER_LITERAL, JetHighlightingColors.NUMBER);
    keys1.put(JetTokens.FLOAT_LITERAL, JetHighlightingColors.NUMBER);

    fillMap(
        keys1,
        TokenSet.andNot(
            JetTokens.OPERATIONS,
            TokenSet.orSet(
                TokenSet.create(JetTokens.IDENTIFIER, JetTokens.LABEL_IDENTIFIER),
                JetTokens.KEYWORDS)),
        JetHighlightingColors.OPERATOR_SIGN);

    keys1.put(JetTokens.LPAR, JetHighlightingColors.PARENTHESIS);
    keys1.put(JetTokens.RPAR, JetHighlightingColors.PARENTHESIS);
    keys1.put(JetTokens.LBRACE, JetHighlightingColors.BRACES);
    keys1.put(JetTokens.RBRACE, JetHighlightingColors.BRACES);
    keys1.put(JetTokens.LBRACKET, JetHighlightingColors.BRACKETS);
    keys1.put(JetTokens.RBRACKET, JetHighlightingColors.BRACKETS);
    keys1.put(JetTokens.COMMA, JetHighlightingColors.COMMA);
    keys1.put(JetTokens.SEMICOLON, JetHighlightingColors.SEMICOLON);
    keys1.put(JetTokens.DOT, JetHighlightingColors.DOT);
    keys1.put(JetTokens.ARROW, JetHighlightingColors.ARROW);

    keys1.put(JetTokens.OPEN_QUOTE, JetHighlightingColors.STRING);
    keys1.put(JetTokens.CLOSING_QUOTE, JetHighlightingColors.STRING);
    keys1.put(JetTokens.REGULAR_STRING_PART, JetHighlightingColors.STRING);
    keys1.put(JetTokens.LONG_TEMPLATE_ENTRY_END, JetHighlightingColors.STRING_ESCAPE);
    keys1.put(JetTokens.LONG_TEMPLATE_ENTRY_START, JetHighlightingColors.STRING_ESCAPE);
    keys1.put(JetTokens.SHORT_TEMPLATE_ENTRY_START, JetHighlightingColors.STRING_ESCAPE);

    keys1.put(JetTokens.ESCAPE_SEQUENCE, JetHighlightingColors.STRING_ESCAPE);

    keys1.put(JetTokens.CHARACTER_LITERAL, JetHighlightingColors.STRING);

    keys1.put(JetTokens.EOL_COMMENT, JetHighlightingColors.LINE_COMMENT);
    keys1.put(JetTokens.SHEBANG_COMMENT, JetHighlightingColors.LINE_COMMENT);
    keys1.put(JetTokens.BLOCK_COMMENT, JetHighlightingColors.BLOCK_COMMENT);
    keys1.put(JetTokens.DOC_COMMENT, JetHighlightingColors.DOC_COMMENT);

    fillMap(keys1, KDocTokens.KDOC_HIGHLIGHT_TOKENS, JetHighlightingColors.DOC_COMMENT);
    keys1.put(KDocTokens.TAG_NAME, JetHighlightingColors.KDOC_TAG);
    keys2.put(KDocTokens.TAG_NAME, JetHighlightingColors.DOC_COMMENT);

    keys1.put(TokenType.BAD_CHARACTER, JetHighlightingColors.BAD_CHARACTER);
  }
public class PerlParserDefinition implements ParserDefinition {
  public static final TokenSet WHITE_SPACES = TokenSet.create(TokenType.WHITE_SPACE);
  public static final TokenSet COMMENT = TokenSet.create(PerlTypes.COMMENT);
  public static final TokenSet LITERAL = TokenSet.create(PerlTypes.LITERAL);

  public static final IFileElementType FILE =
      new IFileElementType(Language.<PerlLanguage>findInstance(PerlLanguage.class));

  @NotNull
  @Override
  public Lexer createLexer(Project project) {
    return new FlexAdapter(new PerlLexer((Reader) null));
  }

  @NotNull
  public TokenSet getWhitespaceTokens() {
    return WHITE_SPACES;
  }

  @NotNull
  public TokenSet getCommentTokens() {
    return COMMENT;
  }

  @NotNull
  public TokenSet getStringLiteralElements() {
    return LITERAL;
  }

  @NotNull
  public PsiParser createParser(final Project project) {
    return new PerlParser();
  }

  @Override
  public IFileElementType getFileNodeType() {
    return FILE;
  }

  public PsiFile createFile(FileViewProvider viewProvider) {
    return new PerlFile(viewProvider);
  }

  public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
    return SpaceRequirements.MAY;
  }

  @NotNull
  public PsiElement createElement(ASTNode node) {
    return PerlTypes.Factory.createElement(node);
  }
}
Exemplo n.º 15
0
/** @author Stepan Koltsov */
public class RustTokenTypes {
  static IElementType WHITE_SPACE = TokenType.WHITE_SPACE;
  static IElementType BAD_CHARACTER = TokenType.BAD_CHARACTER;
  static IElementType NUMBER = new RustElementType("number");

  static IElementType END_OF_LINE_COMMENT = new RustElementType("END_OF_LINE_COMMENT");
  static IElementType KEY_CHARACTERS = new RustElementType("KEY_CHARACTERS");
  static IElementType VALUE_CHARACTERS = new RustElementType("VALUE_CHARACTERS");
  static IElementType KEY_VALUE_SEPARATOR = new RustElementType("KEY_VALUE_SEPARATOR");

  static TokenSet COMMENTS = TokenSet.create(END_OF_LINE_COMMENT);
  static TokenSet WHITESPACES = TokenSet.create(WHITE_SPACE);
}
Exemplo n.º 16
0
 /**
  * There is a possible case that given base node doesn't have {@link
  * AlignmentInColumnsConfig#getTargetDeclarationTypes() target type} but its first child node or
  * first child node of the first child node etc does.
  *
  * <p>This method tries to derive node of the target type from the given node.
  *
  * @param baseNode base node to process
  * @param targetTypes target node types
  * @return base node or its first descendant child that has {@link
  *     AlignmentInColumnsConfig#getTargetDeclarationTypes() target type} target type if the one if
  *     found; <code>null</code> otherwise
  */
 @Nullable
 private static ASTNode deriveNodeOfTargetType(ASTNode baseNode, TokenSet targetTypes) {
   if (targetTypes.contains(baseNode.getElementType())) {
     return baseNode;
   }
   for (ASTNode node = baseNode; node != null; node = node.getFirstChildNode()) {
     IElementType nodeType = node.getElementType();
     if (targetTypes.contains(nodeType)) {
       return node;
     }
   }
   return null;
 }
Exemplo n.º 17
0
  /*
   * for
   *   : "for" "(" annotations ("val" | "var")? (multipleVariableDeclarations | variableDeclarationEntry) "in" expression ")" expression
   *   ;
   *
   *   TODO: empty loop body (at the end of the block)?
   */
  private void parseFor() {
    assert _at(FOR_KEYWORD);

    PsiBuilder.Marker loop = mark();

    advance(); // FOR_KEYWORD

    if (expect(LPAR, "Expecting '(' to open a loop range", EXPRESSION_FIRST)) {
      myBuilder.disableNewlines();

      if (!at(RPAR)) {
        PsiBuilder.Marker parameter = mark();

        if (!at(IN_KEYWORD)) {
          myJetParsing.parseModifierList(DEFAULT, TokenSet.create(IN_KEYWORD, RPAR, COLON));
        }

        if (at(VAL_KEYWORD) || at(VAR_KEYWORD)) advance(); // VAL_KEYWORD or VAR_KEYWORD

        if (at(LPAR)) {
          myJetParsing.parseMultiDeclarationName(TokenSet.create(IN_KEYWORD, LBRACE));
          parameter.done(DESTRUCTURING_DECLARATION);
        } else {
          expect(IDENTIFIER, "Expecting a variable name", TokenSet.create(COLON, IN_KEYWORD));

          if (at(COLON)) {
            advance(); // COLON
            myJetParsing.parseTypeRef(TokenSet.create(IN_KEYWORD));
          }
          parameter.done(VALUE_PARAMETER);
        }

        if (expect(IN_KEYWORD, "Expecting 'in'", TokenSet.create(LPAR, LBRACE, RPAR))) {
          PsiBuilder.Marker range = mark();
          parseExpression();
          range.done(LOOP_RANGE);
        }
      } else {
        error("Expecting a variable name");
      }

      expectNoAdvance(RPAR, "Expecting ')'");
      myBuilder.restoreNewlinesState();
    }

    parseControlStructureBody();

    loop.done(FOR);
  }
Exemplo n.º 18
0
 private static boolean isLiteral(PsiElement element) {
   if (element == null) return false;
   final ASTNode astNode = element.getNode();
   if (astNode == null) {
     return false;
   }
   final IElementType elementType = astNode.getElementType();
   final ParserDefinition parserDefinition =
       LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage());
   if (parserDefinition != null) {
     final TokenSet literals = parserDefinition.getStringLiteralElements();
     return literals.contains(elementType);
   }
   return false;
 }
Exemplo n.º 19
0
/** @author ilyas */
public interface LuaDocTokenTypes {

  IElementType LDOC_TAG_NAME = new LuaDocElementTypeImpl("LDOC_TAG_NAME");
  IElementType LDOC_TAG_PLAIN_VALUE_TOKEN = new LuaDocElementTypeImpl("LDOC_TAG_VALUE_TOKEN");
  IElementType LDOC_TAG_VALUE = new LuaDocTagValueTokenType();

  IElementType LDOC_COMMENT_START = new LuaDocElementTypeImpl("LDOC_COMMENT_START");

  IElementType LDOC_COMMENT_DATA = new LuaDocElementTypeImpl("LDOC_COMMENT_DATA");

  IElementType LDOC_WHITESPACE = new LuaDocElementTypeImpl("LDOC_WHITESPACE");
  IElementType LDOC_DASHES = new LuaDocElementTypeImpl("LDOC_DASHES");
  IElementType LDOC_COMMENT_BAD_CHARACTER = TokenType.BAD_CHARACTER;

  TokenSet LUA_DOC_TOKENS =
      TokenSet.create(
          LDOC_COMMENT_START,
          LDOC_COMMENT_DATA,
          LDOC_WHITESPACE,
          LDOC_DASHES,
          LDOC_TAG_NAME,
          LDOC_TAG_VALUE,
          LDOC_TAG_PLAIN_VALUE_TOKEN,
          LDOC_COMMENT_BAD_CHARACTER);
}
Exemplo n.º 20
0
  @Nullable
  private static PsiBuilder.Marker parseBinary(
      final PsiBuilder builder, final ExprType type, final TokenSet ops) {
    PsiBuilder.Marker left = parseExpression(builder, type);
    if (left == null) return null;

    while (true) {
      final IElementType tokenType = getGtTokenType(builder);
      if (tokenType == null || !ops.contains(tokenType)) break;

      final PsiBuilder.Marker binary = left.precede();
      advanceGtToken(builder, tokenType);

      final PsiBuilder.Marker right = parseExpression(builder, type);
      if (right == null) {
        error(builder, JavaErrorMessages.message("expected.expression"));
        binary.done(JavaElementType.BINARY_EXPRESSION);
        return binary;
      }

      binary.done(JavaElementType.BINARY_EXPRESSION);
      left = binary;
    }

    return left;
  }
  static {
    // in regular case, these tokens should be created in extension class
    TOKENS_MAP.put("inner", RESERVED_INNER);
    TOKENS_MAP.put("with", RESERVED_WITH);
    TOKENS_MAP.put("extends", RESERVED_EXTENDS);
    TOKENS_MAP.put("meta", RESERVED_META);
    TOKENS_MAP.put("override", RESERVED_OVERRIDE);
    TOKENS_MAP.put("around", RESERVED_AROUND);
    TOKENS_MAP.put("super", RESERVED_SUPER);
    TOKENS_MAP.put("augment", RESERVED_AUGMENT);
    TOKENS_MAP.put("after", RESERVED_AFTER);
    TOKENS_MAP.put("before", RESERVED_BEFORE);
    TOKENS_MAP.put("has", RESERVED_HAS);

    RESERVED_TO_STATEMENT_MAP.put(RESERVED_INNER, MOOSE_STATEMENT_INNER);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_WITH, MOOSE_STATEMENT_WITH);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_EXTENDS, MOOSE_STATEMENT_EXTENDS);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_META, MOOSE_STATEMENT_META);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_OVERRIDE, MOOSE_STATEMENT_OVERRIDE);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_AROUND, MOOSE_STATEMENT_AROUND);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_SUPER, MOOSE_STATEMENT_SUPER);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_AUGMENT, MOOSE_STATEMENT_AUGMENT);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_AFTER, MOOSE_STATEMENT_AFTER);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_BEFORE, MOOSE_STATEMENT_BEFORE);
    RESERVED_TO_STATEMENT_MAP.put(RESERVED_HAS, MOOSE_STATEMENT_HAS);

    MOOSE_TOKEN_SET =
        TokenSet.create(TOKENS_MAP.values().toArray(new IElementType[TOKENS_MAP.values().size()]));
  }
  @NotNull
  public Count getCount() {
    final ASTNode[] nodes = getNode().getChildren(RegExpTokens.QUANTIFIERS);
    assert nodes.length > 0;

    final IElementType type = nodes[0].getElementType();
    if (type == RegExpTokens.QUEST) {
      return SimpleCount.ONE_OR_ZERO;
    } else if (type == RegExpTokens.STAR) {
      return SimpleCount.ZERO_OR_MORE;
    } else if (type == RegExpTokens.PLUS) {
      return SimpleCount.ONE_OR_MORE;
    } else if (type == RegExpTokens.LBRACE) {
      final ASTNode[] numbers = getNode().getChildren(TokenSet.create(RegExpTokens.NUMBER));
      if (numbers.length >= 1) {
        final String min = numbers[0].getText();
        final String max;
        if (numbers.length == 2) {
          max = numbers[1].getText();
        } else if (getNode().findChildByType(RegExpTokens.COMMA) != null) {
          max = "";
        } else {
          max = min;
        }
        return new RepeatedCount(min, max);
      }
      // syntactically incorrect
      return new RepeatedCount("", "");
    }

    assert false;
    return null;
  }
class GoBinaryExpressionBlock extends GoBlock {
  public enum Mode {
    Normal,
    Compact
  }

  private static final TokenSet EMPTY_SPACE_TOKENS =
      TokenSet.create(oMUL, oQUOTIENT, oREMAINDER, oSHIFT_LEFT, oSHIFT_RIGHT);

  private final Spacing spacing;

  public GoBinaryExpressionBlock(
      ASTNode node, Alignment alignment, Wrap wrap, CommonCodeStyleSettings settings) {
    super(node, alignment, Indent.getNormalIndent(), wrap, settings);

    GoBinaryExpression psi = node.getPsi(GoBinaryExpression.class);
    if (psi != null && EMPTY_SPACE_TOKENS.contains(psi.getOperator())) {
      spacing = EMPTY_SPACING_KEEP_LINE_BREAKS;
    } else {
      spacing = BASIC_SPACING_KEEP_LINE_BREAKS;
    }
  }

  @Override
  protected Spacing getGoBlockSpacing(GoBlock child1, GoBlock child2) {
    return isCommentBlock(child1) || isCommentBlock(child2)
        ? BASIC_SPACING_KEEP_LINE_BREAKS
        : spacing;
  }

  @Override
  protected Indent getChildIndent(@Nullable PsiElement child) {
    return null;
  }
}
Exemplo n.º 24
0
  /*
   * initializer
   *   : attributes "this" valueArguments
   *   : attributes constructorInvocation // type parameters may (must?) be omitted
   *   ;
   */
  private void parseInitializer() {
    PsiBuilder.Marker initializer = mark();
    parseAnnotations(false);

    IElementType type;
    if (at(THIS_KEYWORD)) {
      PsiBuilder.Marker mark = mark();
      advance(); // THIS_KEYWORD
      mark.done(THIS_CONSTRUCTOR_REFERENCE);
      type = THIS_CALL;
    } else if (atSet(TYPE_REF_FIRST)) {
      PsiBuilder.Marker reference = mark();
      parseTypeRef();
      reference.done(CONSTRUCTOR_CALLEE);
      type = DELEGATOR_SUPER_CALL;
    } else {
      errorWithRecovery(
          "Expecting constructor call (this(...)) or supertype initializer",
          TokenSet.create(LBRACE, COMMA));
      initializer.drop();
      return;
    }
    myExpressionParsing.parseValueArgumentList();

    initializer.done(type);
  }
Exemplo n.º 25
0
  /*
   * whenEntry
   *   // TODO : consider empty after ->
   *   : whenCondition{","} "->" element SEMI
   *   : "else" "->" element SEMI
   *   ;
   */
  private void parseWhenEntry() {
    PsiBuilder.Marker entry = mark();

    if (at(ELSE_KEYWORD)) {
      advance(); // ELSE_KEYWORD

      if (!at(ARROW)) {
        errorUntil("Expecting '->'", TokenSet.create(ARROW, RBRACE, EOL_OR_SEMICOLON));
      }

      if (at(ARROW)) {
        advance(); // ARROW

        if (atSet(WHEN_CONDITION_RECOVERY_SET)) {
          error("Expecting an element");
        } else {
          parseExpressionPreferringBlocks();
        }
      } else if (!atSet(WHEN_CONDITION_RECOVERY_SET)) {
        errorAndAdvance("Expecting '->'");
      }
    } else {
      parseWhenEntryNotElse();
    }

    entry.done(WHEN_ENTRY);
    consumeIf(SEMICOLON);
  }
Exemplo n.º 26
0
 private static ImmutableMap<String, JetToken> tokenSetToMap(TokenSet tokens) {
   ImmutableMap.Builder<String, JetToken> builder = ImmutableMap.builder();
   for (IElementType token : tokens.getTypes()) {
     builder.put(token.toString(), (JetToken) token);
   }
   return builder.build();
 }
Exemplo n.º 27
0
  /*
   * expressions
   *   : SEMI* statement{SEMI+} SEMI*
   */
  public void parseStatements() {
    while (at(SEMICOLON)) advance(); // SEMICOLON
    while (!eof() && !at(RBRACE)) {
      if (!atSet(STATEMENT_FIRST)) {
        errorAndAdvance("Expecting an element");
      }
      if (atSet(STATEMENT_FIRST)) {
        parseStatement();
      }
      if (at(SEMICOLON)) {
        while (at(SEMICOLON)) advance(); // SEMICOLON
      } else if (at(RBRACE)) {
        break;
      } else if (!myBuilder.newlineBeforeCurrentToken()) {
        String severalStatementsError =
            "Unexpected tokens (use ';' to separate expressions on the same line)";

        if (atSet(STATEMENT_NEW_LINE_QUICK_RECOVERY_SET)) {
          error(severalStatementsError);
        } else {
          errorUntil(severalStatementsError, TokenSet.create(EOL_OR_SEMICOLON, LBRACE, RBRACE));
        }
      }
    }
  }
Exemplo n.º 28
0
  /*
   * (SimpleName (":" type)){","}
   */
  public void parseMultiDeclarationName(TokenSet follow) {
    // Parsing multi-name, e.g.
    //   val (a, b) = foo()
    myBuilder.disableNewlines();
    advance(); // LPAR

    TokenSet recoverySet = TokenSet.orSet(PARAMETER_NAME_RECOVERY_SET, follow);
    if (!atSet(follow)) {
      while (true) {
        if (at(COMMA)) {
          errorAndAdvance("Expecting a name");
        } else if (at(RPAR)) {
          error("Expecting a name");
          break;
        }
        PsiBuilder.Marker property = mark();
        expect(IDENTIFIER, "Expecting a name", recoverySet);

        if (at(COLON)) {
          advance(); // COLON
          parseTypeRef(follow);
        }
        property.done(MULTI_VARIABLE_DECLARATION_ENTRY);

        if (!at(COMMA)) break;
        advance(); // COMMA
      }
    }

    expect(RPAR, "Expecting ')'", follow);
    myBuilder.restoreNewlinesState();
  }
Exemplo n.º 29
0
/** User: jansorg Date: 21.05.2009 Time: 10:36:06 */
public class BashWordImpl extends BashPsiElementImpl implements BashWord {
  private static final TokenSet nonWrappableChilds =
      TokenSet.create(BashElementTypes.STRING_ELEMENT, BashTokenTypes.STRING2, BashTokenTypes.WORD);

  public BashWordImpl(final ASTNode astNode) {
    super(astNode, "bash combined word");
  }

  @Override
  public void accept(@NotNull PsiElementVisitor visitor) {
    if (visitor instanceof BashVisitor) {
      ((BashVisitor) visitor).visitCombinedWord(this);
    } else {
      visitor.visitElement(this);
    }
  }

  public boolean isWrappable() {
    return findChildByType(nonWrappableChilds) == null;
  }

  public String getUnwrappedCharSequence() {
    return getText();
  }

  public boolean isStatic() {
    return BashPsiUtils.isStaticWordExpr(getFirstChild());
  }
}
  private boolean parseSliceListTail(
      PsiBuilder.Marker exprStart, @Nullable PsiBuilder.Marker sliceOrTupleStart) {
    boolean inSlice = sliceOrTupleStart == null;
    while (atToken(PyTokenTypes.COMMA)) {
      nextToken();
      PsiBuilder.Marker sliceItemStart = myBuilder.mark();
      parseTestExpression(false, false);
      if (matchToken(PyTokenTypes.COLON)) {
        inSlice = true;
        parseTestExpression(false, false);
        if (matchToken(PyTokenTypes.COLON)) {
          parseTestExpression(false, false);
        }
      }
      sliceItemStart.done(PyElementTypes.SLICE_ITEM);
      if (!BRACKET_OR_COMMA.contains(myBuilder.getTokenType())) {
        myBuilder.error("']' or ',' expected");
        break;
      }
    }
    checkMatches(PyTokenTypes.RBRACKET, message("PARSE.expected.rbracket"));

    if (inSlice) {
      if (sliceOrTupleStart != null) {
        sliceOrTupleStart.drop();
      }
      exprStart.done(PyElementTypes.SLICE_EXPRESSION);
    }
    return inSlice;
  }