@@ -143,16 +143,50 @@ public final class JBBPCompiler {
143143 public static final int EXT_FLAG_EXTRA_AS_EXPRESSION = 0x02 ;
144144
145145 /**
146- * The Flag shows that the type of data should be recognized as float (if int) or as double (if long).
146+ * The Flag shows that the type of data should be recognized differently.
147+ * as float (if int), as double (if long), as virtual value (if skip).
147148 *
148149 * @since 1.4.0
149150 */
150- public static final int EXT_FLAG_EXTRA_AS_FLOAT_DOUBLE_OR_STRING = 0x04 ;
151+ public static final int EXT_FLAG_EXTRA_DIFF_TYPE = 0x04 ;
151152
152153 public static JBBPCompiledBlock compile (final String script ) throws IOException {
153154 return compile (script , null );
154155 }
155156
157+ private static void assertTokenNotArray (final String fieldType , final JBBPToken token ) {
158+ if (token .getArraySizeAsString () != null ) {
159+ final String fieldName = token .getFieldName () == null ? "<ANONYM>" : token .getFieldName ();
160+ throw new JBBPCompilationException ('\'' + fieldType + "' can't be array (" + fieldName + ')' , token );
161+ }
162+ }
163+
164+ private static void assertTokenNamed (final String fieldType , final JBBPToken token ) {
165+ if (token .getFieldName () == null ) {
166+ final String fieldName = token .getFieldName () == null ? "<ANONYM>" : token .getFieldName ();
167+ throw new JBBPCompilationException ('\'' + fieldType + "' must be named (" + fieldName + ')' , token );
168+ }
169+ }
170+
171+ private static void assertTokenNotNamed (final String fieldType , final JBBPToken token ) {
172+ if (token .getFieldName () != null ) {
173+ final String fieldName = token .getFieldName () == null ? "<ANONYM>" : token .getFieldName ();
174+ throw new JBBPCompilationException ('\'' + fieldType + "' must not be named (" + fieldName + ')' , token );
175+ }
176+ }
177+
178+ private static void assertTokenHasExtraData (final String fieldType , final JBBPToken token ) {
179+ if (token .getFieldTypeParameters ().getExtraData () == null ) {
180+ throw new JBBPCompilationException ('\'' + fieldType + "\' doesn't have extra value" , token );
181+ }
182+ }
183+
184+ private static void assertTokenDoesntHaveExtraData (final String fieldType , final JBBPToken token ) {
185+ if (token .getFieldTypeParameters ().getExtraData () != null ) {
186+ throw new JBBPCompilationException ('\'' + fieldType + "\' has extra value" , token );
187+ }
188+ }
189+
156190 /**
157191 * Compile a text script into its byte code representation/
158192 *
@@ -212,7 +246,7 @@ public static JBBPCompiledBlock compile(final String script, final JBBPCustomFie
212246 }
213247
214248 final boolean extraFieldNumericDataAsExpression = ((code >>> 8 ) & EXT_FLAG_EXTRA_AS_EXPRESSION ) != 0 ;
215- final boolean fieldIsFloatOrDouble = ((code >>> 8 ) & EXT_FLAG_EXTRA_AS_FLOAT_DOUBLE_OR_STRING ) != 0 ;
249+ final boolean fieldTypeDiff = ((code >>> 8 ) & EXT_FLAG_EXTRA_DIFF_TYPE ) != 0 ;
216250
217251 switch (code & 0xF ) {
218252 case CODE_BOOL :
@@ -249,11 +283,13 @@ public static JBBPCompiledBlock compile(final String script, final JBBPCustomFie
249283 }
250284 break ;
251285 case CODE_SKIP : {
252- if (token .getArraySizeAsString () != null ) {
253- throw new JBBPCompilationException ("'skip' can't be array" , token );
254- }
255- if (token .getFieldName () != null ) {
256- throw new JBBPCompilationException ("'skip' must not be named" , token );
286+ if (fieldTypeDiff ) {
287+ assertTokenNotArray ("val" , token );
288+ assertTokenNamed ("val" , token );
289+ assertTokenHasExtraData ("val" , token );
290+ } else {
291+ assertTokenNotArray ("skip" , token );
292+ assertTokenNotNamed ("skip" , token );
257293 }
258294 if (extraFieldNumericDataAsExpression ) {
259295 varLengthEvaluators .add (JBBPEvaluatorFactory .getInstance ().make (token .getFieldTypeParameters ().getExtraDataExpression (), namedFields , out .toByteArray ()));
@@ -265,21 +301,19 @@ public static JBBPCompiledBlock compile(final String script, final JBBPCustomFie
265301 } else {
266302 try {
267303 extraFieldNumberAsInt = Integer .parseInt (extraNumberAsStr );
268- assertNonNegativeValue (extraFieldNumberAsInt , token );
269- } catch (NumberFormatException ex ) {
304+ if (!fieldTypeDiff ) {
305+ assertNonNegativeValue (extraFieldNumberAsInt , token );
306+ }
307+ } catch (final NumberFormatException ex ) {
270308 extraFieldNumberAsInt = -1 ;
271309 }
272310 }
273311 }
274312 }
275313 break ;
276314 case CODE_ALIGN : {
277- if (token .getArraySizeAsString () != null ) {
278- throw new JBBPCompilationException ("'align' can't be array" , token );
279- }
280- if (token .getFieldName () != null ) {
281- throw new JBBPCompilationException ("'align' must not be named" , token );
282- }
315+ assertTokenNotArray ("align" , token );
316+ assertTokenNotNamed ("align" , token );
283317
284318 if (extraFieldNumericDataAsExpression ) {
285319 varLengthEvaluators .add (JBBPEvaluatorFactory .getInstance ().make (token .getFieldTypeParameters ().getExtraDataExpression (), namedFields , out .toByteArray ()));
@@ -344,15 +378,9 @@ public static JBBPCompiledBlock compile(final String script, final JBBPCustomFie
344378 }
345379 break ;
346380 case CODE_RESET_COUNTER : {
347- if (token .getArraySizeAsString () != null ) {
348- throw new JBBPCompilationException ("A Reset counter field can't be array" , token );
349- }
350- if (token .getFieldName () != null ) {
351- throw new JBBPCompilationException ("A Reset counter field can't be named [" + token .getFieldName () + ']' , token );
352- }
353- if (token .getFieldTypeParameters ().getExtraData () != null ) {
354- throw new JBBPCompilationException ("A Reset counter field doesn't use extra value [" + token .getFieldName () + ']' , token );
355- }
381+ assertTokenNotArray ("Reset counter" , token );
382+ assertTokenNotNamed ("Reset counter" , token );
383+ assertTokenDoesntHaveExtraData ("Reset counter" , token );
356384 }
357385 break ;
358386 case CODE_STRUCT_START : {
@@ -519,11 +547,11 @@ private static int prepareCodeForToken(final JBBPToken token, final JBBPCustomFi
519547
520548 result |= token .getArraySizeAsString () == null ? 0 : (token .isVarArrayLength () ? FLAG_ARRAY | FLAG_WIDE | (EXT_FLAG_EXPRESSION_OR_WHOLESTREAM << 8 ) : FLAG_ARRAY );
521549 result |= hasExpressionAsExtraNumber ? FLAG_WIDE | (EXT_FLAG_EXTRA_AS_EXPRESSION << 8 ) : 0 ;
522- result |= token .getFieldTypeParameters ().isFloatDoubleOrString () ? FLAG_WIDE | (EXT_FLAG_EXTRA_AS_FLOAT_DOUBLE_OR_STRING << 8 ) : 0 ;
550+ result |= token .getFieldTypeParameters ().isSpecialField () ? FLAG_WIDE | (EXT_FLAG_EXTRA_DIFF_TYPE << 8 ) : 0 ;
523551 result |= token .getFieldName () == null ? 0 : FLAG_NAMED ;
524552
525553 final String name = descriptor .getTypeName ().toLowerCase (Locale .ENGLISH );
526- if ("skip" .equals (name )) {
554+ if ("skip" .equals (name ) || "val" . equals ( name ) ) {
527555 result |= CODE_SKIP ;
528556 } else if ("align" .equals (name )) {
529557 result |= CODE_ALIGN ;
0 commit comments