How will you proceed the following string:
[p('WHITE'),p('GREEN'),p('GREEN','RED'),p('GREEN','YELLOW'),p('GREEN','YELLOW','RED')]
to get a List of Nodes as suggested in the answers.
How will you proceed the following string:
[p('WHITE'),p('GREEN'),p('GREEN','RED'),p('GREEN','YELLOW'),p('GREEN','YELLOW','RED')]
to get a List of Nodes as suggested in the answers.
It depends on how complex the expressions can get. I would definitely write a proper parser for it, since only then can I be confident to have done it properly. Plus, I will parse the string into an abstract syntax tree so that at the end I will automatically have a nicely modeled tree of objects:
class Node { ... }
class StringLiteral extends Node {
public String getValue() { ... }
}
class Term extends Node {
public String getName() { ... }
public int getArity() { ... }
public Node getArgument(int index) { ... }
}
Parser class that has one method per syntactical element that you are interested in. I would call them List<Node> parseArray(), Term parseTerm(), StringLiteral parseStringLiteral(), Node parseExpression(). The implementation of parseArray would look roughly like read('[']); while (next() != ']') { parseExpression(); } read(']');. The methods basically contain sequences of these operations or large switch statements, which decide what to parse next, depending on the next character from the input.