Skip to content

Commit f50ff24

Browse files
committed
Make lexical scanner state into enum
1 parent 095529b commit f50ff24

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

core/src/processing/core/PShapeSVG.java

+28-17
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,14 @@ protected void parsePath() {
515515

516516
StringBuilder pathBuffer = new StringBuilder();
517517

518+
enum LexState {
519+
AFTER_CMD,// Just after a command (i.e. a single alphabet)
520+
NEUTRAL, // Neutral state, waiting for a number expression or a command
521+
INTEGER, // On a sequence of digits possibly led by the '-' sign
522+
DECIMAL, // On a digit sequence following the decimal point '.'
523+
EXP_HEAD, // On the head of the exponent part of a scientific notation; the '-' sign or a digit
524+
EXP_TAIL, // On the integer expression in the exponent part
525+
}
518526
/*
519527
* The state of the lexer:
520528
* -1: just after the command (i.e. a single alphabet)
@@ -524,66 +532,69 @@ protected void parsePath() {
524532
* 3: on a digit or a sign in exponent in scientific notation, e.g. 3.14e-2)
525533
* 4: on a digit sequence in exponent
526534
*/
527-
int lexState = 0;
535+
LexState lexState = LexState.NEUTRAL;
528536

529537
for (int i = 0; i < pathDataChars.length; i++) {
530538
char c = pathDataChars[i];
531539

532540
// Put a separator after a command.
533-
if (lexState == -1) {
541+
if (lexState == LexState.AFTER_CMD) {
534542
pathBuffer.append("|");
535-
lexState = 0;
543+
lexState = LexState.NEUTRAL;
536544
}
537545

538546
if (c >= '0' && c <= '9') {
539-
if (lexState == 0 || lexState == 3) {
540-
// If it is a head of a number representation, enter the 'inside' of the digit sequence.
541-
++lexState;
547+
// If it is a head of a number representation, enter the 'inside' of the digit sequence.
548+
if (lexState == LexState.NEUTRAL) {
549+
lexState = LexState.INTEGER;
550+
}
551+
else if (lexState == LexState.EXP_HEAD) {
552+
lexState = LexState.EXP_TAIL;
542553
}
543554
pathBuffer.append(c);
544555
continue;
545556
}
546557

547558
if (c == '-') {
548-
if (lexState == 0) {
559+
if (lexState == LexState.NEUTRAL) {
549560
// In neutral state, enter 'digit sequence'.
550-
lexState = 1;
561+
lexState = LexState.INTEGER;
551562
}
552-
else if (lexState == 3) {
563+
else if (lexState == LexState.EXP_HEAD) {
553564
// In the begining of an exponent, enter 'exponent digit sequence'.
554-
lexState = 4;
565+
lexState = LexState.EXP_TAIL;
555566
}
556567
else {
557568
// Otherwise, begin a new number representation.
558569
pathBuffer.append("|");
559-
lexState = 1;
570+
lexState = LexState.INTEGER;
560571
}
561572
pathBuffer.append("-");
562573
continue;
563574
}
564575

565576
if (c == '.') {
566-
if (lexState >= 2) {
577+
if (lexState == LexState.DECIMAL || lexState == LexState.EXP_HEAD || lexState == LexState.EXP_TAIL) {
567578
// Begin a new decimal number unless it is in a neutral state or after a digit sequence
568579
pathBuffer.append("|");
569580
}
570581
pathBuffer.append(".");
571-
lexState = 2;
582+
lexState = LexState.DECIMAL;
572583
continue;
573584
}
574585

575586
if (c == 'e' || c == 'E') {
576587
// Found 'e' or 'E', enter the 'exponent' state immediately.
577588
pathBuffer.append("e");
578-
lexState = 3;
589+
lexState = LexState.EXP_HEAD;
579590
continue;
580591
}
581592

582593
// The following are executed for non-numeral elements
583594

584-
if (lexState != 0) {
595+
if (lexState != LexState.NEUTRAL) {
585596
pathBuffer.append("|");
586-
lexState = 0;
597+
lexState = LexState.NEUTRAL;
587598
}
588599

589600
if (c != ',') {
@@ -592,7 +603,7 @@ else if (lexState == 3) {
592603

593604
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
594605
// Every alphabet character except for 'e' and 'E' are considered as a command.
595-
lexState = -1;
606+
lexState = LexState.AFTER_CMD;
596607
}
597608
}
598609

0 commit comments

Comments
 (0)