343ef36dee7616541410b88ecd9c876f8fad428c9520aef4f0aad8807606f0c7df0fd1c62924439c30398a5cdc676843af54a65d753d721c6543e7599005cf 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*jshint node:true */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23. 'use strict';
  24. var InputScanner = require('../core/inputscanner').InputScanner;
  25. var Token = require('../core/token').Token;
  26. var TokenStream = require('../core/tokenstream').TokenStream;
  27. var WhitespacePattern = require('./whitespacepattern').WhitespacePattern;
  28. var TOKEN = {
  29. START: 'TK_START',
  30. RAW: 'TK_RAW',
  31. EOF: 'TK_EOF'
  32. };
  33. var Tokenizer = function(input_string, options) {
  34. this._input = new InputScanner(input_string);
  35. this._options = options || {};
  36. this.__tokens = null;
  37. this._patterns = {};
  38. this._patterns.whitespace = new WhitespacePattern(this._input);
  39. };
  40. Tokenizer.prototype.tokenize = function() {
  41. this._input.restart();
  42. this.__tokens = new TokenStream();
  43. this._reset();
  44. var current;
  45. var previous = new Token(TOKEN.START, '');
  46. var open_token = null;
  47. var open_stack = [];
  48. var comments = new TokenStream();
  49. while (previous.type !== TOKEN.EOF) {
  50. current = this._get_next_token(previous, open_token);
  51. while (this._is_comment(current)) {
  52. comments.add(current);
  53. current = this._get_next_token(previous, open_token);
  54. }
  55. if (!comments.isEmpty()) {
  56. current.comments_before = comments;
  57. comments = new TokenStream();
  58. }
  59. current.parent = open_token;
  60. if (this._is_opening(current)) {
  61. open_stack.push(open_token);
  62. open_token = current;
  63. } else if (open_token && this._is_closing(current, open_token)) {
  64. current.opened = open_token;
  65. open_token.closed = current;
  66. open_token = open_stack.pop();
  67. current.parent = open_token;
  68. }
  69. current.previous = previous;
  70. previous.next = current;
  71. this.__tokens.add(current);
  72. previous = current;
  73. }
  74. return this.__tokens;
  75. };
  76. Tokenizer.prototype._is_first_token = function() {
  77. return this.__tokens.isEmpty();
  78. };
  79. Tokenizer.prototype._reset = function() {};
  80. Tokenizer.prototype._get_next_token = function(previous_token, open_token) { // jshint unused:false
  81. this._readWhitespace();
  82. var resulting_string = this._input.read(/.+/g);
  83. if (resulting_string) {
  84. return this._create_token(TOKEN.RAW, resulting_string);
  85. } else {
  86. return this._create_token(TOKEN.EOF, '');
  87. }
  88. };
  89. Tokenizer.prototype._is_comment = function(current_token) { // jshint unused:false
  90. return false;
  91. };
  92. Tokenizer.prototype._is_opening = function(current_token) { // jshint unused:false
  93. return false;
  94. };
  95. Tokenizer.prototype._is_closing = function(current_token, open_token) { // jshint unused:false
  96. return false;
  97. };
  98. Tokenizer.prototype._create_token = function(type, text) {
  99. var token = new Token(type, text,
  100. this._patterns.whitespace.newline_count,
  101. this._patterns.whitespace.whitespace_before_token);
  102. return token;
  103. };
  104. Tokenizer.prototype._readWhitespace = function() {
  105. return this._patterns.whitespace.read();
  106. };
  107. module.exports.Tokenizer = Tokenizer;
  108. module.exports.TOKEN = TOKEN;