123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- package me.hammerle.snuviscript.code;
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.Array;
- import java.nio.charset.MalformedInputException;
- import java.nio.file.Files;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Random;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- import me.hammerle.snuviscript.exceptions.PreScriptException;
- public class SnuviUtils
- {
- private static final Random RANDOM = new Random();
-
- public static int randomInt(int min, int max)
- {
- return RANDOM.nextInt((max - min) + 1) + min;
- }
-
- // - in the number is handled somewhere else
- private static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9]*[.]{0,1}[0-9]*");
-
- public static boolean isNumber(String s)
- {
- return NUMBER_PATTERN.matcher(s).matches();
- }
-
- private static final Pattern FUNCTION_PATTERN = Pattern.compile("^[a-zA-Z.]*\\(.*\\)");
-
- public static boolean isFunction(String s)
- {
- return FUNCTION_PATTERN.matcher(s).matches();
- }
-
- private static final Pattern ARRAY_PATTERN = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]*\\[[^\\]]*\\]");
-
- public static boolean isArray(String s)
- {
- return ARRAY_PATTERN.matcher(s).matches();
- }
-
- // -------------------------------------------------------------------------
- // line splitter
- // -------------------------------------------------------------------------
-
- private static void addNonEmptyString(HashMap<String, String> strings, LinkedList<String> list, String s)
- {
- s = s.trim();
- if(!s.isEmpty())
- {
- if(s.startsWith("#"))
- {
- String text = strings.get(s);
- if(text != null)
- {
- list.add(text);
- return;
- }
- }
- list.add(s);
- }
- }
-
- private static int findNextClosingBracket(int pos, StringBuilder sb, int line)
- {
- int brackets = 0;
- int length = sb.length();
- while(pos < length)
- {
- switch(sb.charAt(pos))
- {
- case ')':
- brackets--;
- if(brackets == 0)
- {
- return pos;
- }
- else if(brackets < 0)
- {
- throw new PreScriptException(") without (", line);
- }
- break;
- case '(':
- brackets++;
- break;
- }
- pos++;
- }
- throw new PreScriptException("( without )", line);
- }
-
- private static int findNextClosingSBracket(int pos, StringBuilder sb, int line)
- {
- int brackets = 0;
- int length = sb.length();
- while(pos < length)
- {
- switch(sb.charAt(pos))
- {
- case ']':
- brackets--;
- if(brackets == 0)
- {
- return pos;
- }
- else if(brackets < 0)
- {
- throw new PreScriptException("] without [", line);
- }
- break;
- case '[':
- brackets++;
- break;
- }
- pos++;
- }
- throw new PreScriptException("[ without ]", line);
- }
-
- public static String[] split(HashMap<String, String> strings, String s, int line)
- {
- LinkedList<String> list = new LinkedList<>();
-
- int old = 0;
- int pos = 0;
-
- StringBuilder sb = new StringBuilder(s);
- int length = sb.length();
- char c;
- while(pos < length)
- {
- c = sb.charAt(pos);
- if(!Character.isLetterOrDigit(c))
- {
- switch(c)
- {
- case '_':
- case '.':
- case '#':
- case '@':
- break;
- case ')':
- throw new PreScriptException(") without (", line);
- case '(':
- pos = findNextClosingBracket(pos, sb, line) + 1;
- addNonEmptyString(strings, list, sb.substring(old, pos));
- old = pos;
- continue;
- case ']':
- throw new PreScriptException("] without [", line);
- case '[':
- pos = findNextClosingSBracket(pos, sb, line) + 1;
- addNonEmptyString(strings, list, sb.substring(old, pos));
- old = pos;
- continue;
- case '\t':
- case ' ':
- addNonEmptyString(strings, list, sb.substring(old, pos));
- old = pos + 1;
- pos = old;
- continue;
- case ',':
- addNonEmptyString(strings, list, sb.substring(old, pos));
- addNonEmptyString(strings, list, ",");
- old = pos + 1;
- pos = old;
- continue;
- default:
- addNonEmptyString(strings, list, sb.substring(old, pos));
- //System.out.println(old + " " + pos);
- old = pos;
- pos++;
- while(pos <= length && Syntax.getSyntax(sb.substring(old, pos)) != Syntax.UNKNOWN)
- {
- pos++;
- }
- pos--;
- if(old == pos)
- {
- throw new PreScriptException("unknown syntax '" + c + "'", line);
- }
- addNonEmptyString(strings, list, sb.substring(old, pos));
- old = pos;
- continue;
- }
- }
- pos++;
- }
- if(old < length)
- {
- addNonEmptyString(strings, list, sb.substring(old));
- }
-
- return list.toArray(new String[list.size()]);
- }
-
- public static String getArrayString(Object array)
- {
- StringBuilder sb = new StringBuilder("[");
- int length = Array.getLength(array) - 1;
- for(int i = 0; i < length; i++)
- {
- sb.append(Array.get(array, i));
- sb.append(", ");
- }
- if(length > 0)
- {
- sb.append(Array.get(array, length));
- }
- sb.append("]");
- return sb.toString();
- }
-
- // -------------------------------------------------------------------------
- // connectors
- // -------------------------------------------------------------------------
-
- public static String connect(Script sc, InputProvider[] c, int skip)
- {
- return Arrays.stream(c, skip, c.length).map(o -> o.getString(sc)).collect(Collectors.joining());
- }
-
- public static String connect(Collection<Object> c, int skip)
- {
- return c.stream().skip(skip).map(o -> o.toString()).collect(Collectors.joining());
- }
-
- public static String connect(Script sc, InputProvider[] c, String s, int skip)
- {
- return Arrays.stream(c, skip, c.length).map(o -> o.getString(sc)).collect(Collectors.joining(s));
- }
-
- public static String connect(Collection<Object> c, String s, int skip)
- {
- return c.stream().skip(skip).map(o -> String.valueOf(o)).collect(Collectors.joining(s));
- }
-
- // -------------------------------------------------------------------------
- // file stuff
- // -------------------------------------------------------------------------
-
- public static List<String> readCode(String ending, String... filenames)
- {
- LinkedList<List<String>> lists = new LinkedList<>();
- List<String> list;
- File script;
- int lines = 0;
- for(String filename : filenames)
- {
- script = new File("./" + filename + ending);
- if(script.exists())
- {
- try
- {
- list = Files.readAllLines(script.toPath());
- lines += list.size();
- lists.add(list);
- }
- catch (MalformedInputException ex)
- {
- throw new PreScriptException("'" + filename + "' contains an illegal character, change file encoding", 0);
- }
- catch (IOException ex)
- {
- throw new PreScriptException("file '" + filename + "' cannot be read", 0);
- }
- }
- else
- {
- throw new PreScriptException("file '" + filename + "' does not exist", 0);
- }
- }
-
- ArrayList<String> mergedList = new ArrayList<>(lines);
- lists.forEach(l -> mergedList.addAll(l));
- return mergedList;
- }
-
- public static List<String> readCode(String filename)
- {
- return readCode(filename, ".snuvi");
- }
- }
|