Tokeniser.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var Token = require("../lib/Token");
  2. var StringSource = require("../lib/StringSource");
  3. var Tokeniser = module.exports = function(options) {
  4. var keywords = options.keywords;
  5. var tokenise = function(input) {
  6. var source = new StringSource(input);
  7. var createToken = function(startIndex, endIndex) {
  8. var value = input.substring(startIndex, endIndex);
  9. var tokenType = keywords.indexOf(value) === -1 ? "identifier" : "keyword";
  10. return new Token(
  11. tokenType,
  12. value,
  13. source.range(startIndex, endIndex)
  14. );
  15. };
  16. var position = 0;
  17. var tokens = [];
  18. var done = input === "";
  19. while (!done) {
  20. var nextWhitespace = indexOfRegex(input, /\s/, position);
  21. if (nextWhitespace === -1) {
  22. done = true;
  23. tokens.push(createToken(position, input.length));
  24. } else {
  25. tokens.push(createToken(position, nextWhitespace));
  26. position = indexOfRegex(input, /\S/, nextWhitespace);
  27. }
  28. }
  29. tokens.push(new Token("end", null, source.range(input.length, input.length)));
  30. return tokens;
  31. };
  32. var indexOfRegex = function(string, regex, startIndex) {
  33. startIndex = startIndex || 0;
  34. var index = string.substring(startIndex).search(regex);
  35. if (index === -1) {
  36. return -1;
  37. } else {
  38. return index + startIndex;
  39. }
  40. };
  41. return {
  42. tokenise: tokenise
  43. };
  44. };