ada.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. const INTEGER_RE = '\\d(_|\\d)*';
  24. const EXPONENT_RE = '[eE][-+]?' + INTEGER_RE;
  25. const DECIMAL_LITERAL_RE = INTEGER_RE + '(\\.' + INTEGER_RE + ')?' + '(' + EXPONENT_RE + ')?';
  26. // Based literal:
  27. const BASED_INTEGER_RE = '\\w+';
  28. const BASED_LITERAL_RE = INTEGER_RE + '#' + BASED_INTEGER_RE + '(\\.' + BASED_INTEGER_RE + ')?' + '#' + '(' + EXPONENT_RE + ')?';
  29. const NUMBER_RE = '\\b(' + BASED_LITERAL_RE + '|' + DECIMAL_LITERAL_RE + ')';
  30. // Identifier regex
  31. const ID_REGEX = '[A-Za-z](_?[A-Za-z0-9.])*';
  32. // bad chars, only allowed in literals
  33. const BAD_CHARS = `[]\\{\\}%#'"`;
  34. // Ada doesn't have block comments, only line comments
  35. const COMMENTS = hljs.COMMENT('--', '$');
  36. // variable declarations of the form
  37. // Foo : Bar := Baz;
  38. // where only Bar will be highlighted
  39. const 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+',
  44. end: '\\s*(:=|;|\\)|=>|$)',
  45. // endsWithParent: true,
  46. // returnBegin: true,
  47. illegal: BAD_CHARS,
  48. contains: [
  49. {
  50. // workaround to avoid highlighting
  51. // named loops and declare blocks
  52. beginKeywords: 'loop for declare others',
  53. endsParent: true
  54. },
  55. {
  56. // properly highlight all modifiers
  57. className: 'keyword',
  58. beginKeywords: 'not null constant access function procedure in out aliased exception'
  59. },
  60. {
  61. className: 'type',
  62. begin: ID_REGEX,
  63. endsParent: true,
  64. relevance: 0
  65. }
  66. ]
  67. };
  68. return {
  69. name: 'Ada',
  70. case_insensitive: true,
  71. keywords: {
  72. keyword:
  73. 'abort else new return abs elsif not reverse abstract end ' +
  74. 'accept entry select access exception of separate aliased exit or some ' +
  75. 'all others subtype and for out synchronized array function overriding ' +
  76. 'at tagged generic package task begin goto pragma terminate ' +
  77. 'body private then if procedure type case in protected constant interface ' +
  78. 'is raise use declare range delay limited record when delta loop rem while ' +
  79. 'digits renames with do mod requeue xor',
  80. literal:
  81. 'True False'
  82. },
  83. contains: [
  84. COMMENTS,
  85. // strings "foobar"
  86. {
  87. className: 'string',
  88. begin: /"/,
  89. end: /"/,
  90. contains: [{
  91. begin: /""/,
  92. relevance: 0
  93. }]
  94. },
  95. // characters ''
  96. {
  97. // character literals always contain one char
  98. className: 'string',
  99. begin: /'.'/
  100. },
  101. {
  102. // number literals
  103. className: 'number',
  104. begin: NUMBER_RE,
  105. relevance: 0
  106. },
  107. {
  108. // Attributes
  109. className: 'symbol',
  110. begin: "'" + ID_REGEX
  111. },
  112. {
  113. // package definition, maybe inside generic
  114. className: 'title',
  115. begin: '(\\bwith\\s+)?(\\bprivate\\s+)?\\bpackage\\s+(\\bbody\\s+)?',
  116. end: '(is|$)',
  117. keywords: 'package body',
  118. excludeBegin: true,
  119. excludeEnd: true,
  120. illegal: BAD_CHARS
  121. },
  122. {
  123. // function/procedure declaration/definition
  124. // maybe inside generic
  125. begin: '(\\b(with|overriding)\\s+)?\\b(function|procedure)\\s+',
  126. end: '(\\bis|\\bwith|\\brenames|\\)\\s*;)',
  127. keywords: 'overriding function procedure with is renames return',
  128. // we need to re-match the 'function' keyword, so that
  129. // the title mode below matches only exactly once
  130. returnBegin: true,
  131. contains:
  132. [
  133. COMMENTS,
  134. {
  135. // name of the function/procedure
  136. className: 'title',
  137. begin: '(\\bwith\\s+)?\\b(function|procedure)\\s+',
  138. end: '(\\(|\\s+|$)',
  139. excludeBegin: true,
  140. excludeEnd: true,
  141. illegal: BAD_CHARS
  142. },
  143. // 'self'
  144. // // parameter types
  145. VAR_DECLS,
  146. {
  147. // return type
  148. className: 'type',
  149. begin: '\\breturn\\s+',
  150. end: '(\\s+|;|$)',
  151. keywords: 'return',
  152. excludeBegin: true,
  153. excludeEnd: true,
  154. // we are done with functions
  155. endsParent: true,
  156. illegal: BAD_CHARS
  157. }
  158. ]
  159. },
  160. {
  161. // new type declarations
  162. // maybe inside generic
  163. className: 'type',
  164. begin: '\\b(sub)?type\\s+',
  165. end: '\\s+',
  166. keywords: 'type',
  167. excludeBegin: true,
  168. illegal: BAD_CHARS
  169. },
  170. // see comment above the definition
  171. VAR_DECLS
  172. // no markup
  173. // relevance boosters for small snippets
  174. // {begin: '\\s*=>\\s*'},
  175. // {begin: '\\s*:=\\s*'},
  176. // {begin: '\\s+:=\\s+'},
  177. ]
  178. };
  179. }
  180. module.exports = ada;