yang.py 4.4 KB

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