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) { ... }
}
精彩评论