-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.js
131 lines (116 loc) · 2.75 KB
/
grammar.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
/**
* @file PO grammar for tree-sitter
* @author Erasin Wang <[email protected]>
* @author Amaan Qureshi <[email protected]>
* @license MIT
* @see {@link https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html|official syntax spec}
*/
/* eslint-disable arrow-parens */
/* eslint-disable camelcase */
/* eslint-disable-next-line spaced-comment */
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: 'po',
rules: {
source_file: $ => repeat(
choice(
$.message,
$.comment,
),
),
message: $ => seq(
optional($.msgctxt),
$.msgid,
optional($.msgid_plural),
optional($.msgstr),
optional($.msgstr_plural),
),
msgctxt: $ => seq('msgctxt', $.string),
msgid: $ => seq(
choice(
'msgid',
seq('msgid', '[', $.number, ']'),
),
repeat1($.string),
),
msgid_plural: $ => seq(
choice(
'msgid_plural',
seq('msgid_plural', '[', $.number, ']'),
),
repeat1($.string),
),
msgstr: $ => choice(
repeat1(seq(
'msgstr',
'[',
$.number,
']',
repeat1($.string),
)),
seq('msgstr', repeat1($.string)),
),
msgstr_plural: $ => seq(
choice(
'msgstr_plural',
seq('msgstr_plural', '[', $.number, ']'),
),
$.string,
),
comment: $ => choice(
$.translator_comment,
$.extracted_comment,
$.reference,
$.flag,
$.previous_untranslated_string,
seq('#', $.text),
),
translator_comment: $ => seq('#.', optional($.text)),
extracted_comment: $ => seq(
'#|',
optional(choice('msgid', 'msgid_plural')),
$.string,
),
reference: $ => seq('#:', optional($.text)),
flag: $ => seq('#,', optional($.text)),
previous_untranslated_string: $ => seq(
choice('#~', '#~|'),
optional(choice(
'msgctxt',
'msgid',
'msgid_plural',
'msgstr',
seq('msgstr', '[', $.number, ']'),
)),
$.string,
),
number: _ => /([0-9]+|[0-9][0-9,]+[0-9])(\.[0-9]*)?/,
string: $ => choice(
seq(
'"',
repeat(choice(
$.string_fragment,
$._escape_sequence,
)),
'"',
),
),
string_fragment: _ => token.immediate(prec(1, /[^"\\]+/)),
_escape_sequence: $ =>
choice(
prec(2, token.immediate(seq('\\', /[^abfnrtvxu'\"\\\?]/))),
prec(1, $.escape_sequence),
),
escape_sequence: _ => token.immediate(seq(
'\\',
choice(
/[^xu0-7]/,
/[0-7]{1,3}/,
/x[0-9a-fA-F]{2}/,
/u[0-9a-fA-F]{4}/,
/u\{[0-9a-fA-F]+\}/,
))),
text: _ => /.*/,
},
});