Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/bibtex.py: 68%
31 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
1"""
2 pygments.lexers.bibtex
3 ~~~~~~~~~~~~~~~~~~~~~~
5 Lexers for BibTeX bibliography data and styles
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11import re
13from pygments.lexer import RegexLexer, ExtendedRegexLexer, include, default, \
14 words
15from pygments.token import Name, Comment, String, Error, Number, Keyword, \
16 Punctuation, Whitespace
18__all__ = ['BibTeXLexer', 'BSTLexer']
21class BibTeXLexer(ExtendedRegexLexer):
22 """
23 A lexer for BibTeX bibliography data format.
25 .. versionadded:: 2.2
26 """
28 name = 'BibTeX'
29 aliases = ['bibtex', 'bib']
30 filenames = ['*.bib']
31 mimetypes = ["text/x-bibtex"]
32 flags = re.IGNORECASE
34 ALLOWED_CHARS = r'@!$&*+\-./:;<>?\[\\\]^`|~'
35 IDENTIFIER = '[{}][{}]*'.format('a-z_' + ALLOWED_CHARS, r'\w' + ALLOWED_CHARS)
37 def open_brace_callback(self, match, ctx):
38 opening_brace = match.group()
39 ctx.opening_brace = opening_brace
40 yield match.start(), Punctuation, opening_brace
41 ctx.pos = match.end()
43 def close_brace_callback(self, match, ctx):
44 closing_brace = match.group()
45 if (
46 ctx.opening_brace == '{' and closing_brace != '}' or
47 ctx.opening_brace == '(' and closing_brace != ')'
48 ):
49 yield match.start(), Error, closing_brace
50 else:
51 yield match.start(), Punctuation, closing_brace
52 del ctx.opening_brace
53 ctx.pos = match.end()
55 tokens = {
56 'root': [
57 include('whitespace'),
58 (r'@comment(?!ary)', Comment),
59 ('@preamble', Name.Class, ('closing-brace', 'value', 'opening-brace')),
60 ('@string', Name.Class, ('closing-brace', 'field', 'opening-brace')),
61 ('@' + IDENTIFIER, Name.Class,
62 ('closing-brace', 'command-body', 'opening-brace')),
63 ('.+', Comment),
64 ],
65 'opening-brace': [
66 include('whitespace'),
67 (r'[{(]', open_brace_callback, '#pop'),
68 ],
69 'closing-brace': [
70 include('whitespace'),
71 (r'[})]', close_brace_callback, '#pop'),
72 ],
73 'command-body': [
74 include('whitespace'),
75 (r'[^\s\,\}]+', Name.Label, ('#pop', 'fields')),
76 ],
77 'fields': [
78 include('whitespace'),
79 (',', Punctuation, 'field'),
80 default('#pop'),
81 ],
82 'field': [
83 include('whitespace'),
84 (IDENTIFIER, Name.Attribute, ('value', '=')),
85 default('#pop'),
86 ],
87 '=': [
88 include('whitespace'),
89 ('=', Punctuation, '#pop'),
90 ],
91 'value': [
92 include('whitespace'),
93 (IDENTIFIER, Name.Variable),
94 ('"', String, 'quoted-string'),
95 (r'\{', String, 'braced-string'),
96 (r'[\d]+', Number),
97 ('#', Punctuation),
98 default('#pop'),
99 ],
100 'quoted-string': [
101 (r'\{', String, 'braced-string'),
102 ('"', String, '#pop'),
103 (r'[^\{\"]+', String),
104 ],
105 'braced-string': [
106 (r'\{', String, '#push'),
107 (r'\}', String, '#pop'),
108 (r'[^\{\}]+', String),
109 ],
110 'whitespace': [
111 (r'\s+', Whitespace),
112 ],
113 }
116class BSTLexer(RegexLexer):
117 """
118 A lexer for BibTeX bibliography styles.
120 .. versionadded:: 2.2
121 """
123 name = 'BST'
124 aliases = ['bst', 'bst-pybtex']
125 filenames = ['*.bst']
126 flags = re.IGNORECASE | re.MULTILINE
128 tokens = {
129 'root': [
130 include('whitespace'),
131 (words(['read', 'sort']), Keyword),
132 (words(['execute', 'integers', 'iterate', 'reverse', 'strings']),
133 Keyword, ('group')),
134 (words(['function', 'macro']), Keyword, ('group', 'group')),
135 (words(['entry']), Keyword, ('group', 'group', 'group')),
136 ],
137 'group': [
138 include('whitespace'),
139 (r'\{', Punctuation, ('#pop', 'group-end', 'body')),
140 ],
141 'group-end': [
142 include('whitespace'),
143 (r'\}', Punctuation, '#pop'),
144 ],
145 'body': [
146 include('whitespace'),
147 (r"\'[^#\"\{\}\s]+", Name.Function),
148 (r'[^#\"\{\}\s]+\$', Name.Builtin),
149 (r'[^#\"\{\}\s]+', Name.Variable),
150 (r'"[^\"]*"', String),
151 (r'#-?\d+', Number),
152 (r'\{', Punctuation, ('group-end', 'body')),
153 default('#pop'),
154 ],
155 'whitespace': [
156 (r'\s+', Whitespace),
157 ('%.*?$', Comment.Single),
158 ],
159 }