resource.py 2.9 KB

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