ada.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. pygments.lexers.ada
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for Ada family languages.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, bygroups, words, using, this, \
  10. default
  11. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  12. Number, Punctuation
  13. from pygments.lexers._ada_builtins import KEYWORD_LIST, BUILTIN_LIST
  14. __all__ = ['AdaLexer']
  15. class AdaLexer(RegexLexer):
  16. """
  17. For Ada source code.
  18. .. versionadded:: 1.3
  19. """
  20. name = 'Ada'
  21. aliases = ['ada', 'ada95', 'ada2005']
  22. filenames = ['*.adb', '*.ads', '*.ada']
  23. mimetypes = ['text/x-ada']
  24. flags = re.MULTILINE | re.IGNORECASE
  25. tokens = {
  26. 'root': [
  27. (r'[^\S\n]+', Text),
  28. (r'--.*?\n', Comment.Single),
  29. (r'[^\S\n]+', Text),
  30. (r'function|procedure|entry', Keyword.Declaration, 'subprogram'),
  31. (r'(subtype|type)(\s+)(\w+)',
  32. bygroups(Keyword.Declaration, Text, Keyword.Type), 'type_def'),
  33. (r'task|protected', Keyword.Declaration),
  34. (r'(subtype)(\s+)', bygroups(Keyword.Declaration, Text)),
  35. (r'(end)(\s+)', bygroups(Keyword.Reserved, Text), 'end'),
  36. (r'(pragma)(\s+)(\w+)', bygroups(Keyword.Reserved, Text,
  37. Comment.Preproc)),
  38. (r'(true|false|null)\b', Keyword.Constant),
  39. # builtin types
  40. (words(BUILTIN_LIST, suffix=r'\b'), Keyword.Type),
  41. (r'(and(\s+then)?|in|mod|not|or(\s+else)|rem)\b', Operator.Word),
  42. (r'generic|private', Keyword.Declaration),
  43. (r'package', Keyword.Declaration, 'package'),
  44. (r'array\b', Keyword.Reserved, 'array_def'),
  45. (r'(with|use)(\s+)', bygroups(Keyword.Namespace, Text), 'import'),
  46. (r'(\w+)(\s*)(:)(\s*)(constant)',
  47. bygroups(Name.Constant, Text, Punctuation, Text,
  48. Keyword.Reserved)),
  49. (r'<<\w+>>', Name.Label),
  50. (r'(\w+)(\s*)(:)(\s*)(declare|begin|loop|for|while)',
  51. bygroups(Name.Label, Text, Punctuation, Text, Keyword.Reserved)),
  52. # keywords
  53. (words(KEYWORD_LIST, prefix=r'\b', suffix=r'\b'),
  54. Keyword.Reserved),
  55. (r'"[^"]*"', String),
  56. include('attribute'),
  57. include('numbers'),
  58. (r"'[^']'", String.Character),
  59. (r'(\w+)(\s*|[(,])', bygroups(Name, using(this))),
  60. (r"(<>|=>|:=|@|[\[\]]|[()|:;,.'])", Punctuation),
  61. (r'[*<>+=/&-]', Operator),
  62. (r'\n+', Text),
  63. ],
  64. 'numbers': [
  65. (r'[0-9_]+#[0-9a-f_\.]+#', Number.Hex),
  66. (r'[0-9_]+\.[0-9_]*', Number.Float),
  67. (r'[0-9_]+', Number.Integer),
  68. ],
  69. 'attribute': [
  70. (r"(')(\w+)", bygroups(Punctuation, Name.Attribute)),
  71. ],
  72. 'subprogram': [
  73. (r'\(', Punctuation, ('#pop', 'formal_part')),
  74. (r';', Punctuation, '#pop'),
  75. (r'is\b', Keyword.Reserved, '#pop'),
  76. (r'"[^"]+"|\w+', Name.Function),
  77. include('root'),
  78. ],
  79. 'end': [
  80. ('(if|case|record|loop|select)', Keyword.Reserved),
  81. (r'"[^"]+"|[\w.]+', Name.Function),
  82. (r'\s+', Text),
  83. (';', Punctuation, '#pop'),
  84. ],
  85. 'type_def': [
  86. (r';', Punctuation, '#pop'),
  87. (r'\(', Punctuation, 'formal_part'),
  88. (r'\[', Punctuation, 'formal_part'),
  89. (r'with|and|use', Keyword.Reserved),
  90. (r'array\b', Keyword.Reserved, ('#pop', 'array_def')),
  91. (r'record\b', Keyword.Reserved, ('record_def')),
  92. (r'(null record)(;)', bygroups(Keyword.Reserved, Punctuation), '#pop'),
  93. include('root'),
  94. ],
  95. 'array_def': [
  96. (r';', Punctuation, '#pop'),
  97. (r'(\w+)(\s+)(range)', bygroups(Keyword.Type, Text, Keyword.Reserved)),
  98. include('root'),
  99. ],
  100. 'record_def': [
  101. (r'end record', Keyword.Reserved, '#pop'),
  102. include('root'),
  103. ],
  104. 'import': [
  105. # TODO: use Name.Namespace if appropriate. This needs
  106. # work to disinguish imports from aspects.
  107. (r'[\w.]+', Name, '#pop'),
  108. default('#pop'),
  109. ],
  110. 'formal_part': [
  111. (r'\)', Punctuation, '#pop'),
  112. (r'\]', Punctuation, '#pop'),
  113. (r'\w+', Name.Variable),
  114. (r',|:[^=]', Punctuation),
  115. (r'(in|not|null|out|access)\b', Keyword.Reserved),
  116. include('root'),
  117. ],
  118. 'package': [
  119. ('body', Keyword.Declaration),
  120. (r'is\s+new|renames', Keyword.Reserved),
  121. ('is', Keyword.Reserved, '#pop'),
  122. (';', Punctuation, '#pop'),
  123. (r'\(', Punctuation, 'package_instantiation'),
  124. (r'([\w.]+)', Name.Class),
  125. include('root'),
  126. ],
  127. 'package_instantiation': [
  128. (r'("[^"]+"|\w+)(\s+)(=>)', bygroups(Name.Variable, Text, Punctuation)),
  129. (r'[\w.\'"]', Text),
  130. (r'\)', Punctuation, '#pop'),
  131. include('root'),
  132. ],
  133. }