Package com.dci.intellij.dbn.language.common.element.parser

Examples of com.dci.intellij.dbn.language.common.element.parser.ParserBuilder


    }

    @NotNull
    public ASTNode parse(IElementType rootElementType, PsiBuilder psiBuilder, String parseRootId) {
        ParserContext context = new ParserContext(psiBuilder, languageDialect);
        ParserBuilder builder = context.getBuilder();
        if (parseRootId == null ) parseRootId = defaultParseRootId;
        builder.setDebugMode(SettingsUtil.isDebugEnabled);
        PsiBuilder.Marker marker = builder.mark(null);
        NamedElementType root =  elementTypes.getNamedElementType(parseRootId);
        if (root == null) {
            root = elementTypes.getRootElementType();
        }

        boolean advancedLexer = false;
        ParsePathNode rootParseNode = new ParsePathNode(root, null, 0, 0);

        try {
            while (!builder.eof()) {
                int currentOffset =  builder.getCurrentOffset();
                root.getParser().parse(rootParseNode, true, 0, context);
                if (currentOffset == builder.getCurrentOffset()) {
                    TokenType tokenType = (TokenType) builder.getTokenType();
                    /*if (tokenType.isChameleon()) {
                        PsiBuilder.Marker injectedLanguageMarker = builder.mark();
                        builder.advanceLexer();
                        injectedLanguageMarker.done((IElementType) tokenType);
                    }
                    else*/ if (tokenType instanceof ChameleonTokenType) {
                        PsiBuilder.Marker injectedLanguageMarker = builder.mark(null);
                        builder.advanceLexer(rootParseNode);
                        injectedLanguageMarker.done((IElementType) tokenType);
                    } else {
                        builder.advanceLexer(rootParseNode);
                    }
                    advancedLexer = true;
                }
            }
        } catch (ParseException e) {
            while (!builder.eof()) {
                builder.advanceLexer(rootParseNode);
                advancedLexer = true;
            }
        } catch (StackOverflowError e) {
            marker.rollbackTo();
            marker = builder.mark(null);
            while (!builder.eof()) {
                builder.advanceLexer(rootParseNode);
                advancedLexer = true;
            }

        }

        if (!advancedLexer) builder.advanceLexer(rootParseNode);
        marker.done(rootElementType);
        return builder.getTreeBuilt();
    }
View Full Code Here


        return errDescription;
    }


    public static void updateBuilderError(Set<TokenType> expectedTokens, ParserContext context) {
        ParserBuilder builder = context.getBuilder();
        int offset = builder.getCurrentOffset();
        if (ParseBuilderErrorWatcher.show(offset, context.getTimestamp())) {

            Set<String> tokenDescriptions = new THashSet<String>(expectedTokens.size());
            for (TokenType tokenType : expectedTokens) {
                if (tokenType.isFunction()) {
                    tokenDescriptions.add("function");
                    continue;
                }
                String value = tokenType.getValue();
                String description =
                        tokenType.isIdentifier() ? "identifier" :
                        StringUtil.isNotEmptyOrSpaces(value) ? value.toUpperCase() : tokenType.getTypeName();

                tokenDescriptions.add(description);
            }

            String [] tokenDesc = tokenDescriptions.toArray(new String[tokenDescriptions.size()]);
            Arrays.sort(tokenDesc);

            StringBuilder buffer = new StringBuilder("expected");
            buffer.append(tokenDesc.length > 1 ? " one of the following: " : ": ");

            for (int i=0; i<tokenDesc.length; i++) {
                buffer.append(tokenDesc[i]);
                if (i < tokenDesc.length - 1) {
                    buffer.append(" ");
                }
            }
            //buffer.append("\n");
            builder.error(buffer.toString());
        }
    }
View Full Code Here

    public BlockElementTypeParser(BlockElementType elementType) {
        super(elementType);
    }

    public ParseResult parse(@NotNull ParsePathNode parentNode, boolean optional, int depth, ParserContext context) throws ParseException {
        ParserBuilder builder = context.getBuilder();
        PsiBuilder.Marker marker = builder.mark(null);
        ParseResult result = super.parse(parentNode, optional, depth, context);
        if (result.getType() == ParseResultType.NO_MATCH) {
            builder.markerDrop(marker);
        } else {
            builder.markerDone(marker, getElementType());
        }
        return result.getType() == ParseResultType.NO_MATCH ?
                ParseResult.createNoMatchResult() :
                ParseResult.createFullMatchResult(result.getMatchedTokens());
    }
