|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.yang |
|
4 ~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexer for the YANG 1.1 modeling language. See :rfc:`7950`. |
|
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, words) |
|
13 from pygments.token import (Text, Token, Name, String, Comment, |
|
14 Number) |
|
15 |
|
16 __all__ = ['YangLexer'] |
|
17 |
|
18 class YangLexer(RegexLexer): |
|
19 """ |
|
20 Lexer for `YANG <https://tools.ietf.org/html/rfc7950/>`_, based on RFC7950 |
|
21 |
|
22 .. versionadded:: 2.7 |
|
23 """ |
|
24 name = 'YANG' |
|
25 aliases = ['yang'] |
|
26 filenames = ['*.yang'] |
|
27 mimetypes = ['application/yang'] |
|
28 |
|
29 #Keywords from RFC7950 ; oriented at BNF style |
|
30 TOP_STMTS_KEYWORDS = ("module", "submodule") |
|
31 MODULE_HEADER_STMT_KEYWORDS = ("belongs-to", "namespace", "prefix", "yang-version") |
|
32 META_STMT_KEYWORDS = ("contact", "description", "organization", |
|
33 "reference", "revision") |
|
34 LINKAGE_STMTS_KEYWORDS = ("import", "include", "revision-date") |
|
35 BODY_STMT_KEYWORDS = ("action", "argument", "augment", "deviation", |
|
36 "extension", "feature", "grouping", "identity", |
|
37 "if-feature", "input", "notification", "output", |
|
38 "rpc", "typedef") |
|
39 DATA_DEF_STMT_KEYWORDS = ("anydata", "anyxml", "case", "choice", |
|
40 "config", "container", "deviate", "leaf", |
|
41 "leaf-list", "list", "must", "presence", |
|
42 "refine", "uses", "when") |
|
43 TYPE_STMT_KEYWORDS = ("base", "bit", "default", "enum", "error-app-tag", |
|
44 "error-message", "fraction-digits", "length", |
|
45 "max-elements", "min-elements", "modifier", |
|
46 "ordered-by", "path", "pattern", "position", |
|
47 "range", "require-instance", "status", "type", |
|
48 "units", "value", "yin-element") |
|
49 LIST_STMT_KEYWORDS = ("key", "mandatory", "unique") |
|
50 |
|
51 #RFC7950 other keywords |
|
52 CONSTANTS_KEYWORDS = ("add", "current", "delete", "deprecated", "false", |
|
53 "invert-match", "max", "min", "not-supported", |
|
54 "obsolete", "replace", "true", "unbounded", "user") |
|
55 |
|
56 #RFC7950 Built-In Types |
|
57 TYPES = ("binary", "bits", "boolean", "decimal64", "empty", "enumeration", |
|
58 "identityref", "instance-identifier", "int16", "int32", "int64", |
|
59 "int8", "leafref", "string", "uint16", "uint32", "uint64", |
|
60 "uint8", "union") |
|
61 |
|
62 suffix_re_pattern = r'(?=[^\w\-:])' |
|
63 |
|
64 tokens = { |
|
65 'comments': [ |
|
66 (r'[^*/]', Comment), |
|
67 (r'/\*', Comment, '#push'), |
|
68 (r'\*/', Comment, '#pop'), |
|
69 (r'[*/]', Comment), |
|
70 ], |
|
71 "root": [ |
|
72 (r'\s+', Text.Whitespace), |
|
73 (r'[{};]+', Token.Punctuation), |
|
74 (r'(?<![\-\w])(and|or|not|\+|\.)(?![\-\w])', Token.Operator), |
|
75 |
|
76 (r'"(?:\\"|[^"])*?"', String.Double), |
|
77 (r"'(?:\\'|[^'])*?'", String.Single), |
|
78 |
|
79 (r'/\*', Comment, 'comments'), |
|
80 (r'//.*?$', Comment), |
|
81 |
|
82 #match BNF stmt for `node-identifier` with [ prefix ":"] |
|
83 (r'(?:^|(?<=[\s{};]))([\w.-]+)(:)([\w.-]+)(?=[\s{};])', |
|
84 bygroups(Name.Namespace, Token.Punctuation, Name.Variable)), |
|
85 |
|
86 #match BNF stmt `date-arg-str` |
|
87 (r'([0-9]{4}\-[0-9]{2}\-[0-9]{2})(?=[\s{};])', Name.Label), |
|
88 (r'([0-9]+\.[0-9]+)(?=[\s{};])', Number.Float), |
|
89 (r'([0-9]+)(?=[\s{};])', Number.Integer), |
|
90 |
|
91 (words(TOP_STMTS_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), |
|
92 (words(MODULE_HEADER_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), |
|
93 (words(META_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), |
|
94 (words(LINKAGE_STMTS_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), |
|
95 (words(BODY_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), |
|
96 (words(DATA_DEF_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), |
|
97 (words(TYPE_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), |
|
98 (words(LIST_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), |
|
99 (words(TYPES, suffix=suffix_re_pattern), Name.Class), |
|
100 (words(CONSTANTS_KEYWORDS, suffix=suffix_re_pattern), Name.Class), |
|
101 |
|
102 (r'[^;{}\s\'"]+', Name.Variable), |
|
103 ] |
|
104 } |