package io.parsingdata.metal.expression.value.reference;

import static io.parsingdata.metal.Util.checkNotNull;

import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.ParseValue;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.value.ConstantFactory;
import io.parsingdata.metal.expression.value.OptionalValue;
import io.parsingdata.metal.expression.value.ValueExpression;

public class Len implements ValueExpression {

    private final String _name;

    public Len(final String name) {
        _name = checkNotNull(name, "name");
    }

    @Override
    public OptionalValue eval(final Environment env, final Encoding enc) {
        final ParseValue value = env.order.get(_name);
        return OptionalValue.of(value == null || value.getValue() == null ? null : ConstantFactory.createFromNumeric(value.getValue().length, new Encoding(true)));
    }

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

}
@@ -362,24 +362,76 @@
 ionalValue;%0A
+import io.parsingdata.metal.expression.value.Value;%0A
 import io.pa
@@ -874,12 +874,117 @@
 ? nu
-ll :
+m(0) : num(value.getValue().length));%0A    %7D%0A%0A    private static Value num(final long length) %7B%0A        return
  Con
@@ -1014,33 +1014,16 @@
 Numeric(
-value.getValue().
 length, 
@@ -1041,17 +1041,16 @@
 g(true))
-)
 ;%0A    %7D%0A
@@ -1158,16 +1158,15 @@
  + %22)%22;%0A
+
     %7D%0A
-%0A
 %7D

Return zero length when value is null