ada.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Language: Ada
  3. Author: Lars Schulna <kartoffelbrei.mit.muskatnuss@gmail.org>
  4. Description: Ada is a general-purpose programming language that has great support for saftey critical and real-time applications.
  5. It has been developed by the DoD and thus has been used in military and safety-critical applications (like civil aviation).
  6. The first version appeared in the 80s, but it's still actively developed today with
  7. the newest standard being Ada2012.
  8. */
  9. // We try to support full Ada2012
  10. //
  11. // We highlight all appearances of types, keywords, literals (string, char, number, bool)
  12. // and titles (user defined function/procedure/package)
  13. // CSS classes are set accordingly
  14. //
  15. // Languages causing problems for language detection:
  16. // xml (broken by Foo : Bar type), elm (broken by Foo : Bar type), vbscript-html (broken by body keyword)
  17. // sql (ada default.txt has a lot of sql keywords)
  18. /** @type LanguageFn */
  19. function ada(hljs) {
  20. // Regular expression for Ada numeric literals.
  21. // stolen form the VHDL highlighter
  22. // Decimal literal:
  23. var INTEGER_RE = '\\d(_|\\d)*';
  24. var EXPONENT_RE = '[eE][-+]?' + INTEGER_RE;
  25. var DECIMAL_LITERAL_RE = INTEGER_RE + '(\\.' + INTEGER_RE + ')?' + '(' + EXPONENT_RE + ')?';
  26. // Based literal:
  27. var BASED_INTEGER_RE = '\\w+';
  28. var BASED_LITERAL_RE = INTEGER_RE + '#' + BASED_INTEGER_RE + '(\\.' + BASED_INTEGER_RE + ')?' + '#' + '(' + EXPONENT_RE + ')?';
  29. var NUMBER_RE = '\\b(' + BASED_LITERAL_RE + '|' + DECIMAL_LITERAL_RE + ')';
  30. // Identifier regex
  31. var ID_REGEX = '[A-Za-z](_?[A-Za-z0-9.])*';
  32. // bad chars, only allowed in literals
  33. var BAD_CHARS = `[]{}%#'"`;
  34. // Ada doesn't have block comments, only line comments
  35. var COMMENTS = hljs.COMMENT('--', '$');
  36. // variable declarations of the form
  37. // Foo : Bar := Baz;
  38. // where only Bar will be highlighted
  39. var VAR_DECLS = {
  40. // TODO: These spaces are not required by the Ada syntax
  41. // however, I have yet to see handwritten Ada code where
  42. // someone does not put spaces around :
  43. begin: '\\s+:\\s+', end: '\\s*(:=|;|\\)|=>|$)',
  44. // endsWithParent: true,
  45. // returnBegin: true,
  46. illegal: BAD_CHARS,
  47. contains: [
  48. {
  49. // workaround to avoid highlighting
  50. // named loops and declare blocks
  51. beginKeywords: 'loop for declare others',
  52. endsParent: true,
  53. },
  54. {
  55. // properly highlight all modifiers
  56. className: 'keyword',
  57. beginKeywords: 'not null constant access function procedure in out aliased exception'
  58. },
  59. {
  60. className: 'type',
  61. begin: ID_REGEX,
  62. endsParent: true,
  63. relevance: 0,
  64. }
  65. ]
  66. };
  67. return {
  68. name: 'Ada',
  69. case_insensitive: true,
  70. keywords: {
  71. keyword:
  72. 'abort else new return abs elsif not reverse abstract end ' +
  73. 'accept entry select access exception of separate aliased exit or some ' +
  74. 'all others subtype and for out synchronized array function overriding ' +
  75. 'at tagged generic package task begin goto pragma terminate ' +
  76. 'body private then if procedure type case in protected constant interface ' +
  77. 'is raise use declare range delay limited record when delta loop rem while ' +
  78. 'digits renames with do mod requeue xor',
  79. literal:
  80. 'True False',
  81. },
  82. contains: [
  83. COMMENTS,
  84. // strings "foobar"
  85. {
  86. className: 'string',
  87. begin: /"/, end: /"/,
  88. contains: [{begin: /""/, relevance: 0}]
  89. },
  90. // characters ''
  91. {
  92. // character literals always contain one char
  93. className: 'string',
  94. begin: /'.'/
  95. },
  96. {
  97. // number literals
  98. className: 'number',
  99. begin: NUMBER_RE,
  100. relevance: 0
  101. },
  102. {
  103. // Attributes
  104. className: 'symbol',
  105. begin: "'" + ID_REGEX,
  106. },
  107. {
  108. // package definition, maybe inside generic
  109. className: 'title',
  110. begin: '(\\bwith\\s+)?(\\bprivate\\s+)?\\bpackage\\s+(\\bbody\\s+)?', end: '(is|$)',
  111. keywords: 'package body',
  112. excludeBegin: true,
  113. excludeEnd: true,
  114. illegal: BAD_CHARS
  115. },
  116. {
  117. // function/procedure declaration/definition
  118. // maybe inside generic
  119. begin: '(\\b(with|overriding)\\s+)?\\b(function|procedure)\\s+', end: '(\\bis|\\bwith|\\brenames|\\)\\s*;)',
  120. keywords: 'overriding function procedure with is renames return',
  121. // we need to re-match the 'function' keyword, so that
  122. // the title mode below matches only exactly once
  123. returnBegin: true,
  124. contains:
  125. [
  126. COMMENTS,
  127. {
  128. // name of the function/procedure
  129. className: 'title',
  130. begin: '(\\bwith\\s+)?\\b(function|procedure)\\s+',
  131. end: '(\\(|\\s+|$)',
  132. excludeBegin: true,
  133. excludeEnd: true,
  134. illegal: BAD_CHARS
  135. },
  136. // 'self'
  137. // // parameter types
  138. VAR_DECLS,
  139. {
  140. // return type
  141. className: 'type',
  142. begin: '\\breturn\\s+', end: '(\\s+|;|$)',
  143. keywords: 'return',
  144. excludeBegin: true,
  145. excludeEnd: true,
  146. // we are done with functions
  147. endsParent: true,
  148. illegal: BAD_CHARS
  149. },
  150. ]
  151. },
  152. {
  153. // new type declarations
  154. // maybe inside generic
  155. className: 'type',
  156. begin: '\\b(sub)?type\\s+', end: '\\s+',
  157. keywords: 'type',
  158. excludeBegin: true,
  159. illegal: BAD_CHARS
  160. },
  161. // see comment above the definition
  162. VAR_DECLS,
  163. // no markup
  164. // relevance boosters for small snippets
  165. // {begin: '\\s*=>\\s*'},
  166. // {begin: '\\s*:=\\s*'},
  167. // {begin: '\\s+:=\\s+'},
  168. ]
  169. };
  170. }
  171. module.exports = ada;