1 22 23 package org.gjt.sp.jedit.syntax; 24 25 import javax.swing.text.Segment ; 26 27 34 public class DefaultTokenHandler implements TokenHandler 35 { 36 40 public void init() 41 { 42 lastToken = firstToken = null; 43 } 45 50 public Token getTokens() 51 { 52 return firstToken; 53 } 55 66 public void handleToken(Segment seg, byte id, int offset, int length, 67 TokenMarker.LineContext context) 68 { 69 Token token = createToken(id,offset,length,context); 70 if(token != null) 71 addToken(token,context); 72 } 74 82 public TokenMarker.LineContext getLineContext() 83 { 84 return lineContext; 85 } 87 95 public void setLineContext(TokenMarker.LineContext lineContext) 96 { 97 this.lineContext = lineContext; 98 } 100 protected Token firstToken, lastToken; 102 protected TokenMarker.LineContext lineContext; 103 104 protected ParserRuleSet getParserRuleSet(TokenMarker.LineContext context) 106 { 107 while(context != null) 108 { 109 if(!context.rules.isBuiltIn()) 110 return context.rules; 111 112 context = context.parent; 113 } 114 115 return null; 116 } 118 protected Token createToken(byte id, int offset, int length, 120 TokenMarker.LineContext context) 121 { 122 return new Token(id,offset,length,getParserRuleSet(context)); 123 } 125 protected void addToken(Token token, TokenMarker.LineContext context) 127 { 128 if(firstToken == null) 129 { 130 firstToken = lastToken = token; 131 } 132 else 133 { 134 lastToken.next = token; 135 lastToken = lastToken.next; 136 } 137 } 139 } 141 | Popular Tags |