Java Code Examples for com.intellij.lang.ParserDefinition

The following examples show how to use com.intellij.lang.ParserDefinition. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: bamboo-soy   Source File: SoyFileViewProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected PsiFile createFile(@NotNull Language lang) {
  ParserDefinition parserDefinition = getDefinition(lang);
  if (parserDefinition == null) {
    return null;
  }

  if (lang.is(TEMPLATE_DATA_LANGUAGE)) {
    PsiFileImpl file = (PsiFileImpl) parserDefinition.createFile(this);
    file.setContentElementType(TEMPLATE_DATA_ELEMENT_TYPE);
    return file;
  } else if (lang.isKindOf(BASE_LANGUAGE)) {
    return parserDefinition.createFile(this);
  } else {
    return null;
  }
}
 
Example 2
private boolean isComment(int offset) {
  final HighlighterIterator it = myEditor.getHighlighter().createIterator(offset);
  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 false;
    }
    else {
      myComments.put(language, comments);
    }
  }
  return comments.contains(tokenType);
}
 
Example 3
private boolean isComment(int offset) {
  final HighlighterIterator it = myEditor.getHighlighter().createIterator(offset);
  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 false;
    }
    else {
      myComments.put(language, comments);
    }
  }
  return comments.contains(tokenType);
}
 
Example 4
Source Project: intellij   Source File: BuildParserTest.java    License: Apache License 2.0 6 votes vote down vote up
private ASTNode createAST(String text) {
  ParserDefinition definition = new BuildParserDefinition();
  PsiParser parser = definition.createParser(getProject());
  Lexer lexer = definition.createLexer(getProject());
  PsiBuilderImpl psiBuilder =
      new PsiBuilderImpl(
          getProject(), null, definition, lexer, new CharTableImpl(), text, null, null);
  PsiBuilderAdapter adapter =
      new PsiBuilderAdapter(psiBuilder) {
        @Override
        public void error(String messageText) {
          super.error(messageText);
          errors.add(messageText);
        }
      };
  return parser.parse(definition.getFileNodeType(), adapter);
}
 
Example 5
Source Project: idea-gitignore   Source File: IgnoreFile.java    License: MIT License 6 votes vote down vote up
/** Builds a new instance of {@link IgnoreFile}. */
public IgnoreFile(@NotNull FileViewProvider viewProvider, @NotNull IgnoreFileType fileType) {
    super(viewProvider);

    this.fileType = fileType;
    this.language = findLanguage(fileType.getLanguage(), viewProvider);

    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(this.language);
    if (parserDefinition == null) {
        throw new RuntimeException(
                "PsiFileBase: language.getParserDefinition() returned null for: " + this.language
        );
    }
    this.parserDefinition = parserDefinition;

    final IFileElementType nodeType = parserDefinition.getFileNodeType();
    init(nodeType, nodeType);
}
 
Example 6
Source Project: consulo   Source File: DefaultASTLeafFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public LeafElement createLeaf(@Nonnull IElementType type, @Nonnull LanguageVersion languageVersion, @Nonnull CharSequence text) {
  final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(type.getLanguage());
  if(parserDefinition != null) {
    if(parserDefinition.getCommentTokens(languageVersion).contains(type)) {
      return new PsiCoreCommentImpl(type, text);
    }
  }

  // this is special case, then type is WHITE_SPACE, but no parser definition
  if(type == TokenType.WHITE_SPACE) {
    return new PsiWhiteSpaceImpl(text);
  }

  if (type instanceof ILeafElementType) {
    return (LeafElement)((ILeafElementType)type).createLeafNode(text);
  }
  return new LeafPsiElement(type, text);
}
 
Example 7
Source Project: consulo   Source File: CoreStubTreeLoader.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean canHaveStub(VirtualFile file) {
  final FileType fileType = file.getFileType();
  if (fileType instanceof LanguageFileType) {
    Language l = ((LanguageFileType)fileType).getLanguage();
    ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l);
    if (parserDefinition == null) return false;
    final IFileElementType elementType = parserDefinition.getFileNodeType();
    return elementType instanceof IStubFileElementType && ((IStubFileElementType)elementType).shouldBuildStubFor(file);
  }
  else if (fileType.isBinary()) {
    final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
    return builder != null && builder.acceptsFile(file);
  }
  return false;
}
 
Example 8
Source Project: consulo   Source File: CacheUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean isInComments(final IElementType tokenType, LanguageVersion languageVersion) {
  final Language language = tokenType.getLanguage();

  //for (CommentTokenSetProvider provider : CommentTokenSetProvider.EXTENSION.allForLanguage(language)) {
  //  if (provider.isInComments(tokenType)) {
  //    return true;
  //  }
  //}

  boolean inComments = false;
  final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language);

  if (parserDefinition != null) {
    final TokenSet commentTokens = parserDefinition.getCommentTokens(languageVersion);

    if (commentTokens.contains(tokenType)) {
      inComments = true;
    }
  }
  return inComments;
}
 
