Question

Hello this is my first time posting here, I did look around and I couldn't find an answer to the problem I'm having.

I need to turn this string into two strings:

"R(rndInt[-10,10]),R(rndInt[-10,10])"

to

 "R(rndInt[-10,10])" "R(rndInt[-10,10])"

To make a long story short, I am modding a game and creating my own scripting language for people to use. I made a class/method based structure (Notes on that here: http://pastie.org/8825346). Right now I'm trying to make it so that you can use Classes/Methods within methods(which works for single parameter methods).

The problem I'm having right now is splitting that string because it has multiple commas. I can't really think of a way to split it without causing problems.

Can anybody help? I am stumped right now.

Edit: Solved!

The lookahead assertion method works! I haven't heard of it till now.

I forgot to mention that I cannot use the "R". The R references a Class, which can have different names.

I came up with a solution for that issue by finding out what is between the ")," and "(" by sub-stringing the string.

String cName=oldStr.substring(oldStr.indexOf("),")+2);
cName=cName.substring(0, oldStr.indexOf("("));
System.out.println("CNAME:"+cName);
String[] test = oldStr.split(",(?="+cName+")");

Thank you guys, I am so grateful for the help :)

No correct solution

OTHER TIPS

If you're creating your own language, it's probably best to treat it as a language and do proper lex/parse. There are some open-source tools available to assist with this, such as JFlex.

However, based upon your current language requirements, your structure is simple enough that you could get away with doing a split(), as suggested in the other answers. That may or may not remain viable as your language evolves over time.

Why not targeting the concerned comma:

str.split("\\),R");   // as a simple Regexp

UPDATE ----

As pointing in comments and answered by @Reimeus, the regexp would end up to:

str.split(",(?=R)");

You could use a lookahead assertion

String[] array = str.split(",(?=R)");

There are numerous scripting languages out there, are you sure reinventing the wheel is the best use of your time? In case you are:

For non-trivial languages, parsing is typically done with a parser generator.

However, for simple cases, writing it by hand isn't too hard. A reasonable approach is recursive decent parsing. In your case, this would look something like:

import java.util.ArrayList;
import java.util.List;

abstract class Expression {

}

class MethodInvocation extends Expression {
    final String methodName;
    final List<Expression> arguments;

    MethodInvocation(String methodName, List<Expression> arguments) {
        this.methodName = methodName;
        this.arguments = arguments;
    }
}

class Literal extends Expression {
    final int value;

    Literal(int value) {
        this.value = value;
    }
}


public class Parser {
    char[] input;

    /** index of next character to consume */
    int index;

    Parser(String input) {
        this.input = input.toCharArray();
        index = 0;
    }

    void consumeExpected(char ch) {
        if (input[index] != ch) {
            throw new RuntimeException("Syntax Error");
        }
        index++;
    }

    String identifier() {
        int start = index;
        while (index < input.length && Character.isAlphabetic(input[index])) {
            index++;
        }
        return new String(input, start, index - start);
    }

    Literal numericLiteral() {
        int start = index;
        if (input[index] == '-') {
            index++;
        }
        while (index < input.length && '0' <= input[index] && input[index] <= '9') {
            index++;
        }
        return new Literal(Integer.parseInt(new String(input, start, index - start)));
    }

    Expression expression() {
        if (Character.isAlphabetic(input[index])) {
            String methodName = identifier();
            consumeExpected('(');

            List<Expression> arguments = new ArrayList<>();
            if (input[index] != ')') {
                do {
                    arguments.add(expression());
                } while (input[index++] == ',');
                index--;
                consumeExpected(')');
            }
            return new MethodInvocation(methodName, arguments);
        } else {
            return numericLiteral();
        }
    }

    public static void main(String[] args) {
        Expression ex = new Parser("R(rndInt(-10,10)),R(rndInt(-10,10))").expression();
        System.out.println(ex);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top