tiles.rc: revise syntax of tile specification

changed my mind on tile specification syntax

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@728 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-07-17 19:58:14 +00:00
parent 153e0e48f5
commit 2cef8e368a
2 changed files with 77 additions and 91 deletions

View file

@ -86,9 +86,8 @@ public class MakeTiles
{ {
ImageSpec result = null; ImageSpec result = null;
for (String layerStr : spec.getValues("image")) { for (String layerStr : spec.getImages()) {
System.err.println("parsing "+layerStr);
ImageSpec rv = new ImageSpec(); ImageSpec rv = new ImageSpec();
rv.background = result; rv.background = result;
result = rv; result = rv;

View file

@ -4,11 +4,13 @@ import java.util.*;
public class TileSpec public class TileSpec
{ {
Map<String, List<String> > multiValues; Map<String,String> attributes;
List<String> images;
protected TileSpec() protected TileSpec()
{ {
this.multiValues = new HashMap<String, List<String> >(); this.attributes = new HashMap<String,String>();
this.images = new ArrayList<String>();
} }
public static TileSpec parse(String inStr) public static TileSpec parse(String inStr)
@ -18,51 +20,41 @@ public class TileSpec
return ts; return ts;
} }
public String getValue(String key) public String getAttribute(String key)
{ {
List<String> v = multiValues.get(key); return attributes.get(key);
if (v != null && v.size() >= 1) {
return v.get(0);
}
else {
return null;
}
} }
public String [] getValues(String key) public String [] getImages()
{ {
List<String> v = multiValues.get(key); return images.toArray(new String[0]);
if (v != null) {
return v.toArray(new String[0]);
}
else {
return new String[0];
}
} }
protected void load(String inStr) protected void load(String inStr)
{ {
Scanner in = new Scanner(inStr); Scanner in = new Scanner(inStr);
String k;
while ( (k=in.nextKey()) != null ) { while (in.hasMore()) {
String v = in.nextValue();
if (v == null) { if (in.peekChar() == '(') {
if (k.startsWith("no")) { in.eatChar('(');
k = k.substring(2); String k = in.readAttributeKey();
v = "0"; String v = "1";
} if (in.peekChar() == '=') {
else { in.eatChar('=');
v = "1"; v = in.readAttributeValue();
} }
in.eatChar(')');
attributes.put(k, v);
} }
if (multiValues.containsKey(k)) { else if (in.peekChar() == '|' || in.peekChar() == ',') {
multiValues.get(k).add(v); in.eatChar(in.peekChar());
} }
else { else {
ArrayList<String> a = new ArrayList<String>(); String v = in.readImageSpec();
a.add(v); images.add(v);
multiValues.put(k, a);
} }
} }
} }
@ -71,99 +63,94 @@ public class TileSpec
{ {
String str; String str;
int off = 0; int off = 0;
int st = ST_VALUE;
static int ST_KEY = 0;
static int ST_VALUE = 1;
static int ST_COMPAT = 2;
static int ST_EOF = 3;
Scanner(String str) Scanner(String str)
{ {
this.str = str; this.str = str;
if (str.indexOf('(') == -1) {
st = ST_COMPAT;
}
} }
private void skipWhitespace() private void skipWhitespace()
{ {
while (off < str.length() && (Character.isWhitespace(str.charAt(off)) || str.charAt(off) == ',')) { while (off < str.length() && Character.isWhitespace(str.charAt(off))) {
off++; off++;
} }
} }
public String nextKey() public int peekChar()
{ {
if (st == ST_EOF || off >= str.length()) { skipWhitespace();
return null; if (off < str.length()) {
} return str.charAt(off);
if (st == ST_COMPAT) {
st = ST_VALUE;
return "image";
}
assert st == ST_KEY;
if (str.charAt(off) == '|') {
st = ST_VALUE;
off++;
skipWhitespace();
return "image";
} }
else { else {
int start = off; return -1;
while (off < str.length() && (str.charAt(off) == '-' || Character.isLetterOrDigit(str.charAt(off)))) {
off++;
}
st = ST_VALUE;
return str.substring(start, off);
} }
} }
public String nextValue() public void eatChar(int ch)
{ {
assert st == ST_VALUE; skipWhitespace();
assert str.charAt(off) == ch;
off++;
}
if (off == str.length()) { public String readAttributeKey()
st = ST_EOF; {
return null; skipWhitespace();
}
int c = str.charAt(off); int start = off;
if (Character.isWhitespace(c) || c == ',') { while (off < str.length() && (str.charAt(off) == '-' || Character.isLetterOrDigit(str.charAt(off)))) {
skipWhitespace();
st = ST_KEY;
return null;
}
int endQuote = 0;
if (c == '(') {
off++; off++;
endQuote = ')'; }
if (off != start) {
return str.substring(start, off);
}
else {
return null;
}
}
public String readAttributeValue()
{
return readString();
}
public String readImageSpec()
{
return readString();
}
protected String readString()
{
skipWhitespace();
int endQuote = 0; //any whitespace or certain punctuation
if (peekChar() == '"') {
off++;
endQuote = '"';
} }
int start = off; int start = off;
while (off < str.length()) { while (off < str.length()) {
c = str.charAt(off); int c = str.charAt(off);
if (c == endQuote) { if (c == endQuote) {
int end = off; int end = off;
off++; off++;
skipWhitespace();
st = ST_KEY;
return str.substring(start,end); return str.substring(start,end);
} }
else if (endQuote == 0 && (Character.isWhitespace(c) || c == '|')) { else if (endQuote == 0 && (Character.isWhitespace(c) || c == ')' || c == '|')) {
int end = off; int end = off;
skipWhitespace();
st = ST_KEY;
return str.substring(start, end); return str.substring(start, end);
} }
off++; off++;
} }
st = ST_EOF;
return str.substring(start); return str.substring(start);
} }
public boolean hasMore()
{
return peekChar() != -1;
}
} }
} }