Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/lexers/yang.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

23 statements  

1""" 

2 pygments.lexers.yang 

3 ~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexer for the YANG 1.1 modeling language. See :rfc:`7950`. 

6 

7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11from pygments.lexer import RegexLexer, bygroups, words 

12from pygments.token import Text, Token, Name, String, Comment, Number 

13 

14__all__ = ['YangLexer'] 

15 

16 

17class YangLexer(RegexLexer): 

18 """ 

19 Lexer for YANG, based on RFC7950. 

20 """ 

21 name = 'YANG' 

22 url = 'https://tools.ietf.org/html/rfc7950/' 

23 aliases = ['yang'] 

24 filenames = ['*.yang'] 

25 mimetypes = ['application/yang'] 

26 version_added = '2.7' 

27 

28 #Keywords from RFC7950 ; oriented at BNF style 

29 TOP_STMTS_KEYWORDS = ("module", "submodule") 

30 MODULE_HEADER_STMT_KEYWORDS = ("belongs-to", "namespace", "prefix", "yang-version") 

31 META_STMT_KEYWORDS = ("contact", "description", "organization", 

32 "reference", "revision") 

33 LINKAGE_STMTS_KEYWORDS = ("import", "include", "revision-date") 

34 BODY_STMT_KEYWORDS = ("action", "argument", "augment", "deviation", 

35 "extension", "feature", "grouping", "identity", 

36 "if-feature", "input", "notification", "output", 

37 "rpc", "typedef") 

38 DATA_DEF_STMT_KEYWORDS = ("anydata", "anyxml", "case", "choice", 

39 "config", "container", "deviate", "leaf", 

40 "leaf-list", "list", "must", "presence", 

41 "refine", "uses", "when") 

42 TYPE_STMT_KEYWORDS = ("base", "bit", "default", "enum", "error-app-tag", 

43 "error-message", "fraction-digits", "length", 

44 "max-elements", "min-elements", "modifier", 

45 "ordered-by", "path", "pattern", "position", 

46 "range", "require-instance", "status", "type", 

47 "units", "value", "yin-element") 

48 LIST_STMT_KEYWORDS = ("key", "mandatory", "unique") 

49 

50 #RFC7950 other keywords 

51 CONSTANTS_KEYWORDS = ("add", "current", "delete", "deprecated", "false", 

52 "invert-match", "max", "min", "not-supported", 

53 "obsolete", "replace", "true", "unbounded", "user") 

54 

55 #RFC7950 Built-In Types 

56 TYPES = ("binary", "bits", "boolean", "decimal64", "empty", "enumeration", 

57 "identityref", "instance-identifier", "int16", "int32", "int64", 

58 "int8", "leafref", "string", "uint16", "uint32", "uint64", 

59 "uint8", "union") 

60 

61 suffix_re_pattern = r'(?=[^\w\-:])' 

62 

63 tokens = { 

64 'comments': [ 

65 (r'[^*/]', Comment), 

66 (r'/\*', Comment, '#push'), 

67 (r'\*/', Comment, '#pop'), 

68 (r'[*/]', Comment), 

69 ], 

70 "root": [ 

71 (r'\s+', Text.Whitespace), 

72 (r'[{};]+', Token.Punctuation), 

73 (r'(?<![\-\w])(and|or|not|\+|\.)(?![\-\w])', Token.Operator), 

74 

75 (r'"(?:\\"|[^"])*?"', String.Double), 

76 (r"'(?:\\'|[^'])*?'", String.Single), 

77 

78 (r'/\*', Comment, 'comments'), 

79 (r'//.*?$', Comment), 

80 

81 #match BNF stmt for `node-identifier` with [ prefix ":"] 

82 (r'(?:^|(?<=[\s{};]))([\w.-]+)(:)([\w.-]+)(?=[\s{};])', 

83 bygroups(Name.Namespace, Token.Punctuation, Name.Variable)), 

84 

85 #match BNF stmt `date-arg-str` 

86 (r'([0-9]{4}\-[0-9]{2}\-[0-9]{2})(?=[\s{};])', Name.Label), 

87 (r'([0-9]+\.[0-9]+)(?=[\s{};])', Number.Float), 

88 (r'([0-9]+)(?=[\s{};])', Number.Integer), 

89 

90 (words(TOP_STMTS_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), 

91 (words(MODULE_HEADER_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), 

92 (words(META_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), 

93 (words(LINKAGE_STMTS_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), 

94 (words(BODY_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), 

95 (words(DATA_DEF_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), 

96 (words(TYPE_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), 

97 (words(LIST_STMT_KEYWORDS, suffix=suffix_re_pattern), Token.Keyword), 

98 (words(TYPES, suffix=suffix_re_pattern), Name.Class), 

99 (words(CONSTANTS_KEYWORDS, suffix=suffix_re_pattern), Name.Class), 

100 

101 (r'[^;{}\s\'"]+', Name.Variable), 

102 ] 

103 }