@@ -38,17 +38,22 @@ public class SketchParser {
3838 ArrayList <ColorMode > colorModes ;
3939
4040 List <List <Range >> scientificNotations ;
41-
42-
41+
42+ Range setupFunction ;
43+
4344 public SketchParser (String [] codeTabs , boolean requiresComment ) {
4445 this .codeTabs = codeTabs ;
4546 this .requiresComment = requiresComment ;
4647 intVarCount =0 ;
4748 floatVarCount =0 ;
48-
49+
50+ // get setup function range (to ignore all numbers there)
51+ setupFunction = new Range (getSetupStart (codeTabs [0 ]), getSetupEnd (codeTabs [0 ]));
52+
53+ // get all scientific notation (to ignore them)
4954 scientificNotations = getAllScientificNotations ();
5055
51- // find, add, and sort all tweakable numbers in the sketch
56+ // find, add, and sort all tweak-able numbers in the sketch
5257 addAllNumbers ();
5358
5459 // handle colors
@@ -65,11 +70,11 @@ public SketchParser(String[] codeTabs, boolean requiresComment) {
6570
6671
6772 public void addAllNumbers () {
68- //allHandles = new ArrayList[codeTabs.length]; // moved inside addAllDecimalNumbers
73+ allHandles = new ArrayList <>();
74+
6975 addAllDecimalNumbers ();
7076 addAllHexNumbers ();
7177 addAllWebColorNumbers ();
72- //for (int i=0; i<codeTabs.length; i++) {
7378 for (List <Handle > handle : allHandles ) {
7479 //Collections.sort(allHandles[i], new HandleComparator());
7580 Collections .sort (handle , new HandleComparator ());
@@ -83,14 +88,11 @@ public void addAllNumbers() {
8388 * list of all numbers in the sketch (excluding hexadecimals)
8489 */
8590 private void addAllDecimalNumbers () {
86- allHandles = new ArrayList <>();
87-
8891 // for every number found:
8992 // save its type (int/float), name, value and position in code.
9093
9194 Pattern p = Pattern .compile ("[\\ [\\ {<>(),\\ t\\ s\\ +\\ -\\ /\\ *^%!|&=?:~]\\ d+\\ .?\\ d*" );
9295 for (int i = 0 ; i < codeTabs .length ; i ++) {
93- //allHandles[i] = new ArrayList<Handle>();
9496 List <Handle > handles = new ArrayList <Handle >();
9597 allHandles .add (handles );
9698
@@ -106,6 +108,11 @@ private void addAllDecimalNumbers() {
106108 // ignore comments
107109 continue ;
108110 }
111+
112+ if (setupFunction .contains (start )) {
113+ // ignore numbers in setup
114+ continue ;
115+ }
109116
110117 if (requiresComment ) {
111118 // only add numbers that have the "// tweak" comment in their line
@@ -193,6 +200,11 @@ private void addAllHexNumbers() {
193200 // ignore comments
194201 continue ;
195202 }
203+
204+ if (setupFunction .contains (start )) {
205+ // ignore number in setup
206+ continue ;
207+ }
196208
197209 if (requiresComment ) {
198210 // only add numbers that have the "// tweak" comment in their line
@@ -249,6 +261,11 @@ private void addAllWebColorNumbers() {
249261 // ignore comments
250262 continue ;
251263 }
264+
265+ if (setupFunction .contains (start )) {
266+ // ignore number in setup
267+ continue ;
268+ }
252269
253270 if (requiresComment ) {
254271 // only add numbers that have the "// tweak" comment in their line
@@ -326,7 +343,6 @@ private void createColorBoxes() {
326343 Pattern p = Pattern .compile ("color\\ (|color\\ s\\ (|fill[\\ (\\ s]|stroke[\\ (\\ s]|background[\\ (\\ s]|tint[\\ (\\ s]" );
327344
328345 for (int i = 0 ; i < codeTabs .length ; i ++) {
329- //colorBoxes[i] = new ArrayList<ColorControlBox>();
330346 List <ColorControlBox > colorBox = new ArrayList <ColorControlBox >();
331347 colorBoxes .add (colorBox );
332348
@@ -348,6 +364,11 @@ private void createColorBoxes() {
348364 // ignore colors in a comment
349365 continue ;
350366 }
367+
368+ if (setupFunction .contains (m .start ())) {
369+ // ignore number in setup
370+ continue ;
371+ }
351372
352373 // look for handles inside the parenthesis
353374 for (Handle handle : allHandles .get (i )) {
@@ -360,7 +381,7 @@ private void createColorBoxes() {
360381
361382 if (colorHandles .size () > 0 ) {
362383 /* make sure there is no other stuff between '()' like variables.
363- * substract all handle values from string inside parenthesis and
384+ * subtract all handle values from string inside parenthesis and
364385 * check there is no garbage left
365386 */
366387 String insidePar = tab .substring (openPar +1 , closePar );
@@ -427,6 +448,11 @@ private void createColorBoxesForLights() {
427448 // ignore colors in a comment
428449 continue ;
429450 }
451+
452+ if (setupFunction .contains (m .start ())) {
453+ // ignore number in setup
454+ continue ;
455+ }
430456
431457 // put 'colorParamsEnd' after three parameters inside the parenthesis or at the close
432458 int colorParamsEnd = openPar ;
@@ -450,7 +476,7 @@ private void createColorBoxesForLights() {
450476
451477 if (colorHandles .size () > 0 ) {
452478 /* make sure there is no other stuff between '()' like variables.
453- * substract all handle values from string inside parenthesis and
479+ * subtract all handle values from string inside parenthesis and
454480 * check there is no garbage left
455481 */
456482 String insidePar = tab .substring (openPar +1 , colorParamsEnd );
@@ -546,14 +572,10 @@ private List<List<Range>> getAllScientificNotations() {
546572 List <List <Range >> notations = new ArrayList <>();
547573
548574 Pattern p = Pattern .compile ("[+\\ -]?(?:0|[1-9]\\ d*)(?:\\ .\\ d*)?[eE][+\\ -]?\\ d+" );
549- //for (int i = 0; i < codeTabs.length; i++) {
550575 for (String code : codeTabs ) {
551576 List <Range > notation = new ArrayList <Range >();
552- //notations[i] = new ArrayList<Range>();
553- //Matcher m = p.matcher(codeTabs[i]);
554577 Matcher m = p .matcher (code );
555578 while (m .find ()) {
556- //notations[i].add(new Range(m.start(), m.end()));
557579 notation .add (new Range (m .start (), m .end ()));
558580 }
559581 notations .add (notation );
@@ -772,11 +794,37 @@ static public int getSetupStart(String code) {
772794
773795 return -1 ;
774796 }
775-
776-
777- // private String replaceString(String str, int start, int end, String put) {
778- // return str.substring(0, start) + put + str.substring(end, str.length());
779- // }
797+
798+ static public int getSetupEnd (String code ) {
799+ int setupStart = getSetupStart (code );
800+ if (setupStart == -1 ) {
801+ return -1 ;
802+ }
803+
804+ // count brackets to look for setup end
805+ int bracketCount =1 ;
806+ int pos = setupStart ;
807+ while (bracketCount >0 && pos <code .length ()) {
808+ if (code .charAt (pos ) == '{' ) {
809+ if (!isInComment (pos , code )) {
810+ bracketCount ++;
811+ }
812+ }
813+ else if (code .charAt (pos ) == '}' ) {
814+ if (!isInComment (pos , code )) {
815+ bracketCount --;
816+ }
817+ }
818+
819+ pos ++;
820+ }
821+
822+ if (bracketCount == 0 ) {
823+ return pos -1 ;
824+ }
825+
826+ return -1 ;
827+ }
780828
781829
782830 static class Range {
0 commit comments