Advertisement
Guest User

Untitled

a guest
Jun 17th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. function highlight(input) {
  2. var output = '',
  3. string = false,
  4. lineComment = false,
  5. capVar = false,
  6. warnings = [];
  7. for (var i = 0; i < input.length; i++) {
  8. var c = input[i];
  9. if (c == '/' && input[i + 1] == '/') {
  10. lineComment = true;
  11. output += '<span class="comment">' + c + input[++i];
  12. } else if (lineComment) {
  13. if (c == '\n') {
  14. lineComment = false;
  15. output += '</span>';
  16. }
  17. output += c;
  18. } else if (c === string) {
  19. string = false;
  20. output += c + '</span>';
  21. } else if (string && c == '\n') {
  22. warnings.push([i, 'Unexpected line end, string literal not terminated.']);
  23. string = false;
  24. output += '</span>' + c;
  25. } else if (string) {
  26. if (c == '\\') output += '<span class="escape">';
  27. output += c;
  28. if (c == '\\') {
  29. output += c = input[++i];
  30. if (c == 'u') {
  31. output += c = input[++i];
  32. if (c == '{') {
  33. while ((c = input[++i]) && c != '}') output += c;
  34. output += '}';
  35. } else output += input[++i] + input[++i] + input[++i];
  36. } else if (c == 'x') output += input[++i] + input[++i];
  37. output += '</span>';
  38. }
  39. } else if (c == '"' || c == "'") {
  40. string = c;
  41. output += '<span class="string">' + c;
  42. } else if (c != c.toLowerCase() && (input[i - 1] || ' ').match(/\W/)) {
  43. capVar = true;
  44. output += '<span class="cap-var">' + c;
  45. } else if (capVar && c.match(/\W/)) {
  46. capVar = false;
  47. output += '</span>';
  48. i--;
  49. } else if (input.substr(i, 9) == 'prototype') {
  50. output += '<span class="prototype">prototype</span>';
  51. i += 8;
  52. } else if (input.substr(i, 8) == 'function') {
  53. var j = i,
  54. l = c;
  55. while (l = input[--j]) {
  56. if (l == '=') {
  57. var end = output.length + j - i;
  58. output = output.substring(0, end) + '<span class="operator assigns">=</span>' + output.substr(end + 1);
  59. j = end;
  60. while (output[--j].match(/\s/)) end--;
  61. var start = end;
  62. j++;
  63. while (j && !output.replaceAll('<span class=', '<span-class-')[--j].match(/[\s=(]/)) start--;
  64. output = output.substring(0, start) + '<span class="function-name">' + output.substring(start, end) + '</span>' + output.substr(end);
  65. break;
  66. } else if (!l.match(/\s/)) break;
  67. }
  68. output += '<span class="keyword">function</span>';
  69. i += 6;
  70. var stop = ++i + input.substr(i).indexOf('(');
  71. if (!stop) return warnings.push([i, 'Function arguments not found.']);
  72. if ((c = input[++i]).match(/\s/)) {
  73. output += c + '<span class="function-name">';
  74. while ((c = input[++i]) && c != '(') output += c;
  75. output += '</span>';
  76. }
  77. while (i < stop) output += input[++i];
  78. output += '<span class="punctuation">(</span>';
  79. var argString = '';
  80. while ((c = input[++i]) && c != ')') argString += c;
  81. output += argString.replace(/[^\s,]+/g, '<span class="arguments">$&</span>').replaceAll(',', '<span class="punctuation">,</span>') + '<span class="punctuation">)</span>';
  82. } else if (',.;()[]{}'.indexOf(c) != -1) {
  83. output += '<span class="punctuation">' + c + '</span>';
  84. } else output += c;
  85. }
  86. console.log(output);
  87. console.log(warnings);
  88. return output.split('\n');
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement