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 5.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_mesh_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_mesh_dict.m_vars[current_mesh_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_mesh_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_mesh_dict.m_cmds[current_mesh_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_mesh_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_mesh_dict.m_cmds.length; cursor++)
  144. if (current_mesh_dict.m_cmds[cursor].CheckCommand(found_match))
  145. match_data.match_list[match_data.match_list.length] = cursor;
  146. return match_data;
  147. }
  148. }