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.

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