Example usage for org.antlr.v4.runtime Token DEFAULT_CHANNEL

List of usage examples for org.antlr.v4.runtime Token DEFAULT_CHANNEL

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Token DEFAULT_CHANNEL.

Prototype

int DEFAULT_CHANNEL

To view the source code for org.antlr.v4.runtime Token DEFAULT_CHANNEL.

Click Source Link

Document

All tokens go to the parser (unless skip() is called in that rule) on a particular "channel".

Usage

From source file:org.ledyba.sora.parser.FortranTokenStream.java

License:Open Source License

/**
 * Create a subset list of the non-whitespace tokens in the current line.
 *///  w ww  .  java2s . c  o m
private ArrayList<Token> createPackedList() {
    int i = 0;
    Token tk = null;

    ArrayList<Token> pList = new ArrayList<>(this.lineLength + 1);

    for (i = 0; i < currLine.size(); i++) {
        tk = getTokenFromCurrLine(i);
        try {
            if (tk.getChannel() != Token.HIDDEN_CHANNEL) {
                pList.add(tk);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    // need to make sure the line was terminated with a T_EOS.  this may
    // not happen if we're working on a file that ended w/o a newline
    Token last = pList.get(pList.size() - 1);
    if (last.getType() != FortranLexer.T_EOS) {
        Pair<TokenSource, CharStream> src = new Pair<>(last.getTokenSource(), last.getInputStream());
        FortranToken eos = new FortranToken(src, FortranLexer.T_EOS, Token.DEFAULT_CHANNEL,
                last.getTokenIndex(), last.getTokenIndex() + 1);
        eos.setText("\n");
        packedList.add(eos);
    }

    return pList;
}

From source file:org.ledyba.sora.parser.FortranTokenStream.java

License:Open Source License

public void addTokenTo(int index, int line, int col, int tokenType, String tokenText) {
    try {//from www .j a  va2 s  .  co m
        // for example:
        // index = 1
        // packedList == label T_CONTINUE T_EOS  (size is 3)
        // newTokenList.size() == 22
        // 22-3+1=20
        // so, inserted between the label and T_CONTINUE
        Token last = this.tokens.get(index <= 0 ? 0 : index - 1);
        Pair<TokenSource, CharStream> src = new Pair<>(last.getTokenSource(), last.getInputStream());
        FortranToken token = new FortranToken(src, tokenType, Token.DEFAULT_CHANNEL, last.getStopIndex(),
                last.getStopIndex());
        token.setText(tokenText == null ? "" : tokenText);
        token.setLine(line);
        token.setCharPositionInLine(col);
        this.packedList.add(index, token);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

    return;
}

From source file:org.ledyba.sora.parser.FortranTokenStream.java

License:Open Source License

public void addToken(int type, String text, int line, int col) {
    Token last = this.tokens.get(this.tokens.size() - 1);
    Pair<TokenSource, CharStream> src = new Pair<>(last.getTokenSource(), last.getInputStream());
    FortranToken token = new FortranToken(src, type, Token.DEFAULT_CHANNEL, 0, 0);
    token.setLine(line);//from ww  w  .j  a  v  a 2  s.  c  om
    token.setCharPositionInLine(col);
    this.addToken(token);
}

From source file:org.ledyba.sora.parser.FortranTokenStream.java

License:Open Source License

public void addTokenTo(int index, int type, String text, int line, int col) {
    Token last = this.tokens.get(index);
    Pair<TokenSource, CharStream> src = new Pair<>(last.getTokenSource(), last.getInputStream());
    FortranToken token = new FortranToken(src, type, Token.DEFAULT_CHANNEL, 0, 0);
    token.setLine(line);/*w w  w .java 2 s  .c  o m*/
    token.setCharPositionInLine(col);
    this.addTokenTo(index, token);
}

From source file:org.sourcepit.ltk.parser.AbstractParseNode.java

License:Apache License

public List<ParseNode> getVisibleChildren() {
    final List<ParseNode> visibleChildren = new ArrayList<>();
    for (ParseNode child : getChildren()) {
        if (child.isRule() || ((Terminal) child).getToken().getChannel() == Token.DEFAULT_CHANNEL) {
            visibleChildren.add(child);//from   ww w .j  a va 2 s  . co m
        }
    }
    return visibleChildren;
}

From source file:org.tvl.goworks.editor.go.parser.GoLexer.java

License:Open Source License

@Override
public Token nextToken() {
    Token result;/*from  w w w . ja  v  a2s . c o  m*/
    if (deferredEol != null) {
        result = deferredEol;
        deferredEol = null;
    } else {
        result = super.nextToken();
    }

    switch (result.getType()) {
    case IDENTIFIER:
    case INT_LITERAL:
    case FLOAT_LITERAL:
    case IMAGINARY_LITERAL:
    case CharLiteral:
    case StringLiteral:
    case Break:
    case Continue:
    case Fallthrough:
    case Return:
    case Inc:
    case Dec:
    case RightParen:
    case RightBrack:
    case RightBrace:
        insertSemicolonAtEol = true;
        break;

    default:
        if (result.getChannel() == Token.DEFAULT_CHANNEL) {
            insertSemicolonAtEol = false;
        }
        break;
    }

    return result;
}

From source file:org.wso2.ballerinalang.compiler.parser.BLangWSPreservingParserListener.java

License:Open Source License

private void addWSFromRange(Stack<Whitespace> ws, TokenRange range) {
    int rangeStart = range.from;
    int rangeEnd = range.to;
    boolean lastTokenWasHidden = true;

    Token previousNonWS = null;// w  w w. ja v  a 2s . c  om
    for (int j = rangeEnd - 1; j >= -1; j--) {
        if (j == -1) {
            if (!lastTokenWasHidden) {
                // capturing (non-exiting) WS at the start of range, if the range starts at 0.
                // this happens if the file starts with a non-ws token.
                pushWS(ws, previousNonWS, "");
            }
            break;
        }

        Token token = this.tokenStream.get(j);
        if (previousNonWS == null && token.getChannel() == Token.HIDDEN_CHANNEL) {
            continue;
        }
        if (token.getChannel() == Token.DEFAULT_CHANNEL) {
            // we need to capture WS before the start of a range,
            // therefor only break after previous range's first non-WS.
            if (j < rangeStart) {
                if (!lastTokenWasHidden) {
                    pushWS(ws, previousNonWS, "");
                    // capturing (non-exiting) WS at the start of range (when there is no space between ranges).
                }
                break;
            }
            // capturing (non-exiting) WS between two default tokens.
            if (!lastTokenWasHidden) {
                pushWS(ws, previousNonWS, "");
            }
            lastTokenWasHidden = false;
            previousNonWS = token;
        } else {
            if (lastTokenWasHidden) {
                // merging adjacent WS tokens.
                ws.peek().prependWS(token.getText());
            } else {
                // capturing (non-zero-len) WS.
                pushWS(ws, previousNonWS, token.getText());
            }
            lastTokenWasHidden = true;
        }
    }
}

From source file:x10dt.ui.contentProposer.X10ContentProposer.java

License:Open Source License

public ICompletionProposal[] getContentProposals(IParseController controller, int offset, ITextViewer viewer) {

    ArrayList<ICompletionProposal> list = new ArrayList<ICompletionProposal>();

    CommonTokenStream tokens = ((ParseController) controller).getTokens();

    Token tokenToComplete = null;//w ww  .ja va2  s .  c o m
    Token previousToken = null;
    Token nextToken = null;

    int index = 0;
    for (Token t : tokens.getTokens()) {
        index++;
        if (t.getChannel() == Token.DEFAULT_CHANNEL) {
            if (t.getStartIndex() <= offset && t.getStopIndex() + 1 >= offset) {
                tokenToComplete = t;
                break;
            }
            if (t.getStartIndex() > offset) {
                break;
            }
            previousToken = t;
        }
    }

    if (tokenToComplete == null) {
        nextToken = tokens.getTokens().get(index);
    }

    String prefix = tokenToComplete == null ? ""
            : computePrefixOfToken(tokenToComplete, offset, (ParseController) controller);

    PolyglotNodeLocator locator = new PolyglotNodeLocator(
            controller.getProject()/*,((ParseController) controller).getLexStream()*/);
    Node currentAst = (Node) controller.getCurrentAst();
    Node node = tokenToComplete != null
            ? (Node) locator.findNode(currentAst, tokenToComplete.getStartIndex(),
                    tokenToComplete.getStopIndex())
            : null;
    Node previousNode = (previousToken != null)
            ? (Node) locator.findNode(currentAst, previousToken.getStartIndex(), previousToken.getStopIndex())
            : null;
    Node nextNode = (nextToken != null)
            ? (Node) locator.findNode(currentAst, nextToken.getStartIndex(), nextToken.getStopIndex())
            : null;

    if (node != null && node instanceof Eval && tokenToComplete.getType() == X10Parser.DOT) {
        Type type = ((Eval_c) node).expr().type();
        if (type != null && type.isReference()) {
            getCandidates((ObjectType) type, list, prefix, offset, true);
        }
    } else if (node != null && node instanceof Id && previousNode instanceof Field) {
        Type type = ((Field_c) previousNode).target().type();
        if (type != null && type.isReference()) {
            getCandidates((ObjectType) type, list, prefix, offset, true);
        }
    } else if (node != null && node instanceof Id && previousNode instanceof Call) {
        Type type = ((Call_c) previousNode).target().type();
        if (type != null && type.isReference()) {
            getCandidates((ObjectType) type, list, prefix, offset, true);
        }

        //The next case completes an Id with names in scope  
    } else if (node != null && node instanceof Id) {
        Node n = (node instanceof Id) ? node : previousNode;
        String pref = (node instanceof Id) ? prefix
                : computePrefixOfToken(previousToken, offset, (ParseController) controller);
        addNamesInScope(currentAst, n, pref, list, offset, !EMPTY_PREFIX_MATCHES);

    } else if (node == null && previousNode != null) { //Display templates, names in scope -- index < 0 when we are at a white space or comment
        Node location = location(previousNode, nextNode, locator, currentAst);
        if (location instanceof Block && (justAfter(X10Parser.SEMICOLON, previousToken)
                || justAfter(X10Parser.RBRACE, previousToken) || justAfter(X10Parser.LBRACE, previousToken))) { //Statement context. 
            addTemplateProposals(offset, viewer, list, prefix, fTemplates);
            //addNamesInScope(currentAst, node, prefix, list, offset, EMPTY_PREFIX_MATCHES);
        } else if (justAfter(X10Parser.EQUAL, previousToken)
                && (location instanceof Assign || location instanceof LocalDecl)) {
            Template[] templates = new Template[] { fAtExpressionTemplate, fCoercionTemplate, fRegion1DTemplate,
                    fRegion2DTemplate };
            addTemplateProposals(offset, viewer, list, prefix, templates);
        } else if (location instanceof ClassBody) { //Class context
            Template[] templates = new Template[] { fVariableDeclaration, fValueDeclaration, fConstDeclaration,
                    fPropertyDeclaration, fMainMethod, fMethodTemplate, fConstructorTemplate, fClassTemplate,
                    fStructTemplate, fDependentTypeDeclaration, };
            addTemplateProposals(offset, viewer, list, prefix, templates);

        }
    }

    return (ICompletionProposal[]) list.toArray(new ICompletionProposal[list.size()]);
}