PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/syntax/DefaultTokenHandler.java

#
Java | 113 lines | 48 code | 12 blank | 53 comment | 6 complexity | fd4a0d8a2befd3535e039829f1f12dc3 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 4755 2003-06-03 22:02:40Z 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. //{{{ Protected members
  69. protected Token firstToken, lastToken;
  70. //{{{ getParserRuleSet() method
  71. protected ParserRuleSet getParserRuleSet(TokenMarker.LineContext context)
  72. {
  73. while(context != null)
  74. {
  75. if(!context.rules.isBuiltIn())
  76. return context.rules;
  77. context = context.parent;
  78. }
  79. return null;
  80. } //}}}
  81. //{{{ createToken() method
  82. protected Token createToken(byte id, int offset, int length,
  83. TokenMarker.LineContext context)
  84. {
  85. return new Token(id,offset,length,getParserRuleSet(context));
  86. } //}}}
  87. //{{{ addToken() method
  88. protected void addToken(Token token, TokenMarker.LineContext context)
  89. {
  90. if(firstToken == null)
  91. {
  92. firstToken = lastToken = token;
  93. }
  94. else
  95. {
  96. lastToken.next = token;
  97. lastToken = lastToken.next;
  98. }
  99. } //}}}
  100. //}}}
  101. }