bfe279ba52c3a9fabeac1558af693012a29c5f59
[platform/framework/web/crosswalk-tizen.git] /
1 'use strict';
2
3
4 var common = require('./common');
5
6
7 function Mark(name, buffer, position, line, column) {
8   this.name     = name;
9   this.buffer   = buffer;
10   this.position = position;
11   this.line     = line;
12   this.column   = column;
13 }
14
15
16 Mark.prototype.getSnippet = function getSnippet(indent, maxLength) {
17   var head, start, tail, end, snippet;
18
19   if (!this.buffer) {
20     return null;
21   }
22
23   indent = indent || 4;
24   maxLength = maxLength || 75;
25
26   head = '';
27   start = this.position;
28
29   while (start > 0 && -1 === '\x00\r\n\x85\u2028\u2029'.indexOf(this.buffer.charAt(start - 1))) {
30     start -= 1;
31     if (this.position - start > (maxLength / 2 - 1)) {
32       head = ' ... ';
33       start += 5;
34       break;
35     }
36   }
37
38   tail = '';
39   end = this.position;
40
41   while (end < this.buffer.length && -1 === '\x00\r\n\x85\u2028\u2029'.indexOf(this.buffer.charAt(end))) {
42     end += 1;
43     if (end - this.position > (maxLength / 2 - 1)) {
44       tail = ' ... ';
45       end -= 5;
46       break;
47     }
48   }
49
50   snippet = this.buffer.slice(start, end);
51
52   return common.repeat(' ', indent) + head + snippet + tail + '\n' +
53          common.repeat(' ', indent + this.position - start + head.length) + '^';
54 };
55
56
57 Mark.prototype.toString = function toString(compact) {
58   var snippet, where = '';
59
60   if (this.name) {
61     where += 'in "' + this.name + '" ';
62   }
63
64   where += 'at line ' + (this.line + 1) + ', column ' + (this.column + 1);
65
66   if (!compact) {
67     snippet = this.getSnippet();
68
69     if (snippet) {
70       where += ':\n' + snippet;
71     }
72   }
73
74   return where;
75 };
76
77
78 module.exports = Mark;