Example 9
Source Project: consulo   Source File: IndentsPass.java    License: Apache License 2.0 6 votes vote down vote up
private boolean isComment(int offset) {
  final HighlighterIterator it = myEditor.getHighlighter().createIterator(offset);
  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(LanguageVersionUtil.findLanguageVersion(language, myFile));
    }
    if (comments == null) {
      return false;
    }
    else {
      myComments.put(language, comments);
    }
  }
  return comments.contains(tokenType);
}
 
Example 10
Source Project: consulo   Source File: StubUpdatingIndex.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean canHaveStub(@Nonnull ProjectLocator projectLocator, @Nullable Project project, @Nonnull VirtualFile file) {
  FileType fileType = SubstitutedFileType.substituteFileType(file, file.getFileType(), project == null ? projectLocator.guessProjectForFile(file) : project);
  if (fileType instanceof LanguageFileType) {
    final Language l = ((LanguageFileType)fileType).getLanguage();
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l);
    if (parserDefinition == null) {
      return false;
    }

    final IFileElementType elementType = parserDefinition.getFileNodeType();
    if (elementType instanceof IStubFileElementType) {
      if (((IStubFileElementType)elementType).shouldBuildStubFor(file)) {
        return true;
      }
      FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
      if (file instanceof NewVirtualFile &&
          fileBasedIndex instanceof FileBasedIndexImpl &&
          ((FileBasedIndexImpl)fileBasedIndex).getIndex(INDEX_ID).isIndexedStateForFile(((NewVirtualFile)file).getId(), file)) {
        return true;
      }
    }
  }
  final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
  return builder != null && builder.acceptsFile(file);
}
 
Example 11
Source Project: consulo   Source File: StubVersionMap.java    License: Apache License 2.0 6 votes vote down vote up
private static Object getVersionOwner(FileType fileType) {
  Object owner = null;
  if (fileType instanceof LanguageFileType) {
    Language l = ((LanguageFileType)fileType).getLanguage();
    ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l);
    if (parserDefinition != null) {
      final IFileElementType type = parserDefinition.getFileNodeType();
      if (type instanceof IStubFileElementType) {
        owner = type;
      }
    }
  }

  BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
  if (builder != null) {
    owner = builder;
  }
  return owner;
}
 
Example 12
Source Project: consulo   Source File: TextOccurrencesUtil.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean processStringLiteralsContainingIdentifier(@Nonnull String identifier,
                                                                 @Nonnull SearchScope searchScope,
                                                                 PsiSearchHelper helper,
                                                                 final Processor<PsiElement> processor) {
  TextOccurenceProcessor occurenceProcessor = new TextOccurenceProcessor() {
    @Override
    public boolean execute(PsiElement element, int offsetInElement) {
      final ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage());
      final ASTNode node = element.getNode();
      if (definition != null &&
          node != null &&
          definition.getStringLiteralElements(element.getLanguageVersion()).contains(node.getElementType())) {
        return processor.process(element);
      }
      return true;
    }
  };

  return helper.processElementsWithWord(occurenceProcessor, searchScope, identifier, UsageSearchContext.IN_STRINGS, true);
}
 
Example 13
Source Project: BashSupport   Source File: UnescapingPsiBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public UnescapingPsiBuilder(@NotNull final Project project,
                            @NotNull final ParserDefinition parserDefinition,
                            @NotNull final Lexer lexer,
                            @NotNull final ASTNode chameleon,
                            @NotNull final CharSequence originalText,
                            @NotNull final CharSequence processedText,
                            @NotNull final TextPreprocessor textProcessor) {
    this(new PsiBuilderImpl(project, parserDefinition, lexer, chameleon, originalText), textProcessor, processedText);
}
 
Example 14
Source Project: intellij-latte   Source File: LatteFileViewProvider.java    License: MIT License 5 votes vote down vote up
@Nullable
protected PsiFile createFile(@NotNull Language lang) {
	ParserDefinition parser = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
	if (parser == null) {
		return null;
	} else if (lang == XMLLanguage.INSTANCE || lang == HTMLLanguage.INSTANCE) {
		PsiFileImpl file = (PsiFileImpl) parser.createFile(this);
		file.setContentElementType(templateDataElement);
		return file;
	} else {
		return lang == this.getBaseLanguage() ? parser.createFile(this) : null;
	}
}
 
Example 15
Source Project: intellij-haxe   Source File: HaxePsiBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public HaxePsiBuilder(@NotNull ParserDefinition parserDefinition,
                      @NotNull Lexer lexer,
                      @NotNull final CharSequence text) {
  super(ProjectManager.getInstance().getDefaultProject(), null, parserDefinition, lexer, null, text, null, null);
  psiFile = null;
  setupDebugTraces();
}
 
