@@ -39,7 +39,8 @@ public class SketchParser {
3939
4040 List <List <Range >> scientificNotations ;
4141
42- Range setupFunction ;
42+ // currently is used to ignore numbers in 'setup' and 'settings' functions
43+ List <Range > ignoreFunctions ;
4344
4445 List <List <Range >> commentBlocks ;
4546 List <int []> curlyScopes ;
@@ -57,8 +58,12 @@ public SketchParser(String[] codeTabs, boolean requiresComment) {
5758 commentBlocks .add (getCommentBlocks (code ));
5859 }
5960
60- // get setup function range (to ignore all numbers there)
61- setupFunction = new Range (getSetupStart (codeTabs [0 ]), getSetupEnd (codeTabs [0 ]));
61+ // add 'settings' and 'setup' to ignore list (to ignore all numbers there)
62+ ignoreFunctions = new ArrayList <Range >();
63+ ignoreFunctions .add (new Range (getVoidFunctionStart (codeTabs [0 ], "settings" ),
64+ getVoidFunctionEnd (codeTabs [0 ], "settings" )));
65+ ignoreFunctions .add (new Range (getVoidFunctionStart (codeTabs [0 ], "setup" ),
66+ getVoidFunctionEnd (codeTabs [0 ], "setup" )));
6267
6368 // build curly scope for every character in the code
6469 curlyScopes = new ArrayList <>();
@@ -125,8 +130,8 @@ private void addAllDecimalNumbers() {
125130 continue ;
126131 }
127132
128- if (setupFunction . contains (start )) {
129- // ignore numbers in setup
133+ if (isInRangeList (start , ignoreFunctions )) {
134+ // ignore numbers in predefined functions
130135 continue ;
131136 }
132137
@@ -217,8 +222,8 @@ private void addAllHexNumbers() {
217222 continue ;
218223 }
219224
220- if (setupFunction . contains (start )) {
221- // ignore number in setup
225+ if (isInRangeList (start , ignoreFunctions )) {
226+ // ignore numbers in predefined functions
222227 continue ;
223228 }
224229
@@ -278,8 +283,8 @@ private void addAllWebColorNumbers() {
278283 continue ;
279284 }
280285
281- if (setupFunction . contains (start )) {
282- // ignore number in setup
286+ if (isInRangeList (start , ignoreFunctions )) {
287+ // ignore numbers in predefined functions
283288 continue ;
284289 }
285290
@@ -382,8 +387,8 @@ private void createColorBoxes() {
382387 continue ;
383388 }
384389
385- if (setupFunction . contains (m .start ())) {
386- // ignore number in setup
390+ if (isInRangeList (m .start (), ignoreFunctions )) {
391+ // ignore numbers in predefined functions
387392 continue ;
388393 }
389394
@@ -466,8 +471,8 @@ private void createColorBoxesForLights() {
466471 continue ;
467472 }
468473
469- if (setupFunction . contains (m .start ())) {
470- // ignore number in setup
474+ if (isInRangeList (m .start (), ignoreFunctions )) {
475+ // ignore numbers in predefined functions
471476 continue ;
472477 }
473478
@@ -827,6 +832,50 @@ else if (readObject) {
827832 return obj ;
828833 }
829834
835+ static public int getVoidFunctionStart (String code , String functionName ) {
836+ Pattern p = Pattern .compile ("void[\\ s\\ t\\ r\\ n]*" +functionName +"[\\ s\\ t]*\\ (\\ )[\\ s\\ t\\ r\\ n]*\\ {" );
837+ Matcher m = p .matcher (code );
838+
839+ if (m .find ()) {
840+ return m .end ();
841+ }
842+
843+ return -1 ;
844+ }
845+
846+ static public int getVoidFunctionEnd (String code , String functionName ) {
847+ List <Range > comments = getCommentBlocks (code );
848+
849+ int start = getVoidFunctionStart (code , functionName );
850+ if (start == -1 ) {
851+ return -1 ;
852+ }
853+
854+ // count brackets to look for setup end
855+ int bracketCount =1 ;
856+ int pos = start ;
857+ while (bracketCount > 0 && pos < code .length ()) {
858+ if (isInRangeList (pos , comments )) {
859+ // in a comment, ignore and move on
860+ pos ++;
861+ continue ;
862+ }
863+
864+ if (code .charAt (pos ) == '{' ) {
865+ bracketCount ++;
866+ }
867+ else if (code .charAt (pos ) == '}' ) {
868+ bracketCount --;
869+ }
870+ pos ++;
871+ }
872+
873+ if (bracketCount == 0 ) {
874+ return pos -1 ;
875+ }
876+ return -1 ;
877+ }
878+
830879
831880 static public int getSetupStart (String code ) {
832881 Pattern p = Pattern .compile ("void[\\ s\\ t\\ r\\ n]*setup[\\ s\\ t]*\\ (\\ )[\\ s\\ t\\ r\\ n]*\\ {" );
@@ -839,7 +888,6 @@ static public int getSetupStart(String code) {
839888 return -1 ;
840889 }
841890
842-
843891 static public int getSetupEnd (String code ) {
844892 List <Range > comments = getCommentBlocks (code );
845893
@@ -873,6 +921,67 @@ else if (code.charAt(pos) == '}') {
873921 return -1 ;
874922 }
875923
924+ static public int getAfterSizePos (String code ) {
925+ List <Range > comments = getCommentBlocks (code );
926+
927+ // find the size function
928+ Pattern p = Pattern .compile ("size[\\ s\\ t]*\\ (" );
929+ Matcher m = p .matcher (code );
930+
931+ while (m .find ()) {
932+ if (isInRangeList (m .start (), comments ) ||
933+ isInRangeList (m .end (), comments )) {
934+ // this is a comment, next
935+ continue ;
936+ }
937+
938+ // count brackets to look for size call end
939+ int bracketCount =1 ;
940+ int pos = m .end ();
941+ while (bracketCount > 0 && pos < code .length ()) {
942+ if (isInRangeList (pos , comments )) {
943+ // in a comment, ignore and move on
944+ pos ++;
945+ continue ;
946+ }
947+
948+ if (code .charAt (pos ) == '(' ) {
949+ bracketCount ++;
950+ }
951+ else if (code .charAt (pos ) == ')' ) {
952+ bracketCount --;
953+ }
954+ pos ++;
955+ }
956+
957+ if (bracketCount != 0 ) {
958+ // could not find closing ')'. next
959+ continue ;
960+ }
961+
962+ // find ';' sign
963+ boolean found = false ;
964+ while (pos < code .length ()) {
965+ if (code .charAt (pos ) == ';' &&
966+ !isInRangeList (pos , comments )) {
967+ found = true ;
968+ break ;
969+ }
970+ pos ++;
971+ }
972+
973+ if (!found ) {
974+ // didn't find the ';'. next
975+ continue ;
976+ }
977+
978+ // success! we found the place
979+ return pos +1 ;
980+ }
981+
982+ // nothing was found
983+ return -1 ;
984+ }
876985
877986 static class Range {
878987 int start ;
0 commit comments