您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

typedictionnary.js 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. function TypeDictionnary(m_name)
  2. {
  3. this.m_name = m_name;
  4. this.m_alphabet = '';
  5. this.m_cmds = new Array();
  6. this.m_vars = new Array();
  7. }
  8. //-----------------------------------------------------------------------------
  9. //Command Var object
  10. //-----------------------------------------------------------------------------
  11. function CmdVarObj() { }
  12. function CmdVar(m_type, m_syntax)
  13. {
  14. var current_dict = GetCmdDictionnary();
  15. new_obj = new CmdVarObj();
  16. new_obj.m_type = m_type;
  17. new_obj.m_syntax = m_syntax;
  18. current_dict.m_vars[current_dict.m_vars.length] = new_obj;
  19. }
  20. //-----------------------------------------------------------------------------
  21. //Command Argument object
  22. //-----------------------------------------------------------------------------
  23. function CmdArgObj() { }
  24. function CmdArg(m_type, m_name, m_optional)
  25. {
  26. new_obj = new CmdArgObj();
  27. new_obj.m_type = m_type;
  28. new_obj.m_name = m_name;
  29. new_obj.m_optional = m_optional;
  30. return new_obj;
  31. }
  32. //-----------------------------------------------------------------------------
  33. //Command Typing object
  34. //-----------------------------------------------------------------------------
  35. function CmdTypeObj() { }
  36. function CmdType(m_name, m_comment, m_arg)
  37. {
  38. var current_dict = GetCmdDictionnary();
  39. new_obj = new CmdTypeObj();
  40. new_obj.m_name = m_name;
  41. new_obj.m_comment = m_comment;
  42. new_obj.m_arg = m_arg;
  43. new_obj.CheckCommand = function(check_name)
  44. {
  45. if (m_name != undefined && check_name.length > 0)
  46. {
  47. for (var i = 0; i < m_name.length; i++)
  48. {
  49. if (m_name[i] != undefined)
  50. {
  51. var pattern = new RegExp("^[ ]*" + check_name + "[a-z]*");
  52. if (pattern.test(m_name[i]))
  53. return true;
  54. }
  55. }
  56. }
  57. return false;
  58. }
  59. current_dict.m_cmds[current_dict.m_cmds.length] = new_obj;
  60. }
  61. //-----------------------------------------------------------------------------
  62. //Tries to find a matching command in the dictionnary based on the current cursor location in the given TextArea.
  63. //-----------------------------------------------------------------------------
  64. function FindMatchingCommand(src_text_area)
  65. {
  66. if (src_text_area != undefined)
  67. {
  68. var current_dict = GetCmdDictionnary();
  69. var found_match = "";
  70. var check = true;
  71. var cursor = Math.min(src_text_area.selectionStart, src_text_area.value.length - 1);
  72. //Weird test to ensure we don't start searching on the next line or on the next word.
  73. if (!src_text_area.value[cursor].match(/^[ \n]$/))
  74. cursor++;
  75. cursor = Math.min(cursor, src_text_area.value.length - 1);
  76. var o = 0;
  77. while (check && o < 10)
  78. {
  79. //Move backward to find the first letter on this line
  80. for (; cursor > 0; cursor--)
  81. if (src_text_area.value[cursor].match(/^[a-z\n]+$/))
  82. break;
  83. //Move backward to find the start of the command
  84. for (; cursor > 0; cursor--)
  85. if (!src_text_area.value[cursor - 1].match(/^[#0-9a-z]+$/))
  86. break;
  87. //If the cursor is on a "#" and the previous is a " ", we're on a color, repeat the operation
  88. if (cursor > 0 && src_text_area.value[cursor - 1].match(/[ ]/) &&
  89. src_text_area.value[cursor].match(/[#]/))
  90. check = true;
  91. else
  92. check = false;
  93. o++;
  94. }
  95. //Move forward to find the end of the word
  96. for (; cursor < src_text_area.value.length; cursor++)
  97. {
  98. if (src_text_area.value[cursor].match(/^[a-z]+$/))
  99. found_match += src_text_area.value[cursor];
  100. else
  101. break;
  102. }
  103. //Try to match the command with the dictionnary
  104. var match_data = new Object();
  105. match_data.match = found_match;
  106. match_data.match_list = new Array();
  107. for (cursor = 0; cursor < current_dict.m_cmds.length; cursor++)
  108. if (current_dict.m_cmds[cursor].CheckCommand(found_match))
  109. match_data.match_list[match_data.match_list.length] = cursor;
  110. return match_data;
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. //Build a TOC from all commands first letter
  115. //-----------------------------------------------------------------------------
  116. function BuildTOC(with_dot)
  117. {
  118. var res = '';
  119. var current_dict = GetCmdDictionnary();
  120. for (var a = 'a'.charCodeAt(0); a <= 'z'.charCodeAt(0); a++)
  121. {
  122. var stop = false;
  123. for (var i = 0; !stop && i < current_dict.m_cmds.length; i++)
  124. {
  125. for (var j = 0; j < current_dict.m_cmds[i].m_name.length; j++)
  126. {
  127. if (current_dict.m_cmds[i].m_name[j][0] == String.fromCharCode(a))
  128. {
  129. res += String.fromCharCode(a);
  130. stop = true;
  131. break;
  132. }
  133. }
  134. }
  135. if (!stop && with_dot)
  136. res += '.';
  137. }
  138. return res;
  139. }
  140. //-----------------------------------------------------------------------------
  141. //Setup code lookup logic
  142. //-----------------------------------------------------------------------------
  143. function CmdLookup(div_cmds, div_args, div_cmnt, div_vars, text_src)
  144. {
  145. var cur_dict = GetCmdDictionnary();
  146. if (text_src != undefined)
  147. {
  148. var type_list = new Array();
  149. var cmd_size = 8;
  150. var found = FindMatchingCommand(text_src);
  151. if (found.match_list.length > 0)
  152. {
  153. var perfect_match = false;
  154. var best_match = 0;
  155. var best_length = 300000;
  156. //Find the best match to put it in first in the list
  157. for (var i = 0; i < found.match_list.length && best_length > 0; i++)
  158. {
  159. var cur_match = cur_dict.m_cmds[found.match_list[i]];
  160. for (var j = 0; j < cur_match.m_name.length && best_length > 0; j++)
  161. {
  162. if (cur_match.m_name[j].length == found.match.length)
  163. {
  164. perfect_match = true;
  165. best_match = i;
  166. best_length = 0;
  167. }
  168. else if (cur_match.m_name[j].length < best_length &&
  169. cur_match.m_name[j].length > found.match.length)
  170. {
  171. best_match = i;
  172. best_length = cur_match.m_name[j].length;
  173. }
  174. }
  175. }
  176. var tmp = found.match_list[0];
  177. found.match_list[0] = found.match_list[best_match];
  178. found.match_list[best_match] = tmp;
  179. div_cmds[0].innerHTML = "";
  180. div_cmds[1].innerHTML = "";
  181. div_args.innerHTML = "";
  182. div_cmnt.innerHTML = "";
  183. //Go through the found matches and show them.
  184. for (var i = 0; i < found.match_list.length; i++)
  185. {
  186. var cur_match = cur_dict.m_cmds[found.match_list[i]];
  187. div_cmds[0].innerHTML += '[';
  188. var max = cur_match.m_name.length;
  189. var word = 0;
  190. for (var j = 0; j < max; j++)
  191. {
  192. var mth = found.match;
  193. var cmd = cur_match.m_name[j];
  194. //Matching start caracters should be bold
  195. if (mth == cmd.slice(0, mth.length))
  196. {
  197. div_cmds[word].innerHTML += '&nbsp;';
  198. div_cmds[word].innerHTML += '<b>' + mth + '</b>';
  199. div_cmds[word].innerHTML += cmd.slice(mth.length, cmd.length);
  200. word++;
  201. }
  202. }
  203. //Complete empty command by <br> so commands in the two columns are on the same line
  204. word = (word > 0)?(2):(0);
  205. while (word-- > 0)
  206. div_cmds[word].innerHTML += "<br>";
  207. //Go through the arguments and show them, force if we found the perfect match
  208. if ((perfect_match || found.match_list.length < 4) && i == 0)
  209. {
  210. div_args.innerHTML += "&nbsp;>&nbsp;";
  211. if (cur_match.m_arg != undefined)
  212. {
  213. max = cur_match.m_arg.length;
  214. var found_optional = false;
  215. for (var j = 0; j < max; j++)
  216. {
  217. if (cur_match.m_arg[j].m_optional != undefined && found_optional == false)
  218. {
  219. var tab = '<br>'; for (var l = 0; l < 5; l++) tab += '&nbsp;';
  220. div_args.innerHTML += tab + 'Opt: [';
  221. found_optional = true;
  222. }
  223. else if (j > 0)
  224. div_args.innerHTML += ', ';
  225. //Types are bold
  226. div_args.innerHTML += '<b>' + cur_match.m_arg[j].m_type + '</b> ';
  227. type_list[type_list.length] = cur_match.m_arg[j].m_type;
  228. //Names are not
  229. div_args.innerHTML += cur_match.m_arg[j].m_name;
  230. if (cur_match.m_arg[j].m_optional != undefined)
  231. div_args.innerHTML += ' = <b>' + cur_match.m_arg[j].m_optional + '</b>';
  232. }
  233. if (found_optional == true)
  234. div_args.innerHTML += '] ';
  235. div_args.innerHTML += '&nbsp;<br>';
  236. }
  237. //Add the comment
  238. if (cur_match.m_comment != undefined)
  239. {
  240. var tb_i = 16; var in_i = 8;
  241. var tab = '<br>';
  242. if (cur_match.m_arg == undefined) { tb_i -= 8; in_i -= 8; }
  243. for (var l = 0; l < in_i; l++) div_args.innerHTML += '&nbsp;';
  244. for (var l = 0; l < tb_i; l++) tab += '&nbsp;';
  245. div_args.innerHTML += cur_match.m_comment + '&nbsp;';
  246. while (div_args.innerHTML.indexOf('\n') > -1)
  247. div_args.innerHTML = div_args.innerHTML.replace('\n', tab);
  248. }
  249. }
  250. }
  251. }
  252. else
  253. {
  254. div_cmds[0].innerHTML = "[ ...&nbsp;";
  255. div_cmds[1].innerHTML = "";
  256. div_args.innerHTML = "";
  257. div_cmnt.innerHTML = "";
  258. }
  259. //Go through the type list and bold the used ones.
  260. if (cur_dict.m_vars != undefined)
  261. {
  262. div_vars.innerHTML = "";
  263. var max = cur_dict.m_vars.length;
  264. for (var j = 0; j < max; j++)
  265. {
  266. var cur_var = cur_dict.m_vars[j];
  267. div_vars.innerHTML += "&nbsp;>&nbsp;";
  268. var k = 0;
  269. for (; k < type_list.length; k++)
  270. if (cur_var.m_type == type_list[k])
  271. break;
  272. //Bold the used variables
  273. if (k < type_list.length)
  274. div_vars.innerHTML += "<b>" + cur_var.m_type + "</b>";
  275. else
  276. div_vars.innerHTML += cur_var.m_type;
  277. if (cur_var.m_syntax != undefined)
  278. {
  279. var align_size = 9;
  280. var cmd_size = cur_var.m_type.length + 3;
  281. for (var m = 0; m < cur_var.m_syntax.length; m++)
  282. {
  283. for (var l = 0; l < align_size - cmd_size; l++)
  284. div_vars.innerHTML += "&nbsp;";
  285. div_vars.innerHTML += cur_var.m_syntax[m] + "<br>";
  286. cmd_size = 0;
  287. }
  288. var tab = '';
  289. for (var l = 0; l < align_size - cmd_size; l++)
  290. tab += "&nbsp;";
  291. while (div_vars.innerHTML.indexOf('\n') > -1)
  292. div_vars.innerHTML = div_vars.innerHTML.replace('\n', '<br>' + tab);
  293. }
  294. }
  295. }
  296. }
  297. }