1local info = {
2 version = 1.002,
3 comment = "scintilla lpeg lexer for bibtex",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files",
7}
8
9local global, string, table, lpeg = _G, string, table, lpeg
10local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V
11local type = type
12
13local lexer = require("scite-context-lexer")
14local context = lexer.context
15local patterns = context.patterns
16
17local token = lexer.token
18local exact_match = lexer.exact_match
19
20local bibtexlexer = lexer.new("bib","scite-context-lexer-bibtex")
21local whitespace = bibtexlexer.whitespace
22
23local escape, left, right = P("\\"), P('{'), P('}')
24
25patterns.balanced = P {
26 [1] = ((escape * (left+right)) + (1 - (left+right)) + V(2))^0,
27 [2] = left * V(1) * right
28}
29
30
31
32local anything = patterns.anything
33local percent = P("%")
34local start = P("@")
35local comma = P(",")
36local hash = P("#")
37local escape = P("\\")
38local single = P("'")
39local double = P('"')
40local left = P('{')
41local right = P('}')
42local lineending = S("\n\r")
43local space = S(" \t\n\r\f")
44local spaces = space^1
45local equal = P("=")
46
47local keyword = (R("az","AZ","09") + S("@_:-"))^1
48
49
50local s_quoted = ((escape*single) + (1-single))^0
51local d_quoted = ((escape*double) + (1-double))^0
52
53local balanced = patterns.balanced
54
55local t_spacing = token(whitespace, space^1)
56local t_optionalws = token("default", space^1)^0
57
58local t_equal = token("operator",equal)
59local t_left = token("grouping",left)
60local t_right = token("grouping",right)
61local t_comma = token("operator",comma)
62local t_hash = token("operator",hash)
63
64local t_s_value = token("operator",single)
65 * token("text",s_quoted)
66 * token("operator",single)
67local t_d_value = token("operator",double)
68 * token("text",d_quoted)
69 * token("operator",double)
70local t_b_value = token("operator",left)
71 * token("text",balanced)
72 * token("operator",right)
73local t_r_value = token("text",keyword)
74
75local t_keyword = token("keyword",keyword)
76local t_key = token("command",keyword)
77local t_label = token("warning",keyword)
78
79local t_somevalue = t_s_value + t_d_value + t_b_value + t_r_value
80local t_value = t_somevalue
81 * ((t_optionalws * t_hash * t_optionalws) * t_somevalue)^0
82
83local t_assignment = t_optionalws
84 * t_key
85 * t_optionalws
86 * t_equal
87 * t_optionalws
88 * t_value
89
90local t_shortcut = t_keyword
91 * t_optionalws
92 * t_left
93 * t_optionalws
94 * (t_assignment * t_comma^0)^0
95 * t_optionalws
96 * t_right
97
98local t_definition = t_keyword
99 * t_optionalws
100 * t_left
101 * t_optionalws
102 * t_label
103 * t_optionalws
104 * t_comma
105 * (t_assignment * t_comma^0)^0
106 * t_optionalws
107 * t_right
108
109local t_comment = t_keyword
110 * t_optionalws
111 * t_left
112 * token("text",(1-t_right)^0)
113 * t_optionalws
114 * t_right
115
116local t_forget = token("comment",percent^1 * (1-lineending)^0)
117
118local t_rest = token("default",anything)
119
120
121
122
123
124
125
126
127
128
129
130local dummylexer = lexer.load("scite-context-lexer-dummy","bib-dum")
131
132local dummystart = token("embedded",P("\001"))
133local dummystop = token("embedded",P("\002"))
134
135lexer.embed_lexer(bibtexlexer,dummylexer,dummystart,dummystop)
136
137
138
139
140
141bibtexlexer._rules = {
142 { "whitespace", t_spacing },
143 { "forget", t_forget },
144 { "shortcut", t_shortcut },
145 { "definition", t_definition },
146 { "comment", t_comment },
147 { "rest", t_rest },
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180bibtexlexer._tokenstyles = context.styleset
181
182bibtexlexer._foldpattern = P("{") + P("}")
183
184bibtexlexer._foldsymbols = {
185 _patterns = {
186 "{",
187 "}",
188 },
189 ["grouping"] = {
190 ["{"] = 1,
191 ["}"] = -1,
192 },
193}
194
195return bibtexlexer
196 |