ada.js 6.7 KB

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