2323import com .igormaznitsa .jbbp .model .JBBPFieldFloat ;
2424import com .igormaznitsa .jbbp .model .JBBPFieldString ;
2525import com .igormaznitsa .jbbp .utils .JBBPUtils ;
26-
2726import java .util .HashSet ;
2827import java .util .Iterator ;
2928import java .util .NoSuchElementException ;
@@ -44,11 +43,14 @@ public final class JBBPTokenizer implements Iterable<JBBPToken>, Iterator<JBBPTo
4443 * The Pattern to break a string to tokens.
4544 * <b>WARNING! DO NOT UNESCAPE '}' AND '{' CHARS BECAUSE IT MAKES INCOMPATIBILITY WITH ANDROID!</b>
4645 */
47- private static final Pattern PATTERN = Pattern .compile ("\\ s*//.*$|\\ s*(\\ })|\\ s*([^\\ s;\\ [\\ ]\\ }\\ {]+)?\\ s*(?:\\ [\\ s*([^\\ [\\ ]\\ {\\ };]+)\\ s*\\ ])?\\ s*([^\\ d\\ s;\\ [\\ ]\\ }\\ {/][^\\ s;\\ [\\ ]\\ }\\ {/]*)?\\ s*([\\ {;])" , Pattern .MULTILINE );
46+ private static final Pattern PATTERN = Pattern .compile (
47+ "\\ s*//.*$|\\ s*(\\ })|\\ s*([^\\ s;\\ [\\ ]\\ }\\ {]+)?\\ s*(?:\\ [\\ s*([^\\ [\\ ]\\ {\\ };]+)\\ s*\\ ])?\\ s*([^\\ d\\ s;\\ [\\ ]\\ }\\ {/][^\\ s;\\ [\\ ]\\ }\\ {/]*)?\\ s*([\\ {;])" ,
48+ Pattern .MULTILINE );
4849 /**
4950 * The Pattern to break field type to parameters.
5051 */
51- private static final Pattern FIELD_TYPE_BREAK_PATTERN = Pattern .compile ("^([<>])?([\\ w][\\ w$]*)(?::((?:[-]?\\ d+)|(?:\\ (.+\\ ))))?$" );
52+ private static final Pattern FIELD_TYPE_BREAK_PATTERN =
53+ Pattern .compile ("^([<>])?([\\ w][\\ w$]*)(?::((?:[-]?\\ d+)|(?:\\ (.+\\ ))))?$" );
5254 /**
5355 * Inside table to keep disabled names for fields.
5456 */
@@ -102,7 +104,8 @@ public JBBPTokenizer(final String str) {
102104 * @param str a string to be parsed, must not be null.
103105 * @param customFieldTypeProcessor custom field type processor, it can be null
104106 */
105- public JBBPTokenizer (final String str , final JBBPCustomFieldTypeProcessor customFieldTypeProcessor ) {
107+ public JBBPTokenizer (final String str ,
108+ final JBBPCustomFieldTypeProcessor customFieldTypeProcessor ) {
106109 JBBPUtils .assertNotNull (str , "String must not be null" );
107110
108111 if (customFieldTypeProcessor == null ) {
@@ -145,9 +148,12 @@ private void readNextItem() {
145148 final String groupName = this .matcher .group (4 );
146149 final String groupEnder = this .matcher .group (5 );
147150
148- final String skipString = this .processingString .substring (Math .max (this .lastCharSubstringFound , 0 ), matcher .start ()).trim ();
151+ final String skipString =
152+ this .processingString .substring (Math .max (this .lastCharSubstringFound , 0 ), matcher .start ())
153+ .trim ();
149154 if (skipString .length () != 0 && !skipString .startsWith ("//" )) {
150- this .detectedException = new JBBPTokenizerException (skipString , Math .max (this .lastCharSubstringFound , 0 ));
155+ this .detectedException =
156+ new JBBPTokenizerException (skipString , this .processingString , Math .max (this .lastCharSubstringFound , 0 ));
151157 } else {
152158 JBBPTokenType type = JBBPTokenType .ATOM ;
153159
@@ -157,15 +163,21 @@ private void readNextItem() {
157163 // {
158164 type = JBBPTokenType .STRUCT_START ;
159165 if (groupName != null ) {
160- final int position = matcher .start () + groupWholeFound .length () - groupWholeFoundTrimmed .length ();
161- this .detectedException = new JBBPTokenizerException ("Wrong structure format, it must have only name (and may be array definition)" , position );
166+ final int position =
167+ matcher .start () + groupWholeFound .length () - groupWholeFoundTrimmed .length ();
168+ this .detectedException = new JBBPTokenizerException (
169+ "Wrong structure format, it must have only name (and may be array definition)" ,
170+ this .processingString ,
171+ position );
162172 return ;
163173 }
164174 } else if (groupCloseStruct != null ) {
165175 type = JBBPTokenType .STRUCT_END ;
166176 } else if (groupTypeOrName == null ) {
167- final int position = matcher .start () + groupWholeFound .length () - groupWholeFoundTrimmed .length ();
168- this .detectedException = new JBBPTokenizerException ("Detected atomic field definition without type" , position );
177+ final int position =
178+ matcher .start () + groupWholeFound .length () - groupWholeFoundTrimmed .length ();
179+ this .detectedException =
180+ new JBBPTokenizerException ("Detected atomic field definition without type" , this .processingString , position );
169181 return ;
170182 }
171183
@@ -213,17 +225,23 @@ private void readNextItem() {
213225 } else if ("<" .equals (groupTypeByteOrder )) {
214226 byteOrder = JBBPByteOrder .LITTLE_ENDIAN ;
215227 } else {
216- throw new Error ("Illegal byte order char, unexpected error, contact developer please [" + fieldType + ']' );
228+ throw new Error (
229+ "Illegal byte order char, unexpected error, contact developer please ["
230+ + fieldType + ']' );
217231 }
218232 } else {
219233 byteOrder = JBBPByteOrder .BIG_ENDIAN ;
220234 }
221235
222- parsedType = new JBBPFieldTypeParameterContainer (byteOrder , groupTypeName , groupTypeExtraField );
236+ parsedType =
237+ new JBBPFieldTypeParameterContainer (byteOrder , groupTypeName , groupTypeExtraField );
223238 }
224239
225240 if (wrongFormat ) {
226- this .detectedException = new JBBPTokenizerException ("Wrong format of type definition [" + fieldType + ']' , position );
241+ this .detectedException =
242+ new JBBPTokenizerException ("Wrong format of type definition [" + fieldType + ']' ,
243+ this .processingString ,
244+ position );
227245 return ;
228246 }
229247 }
@@ -233,11 +251,14 @@ private void readNextItem() {
233251 }
234252 } else {
235253 if (this .lastCharSubstringFound < 0 ) {
236- this .detectedException = new JBBPTokenizerException ("Wrong format of whole string" , 0 );
254+ this .detectedException = new JBBPTokenizerException ("Wrong format of whole string" , this . processingString , 0 );
237255 } else {
238256 final String restOfString = this .processingString .substring (this .lastCharSubstringFound );
239257 if (restOfString .trim ().length () != 0 ) {
240- throw new JBBPTokenizerException ("Can't recognize a part of script [" + restOfString + ']' , this .lastCharSubstringFound );
258+ throw new JBBPTokenizerException (
259+ "Can't recognize a part of script [" + restOfString + ']' ,
260+ this .processingString ,
261+ this .lastCharSubstringFound );
241262 }
242263 }
243264 this .nextItem = null ;
@@ -256,7 +277,7 @@ private JBBPTokenizerException checkFieldName(final String name, final int posit
256277 if (name != null ) {
257278 final String normalized = JBBPUtils .normalizeFieldNameOrPath (name );
258279 if (normalized .indexOf ('.' ) >= 0 ) {
259- return new JBBPTokenizerException ("Field name must not contain '.' char" , position );
280+ return new JBBPTokenizerException ("Field name must not contain '.' char" , this . processingString , position );
260281 }
261282
262283 if (normalized .length () > 0 ) {
@@ -265,13 +286,13 @@ private JBBPTokenizerException checkFieldName(final String name, final int posit
265286 || normalized .startsWith ("$" )
266287 || Character .isDigit (normalized .charAt (0 ))
267288 ) {
268- return new JBBPTokenizerException ("'" + name + "' can't be field name" , position );
289+ return new JBBPTokenizerException ("'" + name + "' can't be field name" , this . processingString , position );
269290 }
270291
271292 for (int i = 1 ; i < normalized .length (); i ++) {
272293 final char chr = normalized .charAt (i );
273294 if (chr != '_' && !Character .isLetterOrDigit (chr )) {
274- return new JBBPTokenizerException ("Char '" + chr + "' not allowed in name" , position );
295+ return new JBBPTokenizerException ("Char '" + chr + "' not allowed in name" , this . processingString , position );
275296 }
276297 }
277298 }
0 commit comments