dmolony-DiskBrowser/src/com/bytezone/diskbrowser/visicalc/ExpressionList.java

78 lines
2.0 KiB
Java
Raw Normal View History

2017-03-14 11:28:52 +00:00
package com.bytezone.diskbrowser.visicalc;
import java.util.Iterator;
2017-03-15 00:41:45 +00:00
import java.util.regex.Pattern;
2017-03-14 11:28:52 +00:00
2017-03-15 00:41:45 +00:00
public class ExpressionList extends AbstractValue implements Iterable<Value>
2017-03-14 11:28:52 +00:00
{
2017-03-15 00:41:45 +00:00
private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}");
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
2017-03-14 11:28:52 +00:00
private final Sheet parent;
2017-03-15 00:41:45 +00:00
public ExpressionList (Sheet parent, Cell cell, String text)
2017-03-14 11:28:52 +00:00
{
2017-03-15 00:41:45 +00:00
super ("expL");
2017-03-14 11:28:52 +00:00
this.parent = parent;
2017-03-15 00:41:45 +00:00
2017-03-15 07:07:36 +00:00
int ptr = 0;
while (ptr < text.length ())
2017-03-15 00:41:45 +00:00
{
2017-03-15 07:07:36 +00:00
if (text.charAt (ptr) == '@')
{
String functionText = Expression.getBalancedText (text.substring (ptr));
Value v = new Expression (parent, cell, functionText).reduce ();
values.add (v);
ptr += functionText.length ();
}
else
{
String item = getNextItem (text, ptr);
int pos = item.indexOf ("...");
if (pos > 0) // range
{
String fromAddress = item.substring (0, pos);
String toAddress = item.substring (pos + 3);
2017-03-15 00:41:45 +00:00
2017-03-15 07:07:36 +00:00
Address from = new Address (fromAddress);
Address to = new Address (toAddress);
2017-03-15 00:41:45 +00:00
2017-03-15 07:07:36 +00:00
Range range = new Range (parent, from, to);
2017-03-15 00:41:45 +00:00
2017-03-15 07:07:36 +00:00
for (Address address : range)
values.add (parent.getCell (address));
}
else
{
Value v = new Expression (parent, cell, item).reduce ();
values.add (v);
}
ptr += item.length ();
}
2017-03-15 00:41:45 +00:00
2017-03-15 07:07:36 +00:00
if (ptr < text.length () && text.charAt (ptr) == ',')
ptr++;
if (ptr < text.length () && text.charAt (ptr) == ')')
ptr++;
2017-03-15 00:41:45 +00:00
}
}
2017-03-15 07:07:36 +00:00
private String getNextItem (String text, int ptr)
{
int p = ptr;
while (++p < text.length () && text.charAt (p) != ',')
;
return text.substring (ptr, p);
}
2017-03-15 00:41:45 +00:00
public int size ()
{
return values.size ();
2017-03-14 11:28:52 +00:00
}
@Override
2017-03-15 00:41:45 +00:00
public Iterator<Value> iterator ()
2017-03-14 11:28:52 +00:00
{
2017-03-15 00:41:45 +00:00
return values.iterator ();
2017-03-14 11:28:52 +00:00
}
}