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