PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 111 lines | 47 code | 11 blank | 53 comment | 7 complexity | 06f8b2b46a906606170c56a59f675ad6 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. /**
  24. * An implementation of the <code>TokenHandler</code> interface that
  25. * builds a linked list of tokens.
  26. *
  27. * @author Slava Pestov
  28. * @version $Id: DefaultTokenHandler.java 4240 2002-06-04 08:48:13Z 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 id The token type (one of the constants in the
  54. * <code>Token</code> class).
  55. * @param offset The start offset of the token
  56. * @param length The number of characters in the token
  57. * @param context The line context
  58. * @since jEdit 4.1pre1
  59. */
  60. public void handleToken(byte id, int offset, int length,
  61. TokenMarker.LineContext context)
  62. {
  63. Token token = createToken(id,offset,length,context);
  64. if(token != null)
  65. addToken(token,context);
  66. } //}}}
  67. //{{{ Protected members
  68. protected Token firstToken, lastToken;
  69. //{{{ getParserRuleSet() method
  70. protected ParserRuleSet getParserRuleSet(TokenMarker.LineContext context)
  71. {
  72. while(context != null)
  73. {
  74. if(context.rules.getMode() != null)
  75. return context.rules;
  76. context = context.parent;
  77. }
  78. return null;
  79. } //}}}
  80. //{{{ createToken() method
  81. protected Token createToken(byte id, int offset, int length,
  82. TokenMarker.LineContext context)
  83. {
  84. return new Token(id,offset,length,getParserRuleSet(context));
  85. } //}}}
  86. //{{{ addToken() method
  87. protected void addToken(Token token, TokenMarker.LineContext context)
  88. {
  89. if(firstToken == null)
  90. {
  91. firstToken = lastToken = token;
  92. }
  93. else
  94. {
  95. lastToken.next = token;
  96. lastToken = lastToken.next;
  97. }
  98. } //}}}
  99. //}}}
  100. }