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