/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
* Stanislav Poslavsky <stvlpos@mail.ru>
* Bolotin Dmitriy <bolotin.dmitriy@gmail.com>
*
* This file is part of Redberry.
*
* Redberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Redberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Redberry. If not, see <http://www.gnu.org/licenses/>.
*/
package cc.redberry.core.parser;
import cc.redberry.core.context.CC;
import cc.redberry.core.context.NameDescriptor;
import cc.redberry.core.indices.IndicesTypeStructure;
import cc.redberry.core.tensor.SimpleTensor;
public class ParserSimpleTensor implements TensorParser {
public static final ParserSimpleTensor INSTANCE = new ParserSimpleTensor();
private ParserSimpleTensor() {
}
private static final int parserID = 10;
@Override
public SimpleTensor parse(String expression, Parser parser) {
ParserSimpleTensorStructure stp = new ParserSimpleTensorStructure(expression);
NameDescriptor descriptor = new NameDescriptor(stp.name, new IndicesTypeStructure(stp.getIndices()));
int tensorName = CC.getNameManager().mapNameDescriptor(descriptor);
return new SimpleTensor(tensorName, stp.getIndices());
}
// public Tensor parse(String expression, Parser parser) {
// List<Integer> indicesList = new ArrayList<Integer>();
// boolean indexMode = false;
// int indexState = 0;
// StringBuilder nameBuilder = new StringBuilder();
// StringBuilder indicesString = null;
// for (char c : expression.toCharArray()) {
// if (c == '^') {
// if (indexMode)
// parseIndices(indicesList, indicesString, indexState);
// indexMode = true;
// indexState = 1;
// indicesString = new StringBuilder();
// continue;
// }
// if (c == '_') {
// if (indexMode)
// parseIndices(indicesList, indicesString, indexState);
// indexMode = true;
// indexState = 0;
// indicesString = new StringBuilder();
// continue;
// }
// if (!indexMode)
// nameBuilder.append(c);
// else
// indicesString.append(c);
// }
// if (indexMode)
// parseIndices(indicesList, indicesString, indexState);
// int[] indicesArray = new int[indicesList.size()];
// for (int i = 0; i < indicesList.size(); i++)
// indicesArray[i] = indicesList.get(i).intValue();
// Indices indices = new Indices(indicesArray);
// NameDescriptor descriptor = new NameDescriptor(new IndexStructure(indices), nameBuilder.toString(), false);
// int name = Context.get().mapNameDescriptor(descriptor);
// return new SimpleTensor(name, indices);
// }
//
// private void parseIndices(List<Integer> indices, StringBuilder indicesString, int state) {
// boolean longName = false;
// int brackets = 0;
// char c;
// boolean br;
// boolean ignoreThis;
// StringBuilder buffer = null;
// String toAdd = null;
// for (int i = 0; i < indicesString.length(); i++) {
// c = indicesString.charAt(i);
// if (brackets < 0)
// throw new RuntimeException("Syntax error.");
// br = false;
// ignoreThis = false;
// toAdd = null;
// if (c == '{') {
// brackets++;
// br = true;
// ignoreThis = true;
// }
// if (c == '}') {
// brackets--;
// br = true;
// ignoreThis = true;
// }
// if (c == ' ') {
// br = true;
// ignoreThis = true;
// }
// if ((br || c == '\\') && longName) {
// toAdd = buffer.toString();
// longName = false;
// }
// if (c == '\\') {
// longName = true;
// buffer = new StringBuilder();
// }
// if (!ignoreThis)
// if (longName)
// buffer.append(c);
// else
// toAdd = Character.toString(c);
// if (toAdd != null)
// indices.add(Context.get().getIndexConverterFactory().getCode(toAdd) | state << 31);
// }
// if (brackets != 0) {
// }
// }
@Override
public int getPriority() {
return parserID;
}
}