Skip to content
This repository was archived by the owner on Dec 17, 2018. It is now read-only.

Commit bcb7577

Browse files
committed
Split token types into named expressions
1 parent bb2b000 commit bcb7577

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

parser.pegjs

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
11
start = token*
22

33
token
4-
= '{' _ arg:id _ '}' {
5-
return {
6-
type: 'argument',
7-
arg: arg
8-
};
9-
}
10-
/ '{' _ arg:id _ ',' _ 'select' _ ',' _ cases:selectCase+ _ '}' {
11-
return {
12-
type: 'select',
13-
arg: arg,
14-
cases: cases
15-
};
16-
}
17-
/ '{' _ arg:id _ ',' _ type:('plural'/'selectordinal') _ ',' _ offset:offset? cases:pluralCase+ _ '}' {
18-
var ls = ((type === 'selectordinal') ? options.ordinal : options.cardinal)
19-
|| ['zero', 'one', 'two', 'few', 'many', 'other'];
20-
if (ls && ls.length) cases.forEach(function(c) {
21-
if (isNaN(c.key) && ls.indexOf(c.key) < 0) throw new Error(
22-
'Invalid key `' + c.key + '` for argument `' + arg + '`.' +
23-
' Valid ' + type + ' keys for this locale are `' + ls.join('`, `') +
24-
'`, and explicit keys like `=0`.');
25-
});
26-
return {
27-
type: type,
28-
arg: arg,
29-
offset: offset || 0,
30-
cases: cases
31-
};
32-
}
33-
/ '{' _ arg:id _ ',' _ key:id _ params:functionParams* '}' {
34-
return {
35-
type: 'function',
36-
arg: arg,
37-
key: key,
38-
params: params
39-
};
40-
}
4+
= argument / select / plural / function
415
/ '#' { return { type: 'octothorpe' }; }
426
/ str:char+ { return str.join(''); }
437

8+
argument = '{' _ arg:id _ '}' {
9+
return {
10+
type: 'argument',
11+
arg: arg
12+
};
13+
}
14+
15+
select = '{' _ arg:id _ ',' _ 'select' _ ',' _ cases:selectCase+ _ '}' {
16+
return {
17+
type: 'select',
18+
arg: arg,
19+
cases: cases
20+
};
21+
}
22+
23+
plural = '{' _ arg:id _ ',' _ type:('plural'/'selectordinal') _ ',' _ offset:offset? cases:pluralCase+ _ '}' {
24+
var ls = ((type === 'selectordinal') ? options.ordinal : options.cardinal)
25+
|| ['zero', 'one', 'two', 'few', 'many', 'other'];
26+
if (ls && ls.length) cases.forEach(function(c) {
27+
if (isNaN(c.key) && ls.indexOf(c.key) < 0) throw new Error(
28+
'Invalid key `' + c.key + '` for argument `' + arg + '`.' +
29+
' Valid ' + type + ' keys for this locale are `' + ls.join('`, `') +
30+
'`, and explicit keys like `=0`.');
31+
});
32+
return {
33+
type: type,
34+
arg: arg,
35+
offset: offset || 0,
36+
cases: cases
37+
};
38+
}
39+
40+
function = '{' _ arg:id _ ',' _ key:id _ params:functionParams* '}' {
41+
return {
42+
type: 'function',
43+
arg: arg,
44+
key: key,
45+
params: params
46+
};
47+
}
48+
4449
id = $([0-9a-zA-Z$_][^ \t\n\r,.+={}]*)
4550

4651
selectCase = _ key:id _ tokens:caseTokens { return { key: key, tokens: tokens }; }

0 commit comments

Comments
 (0)