Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 573e6a6

Browse files
Turbo87ericf
authored andcommitted
Include "location" information in nodes (#17)
* Update "grunt-peg" to v2.0.1 * Include "location" information in nodes
1 parent f2616bb commit 573e6a6

File tree

3 files changed

+109
-10
lines changed

3 files changed

+109
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"grunt-cli": "^0.1.13",
3838
"grunt-contrib-clean": "^0.6.0",
3939
"grunt-contrib-copy": "^0.7.0",
40-
"grunt-peg": "^1.5.0",
40+
"grunt-peg": "^2.0.1",
4141
"istanbul": "^0.3.2",
4242
"mocha": "^2.0.1",
4343
"xunit-file": "0.0.6"

src/parser.pegjs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ messageFormatPattern
1818
= elements:messageFormatElement* {
1919
return {
2020
type : 'messageFormatPattern',
21-
elements: elements
21+
elements: elements,
22+
location: location()
2223
};
2324
}
2425

@@ -47,7 +48,8 @@ messageTextElement
4748
= messageText:messageText {
4849
return {
4950
type : 'messageTextElement',
50-
value: messageText
51+
value: messageText,
52+
location: location()
5153
};
5254
}
5355

@@ -60,7 +62,8 @@ argumentElement
6062
return {
6163
type : 'argumentElement',
6264
id : id,
63-
format: format && format[2]
65+
format: format && format[2],
66+
location: location()
6467
};
6568
}
6669

@@ -74,7 +77,8 @@ simpleFormat
7477
= type:('number' / 'date' / 'time') _ style:(',' _ chars)? {
7578
return {
7679
type : type + 'Format',
77-
style: style && style[2]
80+
style: style && style[2],
81+
location: location()
7882
};
7983
}
8084

@@ -84,7 +88,8 @@ pluralFormat
8488
type : pluralStyle.type,
8589
ordinal: false,
8690
offset : pluralStyle.offset || 0,
87-
options: pluralStyle.options
91+
options: pluralStyle.options,
92+
location: location()
8893
};
8994
}
9095

@@ -94,15 +99,17 @@ selectOrdinalFormat
9499
type : pluralStyle.type,
95100
ordinal: true,
96101
offset : pluralStyle.offset || 0,
97-
options: pluralStyle.options
102+
options: pluralStyle.options,
103+
location: location()
98104
}
99105
}
100106

101107
selectFormat
102108
= 'select' _ ',' _ options:optionalFormatPattern+ {
103109
return {
104110
type : 'selectFormat',
105-
options: options
111+
options: options,
112+
location: location()
106113
};
107114
}
108115

@@ -115,7 +122,8 @@ optionalFormatPattern
115122
return {
116123
type : 'optionalFormatPattern',
117124
selector: selector,
118-
value : pattern
125+
value : pattern,
126+
location: location()
119127
};
120128
}
121129

@@ -129,7 +137,8 @@ pluralStyle
129137
return {
130138
type : 'pluralFormat',
131139
offset : offset,
132-
options: options
140+
options: options,
141+
location: location()
133142
};
134143
}
135144

test/unit/parser.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ describe('parse()', function () {
5050
expect(element.type).to.equal('messageTextElement');
5151
expect(element).to.have.property('value');
5252
expect(element.value).to.equal(msg);
53+
expect(element).to.have.property('location');
54+
expect(element.location).to.eql({
55+
start: { offset: 0, line: 1, column: 1 },
56+
end: { offset: 13, line: 1, column: 14 },
57+
});
5358
});
5459
});
5560