View Full Code Here

    public ExecVariableElementTypeParser(ExecVariableElementType elementType) {
        super(elementType);
    }

    public ParseResult parse(@NotNull ParsePathNode parentNode, boolean optional, int depth, ParserContext context) throws ParseException {
        ParserBuilder builder = context.getBuilder();
        logBegin(builder, optional, depth);
        TokenType tokenType = builder.getTokenType();
        if (tokenType != null && !tokenType.isChameleon()){
            if (tokenType.isVariable()) {
                PsiBuilder.Marker marker = builder.mark(null);
                builder.advanceLexer(parentNode);
                return stepOut(marker, depth, ParseResultType.FULL_MATCH, 1, null, context);
            }
        }
        return stepOut(null, depth, ParseResultType.NO_MATCH, 0, null, context);
    }
View Full Code Here

    public NamedElementTypeParser(NamedElementType elementType) {
        super(elementType);
    }

    public ParseResult parse(@NotNull ParsePathNode parentNode, boolean optional, int depth, ParserContext context) throws ParseException {
        ParserBuilder builder = context.getBuilder();
        if (isRecursive(parentNode, builder.getCurrentOffset(), 2)) {
            return ParseResult.createNoMatchResult();
        }
        return super.parse(parentNode, optional, depth, context);
    }
View Full Code Here

    public SequenceElementTypeParser(ET elementType) {
        super(elementType);
    }

    public ParseResult parse(@NotNull ParsePathNode parentNode, boolean optional, int depth, ParserContext context) throws ParseException {
        ParserBuilder builder = context.getBuilder();
        logBegin(builder, optional, depth);
        SequenceElementType elementType = getElementType();

        ParsePathNode node = createParseNode(parentNode, builder.getCurrentOffset());
        PsiBuilder.Marker marker = builder.mark(node);
        int matches = 0;
        int matchedTokens = 0;

        TokenType tokenType = builder.getTokenType();
        boolean isDummyToken = isDummyToken(builder.getTokenText());
        boolean isSuppressibleReservedWord =
                !elementType.is(ElementTypeAttribute.STATEMENT) &&
                isSuppressibleReservedWord(tokenType, node);


        if (tokenType != null && !tokenType.isChameleon() && (isDummyToken || isSuppressibleReservedWord || elementType.getLookupCache().canStartWithToken(tokenType))) {
            ElementType[] elementTypes = elementType.getElementTypes();
            while (node.getCurrentSiblingIndex() < elementTypes.length) {
                int index = node.getCurrentSiblingIndex();
                // is end of document
                if (tokenType == null || tokenType.isChameleon()) {
                    ParseResultType resultType =
                            elementType.isOptional(index) && (elementType.isLast(index) || elementType.isOptionalFromIndex(index)) ? ParseResultType.FULL_MATCH :
                            !elementType.isFirst(index) && !elementType.isOptionalFromIndex(index) && !elementType.isExitIndex(index) ? ParseResultType.PARTIAL_MATCH : ParseResultType.NO_MATCH;
                    return stepOut(marker, depth, resultType, matchedTokens, node, context);
                }

                ParseResult result = ParseResult.createNoMatchResult();
                // current token can still be part of the iterated element.
                //if (elementTypes[i].containsToken(tokenType)) {
                if (isDummyToken || elementTypes[index].getLookupCache().canStartWithToken(tokenType) || isSuppressibleReservedWord(tokenType, node)) {

                    //node = node.createVariant(builder.getCurrentOffset(), i);
                    result = elementTypes[index].getParser().parse(node, elementType.isOptional(index), depth + 1, context);

                    if (result.isMatch()) {
                        matchedTokens = matchedTokens + result.getMatchedTokens();
                        tokenType = builder.getTokenType();
                        isDummyToken = isDummyToken(builder.getTokenText());
                        matches++;
                    }
                }

                // not matched and not optional
                if (result.isNoMatch() && !elementType.isOptional(index)) {
                    boolean isWeakMatch = matches < 2 && matchedTokens < 3 && index > 1 && ignoreFirstMatch();
                   
                    if (elementType.isFirst(index) || elementType.isExitIndex(index) || isWeakMatch || matches == 0) {
                        //if (isFirst(i) || isExitIndex(i)) {
                        return stepOut(marker, depth, ParseResultType.NO_MATCH, matchedTokens, node, context);
                    }

                    index = advanceLexerToNextLandmark(node, context);

                    if (index <= 0) {
                        // no landmarks found or landmark in parent found
                        return stepOut(marker, depth, ParseResultType.PARTIAL_MATCH, matchedTokens, node, context);
                    } else {
                        // local landmarks found

                        tokenType = builder.getTokenType();
                        isDummyToken = isDummyToken(builder.getTokenText());

                        node.setCurrentSiblingIndex(index);
                        continue;
                    }
                }

                // if is last element
                if (elementType.isLast(index)) {
                    //matches == 0 reaches this stage only if all sequence elements are optional
                    ParseResultType resultType = matches == 0 ? ParseResultType.NO_MATCH : ParseResultType.FULL_MATCH;
                    return stepOut(marker, depth, resultType, matchedTokens, node, context);
                }
                node.incrementIndex(builder.getCurrentOffset());
            }
        }

        return stepOut(marker, depth, ParseResultType.NO_MATCH, matchedTokens, node, context);
    }
