-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrammar.js
148 lines (119 loc) · 3.55 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
// This is a modified version of the following grammar:
// https://github.com/Aerijo/tree-sitter-bibtex/blob/master/grammar.js
function ignoreCase(str) {
return new RegExp(
str
.split('')
.map(c =>
/[a-zA-Z]/.test(c) ? `[${c.toLowerCase()}${c.toUpperCase()}]` : c
)
.join('')
);
}
const IDENTIFIER_FIRST = /[\!\$\&\*\+\-\.\/:;<>\?@\[\]\\\^_`\|\~a-zA-Z]/;
const IDENTIFIER_LATER = /[\!\$\&\*\+\-\.\/:;<>\?@\[\]\\\^_`\|\~a-zA-Z0-9]/;
const IDENTIFIER = seq(IDENTIFIER_FIRST, repeat(IDENTIFIER_LATER));
/*
Adapted from the PEG grammar given here https://github.com/aclements/biblib
whitespace is ignored by default (tree-sitter-cli)
- it will be recognised if the `extras` property is added though
*/
module.exports = grammar({
name: 'bibtex',
rules: {
document: $ => repeat(choice($._command_or_entry, $.junk)),
junk: $ => /[^@\r\n\s\t][^@]*/,
_command_or_entry: $ => choice($.comment, $.preamble, $.string, $.entry),
comment: $ => token(seq('@', ignoreCase('comment'))),
string_type: $ => token(seq('@', ignoreCase('string'))),
string: $ =>
seq(
field('ty', $.string_type),
choice(
seq(
'{',
field('name', $.identifier),
'=',
field('value', $.value),
'}'
),
seq(
'(',
field('name', $.identifier),
'=',
field('value', $.value),
')'
)
)
),
preamble_type: $ => token(seq('@', ignoreCase('preamble'))),
preamble: $ =>
seq(
field('ty', $.preamble_type),
choice(
seq('{', field('value', $.value), '}'),
seq('(', field('value', $.value), ')')
)
),
entry_type: $ => token(seq('@', IDENTIFIER)),
entry: $ =>
seq(
field('ty', $.entry_type),
choice(
seq(
'{',
field('key', $.key_brace),
repeat(seq(',', field('field', $.field))),
optional(','),
'}'
),
seq(
'(',
field('key', $.key_paren),
repeat(seq(',', field('field', $.field))),
optional(','),
')'
)
)
),
key_brace: $ => /[^,\s\t\n\}]*/, // "braces key" / can be empty (will not throw error) but cannot be referenced this way
key_paren: $ => /[^,\s\t\n]*/, // "parentheses key" // the ) is actually allowed
field: $ => seq(field('name', $.identifier), '=', field('value', $.value)),
identifier: $ => token(IDENTIFIER),
value: $ => seq($.token, repeat(seq('#', $.token))),
token: $ =>
choice(
seq('{', repeat($._brace_balanced), '}'),
seq('"', repeat($._quote_balanced), '"'),
$.number,
$.identifier
),
number: $ => /[0-9]+/,
_brace_balanced: $ =>
prec.right(
choice(
seq('{', repeat($._brace_balanced), '}'),
repeat1(choice($.brace_word, $.command))
)
),
_quote_balanced: $ =>
prec.right(
choice(
seq('{', repeat($._brace_balanced), '}'),
repeat1(choice($.quote_word, $.command))
)
),
brace_word: $ => /[^\{\}\\\s]+/,
quote_word: $ => /[^\"\{\}\\\s]+/,
command: $ =>
prec.right(
seq(
field('name', $.command_name),
optional(seq('{', repeat($._quote_balanced), '}'))
)
),
command_name: $ => /\\([^\r\n]|[@a-zA-Z]+\*?)?/,
},
});