@Override public int getErrorsCount(final CharSequence seq, Language fileLanguage, final Project project) { Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); lexer.start(seq); if (lexer.getTokenType() != JavaTokenType.LBRACE) return IErrorCounterReparseableElementType.FATAL_ERROR; lexer.advance(); int balance = 1; while (true) { IElementType type = lexer.getTokenType(); if (type == null) break; if (balance == 0) return IErrorCounterReparseableElementType.FATAL_ERROR; if (type == JavaTokenType.LBRACE) { balance++; } else if (type == JavaTokenType.RBRACE) { balance--; } lexer.advance(); } return balance; }
private static boolean canStickJavaTokens(ASTNode token1, ASTNode token2) { IElementType type1 = token1.getElementType(); IElementType type2 = token2.getElementType(); Pair<IElementType, IElementType> pair = Pair.create(type1, type2); Boolean res = myCanStickJavaTokensMatrix.get(pair); if (res == null) { if (!checkToken(token1) || !checkToken(token2)) return true; String text = token1.getText() + token2.getText(); Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); lexer.start(text); boolean canMerge = lexer.getTokenType() == type1; lexer.advance(); canMerge &= lexer.getTokenType() == type2; res = canMerge; myCanStickJavaTokensMatrix.put(pair, res); } return res.booleanValue(); }
private static boolean canStickJavaTokens(ASTNode token1, ASTNode token2) { IElementType type1 = token1.getElementType(); IElementType type2 = token2.getElementType(); Pair<IElementType, IElementType> pair = new Pair<IElementType, IElementType>(type1, type2); Boolean res = myCanStickJavaTokensMatrix.get(pair); if (res == null) { if (!checkToken(token1) || !checkToken(token2)) return true; String text = token1.getText() + token2.getText(); Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); lexer.start(text); boolean canMerge = lexer.getTokenType() == type1; lexer.advance(); canMerge &= lexer.getTokenType() == type2; res = canMerge; myCanStickJavaTokensMatrix.put(pair, res); } return res.booleanValue(); }
public JavaHighlightingLexer(LanguageLevel languageLevel) { super(JavaParserDefinition.createLexer(languageLevel)); registerSelfStoppingLayer(new StringLiteralLexer('\"', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.STRING_LITERAL}, IElementType.EMPTY_ARRAY); registerSelfStoppingLayer(new StringLiteralLexer('\'', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.CHARACTER_LITERAL}, IElementType.EMPTY_ARRAY); LayeredLexer docLexer = new LayeredLexer(JavaParserDefinition.createDocLexer(languageLevel)); HtmlHighlightingLexer lexer = new HtmlHighlightingLexer(); lexer.setHasNoEmbeddments(true); docLexer.registerLayer(lexer, JavaDocTokenType.DOC_COMMENT_DATA); registerSelfStoppingLayer(docLexer, new IElementType[]{JavaDocElementType.DOC_COMMENT}, IElementType.EMPTY_ARRAY); }
@Override public boolean isParsable(final CharSequence buffer, Language fileLanguage, final Project project) { if (!StringUtil.startsWith(buffer, "/**") || !StringUtil.endsWith(buffer, "*/")) return false; Lexer lexer = JavaParserDefinition.createLexer(LanguageLevelProjectExtension.getInstance(project).getLanguageLevel()); lexer.start(buffer); if (lexer.getTokenType() == DOC_COMMENT) { lexer.advance(); if (lexer.getTokenType() == null) { return true; } } return false; }
public JavaHighlightingLexer(@NotNull LanguageLevel languageLevel) { super(JavaParserDefinition.createLexer(languageLevel)); registerSelfStoppingLayer(new StringLiteralLexer('\"', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.STRING_LITERAL}, IElementType.EMPTY_ARRAY); registerSelfStoppingLayer(new StringLiteralLexer('\'', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.CHARACTER_LITERAL}, IElementType.EMPTY_ARRAY); LayeredLexer docLexer = new LayeredLexer(JavaParserDefinition.createDocLexer(languageLevel)); HtmlHighlightingLexer htmlLexer = new HtmlHighlightingLexer(null); htmlLexer.setHasNoEmbeddments(true); docLexer.registerLayer(htmlLexer, JavaDocTokenType.DOC_COMMENT_DATA); registerSelfStoppingLayer(docLexer, new IElementType[]{JavaDocElementType.DOC_COMMENT}, IElementType.EMPTY_ARRAY); }
private static boolean checkToken(final ASTNode token1) { Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); final String text = token1.getText(); lexer.start(text); if (lexer.getTokenType() != token1.getElementType()) return false; lexer.advance(); return lexer.getTokenType() == null; }
@Override @Nullable public Lexer getIndexingLexer(@NotNull final PsiFile file) { if (file instanceof PsiJavaFile && !(file instanceof JspFile)) { return JavaParserDefinition.createLexer(((PsiJavaFile)file).getLanguageLevel()); } return null; }
private static PsiBuilderImpl createBuilder(final String text, ASTNode originalTree) { final Language lang = StdFileTypes.JAVA.getLanguage(); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(lang); assertNotNull(parserDefinition); PsiFile psiFile = createFile("x.java", text); return new PsiBuilderImpl(getProject(), psiFile, parserDefinition, JavaParserDefinition.createLexer(LanguageLevel.JDK_1_5), SharedImplUtil.findCharTableByTree(psiFile.getNode()), text, originalTree, null); }
private static Lexer setupLexer(String text) { LayeredLexer lexer = new LayeredLexer(JavaParserDefinition.createLexer(LanguageLevel.JDK_1_3)); lexer.registerSelfStoppingLayer(new StringLiteralLexer('\"', JavaTokenType.STRING_LITERAL), new IElementType[]{JavaTokenType.STRING_LITERAL}, IElementType.EMPTY_ARRAY); lexer.start(text); return lexer; }
@Nullable public static String getPackageName(CharSequence text) { Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.JDK_1_3); lexer.start(text); skipWhiteSpaceAndComments(lexer); final IElementType firstToken = lexer.getTokenType(); if (firstToken != JavaTokenType.PACKAGE_KEYWORD) { if (JAVA_FILE_FIRST_TOKEN_SET.contains(firstToken)) { return ""; } return null; } lexer.advance(); skipWhiteSpaceAndComments(lexer); final StringBuilder buffer = StringBuilderSpinAllocator.alloc(); try { while(true){ if (lexer.getTokenType() != JavaTokenType.IDENTIFIER) break; buffer.append(text, lexer.getTokenStart(), lexer.getTokenEnd()); lexer.advance(); skipWhiteSpaceAndComments(lexer); if (lexer.getTokenType() != JavaTokenType.DOT) break; buffer.append('.'); lexer.advance(); skipWhiteSpaceAndComments(lexer); } String packageName = buffer.toString(); if (packageName.length() == 0 || StringUtil.endsWithChar(packageName, '.')) return null; return packageName; } finally { StringBuilderSpinAllocator.dispose(buffer); } }
private static boolean lexemsEqual(final PsiClass classToBind, final PsiClass newClass) { Lexer oldTextLexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); Lexer newTextLexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); String oldBuffer = classToBind.getText(); String newBuffer = newClass.getText(); oldTextLexer.start(oldBuffer); newTextLexer.start(newBuffer); while(true) { IElementType oldLexem = oldTextLexer.getTokenType(); IElementType newLexem = newTextLexer.getTokenType(); if (oldLexem == null || newLexem == null) { // must terminate at the same time return oldLexem == null && newLexem == null; } if (oldLexem != newLexem) { return false; } if (oldLexem != TokenType.WHITE_SPACE && oldLexem != JavaDocElementType.DOC_COMMENT) { int oldStart = oldTextLexer.getTokenStart(); int newStart = newTextLexer.getTokenStart(); int oldLength = oldTextLexer.getTokenEnd() - oldTextLexer.getTokenStart(); int newLength = newTextLexer.getTokenEnd() - newTextLexer.getTokenStart(); if (oldLength != newLength) { return false; } for(int i=0; i<oldLength; i++) { if (oldBuffer.charAt(oldStart+i) != newBuffer.charAt(newStart+i)) { return false; } } } oldTextLexer.advance(); newTextLexer.advance(); } }
@Override public boolean isParsable(final CharSequence buffer, Language fileLanguage, final Project project) { Lexer lexer = JavaParserDefinition.createLexer(LanguageLevelProjectExtension.getInstance(project).getLanguageLevel()); lexer.start(buffer); if (lexer.getTokenType() == DOC_COMMENT) { lexer.advance(); if (lexer.getTokenType() == null) { return true; } } return false; }
public JavaCoreApplicationEnvironment(Disposable parentDisposable) { super(parentDisposable); registerFileType(JavaClassFileType.INSTANCE, "class"); registerFileType(JavaFileType.INSTANCE, "java"); registerFileType(ArchiveFileType.INSTANCE, "jar;zip"); addExplicitExtension(FileTypeFileViewProviders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileViewProviderFactory()); addExplicitExtension(BinaryFileStubBuilders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileStubBuilder()); addExplicitExtension(LanguageASTFactory.INSTANCE, JavaLanguage.INSTANCE, new CoreJavaASTFactory()); addExplicitExtension(LanguageParserDefinitions.INSTANCE, JavaLanguage.INSTANCE, new JavaParserDefinition()); addExplicitExtension(LanguageConstantExpressionEvaluator.INSTANCE, JavaLanguage.INSTANCE, new PsiExpressionEvaluator()); registerExtensionPoint(Extensions.getRootArea(), ClsStubBuilderFactory.EP_NAME, ClsStubBuilderFactory.class); registerExtensionPoint(Extensions.getRootArea(), PsiAugmentProvider.EP_NAME, PsiAugmentProvider.class); registerExtensionPoint(Extensions.getRootArea(), JavaMainMethodProvider.EP_NAME, JavaMainMethodProvider.class); addExtension(ClsStubBuilderFactory.EP_NAME, new DefaultClsStubBuilderFactory()); myApplication.registerService(PsiPackageImplementationHelper.class, new CorePsiPackageImplementationHelper()); myApplication.registerService(EmptySubstitutor.class, new EmptySubstitutorImpl()); myApplication.registerService(JavaDirectoryService.class, createJavaDirectoryService()); myApplication.registerService(JavaVersionService.class, new JavaVersionService()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiPackage.class, new PackagePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiClass.class, new ClassPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiMethod.class, new MethodPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiField.class, new FieldPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiLocalVariable.class, new VariablePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiParameter.class, new VariablePresentationProvider()); }
@Override @Nullable public Lexer getIndexingLexer(final PsiFile file) { if (file instanceof PsiJavaFile && !(file instanceof JspFile)) { return JavaParserDefinition.createLexer(((PsiJavaFile)file).getLanguageLevel()); } return null; }
public JavaCoreApplicationEnvironment(Disposable parentDisposable) { super(parentDisposable); registerFileType(JavaClassFileType.INSTANCE, "class"); registerFileType(JavaFileType.INSTANCE, "java"); registerFileType(JarArchiveFileType.INSTANCE, "jar"); addExplicitExtension(FileTypeFileViewProviders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileViewProviderFactory()); addExplicitExtension(BinaryFileStubBuilders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileStubBuilder()); addExplicitExtension(LanguageParserDefinitions.INSTANCE, JavaLanguage.INSTANCE, new JavaParserDefinition()); addExplicitExtension(LanguageConstantExpressionEvaluator.INSTANCE, JavaLanguage.INSTANCE, new PsiExpressionEvaluator()); registerExtensionPoint(Extensions.getRootArea(), PsiAugmentProvider.EP_NAME, PsiAugmentProvider.class); registerExtensionPoint(Extensions.getRootArea(), JavaMainMethodProvider.EP_NAME, JavaMainMethodProvider.class); myApplication.registerService(PsiPackageImplementationHelper.class, new CorePsiPackageImplementationHelper()); myApplication.registerService(EmptySubstitutor.class, new EmptySubstitutorImpl()); myApplication.registerService(JavaDirectoryService.class, createJavaDirectoryService()); myApplication.registerService(JavaVersionService.class, new JavaVersionService()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiJavaPackage.class, new PackagePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiClass.class, new ClassPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiMethod.class, new MethodPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiField.class, new FieldPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiLocalVariable.class, new VariablePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiParameter.class, new VariablePresentationProvider()); }
@Override public IElementType getTokenType(){ Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); lexer.start(myText); return lexer.getTokenType(); }
public JavaCoreApplicationEnvironment(@NotNull Disposable parentDisposable) { super(parentDisposable); registerFileType(JavaClassFileType.INSTANCE, "class"); registerFileType(JavaFileType.INSTANCE, "java"); registerFileType(ArchiveFileType.INSTANCE, "jar;zip"); registerFileType(PlainTextFileType.INSTANCE, "txt;sh;bat;cmd;policy;log;cgi;MF;jad;jam;htaccess"); addExplicitExtension(LanguageASTFactory.INSTANCE, PlainTextLanguage.INSTANCE, new PlainTextASTFactory()); addExplicitExtension(LanguageParserDefinitions.INSTANCE, PlainTextLanguage.INSTANCE, new PlainTextParserDefinition()); addExplicitExtension(FileTypeFileViewProviders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileViewProviderFactory()); addExplicitExtension(BinaryFileStubBuilders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileStubBuilder()); addExplicitExtension(LanguageASTFactory.INSTANCE, JavaLanguage.INSTANCE, new CoreJavaASTFactory()); addExplicitExtension(LanguageParserDefinitions.INSTANCE, JavaLanguage.INSTANCE, new JavaParserDefinition()); addExplicitExtension(LanguageConstantExpressionEvaluator.INSTANCE, JavaLanguage.INSTANCE, new PsiExpressionEvaluator()); addExtension(ContainerProvider.EP_NAME, new JavaContainerProvider()); myApplication.registerService(PsiPackageImplementationHelper.class, new CorePsiPackageImplementationHelper()); myApplication.registerService(EmptySubstitutor.class, new EmptySubstitutorImpl()); myApplication.registerService(JavaDirectoryService.class, createJavaDirectoryService()); myApplication.registerService(JavaVersionService.class, new JavaVersionService()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiPackage.class, new PackagePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiClass.class, new ClassPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiMethod.class, new MethodPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiField.class, new FieldPresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiLocalVariable.class, new VariablePresentationProvider()); addExplicitExtension(ItemPresentationProviders.INSTANCE, PsiParameter.class, new VariablePresentationProvider()); registerApplicationService(JavaCodeFoldingSettings.class, new JavaCodeFoldingSettingsBase()); addExplicitExtension(LanguageFolding.INSTANCE, JavaLanguage.INSTANCE, new JavaFoldingBuilderBase() { @Override protected boolean shouldShowExplicitLambdaType(PsiAnonymousClass anonymousClass, PsiNewExpression expression) { return false; } @Override protected boolean isBelowRightMargin(Project project, int lineLength) { return false; } }); registerApplicationExtensionPoint(SuperMethodsSearch.EP_NAME, QueryExecutor.class); addExtension(SuperMethodsSearch.EP_NAME, new MethodSuperSearcher()); }
public static Lexer createIndexingLexer(OccurrenceConsumer consumer) { Lexer javaLexer = JavaParserDefinition.createLexer(LanguageLevel.JDK_1_3); return new JavaFilterLexer(javaLexer, consumer); }
@SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors"}) public JavaParsingTestCase(@NonNls final String dataPath) { super("psi/"+dataPath, "java", new JavaParserDefinition()); }
@Override protected Lexer createLexer() { return JavaParserDefinition.createLexer(LanguageLevel.HIGHEST); }
private static boolean mayContainClassesInside(PsiElement psiElement, final CharSequence fileBuffer) { PsiFile psiFile = psiElement.getContainingFile(); boolean mayHaveClassesInside = false; if (psiFile instanceof PsiJavaFileImpl) { Lexer originalLexer = JavaParserDefinition.createLexer(((PsiJavaFileImpl)psiFile).getLanguageLevel()); FilterLexer lexer = new FilterLexer(originalLexer, new FilterLexer.SetFilter(StdTokenSets.WHITE_SPACE_OR_COMMENT_BIT_SET)); final TextRange range = psiElement.getTextRange(); lexer.start(fileBuffer, range.getStartOffset(), range.getEndOffset()); boolean isInNewExpression = false; boolean isRightAfterNewExpression = false; int angleLevel = 0; int parenLevel = 0; do { IElementType tokenType = lexer.getTokenType(); if (tokenType == null) break; if (tokenType == JavaTokenType.NEW_KEYWORD) { isInNewExpression = true; } else if (tokenType == JavaTokenType.LPARENTH) { if (isInNewExpression) parenLevel++; } else if (tokenType == JavaTokenType.LT) { if (isInNewExpression) angleLevel++; } else if (tokenType == JavaTokenType.GT) { if (isInNewExpression) angleLevel--; } else if (tokenType == JavaTokenType.RPARENTH) { if (isInNewExpression) { parenLevel--; if (parenLevel == 0) { isRightAfterNewExpression = true; } } } else if (tokenType == JavaTokenType.LBRACE) { if (isInNewExpression || isRightAfterNewExpression) { mayHaveClassesInside = true; } } else if (tokenType == JavaTokenType.LBRACKET) { if (parenLevel == 0 && angleLevel == 0) isInNewExpression = false; } else if (tokenType == JavaTokenType.INTERFACE_KEYWORD || tokenType == JavaTokenType.CLASS_KEYWORD || tokenType == JavaTokenType.ENUM_KEYWORD) { mayHaveClassesInside = true; } if (isInNewExpression && isRightAfterNewExpression) { isInNewExpression = false; } else { isRightAfterNewExpression = false; } lexer.advance(); } while (!mayHaveClassesInside); } return mayHaveClassesInside; }
public JavaModuleInsight(@Nullable final ProgressIndicator progress, Set<String> existingModuleNames, Set<String> existingProjectLibraryNames) { super(progress, existingModuleNames, existingProjectLibraryNames); myLexer = JavaParserDefinition.createLexer(LanguageLevel.JDK_1_5); }
/** * Validates a potential package name and returns null if the package name is valid, and otherwise * returns a description for why it is not valid. * <p> * Note that Android package names are more restrictive than general Java package names; * we require at least two segments, limit the character set to [a-zA-Z0-9_] (Java allows any * {@link Character#isLetter(char)} and require that each segment start with a letter (Java allows * underscores at the beginning). * <p> * For details, see core/java/android/content/pm/PackageParser.java#validateName * * @param name the package name * @return null if the package is valid as an Android package name, and otherwise a description for why not */ @Nullable public static String validateAndroidPackageName(@NotNull String name) { if (name.isEmpty()) { return "Package name is missing"; } String packageManagerCheck = validateName(name, true); if (packageManagerCheck != null) { return packageManagerCheck; } // In addition, we have to check that none of the segments are Java identifiers, since // that will lead to compilation errors, which the package manager doesn't need to worry about // (the code wouldn't have compiled) ApplicationManager.getApplication().assertReadAccessAllowed(); Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.JDK_1_5); int index = 0; while (true) { int index1 = name.indexOf('.', index); if (index1 < 0) { index1 = name.length(); } String segment = name.substring(index, index1); lexer.start(segment); if (lexer.getTokenType() != JavaTokenType.IDENTIFIER) { if (lexer.getTokenType() instanceof IKeywordElementType) { return "Package names cannot contain Java keywords like '" + segment + "'"; } if (segment.isEmpty()) { return "Package segments must be of non-zero length"; } return segment + " is not a valid identifier"; } if (index1 == name.length()) { break; } index = index1 + 1; } return null; }
private static Lexer getJavaLexer() { if (ourJavaLexer == null) { ourJavaLexer = JavaParserDefinition.createLexer(LanguageLevel.JDK_1_6); } return ourJavaLexer; }
@SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors"}) public JavaParsingTestCase(@NonNls final String dataPath) { super("psi/"+dataPath, "java", new JavaParserDefinition()); IdeaTestCase.initPlatformPrefix(); }