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.

type_dictionnary.js 13 KiB

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