You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.4 KiB

4 years ago
  1. var tokenizer = require('../../tokenizer');
  2. var TYPE = tokenizer.TYPE;
  3. var WhiteSpace = TYPE.WhiteSpace;
  4. var Semicolon = TYPE.Semicolon;
  5. var LeftCurlyBracket = TYPE.LeftCurlyBracket;
  6. var Delim = TYPE.Delim;
  7. var EXCLAMATIONMARK = 0x0021; // U+0021 EXCLAMATION MARK (!)
  8. function getOffsetExcludeWS() {
  9. if (this.scanner.tokenIndex > 0) {
  10. if (this.scanner.lookupType(-1) === WhiteSpace) {
  11. return this.scanner.tokenIndex > 1
  12. ? this.scanner.getTokenStart(this.scanner.tokenIndex - 1)
  13. : this.scanner.firstCharOffset;
  14. }
  15. }
  16. return this.scanner.tokenStart;
  17. }
  18. // 0, 0, false
  19. function balanceEnd() {
  20. return 0;
  21. }
  22. // LEFTCURLYBRACKET, 0, false
  23. function leftCurlyBracket(tokenType) {
  24. return tokenType === LeftCurlyBracket ? 1 : 0;
  25. }
  26. // LEFTCURLYBRACKET, SEMICOLON, false
  27. function leftCurlyBracketOrSemicolon(tokenType) {
  28. return tokenType === LeftCurlyBracket || tokenType === Semicolon ? 1 : 0;
  29. }
  30. // EXCLAMATIONMARK, SEMICOLON, false
  31. function exclamationMarkOrSemicolon(tokenType, source, offset) {
  32. if (tokenType === Delim && source.charCodeAt(offset) === EXCLAMATIONMARK) {
  33. return 1;
  34. }
  35. return tokenType === Semicolon ? 1 : 0;
  36. }
  37. // 0, SEMICOLON, true
  38. function semicolonIncluded(tokenType) {
  39. return tokenType === Semicolon ? 2 : 0;
  40. }
  41. module.exports = {
  42. name: 'Raw',
  43. structure: {
  44. value: String
  45. },
  46. parse: function(startToken, mode, excludeWhiteSpace) {
  47. var startOffset = this.scanner.getTokenStart(startToken);
  48. var endOffset;
  49. this.scanner.skip(
  50. this.scanner.getRawLength(startToken, mode || balanceEnd)
  51. );
  52. if (excludeWhiteSpace && this.scanner.tokenStart > startOffset) {
  53. endOffset = getOffsetExcludeWS.call(this);
  54. } else {
  55. endOffset = this.scanner.tokenStart;
  56. }
  57. return {
  58. type: 'Raw',
  59. loc: this.getLocation(startOffset, endOffset),
  60. value: this.scanner.source.substring(startOffset, endOffset)
  61. };
  62. },
  63. generate: function(node) {
  64. this.chunk(node.value);
  65. },
  66. mode: {
  67. default: balanceEnd,
  68. leftCurlyBracket: leftCurlyBracket,
  69. leftCurlyBracketOrSemicolon: leftCurlyBracketOrSemicolon,
  70. exclamationMarkOrSemicolon: exclamationMarkOrSemicolon,
  71. semicolonIncluded: semicolonIncluded
  72. }
  73. };