org.sourcepit.ltk.parser.LittleJParserDelegate.java Source code

Java tutorial

Introduction

Here is the source code for org.sourcepit.ltk.parser.LittleJParserDelegate.java

Source

/*
 * Copyright 2015 Bernd Vogt and others.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.sourcepit.ltk.parser;

import java.util.List;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BufferedTokenStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.RuleNode;
import org.sourcepit.antlr4.eclipse.lang.tests.littlej.LittleJLexer;
import org.sourcepit.antlr4.eclipse.lang.tests.littlej.LittleJParser;
import org.sourcepit.ltk.antlr4.ParserUtils;
import org.sourcepit.ltk.jcomment.CommentLexer;
import org.sourcepit.ltk.jcomment.CommentParser;

public class LittleJParserDelegate implements ParserDelegate {
    @Override
    public ParseResult parse(String input, Class<? extends ParserRuleContext> ruleType) {
        final CharStream charStream = new ANTLRInputStream(input);
        final Lexer lexer = new LittleJLexer(charStream);
        final BufferedTokenStream tokenStream = new CommonTokenStream(lexer);
        final LittleJParser parser = new LittleJParser(tokenStream);
        try {
            final ParseTree parseTree = ParserUtils.parse(parser, ruleType);
            return new ParseResult(lexer, tokenStream, parser, parseTree);
        } catch (RecognitionException e) {
            return new ParseResult(lexer, tokenStream, parser, e);
        }
    }

    @Override
    public ParseResult parseNestedLanguage(Class<? extends Lexer> sourceType, Token token) {
        if (LittleJLexer.class == sourceType) {
            if (LittleJLexer.COMMENT == token.getType()) {
                final CharStream charStream = new ANTLRInputStream(token.getText());
                final Lexer lexer = new CommentLexer(charStream);
                final BufferedTokenStream tokenStream = new CommonTokenStream(lexer);
                final CommentParser parser = new CommentParser(tokenStream);
                try {
                    return new ParseResult(lexer, tokenStream, parser, parser.comment());
                } catch (RecognitionException e) {
                    return new ParseResult(lexer, tokenStream, parser, e);
                }
            }
        }
        return null;
    }

    @Override
    public int getLen(RuleNode parent, Token token, List<Token> hiddenTokensToRight, TokenStream tokenStream) {
        int len = 0;
        for (org.antlr.v4.runtime.Token hiddenToken : hiddenTokensToRight) {
            final String text = hiddenToken.getText();
            if (TokenUtils.isWs(text)) {
                len++;
            } else {
                if (hiddenToken.getCharPositionInLine() > token.getCharPositionInLine()) {
                    len++;
                } else {
                    break;
                }
            }
        }
        return len;
    }
}