@@ -64,6 +69,11 @@ describe('parse()', function () {
6469
it('should first contain a `messageTextElement`', function () {
6570
var element = ast.elements[0];
6671
expect(element.value).to.equal('Hello, ');
72+
expect(element).to.have.property('location');
73+
expect(element.location).to.eql({
74+
start: { offset: 0, line: 1, column: 1 },
75+
end: { offset: 7, line: 1, column: 8 },
76+
});
6777
});
6878

6979
it('should then contain an `argumentElement`', function () {
@@ -75,11 +85,21 @@ describe('parse()', function () {
7585
expect(element.id).to.equal('name');
7686
expect(element).to.have.property('format');
7787
expect(element.format).to.equal(null);
88+
expect(element).to.have.property('location');
89+
expect(element.location).to.eql({
90+
start: { offset: 7, line: 1, column: 8 },
91+
end: { offset: 13, line: 1, column: 14 },
92+
});
7893
});
7994

8095
it('should finally contain a `messageTextElement`', function () {
8196
var element = ast.elements[2];
8297
expect(element.value).to.equal('!');
98+
expect(element).to.have.property('location');
99+
expect(element.location).to.eql({
100+
start: { offset: 13, line: 1, column: 14 },
101+
end: { offset: 14, line: 1, column: 15 },
102+
});
83103
});
84104
});
85105

@@ -98,6 +118,11 @@ describe('parse()', function () {
98118
expect(element.type).to.equal('argumentElement');
99119
expect(element).to.have.property('id');
100120
expect(element.id).to.equal('num');
121+
expect(element).to.have.property('location');
122+
expect(element.location).to.eql({
123+
start: { offset: 0, line: 1, column: 1 },
124+
end: { offset: 22, line: 1, column: 23 },
125+
});
101126
expect(element).to.have.property('format');
102127

103128
var format = element.format;
@@ -106,6 +131,11 @@ describe('parse()', function () {
106131
expect(format.type).to.equal('numberFormat');
107132
expect(format).to.have.property('style');
108133
expect(format.style).to.equal('percent');
134+
expect(format).to.have.property('location');
135+
expect(format.location).to.eql({
136+
start: { offset: 6, line: 1, column: 7 },
137+
end: { offset: 21, line: 1, column: 22 },
138+
});
109139
});
110140
});
111141

@@ -124,6 +154,11 @@ describe('parse()', function () {
124154
expect(element.type).to.equal('argumentElement');
125155
expect(element).to.have.property('id');
126156
expect(element.id).to.equal('numPhotos');
157+
expect(element).to.have.property('location');
158+
expect(element.location).to.eql({
159+
start: { offset: 0, line: 1, column: 1 },
160+
end: { offset: 64, line: 1, column: 65 },
161+
});
127162
expect(element).to.have.property('format');
128163

129164
var format = element.format;
@@ -132,6 +167,11 @@ describe('parse()', function () {
132167
expect(format.type).to.equal('pluralFormat');
133168
expect(format).to.have.property('offset');
134169
expect(format.offset).to.equal(0);
170+
expect(format).to.have.property('location');
171+
expect(format.location).to.eql({
172+
start: { offset: 12, line: 1, column: 13 },
173+
end: { offset: 63, line: 1, column: 64 },
174+
});
135175
});
136176

137177
it('should contain 3 `options`', function () {
@@ -146,6 +186,11 @@ describe('parse()', function () {
146186
expect(option.selector).to.equal('=0');
147187
expect(option).to.have.property('value');
148188
expect(option.value).to.be.an('object');
189+
expect(option).to.have.property('location');
190+
expect(option.location).to.eql({
191+
start: { offset: 20, line: 1, column: 21 },
192+
end: { offset: 33, line: 1, column: 34 },
193+
});
149194

150195
expect(options[1].selector).to.equal('=1');
151196
expect(options[2].selector).to.equal('other');
@@ -167,6 +212,11 @@ describe('parse()', function () {
167212
expect(element.type).to.equal('messageTextElement');
168213
expect(element).to.have.property('value');
169214
expect(element.value).to.equal('no photos');
215+
expect(element).to.have.property('location');
216+
expect(element.location).to.eql({
217+
start: { offset: 23, line: 1, column: 24 },
218+
end: { offset: 32, line: 1, column: 33 },
219+
});
170220

171221
expect(options[1].value.elements[0].value).to.equal('one photo');
172222
expect(options[2].value.elements[0].value).to.equal('# photos');
@@ -188,6 +238,11 @@ describe('parse()', function () {
188238
expect(element.type).to.equal('argumentElement');
189239
expect(element).to.have.property('id');
190240
expect(element.id).to.equal('floor');
241+
expect(element).to.have.property('location');
242+
expect(element.location).to.eql({
243+
start: { offset: 0, line: 1, column: 1 },
244+
end: { offset: 72, line: 1, column: 73 },
245+
});
191246
expect(element).to.have.property('format');
192247

193248
var format = element.format;
@@ -197,6 +252,11 @@ describe('parse()', function () {
197252
expect(format).to.have.property('offset');
198253
expect(format.offset).to.equal(0);
199254
expect(format.ordinal).to.equal(true);
255+
expect(format).to.have.property('location');
256+
expect(format.location).to.eql({
257+
start: { offset: 8, line: 1, column: 9 },
258+
end: { offset: 71, line: 1, column: 72 },
259+
});
200260
});
201261

202262
it('should contain 5 `options`', function () {
@@ -211,6 +271,11 @@ describe('parse()', function () {
211271
expect(option.selector).to.equal('=0');
212272
expect(option).to.have.property('value');
213273
expect(option.value).to.be.an('object');
274+
expect(option).to.have.property('location');
275+
expect(option.location).to.eql({
276+
start: { offset: 23, line: 1, column: 24 },
277+
end: { offset: 33, line: 1, column: 34 },
278+
});
214279

215280
expect(options[1].selector).to.equal('one');
216281
expect(options[2].selector).to.equal('two');
@@ -234,6 +299,11 @@ describe('parse()', function () {
234299
expect(element.type).to.equal('messageTextElement');
235300
expect(element).to.have.property('value');
236301
expect(element.value).to.equal('ground');
302+
expect(element).to.have.property('location');
303+
expect(element.location).to.eql({
304+
start: { offset: 26, line: 1, column: 27 },
305+
end: { offset: 32, line: 1, column: 33 },
306+
});
237307

238308
expect(options[0].value.elements[0].value).to.equal('ground');
239309
expect(options[1].value.elements[0].value).to.equal('#st');
@@ -258,12 +328,22 @@ describe('parse()', function () {
258328
expect(element.type).to.equal('argumentElement');
259329
expect(element).to.have.property('id');
260330
expect(element.id).to.equal('gender');
331+
expect(element).to.have.property('location');
332+
expect(element.location).to.eql({
333+
start: { offset: 0, line: 1, column: 1 },
334+
end: { offset: 58, line: 1, column: 59 },
335+
});
261336
expect(element).to.have.property('format');
262337

263338
var format = element.format;
264339
expect(format).to.be.an('object');
265340
expect(format).to.have.property('type');
266341
expect(format.type).to.equal('selectFormat');
342+
expect(format).to.have.property('location');
343+
expect(format.location).to.eql({
344+
start: { offset: 9, line: 1, column: 10 },
345+
end: { offset: 57, line: 1, column: 58 },
346+
});
267347
});
268348

269349
it('should contain 3 `options`', function () {
@@ -278,6 +358,11 @@ describe('parse()', function () {
278358
expect(option.selector).to.equal('female');
279359
expect(option).to.have.property('value');
280360
expect(option.value).to.be.an('object');
361+
expect(option).to.have.property('location');
362+
expect(option.location).to.eql({
363+
start: { offset: 17, line: 1, column: 18 },
364+
end: { offset: 31, line: 1, column: 32 },
365+
});
281366

282367
expect(options[1].selector).to.equal('male');
283368
expect(options[2].selector).to.equal('other');
@@ -299,6 +384,11 @@ describe('parse()', function () {
299384
expect(element.type).to.equal('messageTextElement');
300385
expect(element).to.have.property('value');
301386
expect(element.value).to.equal('woman');
387+
expect(element).to.have.property('location');
388+
expect(element.location).to.eql({
389+
start: { offset: 25, line: 1, column: 26 },
390+
end: { offset: 30, line: 1, column: 31 },
391+
});
302392

303393
expect(options[1].value.elements[0].value).to.equal('man');
304394
expect(options[2].value.elements[0].value).to.equal('person');

0 commit comments

Comments
 (0)