You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
7.3 KiB

3 years ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. /*
  4. * =====================================================================================
  5. *
  6. * Filename: mode/asterisk/asterisk.js
  7. *
  8. * Description: CodeMirror mode for Asterisk dialplan
  9. *
  10. * Created: 05/17/2012 09:20:25 PM
  11. * Revision: none
  12. *
  13. * Author: Stas Kobzar (stas@modulis.ca),
  14. * Company: Modulis.ca Inc.
  15. *
  16. * =====================================================================================
  17. */
  18. (function(mod) {
  19. if (typeof exports == "object" && typeof module == "object") // CommonJS
  20. mod(require("../../lib/codemirror"));
  21. else if (typeof define == "function" && define.amd) // AMD
  22. define(["../../lib/codemirror"], mod);
  23. else // Plain browser env
  24. mod(CodeMirror);
  25. })(function(CodeMirror) {
  26. "use strict";
  27. CodeMirror.defineMode("asterisk", function() {
  28. var atoms = ["exten", "same", "include","ignorepat","switch"],
  29. dpcmd = ["#include","#exec"],
  30. apps = [
  31. "addqueuemember","adsiprog","aelsub","agentlogin","agentmonitoroutgoing","agi",
  32. "alarmreceiver","amd","answer","authenticate","background","backgrounddetect",
  33. "bridge","busy","callcompletioncancel","callcompletionrequest","celgenuserevent",
  34. "changemonitor","chanisavail","channelredirect","chanspy","clearhash","confbridge",
  35. "congestion","continuewhile","controlplayback","dahdiacceptr2call","dahdibarge",
  36. "dahdiras","dahdiscan","dahdisendcallreroutingfacility","dahdisendkeypadfacility",
  37. "datetime","dbdel","dbdeltree","deadagi","dial","dictate","directory","disa",
  38. "dumpchan","eagi","echo","endwhile","exec","execif","execiftime","exitwhile","extenspy",
  39. "externalivr","festival","flash","followme","forkcdr","getcpeid","gosub","gosubif",
  40. "goto","gotoif","gotoiftime","hangup","iax2provision","ices","importvar","incomplete",
  41. "ivrdemo","jabberjoin","jabberleave","jabbersend","jabbersendgroup","jabberstatus",
  42. "jack","log","macro","macroexclusive","macroexit","macroif","mailboxexists","meetme",
  43. "meetmeadmin","meetmechanneladmin","meetmecount","milliwatt","minivmaccmess","minivmdelete",
  44. "minivmgreet","minivmmwi","minivmnotify","minivmrecord","mixmonitor","monitor","morsecode",
  45. "mp3player","mset","musiconhold","nbscat","nocdr","noop","odbc","odbc","odbcfinish",
  46. "originate","ospauth","ospfinish","osplookup","ospnext","page","park","parkandannounce",
  47. "parkedcall","pausemonitor","pausequeuemember","pickup","pickupchan","playback","playtones",
  48. "privacymanager","proceeding","progress","queue","queuelog","raiseexception","read","readexten",
  49. "readfile","receivefax","receivefax","receivefax","record","removequeuemember",
  50. "resetcdr","retrydial","return","ringing","sayalpha","saycountedadj","saycountednoun",
  51. "saycountpl","saydigits","saynumber","sayphonetic","sayunixtime","senddtmf","sendfax",
  52. "sendfax","sendfax","sendimage","sendtext","sendurl","set","setamaflags",
  53. "setcallerpres","setmusiconhold","sipaddheader","sipdtmfmode","sipremoveheader","skel",
  54. "slastation","slatrunk","sms","softhangup","speechactivategrammar","speechbackground",
  55. "speechcreate","speechdeactivategrammar","speechdestroy","speechloadgrammar","speechprocessingsound",
  56. "speechstart","speechunloadgrammar","stackpop","startmusiconhold","stopmixmonitor","stopmonitor",
  57. "stopmusiconhold","stopplaytones","system","testclient","testserver","transfer","tryexec",
  58. "trysystem","unpausemonitor","unpausequeuemember","userevent","verbose","vmauthenticate",
  59. "vmsayname","voicemail","voicemailmain","wait","waitexten","waitfornoise","waitforring",
  60. "waitforsilence","waitmusiconhold","waituntil","while","zapateller"
  61. ];
  62. function basicToken(stream,state){
  63. var cur = '';
  64. var ch = '';
  65. ch = stream.next();
  66. // comment
  67. if(ch == ";") {
  68. stream.skipToEnd();
  69. return "comment";
  70. }
  71. // context
  72. if(ch == '[') {
  73. stream.skipTo(']');
  74. stream.eat(']');
  75. return "header";
  76. }
  77. // string
  78. if(ch == '"') {
  79. stream.skipTo('"');
  80. return "string";
  81. }
  82. if(ch == "'") {
  83. stream.skipTo("'");
  84. return "string-2";
  85. }
  86. // dialplan commands
  87. if(ch == '#') {
  88. stream.eatWhile(/\w/);
  89. cur = stream.current();
  90. if(dpcmd.indexOf(cur) !== -1) {
  91. stream.skipToEnd();
  92. return "strong";
  93. }
  94. }
  95. // application args
  96. if(ch == '$'){
  97. var ch1 = stream.peek();
  98. if(ch1 == '{'){
  99. stream.skipTo('}');
  100. stream.eat('}');
  101. return "variable-3";
  102. }
  103. }
  104. // extension
  105. stream.eatWhile(/\w/);
  106. cur = stream.current();
  107. if(atoms.indexOf(cur) !== -1) {
  108. state.extenStart = true;
  109. switch(cur) {
  110. case 'same': state.extenSame = true; break;
  111. case 'include':
  112. case 'switch':
  113. case 'ignorepat':
  114. state.extenInclude = true;break;
  115. default:break;
  116. }
  117. return "atom";
  118. }
  119. }
  120. return {
  121. startState: function() {
  122. return {
  123. extenStart: false,
  124. extenSame: false,
  125. extenInclude: false,
  126. extenExten: false,
  127. extenPriority: false,
  128. extenApplication: false
  129. };
  130. },
  131. token: function(stream, state) {
  132. var cur = '';
  133. var ch = '';
  134. if(stream.eatSpace()) return null;
  135. // extension started
  136. if(state.extenStart){
  137. stream.eatWhile(/[^\s]/);
  138. cur = stream.current();
  139. if(/^=>?$/.test(cur)){
  140. state.extenExten = true;
  141. state.extenStart = false;
  142. return "strong";
  143. } else {
  144. state.extenStart = false;
  145. stream.skipToEnd();
  146. return "error";
  147. }
  148. } else if(state.extenExten) {
  149. // set exten and priority
  150. state.extenExten = false;
  151. state.extenPriority = true;
  152. stream.eatWhile(/[^,]/);
  153. if(state.extenInclude) {
  154. stream.skipToEnd();
  155. state.extenPriority = false;
  156. state.extenInclude = false;
  157. }
  158. if(state.extenSame) {
  159. state.extenPriority = false;
  160. state.extenSame = false;
  161. state.extenApplication = true;
  162. }
  163. return "tag";
  164. } else if(state.extenPriority) {
  165. state.extenPriority = false;
  166. state.extenApplication = true;
  167. ch = stream.next(); // get comma
  168. if(state.extenSame) return null;
  169. stream.eatWhile(/[^,]/);
  170. return "number";
  171. } else if(state.extenApplication) {
  172. stream.eatWhile(/,/);
  173. cur = stream.current();
  174. if(cur === ',') return null;
  175. stream.eatWhile(/\w/);
  176. cur = stream.current().toLowerCase();
  177. state.extenApplication = false;
  178. if(apps.indexOf(cur) !== -1){
  179. return "def strong";
  180. }
  181. } else{
  182. return basicToken(stream,state);
  183. }
  184. return null;
  185. }
  186. };
  187. });
  188. CodeMirror.defineMIME("text/x-asterisk", "asterisk");
  189. });