Skip to content

Commit

Permalink
Merge pull request #77 from parsingdata/token-names
Browse files Browse the repository at this point in the history
All Tokens now have a(n optional) name
  • Loading branch information
rdvdijk authored Jul 25, 2016
2 parents 517756a + d4627e4 commit 6858faf
Show file tree
Hide file tree
Showing 29 changed files with 333 additions and 303 deletions.
58 changes: 37 additions & 21 deletions core/src/main/java/io/parsingdata/metal/Shorthand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,51 @@ public class Shorthand {
public static Token def(final String name, final ValueExpression size, final Expression predicate, final Encoding encoding) { return new Def(name, size, predicate, encoding); }
public static Token def(final String name, final ValueExpression size, final Expression predicate) { return def(name, size, predicate, null); }
public static Token def(final String name, final ValueExpression size, final Encoding encoding) { return def(name, size, null, encoding); }
public static Token def(final String name, final ValueExpression size) { return def(name, size, null, null); }
public static Token def(final String name, final ValueExpression size) { return def(name, size, (Encoding)null); }
public static Token def(final String name, final long size, final Expression predicate, final Encoding encoding) { return def(name, con(size), predicate, encoding); }
public static Token def(final String name, final long size, final Expression predicate) { return def(name, size, predicate, null); }
public static Token def(final String name, final long size, final Encoding encoding) { return def(name, size, null, encoding); }
public static Token def(final String name, final long size) { return def(name, size, null, null); }
public static Token cho(final Encoding encoding, final Token... tokens) { return new Cho(encoding, tokens); }
public static Token cho(final Token... tokens) { return cho(null, tokens); }
public static Token rep(final Token token, final Encoding encoding) { return new Rep(token, encoding); }
public static Token rep(final Token token) { return new Rep(token, null); }
public static Token repn(final Token token, final ValueExpression n, final Encoding encoding) { return new RepN(token, n, encoding); }
public static Token repn(final Token token, final ValueExpression n) { return new RepN(token, n, null); }
public static Token seq(final Encoding encoding, final Token... tokens) { return new Seq(encoding, tokens); }
public static Token seq(final Token... tokens) { return seq(null, tokens); }
public static Token str(final String scope, final Token token, final Encoding encoding) { return str(scope, token, encoding, null, null); }
public static Token str(final String scope, final Token token) { return str(scope, token, null, null, null); }
public static Token str(final String scope, final Token token, final Encoding encoding, final StructSink sink) { return new Str(scope, token, encoding, sink, null); }
public static Token str(final String scope, final Token token, final StructSink sink) { return str(scope, token, null, sink, null); }
public static Token def(final String name, final long size) { return def(name, size, (Encoding)null); }
public static Token cho(final String name, final Encoding encoding, final Token... tokens) { return new Cho(name, encoding, tokens); }
public static Token cho(final String name, final Token... tokens) { return cho(name, null, tokens); }
public static Token cho(final Encoding encoding, final Token... tokens) { return cho("", encoding, tokens); }
public static Token cho(final Token... tokens) { return cho((Encoding)null, tokens); }
public static Token rep(final String name, final Token token, final Encoding encoding) { return new Rep(name, token, encoding); }
public static Token rep(final String name, final Token token) { return rep(name, token, null); }
public static Token rep(final Token token, final Encoding encoding) { return rep("", token, encoding); }
public static Token rep(final Token token) { return rep(token, null); }
public static Token repn(final String name, final Token token, final ValueExpression n, final Encoding encoding) { return new RepN(name, token, n, encoding); }
public static Token repn(final String name, final Token token, final ValueExpression n) { return repn(name, token, n, null); }
public static Token repn(final Token token, final ValueExpression n, final Encoding encoding) { return repn("", token, n, encoding); }
public static Token repn(final Token token, final ValueExpression n) { return repn(token, n, null); }
public static Token seq(final String name, final Encoding encoding, final Token... tokens) { return new Seq(name, encoding, tokens); }
public static Token seq(final String name, final Token... tokens) { return seq(name, null, tokens); }
public static Token seq(final Encoding encoding, final Token... tokens) { return seq("", encoding, tokens); }
public static Token seq(final Token... tokens) { return seq((Encoding)null, tokens); }
public static Token str(final String scope, final Token token, final Encoding encoding, final StructSink sink, final Expression predicate) { return new Str(scope, token, encoding, sink, predicate); }
public static Token str(final String scope, final Token token, final Encoding encoding, final StructSink sink) { return str(scope, token, encoding, sink, null); }
public static Token str(final String scope, final Token token, final Encoding encoding) { return str(scope, token, encoding, null); }
public static Token str(final String scope, final Token token, final StructSink sink, final Expression predicate) { return str(scope, token, null, sink, predicate); }
public static Token sub(final Token token, final ValueExpression address, final Encoding encoding) { return new io.parsingdata.metal.token.Sub(token, address, encoding); }
public static Token str(final String scope, final Token token, final StructSink sink) { return str(scope, token, null, sink); }
public static Token str(final String scope, final Token token) { return str(scope, token, (Encoding)null); }
public static Token sub(final String name, final Token token, final ValueExpression address, final Encoding encoding) { return new io.parsingdata.metal.token.Sub(name, token, address, encoding); }
public static Token sub(final String name, final Token token, final ValueExpression address) { return sub(name, token, address, null); }
public static Token sub(final Token token, final ValueExpression address, final Encoding encoding) { return sub("", token, address, encoding); }
public static Token sub(final Token token, final ValueExpression address) { return sub(token, address, null); }
public static Token pre(final Token token, final Expression predicate, final Encoding encoding) { return new Pre(token, predicate, encoding); }
public static Token pre(final String name, final Token token, final Expression predicate, final Encoding encoding) { return new Pre(name, token, predicate, encoding); }
public static Token pre(final String name, final Token token, final Expression predicate) { return pre(name, token, predicate, null); }
public static Token pre(final Token token, final Expression predicate, final Encoding encoding) { return pre("", token, predicate, encoding); }
public static Token pre(final Token token, final Expression predicate) { return pre(token, predicate, null); }
public static Token whl(final Token token, final Expression predicate, final Encoding encoding) { return new While(token, predicate, encoding); }
public static Token whl(final Token token, final Expression predicate) { return whl(token, predicate, null); }
public static Token opt(final Token token, final Encoding encoding) { return new Opt(token, encoding); }
public static Token whl(final String name, final Token token, final Expression predicate, final Encoding encoding) { return new While(name, token, predicate, encoding); }
public static Token whl(final String name, final Token token, final Expression predicate) { return whl(name, token, predicate, null); }
public static Token whl(final Token token, final Expression predicate, final Encoding encoding) { return whl("", token, predicate, encoding); }
public static Token whl(final Token token, final Expression predicate) { return whl("", token, predicate); }
public static Token opt(final String name, final Token token, final Encoding encoding) { return new Opt(name, token, encoding); }
public static Token opt(final String name, final Token token) { return opt(name, token, null); }
public static Token opt(final Token token, final Encoding encoding) { return opt("", token, encoding); }
public static Token opt(final Token token) { return opt(token, null); }
public static Token nod(final ValueExpression size, final Encoding encoding) { return new Nod(size, encoding); }
public static Token nod(final ValueExpression size) { return new Nod(size, null); }
public static Token nod(final String name, final ValueExpression size) { return new Nod(name, size, null); }
public static Token nod(final ValueExpression size) { return nod("", size); }

public static BinaryValueExpression add(final ValueExpression left, final ValueExpression right) { return new Add(left, right); }
public static BinaryValueExpression div(final ValueExpression left, final ValueExpression right) { return new Div(left, right); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ParseGraph implements ParseItem {
public final Token definition;
public final long size;

public static final Token NONE = new Token(null) {
public static final Token NONE = new Token("NONE", null) {
@Override protected ParseResult parseImpl(final String scope, final Environment env, final Encoding enc) throws IOException { throw new IllegalStateException("This placeholder may not be invoked."); }
@Override public String toString() { return "None"; };
};
Expand Down
25 changes: 5 additions & 20 deletions core/src/main/java/io/parsingdata/metal/data/ParseValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,27 @@

package io.parsingdata.metal.data;

import static io.parsingdata.metal.Util.checkNotNull;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.value.Value;
import io.parsingdata.metal.token.Token;

public class ParseValue extends Value implements ParseItem {
import static io.parsingdata.metal.Util.checkNotNull;

public static final String SEPARATOR = ".";
public class ParseValue extends Value implements ParseItem {

public final String scope;
public final String name;
public Token definition;
public final long offset;

public ParseValue(final String scope, final String name, final Token definition, final long offset, final byte[] data, final Encoding enc) {
public ParseValue(final String name, final Token definition, final long offset, final byte[] data, final Encoding enc) {
super(data, enc);
this.scope = checkNotNull(scope, "scope");
this.name = checkNotNull(name, "name");
this.definition = checkNotNull(definition, "definition");
this.offset = offset;
}

public String getScope() {
return scope;
}

public String getName() {
return name;
}

public String getFullName() {
return getScope() + SEPARATOR + getName();
}

public boolean matches(final String name) {
return getFullName().equals(name) || getFullName().endsWith(SEPARATOR + name);
return this.name.equals(name) || this.name.endsWith(Token.SEPARATOR + name);
}

public long getOffset() {
Expand All @@ -68,7 +53,7 @@ public long getOffset() {

@Override
public String toString() {
return getName() + "(" + super.toString() + ")";
return name + "(" + super.toString() + ")";
}

}
6 changes: 3 additions & 3 deletions core/src/main/java/io/parsingdata/metal/token/Cho.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class Cho extends Token {

private final Token[] tokens;

public Cho(final Encoding enc, final Token... tokens) {
super(enc);
public Cho(final String name, final Encoding enc, final Token... tokens) {
super(name, enc);
this.tokens = checkContainsNoNulls(tokens, "tokens");
if (tokens.length < 2) { throw new IllegalArgumentException("At least two Tokens are required."); }
}
Expand All @@ -55,7 +55,7 @@ private ParseResult iterate(final String scope, final Environment env, final Enc

@Override
public String toString() {
return getClass().getSimpleName() + "(" + Util.tokensToString(tokens) + ")";
return getClass().getSimpleName() + "(" + makeNameFragment() + Util.tokensToString(tokens) + ")";
}

}
8 changes: 3 additions & 5 deletions core/src/main/java/io/parsingdata/metal/token/Def.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@

public class Def extends Token {

public final String name;
public final ValueExpression size;
public final Expression predicate;

public Def(final String name, final ValueExpression size, final Expression predicate, final Encoding enc) {
super(enc);
this.name = checkNotNull(name, "name");
super(name, enc);
this.size = checkNotNull(size, "size");
this.predicate = predicate == null ? new True() : predicate;
}
Expand All @@ -57,13 +55,13 @@ protected ParseResult parseImpl(final String scope, final Environment env, final
if (env.input.read(env.offset, data) != data.length) {
return new ParseResult(false, env);
}
final Environment newEnv = new Environment(env.order.add(new ParseValue(scope, name, this, env.offset, data, enc)), env.input, env.offset + dataSize);
final Environment newEnv = new Environment(env.order.add(new ParseValue(scope, this, env.offset, data, enc)), env.input, env.offset + dataSize);
return predicate.eval(newEnv, enc) ? new ParseResult(true, newEnv) : new ParseResult(false, env);
}

@Override
public String toString() {
return getClass().getSimpleName() + "(\"" + name + "\"," + size + "," + predicate + ",)";
return getClass().getSimpleName() + "(" + makeNameFragment() + size + "," + predicate + ",)";
}

}
6 changes: 3 additions & 3 deletions core/src/main/java/io/parsingdata/metal/token/Nod.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class Nod extends Token {

public final ValueExpression size;

public Nod(final ValueExpression size, final Encoding enc) {
super(enc);
public Nod(final String name, final ValueExpression size, final Encoding enc) {
super(name, enc);
this.size = checkNotNull(size, "size");
}

Expand All @@ -50,7 +50,7 @@ protected ParseResult parseImpl(final String scope, final Environment env, final

@Override
public String toString() {
return getClass().getSimpleName() + "(" + size + ")";
return getClass().getSimpleName() + "(" + makeNameFragment() + size + ")";
}

}
6 changes: 3 additions & 3 deletions core/src/main/java/io/parsingdata/metal/token/Opt.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class Opt extends Token {

public final Token token;

public Opt(final Token token, final Encoding enc) {
super(enc);
public Opt(final String name, final Token token, final Encoding enc) {
super(name, enc);
this.token = checkNotNull(token, "token");
}

Expand All @@ -42,7 +42,7 @@ protected ParseResult parseImpl(final String scope, final Environment env, final

@Override
public String toString() {
return getClass().getSimpleName() + "(" + token + ")";
return getClass().getSimpleName() + "(" + makeNameFragment() + token + ")";
}

}
6 changes: 3 additions & 3 deletions core/src/main/java/io/parsingdata/metal/token/Pre.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class Pre extends Token {
public final Token token;
public final Expression predicate;

public Pre(final Token token, final Expression predicate, final Encoding enc) {
super(enc);
public Pre(final String name, final Token token, final Expression predicate, final Encoding enc) {
super(name, enc);
this.token = checkNotNull(token, "token");
this.predicate = predicate == null ? expTrue() : predicate;
}
Expand All @@ -47,7 +47,7 @@ protected ParseResult parseImpl(final String scope, final Environment env, final

@Override
public String toString() {
return getClass().getSimpleName() + "(" + token + ", " + predicate + ")";
return getClass().getSimpleName() + "(" + makeNameFragment() + token + ", " + predicate + ")";
}

}
6 changes: 3 additions & 3 deletions core/src/main/java/io/parsingdata/metal/token/Rep.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class Rep extends Token {

public final Token token;

public Rep(final Token token, final Encoding enc) {
super(enc);
public Rep(final String name, final Token token, final Encoding enc) {
super(name, enc);
this.token = checkNotNull(token, "token");
}

Expand All @@ -47,7 +47,7 @@ private ParseResult iterate(final String scope, final Environment env, final Enc

@Override
public String toString() {
return getClass().getSimpleName() + "(" + token + ")";
return getClass().getSimpleName() + "(" + makeNameFragment() + token + ")";
}

}
6 changes: 3 additions & 3 deletions core/src/main/java/io/parsingdata/metal/token/RepN.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class RepN extends Token {
public final Token token;
public final ValueExpression n;

public RepN(final Token token, final ValueExpression n, final Encoding enc) {
super(enc);
public RepN(final String name, final Token token, final ValueExpression n, final Encoding enc) {
super(name, enc);
this.token = checkNotNull(token, "token");
this.n = checkNotNull(n, "n");
}
Expand All @@ -59,7 +59,7 @@ private ParseResult iterate(final String scope, final Environment env, final Enc

@Override
public String toString() {
return getClass().getSimpleName() + "(" + token + "," + n + ")";
return getClass().getSimpleName() + "(" + makeNameFragment() + token + "," + n + ")";
}

}
6 changes: 3 additions & 3 deletions core/src/main/java/io/parsingdata/metal/token/Seq.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class Seq extends Token {

private final Token[] tokens;

public Seq(final Encoding enc, final Token... tokens) {
super(enc);
public Seq(final String name, final Encoding enc, final Token... tokens) {
super(name, enc);
this.tokens = checkContainsNoNulls(tokens, "tokens");
if (tokens.length < 2) { throw new IllegalArgumentException("At least two Tokens are required."); }
}
Expand All @@ -55,7 +55,7 @@ private ParseResult iterate(final String scope, final Environment env, final Enc

@Override
public String toString() {
return getClass().getSimpleName() + "(" + Util.tokensToString(tokens) + ")";
return getClass().getSimpleName() + "(" + makeNameFragment() + Util.tokensToString(tokens) + ")";
}

}
14 changes: 6 additions & 8 deletions core/src/main/java/io/parsingdata/metal/token/Str.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,31 @@

public class Str extends Token {

public final String scope;
public final Token token;
public final StructSink sink;
public final Expression predicate;

public Str(final String scope, final Token token, final Encoding enc, final StructSink sink, final Expression predicate) {
super(enc);
this.scope = checkNotNull(scope, "scope");
public Str(final String name, final Token token, final Encoding enc, final StructSink sink, final Expression predicate) {
super(name, enc);
this.token = checkNotNull(token, "token");
this.sink = sink;
this.predicate = predicate == null ? new True() : predicate;
}

@Override
protected ParseResult parseImpl(final String outerScope, final Environment env, final Encoding enc) throws IOException {
final ParseResult res = token.parse(outerScope + "." + scope, new Environment(env.order.addBranch(this), env.input, env.offset), enc);
protected ParseResult parseImpl(final String scope, final Environment env, final Encoding enc) throws IOException {
final ParseResult res = token.parse(scope, new Environment(env.order.addBranch(this), env.input, env.offset), enc);
if (!res.succeeded) { return new ParseResult(false, env); }
final ParseResult closedResult = new ParseResult(true, new Environment(res.environment.order.closeBranch(), res.environment.input, res.environment.offset));
if (sink != null && predicate.eval(closedResult.environment, enc)) {
sink.handleStruct(outerScope, closedResult.environment, enc, closedResult.environment.order.get(this).asGraph());
sink.handleStruct(scope, closedResult.environment, enc, closedResult.environment.order.get(this).asGraph());
}
return closedResult;
}

@Override
public String toString() {
return getClass().getSimpleName() + "(\"" + scope + "\"," + token + (sink != null ? "," + sink : "") + ")";
return getClass().getSimpleName() + "(" + makeNameFragment() + token + (sink != null ? "," + sink : "") + ")";
}

}
Loading

0 comments on commit 6858faf

Please sign in to comment.