當前位置: 首頁>>代碼示例>>Java>>正文


Java CommonToken類代碼示例

本文整理匯總了Java中org.antlr.runtime.CommonToken的典型用法代碼示例。如果您正苦於以下問題:Java CommonToken類的具體用法?Java CommonToken怎麽用?Java CommonToken使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


CommonToken類屬於org.antlr.runtime包,在下文中一共展示了CommonToken類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: announce

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
@Override
protected void announce(Token start, Token stop, AbstractElement element) {
	if (start != null && start != Token.EOF_TOKEN) {
		if (start == stop) {
			announce(start, element);
		} else {
			CommonToken castedStart = (CommonToken) start;
			if (stop == null) { // possible error condition
				if (start.getTokenIndex() == state.lastErrorIndex) {
					return;
				}
			}
			CommonToken castedEnd = (CommonToken) stop;
			Integer newType = rewriter.rewrite(castedStart, element);
			if (newType != null && castedEnd != null && castedEnd != Token.EOF_TOKEN) {
				LazyTokenStream castedInput = (LazyTokenStream) this.input;
				for (int i = castedStart.getTokenIndex() + 1; i < castedEnd.getTokenIndex(); i++) {
					Token token = castedInput.get(i);
					if (token.getChannel() != Token.HIDDEN_CHANNEL)
						token.setType(newType);
				}
				castedEnd.setType(newType);
			}
		}
	}
}
 
開發者ID:eclipse,項目名稱:n4js,代碼行數:27,代碼來源:InternalHighlightingParser.java

