aboutsummaryrefslogtreecommitdiffstats
path: root/integration/intellij/src/main/java/ai/vespa/intellij/schema/utils/AST.java
blob: f7b3ba33c33a676cbe1eff09adf215eb583c8fa4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.intellij.schema.utils;

import ai.vespa.intellij.schema.psi.SdTypes;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;

import java.util.ArrayList;
import java.util.List;

/**
 * AST navigation tools.
 *
 * @author bratseth
 */
public class AST {

    /** Returns the nodes following "inherits" in the given element. */
    public static List<ASTNode> inherits(PsiElement element) {
        Tokens tokens = Tokens.of(element);
        tokens.requireElement();
        tokens.requireWhitespace();
        tokens.require(SdTypes.IDENTIFIER_VAL, SdTypes.IDENTIFIER_WITH_DASH_VAL);
        if ( ! tokens.skipWhitespace()) return List.of();
        if ( ! tokens.skip(SdTypes.INHERITS)) return List.of();
        tokens.requireWhitespace();
        List<ASTNode> inherited = new ArrayList<>();
        do {
            inherited.add(tokens.require(SdTypes.IDENTIFIER_VAL, SdTypes.IDENTIFIER_WITH_DASH_VAL));
            tokens.skipWhitespace();
            if ( ! tokens.skip(SdTypes.COMMA)) break;
            tokens.skipWhitespace();
        } while (true);
        return inherited;
    }

}