DataUtils.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package me.hammerle.snuviscript.code;
  2. import java.lang.reflect.Array;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.regex.Pattern;
  6. import me.hammerle.snuviscript.exceptions.PreScriptException;
  7. public class DataUtils
  8. {
  9. // - in the number is handled somewhere else
  10. private static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9]*[.]{0,1}[0-9]*");
  11. public static boolean isNumber(String s)
  12. {
  13. return NUMBER_PATTERN.matcher(s).matches();
  14. }
  15. private static final Pattern FUNCTION_PATTERN = Pattern.compile("^[a-zA-Z]*\\(.*\\)");
  16. public static boolean isFunction(String s)
  17. {
  18. return FUNCTION_PATTERN.matcher(s).matches();
  19. }
  20. private static final Pattern ARRAY_PATTERN = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]*\\[[^\\]]*\\]");
  21. public static boolean isArray(String s)
  22. {
  23. return ARRAY_PATTERN.matcher(s).matches();
  24. }
  25. // -------------------------------------------------------------------------
  26. // line splitter
  27. // -------------------------------------------------------------------------
  28. private static void addNonEmptyString(HashMap<String, String> strings, LinkedList<String> list, String s)
  29. {
  30. s = s.trim();
  31. if(!s.isEmpty())
  32. {
  33. if(s.startsWith("#"))
  34. {
  35. String text = strings.get(s);
  36. if(text != null)
  37. {
  38. list.add(text);
  39. return;
  40. }
  41. }
  42. list.add(s);
  43. }
  44. }
  45. private static int findNextClosingBracket(int pos, StringBuilder sb, int line)
  46. {
  47. int brackets = 0;
  48. int length = sb.length();
  49. while(pos < length)
  50. {
  51. switch(sb.charAt(pos))
  52. {
  53. case ')':
  54. brackets--;
  55. if(brackets == 0)
  56. {
  57. return pos;
  58. }
  59. else if(brackets < 0)
  60. {
  61. throw new PreScriptException(") without (", line);
  62. }
  63. break;
  64. case '(':
  65. brackets++;
  66. break;
  67. }
  68. pos++;
  69. }
  70. throw new PreScriptException("( without )", line);
  71. }
  72. private static int findNextClosingSBracket(int pos, StringBuilder sb, int line)
  73. {
  74. int brackets = 0;
  75. int length = sb.length();
  76. while(pos < length)
  77. {
  78. switch(sb.charAt(pos))
  79. {
  80. case ']':
  81. brackets--;
  82. if(brackets == 0)
  83. {
  84. return pos;
  85. }
  86. else if(brackets < 0)
  87. {
  88. throw new PreScriptException("] without [", line);
  89. }
  90. break;
  91. case '[':
  92. brackets++;
  93. break;
  94. }
  95. pos++;
  96. }
  97. throw new PreScriptException("[ without ]", line);
  98. }
  99. public static String[] split(HashMap<String, String> strings, String s, int line)
  100. {
  101. LinkedList<String> list = new LinkedList<>();
  102. int old = 0;
  103. int pos = 0;
  104. StringBuilder sb = new StringBuilder(s);
  105. int length = sb.length();
  106. char c;
  107. while(pos < length)
  108. {
  109. c = sb.charAt(pos);
  110. if(!Character.isLetterOrDigit(c))
  111. {
  112. switch(c)
  113. {
  114. case '_':
  115. case '.':
  116. case '#':
  117. case '@':
  118. break;
  119. case ')':
  120. throw new PreScriptException(") without (", line);
  121. case '(':
  122. pos = findNextClosingBracket(pos, sb, line) + 1;
  123. addNonEmptyString(strings, list, sb.substring(old, pos));
  124. old = pos;
  125. continue;
  126. case ']':
  127. throw new PreScriptException("] without [", line);
  128. case '[':
  129. pos = findNextClosingSBracket(pos, sb, line) + 1;
  130. addNonEmptyString(strings, list, sb.substring(old, pos));
  131. old = pos;
  132. continue;
  133. case ' ':
  134. addNonEmptyString(strings, list, sb.substring(old, pos));
  135. old = pos + 1;
  136. pos = old;
  137. continue;
  138. case ',':
  139. addNonEmptyString(strings, list, sb.substring(old, pos));
  140. addNonEmptyString(strings, list, ",");
  141. old = pos + 1;
  142. pos = old;
  143. continue;
  144. default:
  145. addNonEmptyString(strings, list, sb.substring(old, pos));
  146. //System.out.println(old + " " + pos);
  147. old = pos;
  148. pos++;
  149. while(pos <= length && Syntax.getSyntax(sb.substring(old, pos)) != Syntax.UNKNOWN)
  150. {
  151. pos++;
  152. }
  153. pos--;
  154. if(old == pos)
  155. {
  156. throw new PreScriptException("unknown syntax " + c, line);
  157. }
  158. addNonEmptyString(strings, list, sb.substring(old, pos));
  159. old = pos;
  160. continue;
  161. }
  162. }
  163. pos++;
  164. }
  165. if(old < length)
  166. {
  167. addNonEmptyString(strings, list, sb.substring(old));
  168. }
  169. return list.toArray(new String[list.size()]);
  170. }
  171. public static String getArrayString(Object array)
  172. {
  173. StringBuilder sb = new StringBuilder("[");
  174. int length = Array.getLength(array) - 1;
  175. for(int i = 0; i < length; i++)
  176. {
  177. sb.append(Array.get(array, i));
  178. sb.append(", ");
  179. }
  180. if(length > 0)
  181. {
  182. sb.append(Array.get(array, length));
  183. }
  184. sb.append("]");
  185. return sb.toString();
  186. }
  187. }