PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Scripting/Runtime/TokenizerService.cs

https://bitbucket.org/stefanrusek/xronos
C# | 107 lines | 41 code | 17 blank | 49 comment | 6 complexity | 2b78d6d7ee90c73073299d91f5f90b09 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. using System.Collections.Generic;
  17. #if CODEPLEX_40
  18. using System.Dynamic;
  19. #else
  20. using Microsoft.Scripting;
  21. #endif
  22. using System.IO;
  23. namespace Microsoft.Scripting.Runtime {
  24. public abstract class TokenizerService {
  25. // static contract:
  26. protected TokenizerService() {
  27. }
  28. public abstract void Initialize(object state, TextReader sourceReader, SourceUnit sourceUnit, SourceLocation initialLocation);
  29. /// <summary>
  30. /// The current internal state of the scanner.
  31. /// </summary>
  32. public abstract object CurrentState { get; }
  33. /// <summary>
  34. /// The current startLocation of the scanner.
  35. /// </summary>
  36. public abstract SourceLocation CurrentPosition { get; }
  37. /// <summary>
  38. /// Move the tokenizer past the next token and return its category.
  39. /// </summary>
  40. /// <returns>The token information associated with the token just scanned.</returns>
  41. public abstract TokenInfo ReadToken();
  42. public abstract bool IsRestartable { get; }
  43. public abstract ErrorSink ErrorSink { get; set; }
  44. /// <summary>
  45. /// Move the tokenizer past the next token.
  46. /// </summary>
  47. /// <returns><c>False</c> if the end of stream has been reached, <c>true</c> otherwise.</returns>
  48. public virtual bool SkipToken() {
  49. return ReadToken().Category != TokenCategory.EndOfStream;
  50. }
  51. // TODO: shouldn't be virutal (JS tokenizer needs to be fixed)
  52. /// <summary>
  53. /// Get all tokens over a block of the stream.
  54. /// </summary>
  55. /// <remarks>
  56. /// <para>
  57. /// The scanner should return full tokens. If startLocation + length lands in the middle of a token, the full token
  58. /// should be returned.
  59. /// </para>
  60. /// </remarks>
  61. /// <param name="countOfChars">The mininum number of characters to process while getting tokens.</param>
  62. /// <returns>A enumeration of tokens.</returns>
  63. public virtual IEnumerable<TokenInfo> ReadTokens(int countOfChars) {
  64. List<TokenInfo> tokens = new List<TokenInfo>();
  65. int start_index = CurrentPosition.Index;
  66. while (CurrentPosition.Index - start_index < countOfChars) {
  67. TokenInfo token = ReadToken();
  68. if (token.Category == TokenCategory.EndOfStream) break;
  69. tokens.Add(token);
  70. }
  71. return tokens;
  72. }
  73. /// <summary>
  74. /// Scan from startLocation to at least startLocation + length.
  75. /// </summary>
  76. /// <param name="countOfChars">The mininum number of characters to process while getting tokens.</param>
  77. /// <remarks>
  78. /// This method is used to determine state at arbitrary startLocation.
  79. /// </remarks>
  80. /// <returns><c>False</c> if the end of stream has been reached, <c>true</c> otherwise.</returns>
  81. public bool SkipTokens(int countOfChars) {
  82. bool eos = false;
  83. int start_index = CurrentPosition.Index;
  84. while (CurrentPosition.Index - start_index < countOfChars && (eos = SkipToken())) {
  85. ;
  86. }
  87. return eos;
  88. }
  89. }
  90. }