Ohm-Management - Projektarbeit B-ME
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.

mark.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. var common = require('./common');
  3. function Mark(name, buffer, position, line, column) {
  4. this.name = name;
  5. this.buffer = buffer;
  6. this.position = position;
  7. this.line = line;
  8. this.column = column;
  9. }
  10. Mark.prototype.getSnippet = function getSnippet(indent, maxLength) {
  11. var head, start, tail, end, snippet;
  12. if (!this.buffer) return null;
  13. indent = indent || 4;
  14. maxLength = maxLength || 75;
  15. head = '';
  16. start = this.position;
  17. while (start > 0 && '\x00\r\n\x85\u2028\u2029'.indexOf(this.buffer.charAt(start - 1)) === -1) {
  18. start -= 1;
  19. if (this.position - start > (maxLength / 2 - 1)) {
  20. head = ' ... ';
  21. start += 5;
  22. break;
  23. }
  24. }
  25. tail = '';
  26. end = this.position;
  27. while (end < this.buffer.length && '\x00\r\n\x85\u2028\u2029'.indexOf(this.buffer.charAt(end)) === -1) {
  28. end += 1;
  29. if (end - this.position > (maxLength / 2 - 1)) {
  30. tail = ' ... ';
  31. end -= 5;
  32. break;
  33. }
  34. }
  35. snippet = this.buffer.slice(start, end);
  36. return common.repeat(' ', indent) + head + snippet + tail + '\n' +
  37. common.repeat(' ', indent + this.position - start + head.length) + '^';
  38. };
  39. Mark.prototype.toString = function toString(compact) {
  40. var snippet, where = '';
  41. if (this.name) {
  42. where += 'in "' + this.name + '" ';
  43. }
  44. where += 'at line ' + (this.line + 1) + ', column ' + (this.column + 1);
  45. if (!compact) {
  46. snippet = this.getSnippet();
  47. if (snippet) {
  48. where += ':\n' + snippet;
  49. }
  50. }
  51. return where;
  52. };
  53. module.exports = Mark;