ada.py 5.2 KB

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