PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/syntax/DefaultTokenHandler.java

#
Java | 140 lines | 57 code | 14 blank | 69 comment | 6 complexity | b5be20f93903490c829e500fa7eb271f MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * DefaultTokenHandler.java - Builds a linked list of Token objects
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2002 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.syntax;
  23. import javax.swing.text.Segment;
  24. /**
  25. * Builds a linked list of tokens without any additional processing.
  26. *
  27. * @author Slava Pestov
  28. * @version $Id: DefaultTokenHandler.java 4902 2003-10-26 19:43:58Z spestov $
  29. * @since jEdit 4.1pre1
  30. */
  31. public class DefaultTokenHandler implements TokenHandler
  32. {
  33. //{{{ reset() method
  34. /**
  35. * Clears the list of tokens.
  36. */
  37. public void init()
  38. {
  39. lastToken = firstToken = null;
  40. } //}}}
  41. //{{{ getTokens() method
  42. /**
  43. * Returns the first syntax token.
  44. * @since jEdit 4.1pre1
  45. */
  46. public Token getTokens()
  47. {
  48. return firstToken;
  49. } //}}}
  50. //{{{ handleToken() method
  51. /**
  52. * Called by the token marker when a syntax token has been parsed.
  53. * @param seg The segment containing the text
  54. * @param id The token type (one of the constants in the
  55. * {@link Token} class).
  56. * @param offset The start offset of the token
  57. * @param length The number of characters in the token
  58. * @param context The line context
  59. * @since jEdit 4.2pre3
  60. */
  61. public void handleToken(Segment seg, byte id, int offset, int length,
  62. TokenMarker.LineContext context)
  63. {
  64. Token token = createToken(id,offset,length,context);
  65. if(token != null)
  66. addToken(token,context);
  67. } //}}}
  68. //{{{ getLineContext() method
  69. /**
  70. * The token handler can compare this object with the object
  71. * previously given for this line to see if the token type at the end
  72. * of the line has changed (meaning subsequent lines might need to be
  73. * retokenized).
  74. * @since jEdit 4.2pre6
  75. */
  76. public TokenMarker.LineContext getLineContext()
  77. {
  78. return lineContext;
  79. } //}}}
  80. //{{{ setLineContext() method
  81. /**
  82. * The token handler can compare this object with the object
  83. * previously given for this line to see if the token type at the end
  84. * of the line has changed (meaning subsequent lines might need to be
  85. * retokenized).
  86. * @since jEdit 4.2pre6
  87. */
  88. public void setLineContext(TokenMarker.LineContext lineContext)
  89. {
  90. this.lineContext = lineContext;
  91. } //}}}
  92. //{{{ Protected members
  93. protected Token firstToken, lastToken;
  94. protected TokenMarker.LineContext lineContext;
  95. //{{{ getParserRuleSet() method
  96. protected ParserRuleSet getParserRuleSet(TokenMarker.LineContext context)
  97. {
  98. while(context != null)
  99. {
  100. if(!context.rules.isBuiltIn())
  101. return context.rules;
  102. context = context.parent;
  103. }
  104. return null;
  105. } //}}}
  106. //{{{ createToken() method
  107. protected Token createToken(byte id, int offset, int length,
  108. TokenMarker.LineContext context)
  109. {
  110. return new Token(id,offset,length,getParserRuleSet(context));
  111. } //}}}
  112. //{{{ addToken() method
  113. protected void addToken(Token token, TokenMarker.LineContext context)
  114. {
  115. if(firstToken == null)
  116. {
  117. firstToken = lastToken = token;
  118. }
  119. else
  120. {
  121. lastToken.next = token;
  122. lastToken = lastToken.next;
  123. }
  124. } //}}}
  125. //}}}
  126. }