示例2: test_getTrimmedNodeRange

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
public void test_getTrimmedNodeRange() {
    String source = " hello! ";
    //               012345678
    CommonToken token = new CommonToken(Css3Lexer.IDENT);
    token.setText(source);
    token.setStartIndex(0);
    token.setStopIndex(7); //len - 1 -> points to last char not the end!
    
    Node node = new TokenNode(source, token);
    
    assertEquals(" hello! ", node.image().toString());
    int[] result = NodeUtil.getTrimmedNodeRange(node);

    assertEquals(1, result[0]);
    assertEquals(7, result[1]);
    
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:18,代碼來源:NodeUtilTest.java

示例3: toQualName

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/** Creates a qualified name tree by extending an existing one
 * with an additional fragment in front.
 * @param init token holding the additional level text
 * @param subTree the existing tree; may be {@code null}, in which case
 * the result is calculated using {@link #toQualName(Token,Token)} with first
 * argument {@code null}
 */
CommonTree toQualName(Token init, CtrlTree subTree) {
    CtrlTree result;
    if (subTree == null || this.namespace.hasErrors()) {
        result = toQualName(null, init);
    } else {
        Token subTop = subTree.getToken();
        QualName qualName = subTree.getQualName()
            .nest(getText(init));
        CommonToken top = new CommonToken(subTop.getType(), qualName.toString());
        top.setLine(init.getLine());
        top.setTokenIndex(init.getTokenIndex());
        result = new CtrlTree(top);
        result.setQualName(qualName);
        result.addChild(subTree.getChild(0));
    }
    return result;
}
 
開發者ID:meteoorkip,項目名稱:JavaGraph,代碼行數:25,代碼來源:CtrlHelper.java

示例4: qualify

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/**
 * Tests if the rule name is qualified;
 * if not, first tries to look it up in the import map, and if that fails,
 * prefixes it with the package name.
 */
CtrlTree qualify(CtrlTree ruleNameToken) {
    CtrlTree result = ruleNameToken;
    QualName qualName = ruleNameToken.getQualName();
    if (!this.namespace.hasErrors() && qualName.parent()
        .isTop()) {
        String simpleName = qualName.last();
        Map<String,QualName> importMap = getNamespace().getImportMap();
        if (importMap.containsKey(simpleName)) {
            qualName = importMap.get(simpleName);
        } else if (!isAnyOther(simpleName)) {
            qualName = this.packageName.extend(simpleName);
        }
        CommonToken token = new CommonToken(ruleNameToken.getType(), qualName.toString());
        token.setLine(ruleNameToken.getLine());
        token.setTokenIndex(ruleNameToken.getToken()
            .getTokenIndex());
        result = new CtrlTree(token);
        result.setQualName(qualName);
        result.addChild(ruleNameToken.getChild(0));
    }
    return result;
}
 
開發者ID:meteoorkip,項目名稱:JavaGraph,代碼行數:28,代碼來源:CtrlHelper.java

示例5: defineTemplate

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/** for testing */

    public CompiledST defineTemplate(String templateName, String template) {
        if ( templateName.charAt(0) !='/' ) templateName = "/"+templateName;
        try {
            CompiledST impl = defineTemplate(templateName,
                                             new CommonToken(GroupParser.ID, templateName),
                                             null,
                                             template,
                                             null);
            return impl;
        }
        catch (STException se) {
            // we have reported the error; the exception just blasts us
            // out of parsing this template
        }
        return null;
    }
 
開發者ID:antlr,項目名稱:codebuff,代碼行數:19,代碼來源:STGroup.java

示例6: defineTemplate

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/** for testing */

    public CompiledST defineTemplate(String templateName, String template) {
        if ( templateName.charAt(0)!='/' ) templateName = "/"+templateName;
        try {
            CompiledST impl = defineTemplate(templateName,
                                             new CommonToken(GroupParser.ID, templateName),
                                             null,
                                             template,
                                             null);
            return impl;
        }
        catch (STException se) {
            // we have reported the error; the exception just blasts us
            // out of parsing this template
        }
        return null;
    }
 
開發者ID:antlr,項目名稱:codebuff,代碼行數:19,代碼來源:STGroup.java

示例7: testTokenRegionContainsRegion

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
@Test 
	public void testTokenRegionContainsRegion() throws Exception {
		String modelAsString = "a1 / /* comment */ b2";
		List<CommonToken> tokens = getTokens(modelAsString);
		for(int length=0; length < modelAsString.length(); ++length) {
			for(int offset = 0; offset + length < modelAsString.length(); ++offset) {
				ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(modelAsString, new TextRegion(offset, length));
//				System.out.println(offset + ":" + length + " -> " + tokenRegion);
				CommonToken firstToken = findTokenStartingAt(tokenRegion.getOffset(), tokens);
				assertTrue(firstToken.getStartIndex() <= offset);
				if(tokenRegion.getLength() != 0) {
					CommonToken lastToken = findTokenStopingAt(tokenRegion.getOffset() + tokenRegion.getLength()-1, tokens);
					assertTrue(lastToken.getStopIndex() >= offset + length -1);
				}
			}
		}
	}
 
開發者ID:eclipse,項目名稱:xtext-core,代碼行數:18,代碼來源:TokenRegionProviderTest.java

示例8: createToken

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/**
 * Create a new token from the given prototype. Any argument besides the prototype is optional and
 * will be ignored if its value is <code>null</code>.
 */
protected CommonToken createToken(CommonToken prototype, String text, 
		Integer charPosInLine, Integer channel, Integer start, Integer stop, Integer type) {
	if (prototype == null)
		throw new IllegalArgumentException("Prototype may not be null.");
	CommonToken result = new CommonToken(prototype);
	if (text != null)
		result.setText(text);
	if (charPosInLine != null)
		result.setCharPositionInLine(charPosInLine.intValue());
	if (channel != null)
		result.setChannel(channel.intValue());
	if (start != null)
		result.setStartIndex(start.intValue());
	if (stop != null)
		result.setStopIndex(stop.intValue());
	if (type != null)
		result.setType(type.intValue());
	return result;
}
 
開發者ID:eclipse,項目名稱:xtext-core,代碼行數:24,代碼來源:AbstractSplittingTokenSource.java

示例9: defineTemplate

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/** for testing */
public CompiledST defineTemplate(String templateName, String template) {
	if ( templateName.charAt(0)!='/' ) templateName = "/"+templateName;
	try {
		CompiledST impl =
			defineTemplate(templateName,
						   new CommonToken(GroupParser.ID, templateName),
						   null, template, null);
		return impl;
	}
	catch (STException se) {
		// we have reported the error; the exception just blasts us
		// out of parsing this template
	}
	return null;
}
 
開發者ID:antlr,項目名稱:codebuff,代碼行數:17,代碼來源:STGroup.java

示例10: splitIntoBeginToken

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
private void splitIntoBeginToken(Token token, int indentation, ITokenAcceptor result) {
	result.accept(token);
	if (shouldEmitPendingEndTokens()) {
		Token nextToken = getDelegate().nextToken();
		if (shouldSplitToken(nextToken)) {
			nextOffset = ((CommonToken) token).getStopIndex() + 1;
			doSplitToken(nextToken, result);
		} else {
			indentationStack.push(indentation);
			currentIndentation = indentation;
			result.accept(createBeginToken(((CommonToken) token).getStopIndex() + 1));
			result.accept(nextToken);
		}	
	} else {
		indentationStack.push(indentation);
		currentIndentation = indentation;
		result.accept(createBeginToken(((CommonToken) token).getStopIndex() + 1));
	}
}
 
開發者ID:eclipse,項目名稱:xtext-core,代碼行數:20,代碼來源:AbstractIndentationTokenSource.java

示例11: nextToken

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
public Token nextToken()
{
    super.nextToken();
    if (tokens.size() == 0)
        return new CommonToken(Token.EOF);
    return tokens.remove(0);
}
 
開發者ID:Netflix,項目名稱:sstable-adaptor,代碼行數:8,代碼來源:CqlLexer.java

示例12: adjustASIAndCollectFollowElements

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/**
 * The second pass over the given input. If the input ends with an ASI, the semicolon is removed and it is parsed
 * again since the production may have skipped the ASI if more input was present. Same applies for the opposite
 * direction, e.g if it does not end with an ASI but the prev token suggests that there may have been a semicolon,
 * it is inserted.
 */
private void adjustASIAndCollectFollowElements(CustomInternalN4JSParser previousParser, boolean strict,
		Set<FollowElement> result) {
	ObservableXtextTokenStream tokens = (ObservableXtextTokenStream) previousParser.getTokenStream();
	int lastTokenIndex = tokens.size() - 1;
	if (lastTokenIndex >= 0) {
		CommonToken lastToken = (CommonToken) tokens.LT(-1);
		@SuppressWarnings("unchecked")
		List<Token> tokenList = tokens.getTokens();
		if (lastToken == null) {
			return; // TODO ask Sebastian why and how this can happen... it happens!
		}

		if (shouldSkipASI(lastToken)) {
			// Some statements may not span multiple lines, e.g between the return keyword
			// and the returned expression, there may not be an ASI. Filter these locations.
			if (maySkipASI(lastToken, tokens)) {
				tokenList.remove(lastTokenIndex);
				result.addAll(resetAndGetFollowElements(tokens, strict));
				// If a postfix operator sneaked into the result, remove it since
				// we'd have produce an ASI before that
				removePostfixOperator(result);
			}
		} else if (shouldAddSyntheticSemicolon(previousParser, lastTokenIndex, lastToken)) {
			CommonToken token = new CommonToken(semi);
			tokenList.add(token);
			result.addAll(resetAndGetFollowElements(tokens, strict));
			// Same here, if we had added an ASI, the postfix operator would be rendered
			// invalid, remove it.
			removePostfixOperator(result);
		}
	}
}
 
開發者ID:eclipse,項目名稱:n4js,代碼行數:39,代碼來源:CustomN4JSParser.java

示例13: maySkipASI

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/**
 * Prevent ASIs to be skipped at certain locations, e.g. after a return keyword.
 */
private boolean maySkipASI(CommonToken lastToken, ObservableXtextTokenStream tokens) {
	int countDownFrom = lastToken.getTokenIndex();
	for (int i = countDownFrom - 1; i >= 0; i--) {
		Token prevToken = tokens.get(i);
		if (prevToken.getChannel() == Token.DEFAULT_CHANNEL) {
			if (mandatoryASI.get(prevToken.getType())) {
				return false;
			}
			return true;
		}
	}
	return true;
}
 
開發者ID:eclipse,項目名稱:n4js,代碼行數:17,代碼來源:CustomN4JSParser.java

示例14: shouldSkipASI

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/**
 * Returns true if the ASI at the end should be skipped for a second pass of the CA parser. Otherwise false.
 */
private boolean shouldSkipASI(CommonToken lastToken) {
	if (lastToken.getType() == eol) {
		return true;
	}
	if (lastToken.getType() == semi && lastToken.getText() != null && !";".equals(lastToken.getText())) {
		return true;
	}
	return false;
}
 
開發者ID:eclipse,項目名稱:n4js,代碼行數:13,代碼來源:CustomN4JSParser.java

示例15: nextToken

import org.antlr.runtime.CommonToken; //導入依賴的package包/類
/**
 * Implementation of the {@link TokenSource} interface. Return new tokens as long as there are some, afterwards
 * return {@link Token#EOF_TOKEN}.
 */
@Override
public Token nextToken() {
	if (next != null) {
		Token result = next;
		next = null;
		return result;
	}
	if (!leafNodes.hasNext()) {
		return Token.EOF_TOKEN;
	}
	ILeafNode leaf = leafNodes.next();
	if (leaf.getTotalOffset() >= endOffset) {
		leafNodes = Iterators.emptyIterator();
		return Token.EOF_TOKEN;
	}
	if (leaf.getTotalEndOffset() <= startOffset) {
		return nextToken();
	}
	if (leaf.getTotalEndOffset() > endOffset) {
		return toPrefixToken(leaf);
	}
	SyntaxErrorMessage syntaxErrorMessage = leaf.getSyntaxErrorMessage();
	if (syntaxErrorMessage != null && SEMICOLON_INSERTED.equals(syntaxErrorMessage.getIssueCode())) {
		return toASIToken(leaf);
	}
	if (leaf.isHidden()) {
		return processHiddenToken(leaf);
	}
	int tokenType = tokenTypeMapper.getInternalTokenType(leaf);
	return new CommonToken(tokenType, leaf.getText());
}
 
開發者ID:eclipse,項目名稱:n4js,代碼行數:36,代碼來源:NodeModelTokenSource.java


注:本文中的org.antlr.runtime.CommonToken類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。