View Full Code Here

        return false;
    }

    @Override
    protected ParseResult stepOut(PsiBuilder.Marker marker, int depth, ParseResultType resultType, int matchedTokens, ParsePathNode node, ParserContext context) {
        ParserBuilder builder = context.getBuilder();
        if (resultType == ParseResultType.NO_MATCH) {
            builder.markerRollbackTo(marker, node);
        } else {
            if (getElementType() instanceof BlockElementType)
                builder.markerDrop(marker); else
                builder.markerDone(marker, getElementType(), node);
        }

        return super.stepOut(null, depth, resultType, matchedTokens, node, context);
    }   
View Full Code Here

        return super.stepOut(null, depth, resultType, matchedTokens, node, context);
    }   

    private int advanceLexerToNextLandmark(ParsePathNode node, ParserContext context) throws ParseException {
        int siblingPosition = node.getCurrentSiblingIndex();
        ParserBuilder builder = context.getBuilder();
        PsiBuilder.Marker marker = builder.mark(null);
        SequenceElementType elementType = getElementType();
        ParseBuilderErrorHandler.updateBuilderError(elementType.getFirstPossibleTokensFromIndex(siblingPosition), context);

        TokenType tokenType = builder.getTokenType();
        siblingPosition++;
        while (tokenType != null) {
            int newIndex = getLandmarkIndex(tokenType, siblingPosition, node);

            // no landmark hit -> spool the builder
            if (newIndex == 0) {
                builder.advanceLexer(node);
                tokenType = builder.getTokenType();
            } else {
                //builder.markerDone(marker, getElementBundle().getUnknownElementType());
                marker.error("Unrecognized statement");
                return newIndex;
            }
View Full Code Here

    public QualifiedIdentifierElementTypeParser(QualifiedIdentifierElementType elementType) {
        super(elementType);
    }

    public ParseResult parse(@NotNull ParsePathNode parentNode, boolean optional, int depth, ParserContext context) throws ParseException {
        ParserBuilder builder = context.getBuilder();
        logBegin(builder, optional, depth);
        ParsePathNode node = createParseNode(parentNode, builder.getCurrentOffset());

        TokenElementType separatorToken = getElementType().getSeparatorToken();

        PsiBuilder.Marker marker = builder.mark(node);
        int matchedTokens = 0;

        QualifiedIdentifierVariant variant = getMostProbableParseVariant(builder, node);
        if (variant != null) {
            LeafElementType[] elementTypes = variant.getLeafs();

            int currentSiblingPosition = 0;
            for (LeafElementType elementType : elementTypes) {
                ParseResult result = elementType.getParser().parse(node, true, depth + 1, context);
                if (result.isNoMatch()) breakelse matchedTokens = matchedTokens + result.getMatchedTokens();

                if (elementType != elementTypes[elementTypes.length -1])  {
                    result = separatorToken.getParser().parse(node, true, depth + 1, context);
                    if (result.isNoMatch()) break; else matchedTokens = matchedTokens + result.getMatchedTokens();
                }
                node.incrementIndex(builder.getCurrentOffset());
            }

            if (variant.isIncomplete()) {
                Set<TokenType> expected = new THashSet<TokenType>();
                expected.add(separatorToken.getTokenType());
View Full Code Here

    public OneOfElementTypeParser(OneOfElementType elementType) {
        super(elementType);
    }

    public ParseResult parse(@NotNull ParsePathNode parentNode, boolean optional, int depth, ParserContext context) throws ParseException {
        ParserBuilder builder = context.getBuilder();
        logBegin(builder, optional, depth);
        ParsePathNode node = createParseNode(parentNode, builder.getCurrentOffset());
        PsiBuilder.Marker marker = builder.mark(node);

        getElementType().sort();
        TokenType tokenType = builder.getTokenType();

        if (tokenType!= null && !tokenType.isChameleon()) {
            String tokenText = builder.getTokenText();
            // TODO !!!! if elementType is an identifier: then BUILD VARIANTS!!!
            for (ElementType elementType : getElementType().getPossibleElementTypes()) {
                if (isDummyToken(tokenText) || elementType.getLookupCache().canStartWithToken(tokenType) || isSuppressibleReservedWord(tokenType, node)) {
                    ParseResult result = elementType.getParser().parse(node, true, depth + 1, context);
                    if (result.isMatch()) {
View Full Code Here

TOP

Related Classes of com.dci.intellij.dbn.language.common.element.parser.ParserBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.