Search in sources :

Example 16 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class JavaParserUtil method createBuilder.

@NotNull
public static PsiBuilder createBuilder(final ASTNode chameleon) {
    final PsiElement psi = chameleon.getPsi();
    assert psi != null : chameleon;
    final Project project = psi.getProject();
    CharSequence text;
    if (TreeUtil.isCollapsedChameleon(chameleon)) {
        text = chameleon.getChars();
    } else {
        text = psi.getUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY);
        if (text == null)
            text = chameleon.getChars();
    }
    final PsiBuilderFactory factory = PsiBuilderFactory.getInstance();
    final LanguageLevel level = PsiUtil.getLanguageLevel(psi);
    final Lexer lexer = JavaParserDefinition.createLexer(level);
    Language language = psi.getLanguage();
    if (!language.isKindOf(JavaLanguage.INSTANCE))
        language = JavaLanguage.INSTANCE;
    final PsiBuilder builder = factory.createBuilder(project, chameleon, lexer, language, text);
    setLanguageLevel(builder, level);
    return builder;
}
Also used : Project(com.intellij.openapi.project.Project) Lexer(com.intellij.lexer.Lexer) JavaLanguage(com.intellij.lang.java.JavaLanguage) LanguageLevel(com.intellij.pom.java.LanguageLevel) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class JavaSourceRootDetectionUtil method getPackageName.

@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);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Lexer(com.intellij.lexer.Lexer) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class PsiElementFactoryImpl method createDummyHolder.

@NotNull
@Override
public PsiElement createDummyHolder(@NotNull final String text, @NotNull final IElementType type, @Nullable final PsiElement context) {
    final DummyHolder result = DummyHolderFactory.createHolder(myManager, context);
    final FileElement holder = result.getTreeElement();
    final Language language = type.getLanguage();
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
    assert parserDefinition != null : "No parser definition for language " + language;
    final Project project = myManager.getProject();
    final Lexer lexer = parserDefinition.createLexer(project);
    final PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, holder, lexer, language, text);
    final ASTNode node = parserDefinition.createParser(project).parse(type, builder);
    holder.rawAddChildren((TreeElement) node);
    final PsiElement psi = node.getPsi();
    assert psi != null : text;
    return psi;
}
Also used : Project(com.intellij.openapi.project.Project) JavaLexer(com.intellij.lang.java.lexer.JavaLexer) Lexer(com.intellij.lexer.Lexer) FileElement(com.intellij.psi.impl.source.tree.FileElement) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class LightKeyword method getTokenType.

@Override
public IElementType getTokenType() {
    Lexer lexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST);
    lexer.start(myText);
    return lexer.getTokenType();
}
Also used : Lexer(com.intellij.lexer.Lexer)

Example 20 with Lexer

use of com.intellij.lexer.Lexer in project intellij-community by JetBrains.

the class IElementTypeTest method testCount.

// load all parser definitions, instantiate all lexers & parsers to initialize all IElementType constants
@SuppressWarnings("UnusedDeclaration")
public void testCount() throws Exception {
    int count = IElementType.getAllocatedTypesCount();
    System.out.println("Preloaded: " + count + " element types");
    LanguageExtensionPoint[] extensions = Extensions.getExtensions(new ExtensionPointName<LanguageExtensionPoint>("com.intellij.lang.parserDefinition"));
    System.out.println("ParserDefinitions: " + extensions.length);
    THashMap<Language, String> languageMap = new THashMap<>();
    languageMap.put(Language.ANY, "platform");
    final TObjectIntHashMap<String> map = new TObjectIntHashMap<>();
    for (LanguageExtensionPoint e : extensions) {
        String key = e.getPluginDescriptor().getPluginId().getIdString();
        int curCount = IElementType.getAllocatedTypesCount();
        ParserDefinition definition = (ParserDefinition) e.getInstance();
        IFileElementType type = definition.getFileNodeType();
        Language language = type.getLanguage();
        languageMap.put(language, key);
        if (language.getBaseLanguage() != null && !languageMap.containsKey(language.getBaseLanguage())) {
            languageMap.put(language.getBaseLanguage(), key);
        }
        try {
            Lexer lexer = definition.createLexer(getProject());
            PsiParser parser = definition.createParser(getProject());
        } catch (UnsupportedOperationException e1) {
        }
    // language-based calculation: per-class-loading stuff commented
    //int diff = IElementType.getAllocatedTypesCount() - curCount;
    //map.put(key, map.get(key) + diff);
    }
    // language-based calculation
    count = IElementType.getAllocatedTypesCount();
    for (short i = 0; i < count; i++) {
        IElementType type = IElementType.find(i);
        Language language = type.getLanguage();
        String key = null;
        for (Language cur = language; cur != null && key == null; cur = cur.getBaseLanguage()) {
            key = languageMap.get(cur);
        }
        key = StringUtil.notNullize(key, "unknown");
        map.put(key, map.get(key) + 1);
    //if (key.equals("unknown")) System.out.println(type +"   " + language);
    }
    System.out.println("Total: " + IElementType.getAllocatedTypesCount() + " element types");
    // Show per-plugin statistics
    Object[] keys = map.keys();
    Arrays.sort(keys, (o1, o2) -> map.get((String) o2) - map.get((String) o1));
    int sum = 0;
    for (Object key : keys) {
        int value = map.get((String) key);
        if (value == 0)
            continue;
        sum += value;
        System.out.println("  " + key + ": " + value);
    }
    // leave some index-space for plugin developers
    assertTrue(IElementType.getAllocatedTypesCount() < 10000);
    assertEquals(IElementType.getAllocatedTypesCount(), sum);
// output on 11.05.2012
//   Preloaded: 3485 types
//   95 definitions
//   Total: 7694 types
// 14.05.2015:
// Preloaded: 4106 element types
// ParserDefinitions: 128
// Total: 8864 element types
// 19.04.2016:
// Preloaded: 4397 element types
// ParserDefinitions: 135
// Total: 9231 element types
}
Also used : Lexer(com.intellij.lexer.Lexer) THashMap(gnu.trove.THashMap) TObjectIntHashMap(gnu.trove.TObjectIntHashMap)

Aggregations

Lexer (com.intellij.lexer.Lexer)53 IElementType (com.intellij.psi.tree.IElementType)17 Project (com.intellij.openapi.project.Project)11 NotNull (org.jetbrains.annotations.NotNull)9 Nullable (org.jetbrains.annotations.Nullable)9 PsiElement (com.intellij.psi.PsiElement)6 TextRange (com.intellij.openapi.util.TextRange)5 LayeredLexer (com.intellij.lexer.LayeredLexer)4 CfmlLexer (com.intellij.coldFusion.model.lexer.CfmlLexer)3 JavaLexer (com.intellij.lang.java.lexer.JavaLexer)3 FilterLexer (com.intellij.lexer.FilterLexer)3 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)3 LanguageLevel (com.intellij.pom.java.LanguageLevel)3 Language (com.intellij.lang.Language)2 PropertiesLexer (com.intellij.lang.properties.parsing.PropertiesLexer)2 FileType (com.intellij.openapi.fileTypes.FileType)2 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)2 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)2 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)2 PsiFile (com.intellij.psi.PsiFile)2