asn1.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. """
  2. pygments.lexers.asn1
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Pygments lexers for ASN.1.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.token import Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. from pygments.lexer import RegexLexer, words, bygroups
  12. __all__ = ['Asn1Lexer']
  13. SINGLE_WORD_KEYWORDS = [
  14. "ENCODED",
  15. "ABSTRACT-SYNTAX",
  16. "END",
  17. "APPLICATION",
  18. "EXPLICIT",
  19. "IMPLICIT",
  20. "AUTOMATIC",
  21. "TAGS",
  22. "BEGIN",
  23. "EXTENSIBILITY",
  24. "BY",
  25. "FROM",
  26. "COMPONENT",
  27. "UNIVERSAL",
  28. "COMPONENTS",
  29. "CONSTRAINED",
  30. "IMPLIED",
  31. "DEFINITIONS",
  32. "INCLUDES",
  33. "PRIVATE",
  34. "WITH",
  35. "OF",
  36. ]
  37. OPERATOR_WORDS = [
  38. "EXCEPT",
  39. "UNION",
  40. "INTERSECTION",
  41. ]
  42. SINGLE_WORD_NAMESPACE_KEYWORDS = [
  43. "EXPORTS",
  44. "IMPORTS",
  45. ]
  46. MULTI_WORDS_DECLARATIONS = [
  47. "SEQUENCE OF",
  48. "SET OF",
  49. "INSTANCE OF",
  50. "WITH SYNTAX",
  51. ]
  52. SINGLE_WORDS_DECLARATIONS = [
  53. "SIZE",
  54. "SEQUENCE",
  55. "SET",
  56. "CLASS",
  57. "UNIQUE",
  58. "DEFAULT",
  59. "CHOICE",
  60. "PATTERN",
  61. "OPTIONAL",
  62. "PRESENT",
  63. "ABSENT",
  64. "CONTAINING",
  65. "ENUMERATED",
  66. "ALL",
  67. ]
  68. TWO_WORDS_TYPES = [
  69. "OBJECT IDENTIFIER",
  70. "BIT STRING",
  71. "OCTET STRING",
  72. "CHARACTER STRING",
  73. "EMBEDDED PDV",
  74. ]
  75. SINGLE_WORD_TYPES = [
  76. "RELATIVE-OID",
  77. "TYPE-IDENTIFIER",
  78. "ObjectDescriptor",
  79. "IA5String",
  80. "INTEGER",
  81. "ISO646String",
  82. "T61String",
  83. "BMPString",
  84. "NumericString",
  85. "TeletexString",
  86. "GeneralizedTime",
  87. "REAL",
  88. "BOOLEAN",
  89. "GeneralString",
  90. "GraphicString",
  91. "UniversalString",
  92. "UTCTime",
  93. "VisibleString",
  94. "UTF8String",
  95. "PrintableString",
  96. "VideotexString",
  97. "EXTERNAL",
  98. ]
  99. def word_sequences(tokens):
  100. return "(" + '|'.join(token.replace(' ', r'\s+') for token in tokens) + r')\b'
  101. class Asn1Lexer(RegexLexer):
  102. """
  103. Lexer for ASN.1 module definition
  104. """
  105. flags = re.MULTILINE
  106. name = 'ASN.1'
  107. aliases = ['asn1']
  108. filenames = ["*.asn1"]
  109. url = "https://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf"
  110. version_added = '2.16'
  111. tokens = {
  112. 'root': [
  113. # Whitespace:
  114. (r'\s+', Whitespace),
  115. # Comments:
  116. (r'--.*$', Comment.Single),
  117. (r'/\*', Comment.Multiline, 'comment'),
  118. # Numbers:
  119. (r'\d+\.\d*([eE][-+]?\d+)?', Number.Float),
  120. (r'\d+', Number.Integer),
  121. # Identifier:
  122. (r"&?[a-z][-a-zA-Z0-9]*[a-zA-Z0-9]\b", Name.Variable),
  123. # Constants:
  124. (words(("TRUE", "FALSE", "NULL", "MINUS-INFINITY", "PLUS-INFINITY", "MIN", "MAX"), suffix=r'\b'), Keyword.Constant),
  125. # Builtin types:
  126. (word_sequences(TWO_WORDS_TYPES), Keyword.Type),
  127. (words(SINGLE_WORD_TYPES, suffix=r'\b'), Keyword.Type),
  128. # Other keywords:
  129. (r"EXPORTS\s+ALL\b", Keyword.Namespace),
  130. (words(SINGLE_WORD_NAMESPACE_KEYWORDS, suffix=r'\b'), Operator.Namespace),
  131. (word_sequences(MULTI_WORDS_DECLARATIONS), Keyword.Declaration),
  132. (words(SINGLE_WORDS_DECLARATIONS, suffix=r'\b'), Keyword.Declaration),
  133. (words(OPERATOR_WORDS, suffix=r'\b'), Operator.Word),
  134. (words(SINGLE_WORD_KEYWORDS), Keyword),
  135. # Type identifier:
  136. (r"&?[A-Z][-a-zA-Z0-9]*[a-zA-Z0-9]\b", Name.Type),
  137. # Operators:
  138. (r"(::=|\.\.\.|\.\.|\[\[|\]\]|\||\^)", Operator),
  139. # Punctuation:
  140. (r"(\.|,|\{|\}|\(|\)|\[|\])", Punctuation),
  141. # String:
  142. (r'"', String, 'string'),
  143. # Binary string:
  144. (r"('[01 ]*')(B)\b", bygroups(String, String.Affix)),
  145. (r"('[0-9A-F ]*')(H)\b",bygroups(String, String.Affix)),
  146. ],
  147. 'comment': [
  148. (r'[^*/]+', Comment.Multiline),
  149. (r'/\*', Comment.Multiline, '#push'),
  150. (r'\*/', Comment.Multiline, '#pop'),
  151. (r'[*/]', Comment.Multiline)
  152. ],
  153. 'string': [
  154. (r'""', String),
  155. (r'"', String, "#pop"),
  156. (r'[^"]', String),
  157. ]
  158. }