applescript.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @param {string} value
  3. * @returns {RegExp}
  4. * */
  5. /**
  6. * @param {RegExp | string } re
  7. * @returns {string}
  8. */
  9. function source(re) {
  10. if (!re) return null;
  11. if (typeof re === "string") return re;
  12. return re.source;
  13. }
  14. /**
  15. * @param {...(RegExp | string) } args
  16. * @returns {string}
  17. */
  18. function concat(...args) {
  19. const joined = args.map((x) => source(x)).join("");
  20. return joined;
  21. }
  22. /**
  23. * Any of the passed expresssions may match
  24. *
  25. * Creates a huge this | this | that | that match
  26. * @param {(RegExp | string)[] } args
  27. * @returns {string}
  28. */
  29. function either(...args) {
  30. const joined = '(' + args.map((x) => source(x)).join("|") + ")";
  31. return joined;
  32. }
  33. /*
  34. Language: AppleScript
  35. Authors: Nathan Grigg <nathan@nathanamy.org>, Dr. Drang <drdrang@gmail.com>
  36. Category: scripting
  37. Website: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html
  38. Audit: 2020
  39. */
  40. /** @type LanguageFn */
  41. function applescript(hljs) {
  42. const STRING = hljs.inherit(
  43. hljs.QUOTE_STRING_MODE, {
  44. illegal: null
  45. });
  46. const PARAMS = {
  47. className: 'params',
  48. begin: /\(/,
  49. end: /\)/,
  50. contains: [
  51. 'self',
  52. hljs.C_NUMBER_MODE,
  53. STRING
  54. ]
  55. };
  56. const COMMENT_MODE_1 = hljs.COMMENT(/--/, /$/);
  57. const COMMENT_MODE_2 = hljs.COMMENT(
  58. /\(\*/,
  59. /\*\)/,
  60. {
  61. contains: [
  62. 'self', // allow nesting
  63. COMMENT_MODE_1
  64. ]
  65. }
  66. );
  67. const COMMENTS = [
  68. COMMENT_MODE_1,
  69. COMMENT_MODE_2,
  70. hljs.HASH_COMMENT_MODE
  71. ];
  72. const KEYWORD_PATTERNS = [
  73. /apart from/,
  74. /aside from/,
  75. /instead of/,
  76. /out of/,
  77. /greater than/,
  78. /isn't|(doesn't|does not) (equal|come before|come after|contain)/,
  79. /(greater|less) than( or equal)?/,
  80. /(starts?|ends|begins?) with/,
  81. /contained by/,
  82. /comes (before|after)/,
  83. /a (ref|reference)/,
  84. /POSIX (file|path)/,
  85. /(date|time) string/,
  86. /quoted form/
  87. ];
  88. const BUILT_IN_PATTERNS = [
  89. /clipboard info/,
  90. /the clipboard/,
  91. /info for/,
  92. /list (disks|folder)/,
  93. /mount volume/,
  94. /path to/,
  95. /(close|open for) access/,
  96. /(get|set) eof/,
  97. /current date/,
  98. /do shell script/,
  99. /get volume settings/,
  100. /random number/,
  101. /set volume/,
  102. /system attribute/,
  103. /system info/,
  104. /time to GMT/,
  105. /(load|run|store) script/,
  106. /scripting components/,
  107. /ASCII (character|number)/,
  108. /localized string/,
  109. /choose (application|color|file|file name|folder|from list|remote application|URL)/,
  110. /display (alert|dialog)/
  111. ];
  112. return {
  113. name: 'AppleScript',
  114. aliases: [ 'osascript' ],
  115. keywords: {
  116. keyword:
  117. 'about above after against and around as at back before beginning ' +
  118. 'behind below beneath beside between but by considering ' +
  119. 'contain contains continue copy div does eighth else end equal ' +
  120. 'equals error every exit fifth first for fourth from front ' +
  121. 'get given global if ignoring in into is it its last local me ' +
  122. 'middle mod my ninth not of on onto or over prop property put ref ' +
  123. 'reference repeat returning script second set seventh since ' +
  124. 'sixth some tell tenth that the|0 then third through thru ' +
  125. 'timeout times to transaction try until where while whose with ' +
  126. 'without',
  127. literal:
  128. 'AppleScript false linefeed return pi quote result space tab true',
  129. built_in:
  130. 'alias application boolean class constant date file integer list ' +
  131. 'number real record string text ' +
  132. 'activate beep count delay launch log offset read round ' +
  133. 'run say summarize write ' +
  134. 'character characters contents day frontmost id item length ' +
  135. 'month name paragraph paragraphs rest reverse running time version ' +
  136. 'weekday word words year'
  137. },
  138. contains: [
  139. STRING,
  140. hljs.C_NUMBER_MODE,
  141. {
  142. className: 'built_in',
  143. begin: concat(
  144. /\b/,
  145. either(...BUILT_IN_PATTERNS),
  146. /\b/
  147. )
  148. },
  149. {
  150. className: 'built_in',
  151. begin: /^\s*return\b/
  152. },
  153. {
  154. className: 'literal',
  155. begin:
  156. /\b(text item delimiters|current application|missing value)\b/
  157. },
  158. {
  159. className: 'keyword',
  160. begin: concat(
  161. /\b/,
  162. either(...KEYWORD_PATTERNS),
  163. /\b/
  164. )
  165. },
  166. {
  167. beginKeywords: 'on',
  168. illegal: /[${=;\n]/,
  169. contains: [
  170. hljs.UNDERSCORE_TITLE_MODE,
  171. PARAMS
  172. ]
  173. },
  174. ...COMMENTS
  175. ],
  176. illegal: /\/\/|->|=>|\[\[/
  177. };
  178. }
  179. module.exports = applescript;