devicetree.py 3.9 KB

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