static void parse(String expression, IntArrayList indices, int state) { Matcher matcher = pattern.matcher(expression); String singleIndex; while (matcher.find()) { singleIndex = matcher.group(); indices.add(CC.getIndexConverterManager().getCode(singleIndex) | state); } String remainder = matcher.replaceAll(""); remainder = remainder.replaceAll("[\\{\\}\\s]*", ""); if (remainder.length() != 0) throw new ParserException("Incorrect indices: " + expression); } }
@Override public ParseToken parseToken(String expression, Parser parser) { if (!expression.contains("=")) return null; if (expression.indexOf('=') != expression.lastIndexOf('=')) throw new ParserException("Several '=' symbols."); String[] parts = expression.split("="); parts[0] = parts[0].trim(); boolean preprocessing = false; if (parts[0].charAt(parts[0].length() - 1) == ':') { preprocessing = true; parts[0] = parts[0].substring(0, parts[0].length() - 1); } ParseToken left = parser.parse(parts[0]); ParseToken right = parser.parse(parts[1]); return new ParseTokenExpression(preprocessing, left, right); }
/** * Parse string expression into AST. * * @param expression string expression * @return AST */ public ParseToken parse(String expression) { //replacing comments expression = deleteComments(expression); if (expression.isEmpty()) throw new IllegalArgumentException("Empty expression."); for (TokenParser tokenParser : tokenParsers) { ParseToken node = tokenParser.parseToken(expression.trim(), this); if (node != null) return node; } throw new ParserException("No appropriate parser for expression: \"" + expression + "\""); }
@Override public ParseToken parseToken(String expression, Parser parser) { if (expression.length() <= minLength) return null; if (!(power + '[').equals(expression.substring(0, power.length() + 1)) || expression.charAt(expression.length() - 1) != ']') return null; int level = 0, i, comma = -1; char c; for (i = power.length(); i < expression.length(); ++i) { c = expression.charAt(i); if (c == '[') ++level; if (level < 1) return null; if (c == ']') --level; if (c == ',' && level == 1) { if (comma != -1) throw new ParserException("Power takes only two arguments."); comma = i; } } ParseToken arg = parser.parse(expression.substring(power.length() + 1, comma)); ParseToken power = parser.parse(expression.substring(comma + 1, expression.length() - 1)); return new ParseToken(TokenType.Power, arg, power); } }
private void modeParser(String expression, Mode mode, Parser parser, List<ParseToken> nodes) { if (mode == Mode.Direct) { nodes.add(parser.parse(expression)); return; } if (mode == Mode.Inverse) nodes.add(inverseOperation(parser.parse(expression))); else throw new ParserException("unrepoted operator parser mode"); }
@Override public final ParseTokenSimpleTensor parseToken(String expression, Parser parser) { expression = expression.replaceAll("\\{[\\s]*\\}", ""); int indicesBegin = expression.indexOf('_'), i = expression.indexOf('^'); if (indicesBegin < 0 && i >= 0) indicesBegin = i; if (indicesBegin >= 0 && i >= 0) indicesBegin = Math.min(indicesBegin, i); if (indicesBegin < 0) indicesBegin = expression.length(); String name = expression.substring(0, indicesBegin); if (name.isEmpty()) throw new ParserException("Simple tensor with empty name."); SimpleIndices indices; if (parser.isAllowSameVariance()) indices = ParserIndices.parseSimpleIgnoringVariance(expression.substring(indicesBegin)); else indices = ParserIndices.parseSimple(expression.substring(indicesBegin)); return new ParseTokenSimpleTensor(indices, name); }
return null; if (c == ',' && level == 0)//case for Sin[x,y] throw new ParserException("Sin, Cos, Tan and others scalar functions take only one argument.");
ordersDescriptor = ordersDescriptor.replace(" ", ""); if (ordersDescriptor.length() == 0) throw new ParserException("Error in derivative orders in \"" + name + "\""); throw new ParserException("Unbalanced brackets in derivative orders in \"" + name + "\""); throw new ParserException("Number of arguments does not match number of derivative orders in \"" + name + "\""); int[] orders = new int[ordersStr.length]; for (i = orders.length - 1; i >= 0; --i) orders[i] = Integer.parseInt(ordersStr[i], 10); } catch (NumberFormatException nfe) { throw new ParserException("Illegal order of derivative: \"" + ordersStr[i] + "\" in \"" + name + "\"");
} else { if (split.length != 2) throw new ParserException(expression); a = parser.parse(split[0]); aIndices = ParserIndices.parseSimple(split[1]);
private NameDescriptor createDescriptor(final String sname, final StructureOfIndices[] structuresOfIndices, int id) { if (structuresOfIndices.length != 1) return new NameDescriptorForTensorFieldImpl(sname, structuresOfIndices, id, sname.equals(diracDeltaName) && structuresOfIndices.length == 3); final StructureOfIndices its = structuresOfIndices[0]; if (its.size() != 2) return new NameDescriptorForSimpleTensor(sname, structuresOfIndices, id); for (byte b = 0; b < IndexType.TYPES_COUNT; ++b) if (its.typeCount(b) == 2) { if (CC.isMetric(b)) { if (sname.equals(kroneckerAndMetricNames[0]) || sname.equals(kroneckerAndMetricNames[1])) { NameDescriptor descriptor = new NameDescriptorForMetricAndKronecker(kroneckerAndMetricNames, b, id); descriptor.getSymmetries().add(b, false, 1, 0); return descriptor; } } else { if (sname.equals(kroneckerAndMetricNames[1])) throw new ParserException("Metric is not specified for non metric index type."); if (sname.equals(kroneckerAndMetricNames[0])) { if (its.getTypeData(b).states.get(0) != true || its.getTypeData(b).states.get(1) != false) throw new ParserException("Illegal Kroneckers indices states."); NameDescriptor descriptor = new NameDescriptorForMetricAndKronecker(kroneckerAndMetricNames, b, id); return descriptor; } } } return new NameDescriptorForSimpleTensor(sname, structuresOfIndices, id); }
/** * Returns {@link Indices} of the corresponding mathematical expression. * * @return {@link Indices} of the corresponding mathematical expression */ public Indices getIndices() { switch (tokenType) { case Product: case Trace: IndicesBuilder builder = new IndicesBuilder(); for (ParseToken node : content) builder.append(node.getIndices()); return builder.getIndices(); case Sum: return IndicesFactory.create(content[0].getIndices()); case Power: return IndicesFactory.EMPTY_INDICES; } throw new ParserException("Unknown tensor type: " + tokenType); }
/** * Converts this AST to tensor. * * @return resulting tensor */ public Tensor toTensor() { switch (tokenType) { case Sum: return Tensors.sum(contentToTensors()); case Power: assert content.length == 2; return Tensors.pow(content[0].toTensor(), content[1].toTensor()); case Trace: case Product: return Tensors.multiplyAndRenameConflictingDummies(contentToTensors()); } throw new ParserException("Unknown tensor type: " + tokenType); }