use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.
the class ExtensionalIndexExpressionsSet method getSubSyntaxTree.
@Override
public SyntaxTree getSubSyntaxTree() {
List<SyntaxTree> indexExpressionsSyntaxTrees = mapIntoArrayList(getList(), Expression::getSyntaxTree);
SyntaxTree result = SyntaxTrees.makeKleeneListIfNeeded(indexExpressionsSyntaxTrees);
return result;
}
use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.
the class SyntaxTrees method parse.
/**
* Parse a string into a syntax tree by parsing it as an expression using {@link AntlrGrinderParserWrapper}
* returning its syntax tree.
*/
public static SyntaxTree parse(String string) {
Expression expression = parser.parse(string);
SyntaxTree result = expression.getSyntaxTree();
return result;
}
use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.
the class SyntaxTrees method replaceAtPath.
/**
* Given an syntax tree, a path, a position i in the path, and a sub-syntax tree,
* returns the syntax tree with its path-i-sub-syntax tree replaced by the given sub-syntax tree.
* The path is a list of indices indicating a path in the syntax tree.
* The path-i-sub-syntax tree is the syntax tree obtained by following the path from the position i on.
* If there are no indices to be followed (i is equal to the path's length), the sub-syntax tree is returned.
* The method assumes the path describes an existing path-i-sub-syntax tree.
*/
private static SyntaxTree replaceAtPath(SyntaxTree syntaxTree, List<Integer> path, int i, SyntaxTree subTree) {
if (i != path.size()) {
int index = path.get(i);
SyntaxTree subTreeAtI = replaceAtPath(syntaxTree.getSubTree(index), path, i + 1, subTree);
// does need to be sub tree
SyntaxTree result = syntaxTree.setImmediateSubTree(index, subTreeAtI);
return result;
}
return subTree;
}
use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.
the class DefaultCompoundSyntaxTree method compareTo.
@Override
public /**
* Compares this CompoundSyntaxTree to other syntax trees, placing it after {@link Symbol}s and comparing
* it lexicographically to other CompoundSyntaxTree, using both the label and sub trees.
*/
int compareTo(Object anotherObject) {
int result;
if (anotherObject instanceof CompoundSyntaxTree) {
CompoundSyntaxTree normalizedAnotherCompoundSyntaxTree = (CompoundSyntaxTree) anotherObject;
Iterator<SyntaxTree> subTreesIncludingLabel = getImmediateSubTreesIncludingRootOneIterator();
Iterator<SyntaxTree> anotherSubTreesIncludingLabel = normalizedAnotherCompoundSyntaxTree.getImmediateSubTreesIncludingRootOneIterator();
result = lexicographicComparison.compare(subTreesIncludingLabel, anotherSubTreesIncludingLabel);
} else {
// Symbols "come first" in the default order
result = 1;
}
return result;
}
use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.
the class DefaultCompoundSyntaxTree method hashCode.
@Override
public int hashCode() {
if (hashCode == -1) {
SyntaxTree rootTree = getRootTree();
int rootHashCode = rootTree.hashCode();
List<SyntaxTree> immediateSubTrees = getImmediateSubTrees();
int subTreesHashCode = immediateSubTrees.hashCode();
hashCode = rootHashCode + subTreesHashCode;
}
return hashCode;
}
Aggregations