/**
* Creates a new template token for the given token id. A template token is in fact a list of tokens for the
* colorer,e.g a single terminal on the grammar level may yield varIoUs tokens for the Syntax coloring.
*/
protected Templatetoken createTemplatetoken(int id,ILexerTokenRegion token) {
int tokenOffset = token.getoffset();
int tokenLength = token.getLength();
Templatetoken result = new Templatetoken();
switch (id) {
case RULE_TEMPLATE_HEAD:
result.delimiter = false; // ranges defined by 'offsets' and 'lengths' do *not* start with a delimiter
result.offsets = new int[] { tokenOffset,tokenOffset + tokenLength - 2 };
result.lengths = new int[] { tokenLength - 2,2 };
break;
case RULE_TEMPLATE_MIDDLE:
if (tokenLength == 2) {
result.delimiter = true; // ranges defined by 'offsets' and 'lengths' start with a delimiter
result.offsets = new int[] { tokenOffset };
result.lengths = new int[] { tokenLength };
} else {
result.delimiter = false; // ranges defined by 'offsets' and 'lengths' do *not* start with a delimiter
result.offsets = new int[] { tokenOffset,tokenOffset + tokenLength - 2 };
result.lengths = new int[] { tokenLength - 2,2 };
}
}
return result;
}
@Override
public IToken nextToken() {
if (currentTemplateTextToken != null) {
if (currentTemplateTextToken.hasNext())
return currentTemplateTextToken.nextToken();
else
currentTemplateTextToken = null;
}
if (!getIterator().hasNext())
return Token.EOF;
ILexerTokenRegion next = getIterator().next();
int tokenType = next.getLexerTokenType();
switch (tokenType) {
case RULE_TEMPLATE_HEAD:
case RULE_TEMPLATE_MIDDLE: {
currentTemplateTextToken = createTemplatetoken(tokenType,next);
return currentTemplateTextToken.nextToken();
}
default:
setCurrentToken(next);
return createtoken(next);
}
}
项目:statecharts
文件:StyleRanges.java
public List<StyleRange> getRanges(String expression) {
final List<StyleRange> ranges = Lists.newArrayList();
DocumentEvent event = new DocumentEvent();
event.fDocument = new DummyDocument(expression);
DocumentTokenSource tokenSource = tokenSourceProvider.get();
tokenSource.updateStructure(event);
Iterator<ILexerTokenRegion> iterator = tokenSource.getTokenInfos().iterator();
while (iterator.hasNext()) {
ILexerTokenRegion next = iterator.next();
TextAttribute attribute = attributeProvider.getAttribute(tokenTypeMapper.getId(next.getLexerTokenType()));
StyleRange range = new StyleRange(next.getoffset(),next.getLength(),attribute.getForeground(),attribute.getBackground());
range.font = attribute.getFont();
range.fontStyle = attribute.getStyle();
ranges.add(range);
}
return ranges;
}
@Override
protected IRegion findWord(IDocument document,int offset) {
if (document instanceof XtextDocument) {
Iterator<ILexerTokenRegion> tokenIterator = ((XtextDocument) document).getTokens().iterator();
ILexerTokenRegion leadingToken = null;
ILexerTokenRegion trailingToken = null;
while(tokenIterator.hasNext()) {
ILexerTokenRegion token = tokenIterator.next();
if (token.getoffset() <= offset && token.getoffset() + token.getLength() >= offset) {
if (leadingToken != null)
trailingToken = token;
else
leadingToken = token;
}
if (token.getoffset() > offset)
break;
}
if (leadingToken != null) {
try {
if (leadingToken.getLength() > 1 && (trailingToken == null || !Character.isLetter(document.getChar(trailingToken.getoffset())))) {
return new Region(leadingToken.getoffset(),leadingToken.getLength());
} else if (trailingToken != null) {
return new Region(trailingToken.getoffset(),trailingToken.getLength());
}
} catch(BadLocationException ignore) {}
}
}
return super.findWord(document,offset);
}
/**
* @return the common region of the given partition and the changed region in the DocumentEvent based on the underlying tokens.
*/
protected IRegion computeInterSection(ITypedRegion partition,DocumentEvent e,XtextDocument document) {
Iterable<ILexerTokenRegion> tokensInPartition = Iterables.filter(document.getTokens(),Regions.overlaps(partition.getoffset(),partition.getLength()));
Iterator<ILexerTokenRegion> tokens = Iterables.filter(tokensInPartition,Regions.overlaps(e.getoffset(),e.getLength())).iterator();
if (tokens.hasNext()) {
ILexerTokenRegion first = tokens.next();
ILexerTokenRegion last = first;
while(tokens.hasNext())
last = tokens.next();
return new Region(first.getoffset(),last.getoffset()+last.getLength() -first.getoffset());
}
// this shouldn't happen,but just in case return the whole partition
return partition;
}
项目:bts
文件:TokenScanner.java
public ILexerTokenRegion next() {
if (!computedHasNext) {
// compute hasNext on demand to make sure the internal state is valid
hasNext();
}
if (!hasNext) {
throw new NoSuchElementException("You should check for #hasNext prior to calling #next");
}
ILexerTokenRegion result = current;
current = null;
computedHasNext = false;
hasNext = false;
return result;
}
项目:bts
文件:TokenScanner.java
public void setRange(IDocument document,final int offset,final int length) {
this.regionOffset = offset;
overlapFilter = Regions.overlaps(offset,length);
Iterable<ILexerTokenRegion> newIterable = getTokens(document);
if (delegateIterable != null) {
if (!delegateIterable.equals(newIterable)) {
current = null;
}
}
if (current == null) {
delegate = newIterable.iterator();
delegateIterable = newIterable;
computedHasNext = false;
hasNext = false;
} else {
if (current.getoffset() <= offset && current.getoffset() + current.getLength() > offset) {
// offset is inside of current - return current as next
computedHasNext = true;
hasNext = true;
} else {
// restart - use a new delegate
computedHasNext = false;
hasNext = false;
delegate = newIterable.iterator();
delegateIterable = newIterable;
}
}
}
项目:bts
文件:TokenScanner.java
protected Iterable<ILexerTokenRegion> getTokens(IDocument document) {
XtextDocument doc = (XtextDocument) document;
return doc.getTokens();
}
项目:bts
文件:TokenScanner.java
protected IToken createtoken(ILexerTokenRegion currentToken) {
String id = tokenIdMapper.getId(currentToken.getLexerTokenType());
Token token = new Token(getAttribute(id));
return token;
}
项目:bts
文件:TokenScanner.java
项目:bts
文件:TokenScanner.java
protected ILexerTokenRegion getCurrentToken() {
return currentToken;
}
项目:bts
文件:TokenScanner.java
protected void setCurrentToken(ILexerTokenRegion currentToken) {
this.currentToken = currentToken;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。