resource.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """
  2. pygments.lexers.resource
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for resource definition files.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, bygroups, words
  10. from pygments.token import Comment, String, Number, Operator, Text, \
  11. Keyword, Name
  12. __all__ = ['ResourceLexer']
  13. class ResourceLexer(RegexLexer):
  14. """Lexer for `ICU Resource bundles
  15. <http://userguide.icu-project.org/locale/resources>`_.
  16. .. versionadded:: 2.0
  17. """
  18. name = 'ResourceBundle'
  19. aliases = ['resourcebundle', 'resource']
  20. filenames = []
  21. _types = (':table', ':array', ':string', ':bin', ':import', ':intvector',
  22. ':int', ':alias')
  23. flags = re.MULTILINE | re.IGNORECASE
  24. tokens = {
  25. 'root': [
  26. (r'//.*?$', Comment),
  27. (r'"', String, 'string'),
  28. (r'-?\d+', Number.Integer),
  29. (r'[,{}]', Operator),
  30. (r'([^\s{:]+)(\s*)(%s?)' % '|'.join(_types),
  31. bygroups(Name, Text, Keyword)),
  32. (r'\s+', Text),
  33. (words(_types), Keyword),
  34. ],
  35. 'string': [
  36. (r'(\\x[0-9a-f]{2}|\\u[0-9a-f]{4}|\\U00[0-9a-f]{6}|'
  37. r'\\[0-7]{1,3}|\\c.|\\[abtnvfre\'"?\\]|\\\{|[^"{\\])+', String),
  38. (r'\{', String.Escape, 'msgname'),
  39. (r'"', String, '#pop')
  40. ],
  41. 'msgname': [
  42. (r'([^{},]+)(\s*)', bygroups(Name, String.Escape), ('#pop', 'message'))
  43. ],
  44. 'message': [
  45. (r'\{', String.Escape, 'msgname'),
  46. (r'\}', String.Escape, '#pop'),
  47. (r'(,)(\s*)([a-z]+)(\s*\})',
  48. bygroups(Operator, String.Escape, Keyword, String.Escape), '#pop'),
  49. (r'(,)(\s*)([a-z]+)(\s*)(,)(\s*)(offset)(\s*)(:)(\s*)(-?\d+)(\s*)',
  50. bygroups(Operator, String.Escape, Keyword, String.Escape, Operator,
  51. String.Escape, Operator.Word, String.Escape, Operator,
  52. String.Escape, Number.Integer, String.Escape), 'choice'),
  53. (r'(,)(\s*)([a-z]+)(\s*)(,)(\s*)',
  54. bygroups(Operator, String.Escape, Keyword, String.Escape, Operator,
  55. String.Escape), 'choice'),
  56. (r'\s+', String.Escape)
  57. ],
  58. 'choice': [
  59. (r'(=|<|>|<=|>=|!=)(-?\d+)(\s*\{)',
  60. bygroups(Operator, Number.Integer, String.Escape), 'message'),
  61. (r'([a-z]+)(\s*\{)', bygroups(Keyword.Type, String.Escape), 'str'),
  62. (r'\}', String.Escape, ('#pop', '#pop')),
  63. (r'\s+', String.Escape)
  64. ],
  65. 'str': [
  66. (r'\}', String.Escape, '#pop'),
  67. (r'\{', String.Escape, 'msgname'),
  68. (r'[^{}]+', String)
  69. ]
  70. }
  71. def analyse_text(text):
  72. if text.startswith('root:table'):
  73. return 1.0