2929/**
3030 * The Class implements the compiler of a bin source script represented as a
3131 * text value into byte codes.
32+ *
3233 * @since 1.0
3334 */
3435public final class JBBPCompiler {
@@ -55,11 +56,14 @@ private static final class StructStackItem {
5556 * Named field counter value for the structure start.
5657 */
5758 private final int namedFieldCounter ;
58-
59+
5960 /**
6061 * The Constructor.
61- * @param namedFieldCounter the named field counter value for the structure start
62- * @param startStructureOffset the offset of the start structure byte-code instruction
62+ *
63+ * @param namedFieldCounter the named field counter value for the structure
64+ * start
65+ * @param startStructureOffset the offset of the start structure byte-code
66+ * instruction
6367 * @param code the start byte code
6468 * @param token the token
6569 */
@@ -125,12 +129,14 @@ private StructStackItem(final int namedFieldCounter, final int startStructureOff
125129 public static final int CODE_SKIP = 0x0C ;
126130
127131 /**
128- * The Byte code of the VAR command. It describes a request to an external processor to load values from a stream.
132+ * The Byte code of the VAR command. It describes a request to an external
133+ * processor to load values from a stream.
129134 */
130135 public static final int CODE_VAR = 0x0D ;
131136
132137 /**
133- * The Byte code of the RESET COUNTER command. It resets the inside counter of the input stream.
138+ * The Byte code of the RESET COUNTER command. It resets the inside counter of
139+ * the input stream.
134140 */
135141 public static final int CODE_RESET_COUNTER = 0x0E ;
136142
@@ -181,7 +187,7 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
181187 int fieldUnrestrictedArrayOffset = -1 ;
182188
183189 boolean hasVarFields = false ;
184-
190+
185191 for (final JBBPToken token : parser ) {
186192 if (token .isComment ()) {
187193 continue ;
@@ -197,11 +203,9 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
197203 boolean extraFieldPresented = false ;
198204 int extraField = -1 ;
199205
200- if ((code & 0xF ) != CODE_STRUCT_END && fieldUnrestrictedArrayOffset >= 0 ) {
201- // check that the field is not in the current structure which is a whole stream one
202- if (structureStack .isEmpty () || structureStack .get (structureStack .size () - 1 ).startStructureOffset != fieldUnrestrictedArrayOffset ) {
203- throw new JBBPCompilationException ("Attempt to read field or structure after a full stream field" , token );
204- }
206+ // check that the field is not in the current structure which is a whole stream one
207+ if ((code & 0xF ) != CODE_STRUCT_END && fieldUnrestrictedArrayOffset >= 0 && (structureStack .isEmpty () || structureStack .get (structureStack .size () - 1 ).startStructureOffset != fieldUnrestrictedArrayOffset )) {
208+ throw new JBBPCompilationException ("Attempt to read field or structure after a full stream field" , token );
205209 }
206210
207211 switch (code & 0xF ) {
@@ -309,12 +313,13 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
309313 if (token .getFieldName () != null ) {
310314 throw new JBBPCompilationException ("A Reset counter field can't be named [" + token .getFieldName () + ']' , token );
311315 }
312- if (token .getFieldTypeParameters ().getExtraData ()!= null ){
316+ if (token .getFieldTypeParameters ().getExtraData () != null ) {
313317 throw new JBBPCompilationException ("A Reset counter field doesn't use extra value [" + token .getFieldName () + ']' , token );
314318 }
315- }break ;
319+ }
320+ break ;
316321 case CODE_STRUCT_START : {
317- structureStack .add (new StructStackItem (namedFields .size () + ((code & JBBPCompiler .FLAG_NAMED )== 0 ? 0 : 1 ), offset - 1 , code , token ));
322+ structureStack .add (new StructStackItem (namedFields .size () + ((code & JBBPCompiler .FLAG_NAMED ) == 0 ? 0 : 1 ), offset - 1 , code , token ));
318323 }
319324 break ;
320325 case CODE_STRUCT_END : {
@@ -361,7 +366,7 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
361366 if ((code & FLAG_NAMED ) != 0 ) {
362367 final String normalizedName = JBBPUtils .normalizeFieldNameOrPath (token .getFieldName ());
363368 assertName (normalizedName , token );
364- registerNamedField (normalizedName , structureStack .isEmpty () ? 0 : structureStack .get (structureStack .size ()- 1 ).namedFieldCounter , startFieldOffset , namedFields , token );
369+ registerNamedField (normalizedName , structureStack .isEmpty () ? 0 : structureStack .get (structureStack .size () - 1 ).namedFieldCounter , startFieldOffset , namedFields , token );
365370 }
366371 else {
367372 if (currentClosedStructure != null && (currentClosedStructure .code & FLAG_NAMED ) != 0 ) {
@@ -399,16 +404,17 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
399404
400405 /**
401406 * The Method checks a value for negative.
407+ *
402408 * @param value a value to be checked
403409 * @param token the tokens related to the value
404410 * @throws JBBPCompilationException if the value is a negative one
405411 */
406- private static void assertNonNegativeValue (final int value , final JBBPToken token ){
407- if (value < 0 ) {
412+ private static void assertNonNegativeValue (final int value , final JBBPToken token ) {
413+ if (value < 0 ) {
408414 throw new JBBPCompilationException ("Detected unsupported negative value for a field must have only zero or a positive one" , token );
409415 }
410416 }
411-
417+
412418 /**
413419 * The Method check that a field name supports business rules.
414420 *
@@ -433,9 +439,9 @@ private static void assertName(final String name, final JBBPToken token) {
433439 * the path
434440 */
435441 private static void registerNamedField (final String normalizedName , final int structureBorder , final int offset , final List <JBBPNamedFieldInfo > namedFields , final JBBPToken token ) {
436- for (int i = namedFields .size ()- 1 ; i >= structureBorder ;i --){
442+ for (int i = namedFields .size () - 1 ; i >= structureBorder ; i --) {
437443 final JBBPNamedFieldInfo info = namedFields .get (i );
438- if (info .getFieldPath ().equals (normalizedName )){
444+ if (info .getFieldPath ().equals (normalizedName )) {
439445 throw new JBBPCompilationException ("Duplicated named field detected [" + normalizedName + ']' , token );
440446 }
441447 }
0 commit comments