Kaynağa Gözat

Skip whitespace characters outside of string, key

Michael Dresser 4 yıl önce
ebeveyn
işleme
6780f9e926

+ 3 - 1
pkg/util/allocationfilterutil/v2/lexer.go

@@ -178,12 +178,14 @@ func (s *scanner) scanToken() {
 			s.errors = append(s.errors, fmt.Errorf("Position %d: Unexpected '!'", s.nextByte-1))
 			s.errors = append(s.errors, fmt.Errorf("Position %d: Unexpected '!'", s.nextByte-1))
 		}
 		}
 	// strings
 	// strings
-	// TODO: reject whitespace chars
 	case '"':
 	case '"':
 		s.string()
 		s.string()
 	// keyed access
 	// keyed access
 	case '[':
 	case '[':
 		s.keyedAccess()
 		s.keyedAccess()
+	// Ignore whitespace chars outside of "" and [].
+	case ' ', '\t', '\n', '\r':
+		break
 	default:
 	default:
 		// identifiers
 		// identifiers
 		// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/
 		// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/

+ 12 - 1
pkg/util/allocationfilterutil/v2/lexer_test.go

@@ -63,7 +63,18 @@ func TestLexer(t *testing.T) {
 			input:    "app[kubecost]",
 			input:    "app[kubecost]",
 			expected: []token{{kind: identifier, s: "app"}, {kind: keyedAccess, s: "kubecost"}, {kind: eof}},
 			expected: []token{{kind: identifier, s: "app"}, {kind: keyedAccess, s: "kubecost"}, {kind: eof}},
 		},
 		},
-		// TODO: more cases
+		{
+			name:  "whitespace variety",
+			input: "1 2" + string('\n') + `" ` + string('\n') + string('\t') + string('\r') + `a"` + string('\t') + string('\r') + "abc[foo a]" + " ",
+			expected: []token{
+				{kind: identifier, s: "1"},
+				{kind: identifier, s: "2"},
+				{kind: str, s: " " + string('\n') + string('\t') + string('\r') + "a"},
+				{kind: identifier, s: "abc"},
+				{kind: keyedAccess, s: "foo a"},
+				{kind: eof},
+			},
+		},
 	}
 	}
 
 
 	for _, c := range cases {
 	for _, c := range cases {