codekingpro/portable-devtools
114k
1"""2 pygments.lexers.bibtex3 ~~~~~~~~~~~~~~~~~~~~~~4 5 Lexers for BibTeX bibliography data and styles6 7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.8 :license: BSD, see LICENSE for details.9"""10 11import re12 13from pygments.lexer import RegexLexer, ExtendedRegexLexer, include, default, \14 words15from pygments.token import Name, Comment, String, Error, Number, Keyword, \16 Punctuation, Whitespace17 18__all__ = ['BibTeXLexer', 'BSTLexer']19 20 21class BibTeXLexer(ExtendedRegexLexer):22 """23 A lexer for BibTeX bibliography data format.24 """25 26 name = 'BibTeX'27 aliases = ['bibtex', 'bib']28 filenames = ['*.bib']29 mimetypes = ["text/x-bibtex"]30 version_added = '2.2'31 flags = re.IGNORECASE32 url = 'https://texfaq.org/FAQ-BibTeXing'33 34 ALLOWED_CHARS = r'@!$&*+\-./:;<>?\[\\\]^`|~'35 IDENTIFIER = '[{}][{}]*'.format('a-z_' + ALLOWED_CHARS, r'\w' + ALLOWED_CHARS)36 37 def open_brace_callback(self, match, ctx):38 opening_brace = match.group()39 ctx.opening_brace = opening_brace40 yield match.start(), Punctuation, opening_brace41 ctx.pos = match.end()42 43 def close_brace_callback(self, match, ctx):44 closing_brace = match.group()45 if (46 ctx.opening_brace == '{' and closing_brace != '}' or47 ctx.opening_brace == '(' and closing_brace != ')'48 ):49 yield match.start(), Error, closing_brace50 else:51 yield match.start(), Punctuation, closing_brace52 del ctx.opening_brace53 ctx.pos = match.end()54 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 }114 115 116class BSTLexer(RegexLexer):117 """118 A lexer for BibTeX bibliography styles.119 """120 121 name = 'BST'122 aliases = ['bst', 'bst-pybtex']123 filenames = ['*.bst']124 version_added = '2.2'125 flags = re.IGNORECASE | re.MULTILINE126 url = 'https://texfaq.org/FAQ-BibTeXing'127 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 }160 