-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (153 loc) · 3.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var EventEmitter = require("events");
var util = require("util");
function SimpleApiParser() {
EventEmitter.call(this);
this.inValue = false;
this.inEntity = false;
this.index = 0;
this.string = null;
this.end = false;
this._space = /\s/;
}
util.inherits(SimpleApiParser, EventEmitter);
SimpleApiParser.prototype._checkUnexpectedDocEnd = function() {
if(this.index >= this.length) {
throw new Error("Unexpected document end");
}
}
SimpleApiParser.prototype._skipSpaces = function() {
for(;;) {
this.index++;
this._checkUnexpectedDocEnd();
if(!this._space.test(this.string[this.index])) {
break;
}
}
}
SimpleApiParser.prototype._readUntil = function(char) {
for(;;) {
this.index++;
this._checkUnexpectedDocEnd();
if(this.string[this.index] === char) {
break;
}
}
this.index--;
}
SimpleApiParser.prototype._getNext = function(n) {
if(n <= 0) {
return "";
}
var res = "";
while(n--) {
res += this.string[this.index];
this.index++;
}
return res;
}
SimpleApiParser.prototype._next = function () {
var char;
if (this.index === 0) {
this.emit("document_start");
}
if (this.index >= this.string.length) {
this.end = true;
return this.emit("document_end");
}
var symbol = this.string[this.index];
if (symbol === "<") {
this.index++;
var next7Symbols = this._getNext(7);
if(next7Symbols.toLowerCase() !== "!entity") {
throw new Error("Expected !entity, got \"" + next7Symbols + "(" + next7Symbols.toLowerCase() + ")\"");
}
// reading entity name
// first of skip space chars
this._skipSpaces();
// reading entity name until space
var entityName = "";
for (;;) {
this._checkUnexpectedDocEnd();
char = this.string[this.index];
if(this._space.test(char)) {
break;
}
this.index++;
entityName += char;
}
this.inEntity = true;
this.entityValueQuote = false;
this.emit("entity_name", entityName);
this._skipSpaces();
char = this.string[this.index];
var entityValueQuote = null;
if (char === '"' || char === "'") {
entityValueQuote = char;
}
var value = "";
var symbolEscaped = false;
var addChar;
for (;;) {
addChar = true;
this.index++;
this._checkUnexpectedDocEnd();
char = this.string[this.index];
if(entityValueQuote) {
if (!symbolEscaped && char === entityValueQuote) {
break;
}
} else if (this._space.test(char)) {
this._readUntil(">");
break;
} else if (char === ">") {
break;
}
if (symbolEscaped) {
symbolEscaped = false;
} else if (entityValueQuote && char === '\\') {
var nextSymbol = this.string[this.index + 1];
if(nextSymbol === entityValueQuote) {
symbolEscaped = true;
addChar = false;
}
}
if(addChar) {
value += char;
}
}
this.emit("entity_value", value);
}
this.index++;
};
SimpleApiParser.prototype.parse = function(str) {
this.string = str;
var i = 0;
while (!this._end) {
i++;
if(i > this.string.length) {
break;
}
this._next();
}
};
exports.parse = function(string) {
var parser = new SimpleApiParser();
var res = {};
var currentName;
parser.on("entity_name", function(name) {
currentName = name;
});
parser.on("entity_value", function(value) {
res[currentName] = value;
});
parser.parse(string);
return res;
};
exports.stringify = function(object) {
var strings = [];
for(var key in object) {
var value = object[key].replace(/"/g, "\\\"");
strings.push('<!ENTITY ' + key + ' "' + value + '">');
}
return strings.join("\n");
};