Example 16
Source Project: intellij-haxe   Source File: HaxePsiBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public HaxePsiBuilder(@NotNull Project project,
                      @NotNull ParserDefinition parserDefinition,
                      @NotNull Lexer lexer,
                      @NotNull ASTNode chameleon,
                      @NotNull CharSequence text) {
  super(project, parserDefinition, lexer, chameleon, text);
  psiFile = SharedImplUtil.getContainingFile(chameleon);
  setupDebugTraces();
}
 
Example 17
Source Project: intellij-haxe   Source File: HaxePsiBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public HaxePsiBuilder(@NotNull Project project,
                      @NotNull ParserDefinition parserDefinition,
                      @NotNull Lexer lexer,
                      @NotNull LighterLazyParseableNode chameleon,
                      @NotNull CharSequence text) {
  super(project, parserDefinition, lexer, chameleon, text);
  psiFile = chameleon.getContainingFile();
  setupDebugTraces();
}
 
Example 18
Source Project: consulo   Source File: PlatformIdTableBuilding.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static DataIndexer<TodoIndexEntry, Integer, FileContent> getTodoIndexer(FileType fileType, Project project, final VirtualFile virtualFile) {
  final DataIndexer<TodoIndexEntry, Integer, FileContent> extIndexer;
  if (fileType instanceof SubstitutedFileType && !((SubstitutedFileType)fileType).isSameFileType()) {
    SubstitutedFileType sft = (SubstitutedFileType)fileType;
    extIndexer = new CompositeTodoIndexer(getTodoIndexer(sft.getOriginalFileType(), project, virtualFile), getTodoIndexer(sft.getFileType(), project, virtualFile));
  }
  else {
    extIndexer = TodoIndexers.INSTANCE.forFileType(fileType);
  }
  if (extIndexer != null) {
    return extIndexer;
  }

  if (fileType instanceof LanguageFileType) {
    final Language lang = ((LanguageFileType)fileType).getLanguage();
    final ParserDefinition parserDef = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
    LanguageVersion languageVersion = LanguageVersionUtil.findLanguageVersion(lang, project, virtualFile);
    final TokenSet commentTokens = parserDef != null ? parserDef.getCommentTokens(languageVersion) : null;
    if (commentTokens != null) {
      return new TokenSetTodoIndexer(commentTokens, languageVersion, virtualFile, project);
    }
  }

  if (fileType instanceof CustomSyntaxTableFileType) {
    return new TokenSetTodoIndexer(ABSTRACT_FILE_COMMENT_TOKENS, null, virtualFile, project);
  }

  return null;
}
 
Example 19
Source Project: consulo   Source File: SkipAutopopupInStrings.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isInStringLiteral(PsiElement element) {
  LanguageVersion languageVersion = PsiUtilCore.findLanguageVersionFromElement(element);

  ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(languageVersion.getLanguage());
  if (definition == null) {
    return false;
  }

  return isStringLiteral(element, definition, languageVersion) || isStringLiteral(element.getParent(), definition, languageVersion) ||
          isStringLiteralWithError(element, definition, languageVersion) || isStringLiteralWithError(element.getParent(), definition, languageVersion);
}
 
Example 20
Source Project: consulo   Source File: AbstractFileViewProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
protected PsiFile createFile(@Nonnull Language lang) {
  if (lang != getBaseLanguage()) return null;
  final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
  if (parserDefinition != null) {
    return parserDefinition.createFile(this);
  }
  return null;
}
 
Example 21
Source Project: consulo   Source File: FindManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
private static TokenSet addTokenTypesForLanguage(FindModel model, Language lang, TokenSet tokensOfInterest) {
  ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
  if (definition != null) {
    for (LanguageVersion languageVersion : lang.getVersions()) {
      tokensOfInterest = TokenSet.orSet(tokensOfInterest, model.isInCommentsOnly() ? definition.getCommentTokens(languageVersion) : TokenSet.EMPTY);
      tokensOfInterest =
              TokenSet.orSet(tokensOfInterest, model.isInStringLiteralsOnly() ? definition.getStringLiteralElements(languageVersion) : TokenSet.EMPTY);
    }
  }
  return tokensOfInterest;
}
 
Example 22
Source Project: reasonml-idea-plugin   Source File: OclP4ParserDefinition.java    License: MIT License 4 votes vote down vote up
@NotNull
public ParserDefinition.SpaceRequirements spaceExistenceTypeBetweenTokens(ASTNode left, ASTNode right) {
    return ParserDefinition.SpaceRequirements.MAY;
}
 
