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