ada.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. const KEYWORDS = [
  69. "abort",
  70. "else",
  71. "new",
  72. "return",
  73. "abs",
  74. "elsif",
  75. "not",
  76. "reverse",
  77. "abstract",
  78. "end",
  79. "accept",
  80. "entry",
  81. "select",
  82. "access",
  83. "exception",
  84. "of",
  85. "separate",
  86. "aliased",
  87. "exit",
  88. "or",
  89. "some",
  90. "all",
  91. "others",
  92. "subtype",
  93. "and",
  94. "for",
  95. "out",
  96. "synchronized",
  97. "array",
  98. "function",
  99. "overriding",
  100. "at",
  101. "tagged",
  102. "generic",
  103. "package",
  104. "task",
  105. "begin",
  106. "goto",
  107. "pragma",
  108. "terminate",
  109. "body",
  110. "private",
  111. "then",
  112. "if",
  113. "procedure",
  114. "type",
  115. "case",
  116. "in",
  117. "protected",
  118. "constant",
  119. "interface",
  120. "is",
  121. "raise",
  122. "use",
  123. "declare",
  124. "range",
  125. "delay",
  126. "limited",
  127. "record",
  128. "when",
  129. "delta",
  130. "loop",
  131. "rem",
  132. "while",
  133. "digits",
  134. "renames",
  135. "with",
  136. "do",
  137. "mod",
  138. "requeue",
  139. "xor"
  140. ];
  141. return {
  142. name: 'Ada',
  143. case_insensitive: true,
  144. keywords: {
  145. keyword: KEYWORDS,
  146. literal: [
  147. "True",
  148. "False"
  149. ]
  150. },
  151. contains: [
  152. COMMENTS,
  153. // strings "foobar"
  154. {
  155. className: 'string',
  156. begin: /"/,
  157. end: /"/,
  158. contains: [
  159. {
  160. begin: /""/,
  161. relevance: 0
  162. }
  163. ]
  164. },
  165. // characters ''
  166. {
  167. // character literals always contain one char
  168. className: 'string',
  169. begin: /'.'/
  170. },
  171. {
  172. // number literals
  173. className: 'number',
  174. begin: NUMBER_RE,
  175. relevance: 0
  176. },
  177. {
  178. // Attributes
  179. className: 'symbol',
  180. begin: "'" + ID_REGEX
  181. },
  182. {
  183. // package definition, maybe inside generic
  184. className: 'title',
  185. begin: '(\\bwith\\s+)?(\\bprivate\\s+)?\\bpackage\\s+(\\bbody\\s+)?',
  186. end: '(is|$)',
  187. keywords: 'package body',
  188. excludeBegin: true,
  189. excludeEnd: true,
  190. illegal: BAD_CHARS
  191. },
  192. {
  193. // function/procedure declaration/definition
  194. // maybe inside generic
  195. begin: '(\\b(with|overriding)\\s+)?\\b(function|procedure)\\s+',
  196. end: '(\\bis|\\bwith|\\brenames|\\)\\s*;)',
  197. keywords: 'overriding function procedure with is renames return',
  198. // we need to re-match the 'function' keyword, so that
  199. // the title mode below matches only exactly once
  200. returnBegin: true,
  201. contains:
  202. [
  203. COMMENTS,
  204. {
  205. // name of the function/procedure
  206. className: 'title',
  207. begin: '(\\bwith\\s+)?\\b(function|procedure)\\s+',
  208. end: '(\\(|\\s+|$)',
  209. excludeBegin: true,
  210. excludeEnd: true,
  211. illegal: BAD_CHARS
  212. },
  213. // 'self'
  214. // // parameter types
  215. VAR_DECLS,
  216. {
  217. // return type
  218. className: 'type',
  219. begin: '\\breturn\\s+',
  220. end: '(\\s+|;|$)',
  221. keywords: 'return',
  222. excludeBegin: true,
  223. excludeEnd: true,
  224. // we are done with functions
  225. endsParent: true,
  226. illegal: BAD_CHARS
  227. }
  228. ]
  229. },
  230. {
  231. // new type declarations
  232. // maybe inside generic
  233. className: 'type',
  234. begin: '\\b(sub)?type\\s+',
  235. end: '\\s+',
  236. keywords: 'type',
  237. excludeBegin: true,
  238. illegal: BAD_CHARS
  239. },
  240. // see comment above the definition
  241. VAR_DECLS
  242. // no markup
  243. // relevance boosters for small snippets
  244. // {begin: '\\s*=>\\s*'},
  245. // {begin: '\\s*:=\\s*'},
  246. // {begin: '\\s+:=\\s+'},
  247. ]
  248. };
  249. }
  250. export { ada as default };