devicetree.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. pygments.lexers.devicetree
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Devicetree language.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, include, default, words
  9. from pygments.token import Comment, Keyword, Name, Number, Operator, \
  10. Punctuation, String, Text
  11. __all__ = ['DevicetreeLexer']
  12. class DevicetreeLexer(RegexLexer):
  13. """
  14. Lexer for `Devicetree <https://www.devicetree.org/>`_ files.
  15. .. versionadded:: 2.7
  16. """
  17. name = 'Devicetree'
  18. aliases = ['devicetree', 'dts']
  19. filenames = ['*.dts', '*.dtsi']
  20. mimetypes = ['text/x-c']
  21. #: optional Whitespace or /*...*/ style comment
  22. _ws = r'\s*(?:/[*][^*/]*?[*]/\s*)*'
  23. tokens = {
  24. 'macro': [
  25. # Include preprocessor directives (C style):
  26. (r'(#include)(' + _ws + r')([^\n]+)',
  27. bygroups(Comment.Preproc, Comment.Multiline, Comment.PreprocFile)),
  28. # Define preprocessor directives (C style):
  29. (r'(#define)(' + _ws + r')([^\n]+)',
  30. bygroups(Comment.Preproc, Comment.Multiline, Comment.Preproc)),
  31. # devicetree style with file:
  32. (r'(/[^*/{]+/)(' + _ws + r')("[^\n{]+")',
  33. bygroups(Comment.Preproc, Comment.Multiline, Comment.PreprocFile)),
  34. # devicetree style with property:
  35. (r'(/[^*/{]+/)(' + _ws + r')([^\n;{]*)([;]?)',
  36. bygroups(Comment.Preproc, Comment.Multiline, Comment.Preproc, Punctuation)),
  37. ],
  38. 'whitespace': [
  39. (r'\n', Text),
  40. (r'\s+', Text),
  41. (r'\\\n', Text), # line continuation
  42. (r'//(\n|[\w\W]*?[^\\]\n)', Comment.Single),
  43. (r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline),
  44. # Open until EOF, so no ending delimeter
  45. (r'/(\\\n)?[*][\w\W]*', Comment.Multiline),
  46. ],
  47. 'statements': [
  48. (r'(L?)(")', bygroups(String.Affix, String), 'string'),
  49. (r'0x[0-9a-fA-F]+', Number.Hex),
  50. (r'\d+', Number.Integer),
  51. (r'([^\s{}/*]*)(\s*)(:)', bygroups(Name.Label, Text, Punctuation), '#pop'),
  52. (words(('compatible', 'model', 'phandle', 'status', '#address-cells',
  53. '#size-cells', 'reg', 'virtual-reg', 'ranges', 'dma-ranges',
  54. 'device_type', 'name'), suffix=r'\b'), Keyword.Reserved),
  55. (r'([~!%^&*+=|?:<>/#-])', Operator),
  56. (r'[()\[\]{},.]', Punctuation),
  57. (r'[a-zA-Z_][\w-]*(?=(?:\s*,\s*[a-zA-Z_][\w-]*|(?:' + _ws + r'))*\s*[=;])',
  58. Name),
  59. (r'[a-zA-Z_]\w*', Name.Attribute),
  60. ],
  61. 'root': [
  62. include('whitespace'),
  63. include('macro'),
  64. # Nodes
  65. (r'([^/*@\s&]+|/)(@?)([0-9a-fA-F,]*)(' + _ws + r')(\{)',
  66. bygroups(Name.Function, Operator, Number.Integer,
  67. Comment.Multiline, Punctuation), 'node'),
  68. default('statement'),
  69. ],
  70. 'statement': [
  71. include('whitespace'),
  72. include('statements'),
  73. (';', Punctuation, '#pop'),
  74. ],
  75. 'node': [
  76. include('whitespace'),
  77. include('macro'),
  78. (r'([^/*@\s&]+|/)(@?)([0-9a-fA-F,]*)(' + _ws + r')(\{)',
  79. bygroups(Name.Function, Operator, Number.Integer,
  80. Comment.Multiline, Punctuation), '#push'),
  81. include('statements'),
  82. (r'\};', Punctuation, '#pop'),
  83. (';', Punctuation),
  84. ],
  85. 'string': [
  86. (r'"', String, '#pop'),
  87. (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|'
  88. r'u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})', String.Escape),
  89. (r'[^\\"\n]+', String), # all other characters
  90. (r'\\\n', String), # line continuation
  91. (r'\\', String), # stray backslash
  92. ],
  93. }