Example 23
Source Project: reasonml-idea-plugin   Source File: BaseParsingTestCase.java    License: MIT License 4 votes vote down vote up
protected BaseParsingTestCase(@NotNull String dataPath, @NotNull String fileExt, @NotNull ParserDefinition... definitions) {
    super(dataPath, fileExt, definitions);
}
 
Example 24
Source Project: bamboo-soy   Source File: SoyFileViewProvider.java    License: Apache License 2.0 4 votes vote down vote up
private ParserDefinition getDefinition(Language lang) {
  return LanguageParserDefinitions.INSTANCE.forLanguage(lang);
}
 
Example 25
Source Project: BashSupport   Source File: BashEvalElementType.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected ASTNode doParseContents(@NotNull ASTNode chameleon, @NotNull PsiElement psi) {
    Project project = psi.getProject();
    boolean supportEvalEscapes = BashProjectSettings.storedSettings(project).isEvalEscapesEnabled();

    String originalText = chameleon.getChars().toString();
    ParserDefinition def = LanguageParserDefinitions.INSTANCE.forLanguage(BashFileType.BASH_LANGUAGE);

    boolean isDoubleQuoted = originalText.startsWith("\"") && originalText.endsWith("\"");
    boolean isSingleQuoted = originalText.startsWith("'") && originalText.endsWith("'");
    boolean isEscapingSingleQuoted = originalText.startsWith("$'") && originalText.endsWith("'");
    boolean isUnquoted = !isDoubleQuoted && !isSingleQuoted && !isEscapingSingleQuoted;

    String prefix = isUnquoted ? "" : originalText.subSequence(0, isEscapingSingleQuoted ? 2 : 1).toString();
    String content = isUnquoted ? originalText : originalText.subSequence(isEscapingSingleQuoted ? 2 : 1, originalText.length() - 1).toString();
    String suffix = isUnquoted ? "" : originalText.subSequence(originalText.length() - 1, originalText.length()).toString();

    TextPreprocessor textProcessor;
    if (supportEvalEscapes) {
        if (isEscapingSingleQuoted) {
            textProcessor = new BashEnhancedTextPreprocessor(TextRange.from(prefix.length(), content.length()));
        } else if (isSingleQuoted) {
            //no escape handling for single-quoted strings
            textProcessor = new BashIdentityTextPreprocessor(TextRange.from(prefix.length(), content.length()));
        } else {
            //fallback to simple escape handling
            textProcessor = new BashSimpleTextPreprocessor(TextRange.from(prefix.length(), content.length()));
        }
    } else {
        textProcessor = new BashIdentityTextPreprocessor(TextRange.from(prefix.length(), content.length()));
    }

    StringBuilder unescapedContent = new StringBuilder(content.length());
    textProcessor.decode(content, unescapedContent);

    Lexer lexer = isUnquoted
            ? def.createLexer(project)
            : new PrefixSuffixAddingLexer(def.createLexer(project), prefix, TokenType.WHITE_SPACE, suffix, TokenType.WHITE_SPACE);

    PsiBuilder psiBuilder = new UnescapingPsiBuilder(project,
            def,
            lexer,
            chameleon,
            originalText,
            prefix + unescapedContent + suffix,
            textProcessor);

    return def.createParser(project).parse(this, psiBuilder).getFirstChildNode();
}
 
Example 26
Source Project: intellij-haskforce   Source File: HaskellParserTestBase.java    License: Apache License 2.0 4 votes vote down vote up
public HaskellParserTestBase(String dataPath, String fileExt, boolean lowercaseFirstLetter,
                             ParserDefinition... definitions) {
    super(dataPath, fileExt, lowercaseFirstLetter, definitions);
}
 
Example 27
Source Project: consulo   Source File: SkipAutopopupInStrings.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isStringLiteral(PsiElement element, ParserDefinition definition, LanguageVersion languageVersion) {
  return PlatformPatterns.psiElement().withElementType(definition.getStringLiteralElements(languageVersion)).accepts(element);
}
 
Example 28
Source Project: consulo   Source File: SkipAutopopupInStrings.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isStringLiteralWithError(PsiElement element, ParserDefinition definition, LanguageVersion languageVersion) {
  return isStringLiteral(element, definition, languageVersion) && PsiTreeUtil.nextLeaf(element) instanceof PsiErrorElement;
}
 
Example 29
Source Project: consulo   Source File: PsiFileBase.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
public ParserDefinition getParserDefinition() {
  return myParserDefinition;
}
 
Example 30
Source Project: idea-gitignore   Source File: IgnoreFile.java    License: MIT License 2 votes vote down vote up
/**
 * Returns current parser definition.
 *
 * @return current {@link ParserDefinition}
 */
@NotNull
public ParserDefinition getParserDefinition() {
    return parserDefinition;
}