|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.devicetree |
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for Devicetree language. |
|
7 |
|
8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 from pygments.lexer import RegexLexer, bygroups, include, default, words |
|
13 from pygments.token import Comment, Keyword, Name, Number, Operator, \ |
|
14 Punctuation, String, Text |
|
15 |
|
16 __all__ = ['DevicetreeLexer'] |
|
17 |
|
18 |
|
19 class DevicetreeLexer(RegexLexer): |
|
20 """ |
|
21 Lexer for `Devicetree <https://www.devicetree.org/>`_ files. |
|
22 |
|
23 .. versionadded:: 2.7 |
|
24 """ |
|
25 |
|
26 name = 'Devicetree' |
|
27 aliases = ['devicetree', 'dts'] |
|
28 filenames = ['*.dts', '*.dtsi'] |
|
29 mimetypes = ['text/x-c'] |
|
30 |
|
31 #: optional Whitespace or /*...*/ style comment |
|
32 _ws = r'\s*(?:/[*][^*/]*?[*]/\s*)*' |
|
33 |
|
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', Text), |
|
51 (r'\s+', Text), |
|
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 delimeter |
|
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)), |
|
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'), |
|
75 |
|
76 # Nodes |
|
77 (r'([^/*@\s&]+|/)(@?)([0-9a-fA-F,]*)(' + _ws + r')(\{)', |
|
78 bygroups(Name.Function, Operator, Number.Integer, |
|
79 Comment.Multiline, Punctuation), 'node'), |
|
80 |
|
81 default('statement'), |
|
82 ], |
|
83 'statement': [ |
|
84 include('whitespace'), |
|
85 include('statements'), |
|
86 (';', Punctuation, '#pop'), |
|
87 ], |
|
88 'node': [ |
|
89 include('whitespace'), |
|
90 include('macro'), |
|
91 |
|
92 (r'([^/*@\s&]+|/)(@?)([0-9a-fA-F,]*)(' + _ws + r')(\{)', |
|
93 bygroups(Name.Function, Operator, Number.Integer, |
|
94 Comment.Multiline, Punctuation), '#push'), |
|
95 |
|
96 include('statements'), |
|
97 |
|
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 } |