Java Code Examples for org.antlr.runtime.CommonToken#setType()

The following examples show how to use org.antlr.runtime.CommonToken#setType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InternalHighlightingParser.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@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);
			}
		}
	}
}
 
Example 2
Source File: AbstractSplittingTokenSource.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: ParserBasedDocumentTokenSource.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * JSDoc comments are identified by the lexer as normal ML comments to simplify the ASI code. If a comment starts
 * with the sequence @çode{/**} it is remapped to a JS Doc token.
 */
@Override
protected TokenInfo createTokenInfo(CommonToken token) {
	if (token.getType() == InternalN4JSParser.RULE_ML_COMMENT) {
		String text = token.getText();
		if (text.length() > 4 && text.startsWith("/**") && text.charAt(3) != '*') {
			CommonToken jsDoc = new CommonToken(token);
			jsDoc.setType(JS_DOC_TOKEN);
			return super.createTokenInfo(jsDoc);
		}
	}
	return super.createTokenInfo(token);
}
 
Example 4
Source File: TokenTypeRewriter.java    From n4js with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Overrides the token type if the given element was configured as a special token type. Returns {@code null} if no
 * rewrite should happen.
 *
 * @param token
 *            the token to rewrite. Optional, if {@code null} no rewrite will be performed.
 * @param element
 *            the abstract element.
 * @return returns with the rewritten token type or {@code null} if no rewrite were performed.
 */
public Integer rewrite(CommonToken token, AbstractElement element) {
	Integer mappedType = mapping.get(element);
	if (mappedType != null) {
		if (null != token) {
			token.setType(mappedType);
		}
	}
	return mappedType;
}