List of usage examples for org.antlr.v4.runtime Token HIDDEN_CHANNEL
int HIDDEN_CHANNEL
To view the source code for org.antlr.v4.runtime Token HIDDEN_CHANNEL.
Click Source Link
From source file:org.ballerinalang.langserver.completions.BallerinaCustomErrorStrategy.java
License:Open Source License
/** * Checks whether cursor is within the whitespace region between current token to last token. * @param token Token to be evaluated/*from w w w .ja va2 s. c o m*/ * @param parser Parser Instance * @return true|false */ private boolean isCursorBetweenGivenTokenAndLastNonHiddenToken(Token token, Parser parser) { this.setContextException(parser); boolean isCursorBetween = false; int line = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine(); int character = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter(); Token lastNonHiddenToken = null; for (int tokenIdx = token.getTokenIndex() - 1; tokenIdx >= 0; tokenIdx--) { Token lastToken = parser.getTokenStream().get(tokenIdx); if (lastToken.getChannel() != Token.HIDDEN_CHANNEL) { lastNonHiddenToken = lastToken; break; } } if (lastNonHiddenToken != null) { // Convert the token lines and char positions to zero based indexing int lastNonHiddenTokenLine = lastNonHiddenToken.getLine() - 1; int lastNonHiddenTokenChar = lastNonHiddenToken.getCharPositionInLine(); int tokenLine = token.getLine() - 1; int tokenChar = token.getCharPositionInLine(); if (line >= lastNonHiddenTokenLine && line <= tokenLine) { if (line == lastNonHiddenTokenLine) { isCursorBetween = character >= (lastNonHiddenTokenChar + lastNonHiddenToken.getText().length()); } else { isCursorBetween = line != tokenLine || character <= tokenChar; } } } return isCursorBetween; }
From source file:org.ballerinalang.langserver.completions.providers.contextproviders.ImportDeclarationContextProvider.java
License:Open Source License
@Override public List<CompletionItem> getCompletions(LSContext ctx) { ArrayList<CompletionItem> completionItems = new ArrayList<>(); List<BallerinaPackage> packagesList = new ArrayList<>(); Stream.of(LSPackageLoader.getSdkPackages(), LSPackageLoader.getHomeRepoPackages()) .forEach(packagesList::addAll); List<CommonToken> lhsTokens = ctx.get(CompletionKeys.LHS_TOKENS_KEY); List<CommonToken> lhsDefaultTokens = lhsTokens.stream() .filter(commonToken -> commonToken.getChannel() == Token.DEFAULT_CHANNEL) .collect(Collectors.toList()); List<Integer> lhsDefaultTokenTypes = lhsDefaultTokens.stream().map(CommonToken::getType) .collect(Collectors.toList()); int divIndex = lhsDefaultTokenTypes.indexOf(BallerinaParser.DIV); int importTokenIndex = lhsDefaultTokenTypes.indexOf(BallerinaParser.IMPORT); CommonToken lastToken = CommonUtil.getLastItem(lhsTokens); if (divIndex > -1 && (divIndex == lhsDefaultTokenTypes.size() - 1 || divIndex == lhsDefaultTokenTypes.size() - 2)) { String orgName = lhsDefaultTokens.get(lhsDefaultTokenTypes.indexOf(BallerinaParser.DIV) - 1).getText(); completionItems.addAll(this.getPackageNameCompletions(orgName, packagesList)); } else if (importTokenIndex > -1 && (importTokenIndex == lhsDefaultTokenTypes.size() - 1 || importTokenIndex == lhsDefaultTokenTypes.size() - 2)) { completionItems.addAll(this.getItemsIncludingOrgName(packagesList)); } else if (importTokenIndex > -1 && lhsDefaultTokenTypes.size() >= 2 && (lastToken.getChannel() == Token.HIDDEN_CHANNEL || lhsTokens.get(lhsTokens.size() - 2).getChannel() == Token.HIDDEN_CHANNEL)) { completionItems.add(getAsKeyword()); }/*from w w w . j a v a2 s . c om*/ return completionItems; }
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. *//*from ww w. ja v a2s .c om*/ 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 int getLineLength(int start) { int lineLength; Token token;// w ww. j a v a2 s . c om lineLength = 0; if (start >= super.tokens.size()) return lineLength; // this will not give you a lexer.EOF, so may need to // add a T_EOS token when creating the packed list if the file // ended w/o a T_EOS (now new line at end of the file). do { token = super.get(start + lineLength); lineLength++; } while ((start + lineLength) < super.tokens.size() && (token.getChannel() == Token.HIDDEN_CHANNEL || token.getType() != FortranLexer.T_EOS && token.getType() != FortranLexer.EOF)); return lineLength; }
From source file:org.ledyba.sora.parser.FortranTokenStream.java
License:Open Source License
/** * This will use the super classes methods to keep track of the * start and end of the original line, not the line buffered by * this class./*from w ww . ja v a 2 s .com*/ */ public int findTokenInSuper(int lineStart, int desiredToken) { int lookAhead = 0; int tk, channel; /*****OBSOLETE NOTE: returning -1 is painful when looking for T_EOS // if this line is a comment, skip scanning it if (super.LA(1) == FortranLexer.LINE_COMMENT) { return -1; } OBSOLETE*****/ do { // lookAhead was initialized to 0 lookAhead++; // get the token Token token = LT(lookAhead); tk = token.getType(); channel = token.getChannel(); // continue until find what looking for or reach end } while ((tk != FortranLexer.EOF && tk != FortranLexer.T_EOS && tk != desiredToken) || channel == Token.HIDDEN_CHANNEL); if (tk == desiredToken) { // we found a what we wanted to return lookAhead; } return -1; }
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;//from w ww .ja v a2 s. 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; } } }