Kaynağa Gözat

Implement #TG6

Alois Mahdal 3 gün önce
ebeveyn
işleme
3c676f9864
2 değiştirilmiş dosya ile 26 ekleme ve 0 silme
  1. 16
    0
      app/tokenize/tokenize.go
  2. 10
    0
      app/tokenize/tokenize_test.go

+ 16
- 0
app/tokenize/tokenize.go Dosyayı Görüntüle

@@ -126,6 +126,8 @@ func (self tokenizeResultCode) String() string {
126 126
 		return "tokenizeResultCodeOk"
127 127
 	case tokenizeResultCodeMissingEndSingleQuote:
128 128
 		return "tokenizeResultCodeMissingEndSingleQuote"
129
+	case tokenizeResultCodeMissingEndDoubleQuote:
130
+		return "tokenizeResultCodeMissingEndDoubleQuote"
129 131
 	default:
130 132
 		return fmt.Sprintf("unknown!tokenizeResultCode(%d)", self)
131 133
 	}
@@ -134,6 +136,7 @@ func (self tokenizeResultCode) String() string {
134 136
 const (
135 137
 	tokenizeResultCodeOk tokenizeResultCode = iota
136 138
 	tokenizeResultCodeMissingEndSingleQuote
139
+	tokenizeResultCodeMissingEndDoubleQuote
137 140
 )
138 141
 
139 142
 func tokenize(str string) tokenizeResult {
@@ -162,6 +165,14 @@ func tokenize(str string) tokenizeResult {
162 165
 			}
163 166
 			b.bufAppend(new_chars)
164 167
 			rdr.tossChar() // the second `'`
168
+		case '"':
169
+			rdr.tossChar() // first `"`
170
+			new_chars, found := rdr.takeUntil("\"")
171
+			if !found {
172
+				return tokenizeResult{code: tokenizeResultCodeMissingEndDoubleQuote, err_loc: rdr.pos - 1}
173
+			}
174
+			b.bufAppend(new_chars)
175
+			rdr.tossChar() // the second `"`
165 176
 		default:
166 177
 			b.bufAppendChar(this_byte)
167 178
 			rdr.tossChar()
@@ -178,6 +189,8 @@ func Tokenize(str string) ([]string, error) {
178 189
 		return res.tokens, nil
179 190
 	case tokenizeResultCodeMissingEndSingleQuote:
180 191
 		return nil, TokenizeError{code: TokenizeErrorCodeMissingEndSingleQuote, loc: res.err_loc}
192
+	case tokenizeResultCodeMissingEndDoubleQuote:
193
+		return nil, TokenizeError{code: TokenizeErrorCodeMissingEndDoubleQuote, loc: res.err_loc}
181 194
 	default:
182 195
 		return nil, TokenizeError{code: TokenizeErrorCodeGeneral, loc: res.err_loc}
183 196
 	}
@@ -192,12 +205,15 @@ type TokenizeErrorCode uint8
192 205
 const (
193 206
 	TokenizeErrorCodeGeneral TokenizeErrorCode = iota
194 207
 	TokenizeErrorCodeMissingEndSingleQuote
208
+	TokenizeErrorCodeMissingEndDoubleQuote
195 209
 )
196 210
 
197 211
 func (e TokenizeError) Error() string {
198 212
 	switch e.code {
199 213
 	case TokenizeErrorCodeMissingEndSingleQuote:
200 214
 		return fmt.Sprintf("unterminated single-quote: at %d", e.loc)
215
+	case TokenizeErrorCodeMissingEndDoubleQuote:
216
+		return fmt.Sprintf("unterminated double-quote: at %d", e.loc)
201 217
 	default:
202 218
 		return fmt.Sprintf("unknown TokenizeError code: .code=%d .loc=%d", e.code, e.loc)
203 219
 	}

+ 10
- 0
app/tokenize/tokenize_test.go Dosyayı Görüntüle

@@ -33,6 +33,16 @@ func Test_tokenize(t *testing.T) {
33 33
 		{"foo ' space bar '", tokenizeResult{tokens: []string{"foo", " space bar "}}},
34 34
 		{"foo 'John \"Spaceman\" Doe'", tokenizeResult{tokens: []string{"foo", "John \"Spaceman\" Doe"}}},
35 35
 
36
+		// double quotes
37
+		{"\"", tokenizeResult{tokens: []string{}, code: tokenizeResultCodeMissingEndDoubleQuote}},
38
+		{"\"\"", tokenizeResult{tokens: []string{""}}},
39
+		{"fo\"\"o", tokenizeResult{tokens: []string{"foo"}}},
40
+		{"foo \"\" bar", tokenizeResult{tokens: []string{"foo", "", "bar"}}},
41
+		{"foo \"and\" bar", tokenizeResult{tokens: []string{"foo", "and", "bar"}}},
42
+		{"foo \"\\\t\n\" bar", tokenizeResult{tokens: []string{"foo", "\\\t\n", "bar"}}},
43
+		{"foo \" space bar \"", tokenizeResult{tokens: []string{"foo", " space bar "}}},
44
+		{"foo \"Joe's lunch\"", tokenizeResult{tokens: []string{"foo", "Joe's lunch"}}},
45
+
36 46
 		//
37 47
 	}
38 48
 	for _, tc := range test_cases {