OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Parse nodes. | 5 // Parse nodes. |
6 | 6 |
7 package parse | 7 package parse |
8 | 8 |
9 import ( | 9 import ( |
10 "bytes" | 10 "bytes" |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 // fmt.Sscan can parse the pair, so let it do the work. | 260 // fmt.Sscan can parse the pair, so let it do the work. |
261 if _, err := fmt.Sscan(text, &n.Complex128); err != nil { | 261 if _, err := fmt.Sscan(text, &n.Complex128); err != nil { |
262 return nil, err | 262 return nil, err |
263 } | 263 } |
264 n.IsComplex = true | 264 n.IsComplex = true |
265 n.simplifyComplex() | 265 n.simplifyComplex() |
266 return n, nil | 266 return n, nil |
267 } | 267 } |
268 // Imaginary constants can only be complex unless they are zero. | 268 // Imaginary constants can only be complex unless they are zero. |
269 if len(text) > 0 && text[len(text)-1] == 'i' { | 269 if len(text) > 0 && text[len(text)-1] == 'i' { |
270 » » f, err := strconv.Atof64(text[:len(text)-1]) | 270 » » f, err := strconv.ParseFloat(text[:len(text)-1], 64) |
271 if err == nil { | 271 if err == nil { |
272 n.IsComplex = true | 272 n.IsComplex = true |
273 n.Complex128 = complex(0, f) | 273 n.Complex128 = complex(0, f) |
274 n.simplifyComplex() | 274 n.simplifyComplex() |
275 return n, nil | 275 return n, nil |
276 } | 276 } |
277 } | 277 } |
278 // Do integer test first so we get 0x123 etc. | 278 // Do integer test first so we get 0x123 etc. |
279 » u, err := strconv.Btoui64(text, 0) // will fail for -0; fixed below. | 279 » u, err := strconv.ParseUint(text, 0, 64) // will fail for -0; fixed belo
w. |
280 if err == nil { | 280 if err == nil { |
281 n.IsUint = true | 281 n.IsUint = true |
282 n.Uint64 = u | 282 n.Uint64 = u |
283 } | 283 } |
284 » i, err := strconv.Btoi64(text, 0) | 284 » i, err := strconv.ParseInt(text, 0, 64) |
285 if err == nil { | 285 if err == nil { |
286 n.IsInt = true | 286 n.IsInt = true |
287 n.Int64 = i | 287 n.Int64 = i |
288 if i == 0 { | 288 if i == 0 { |
289 n.IsUint = true // in case of -0. | 289 n.IsUint = true // in case of -0. |
290 n.Uint64 = u | 290 n.Uint64 = u |
291 } | 291 } |
292 } | 292 } |
293 // If an integer extraction succeeded, promote the float. | 293 // If an integer extraction succeeded, promote the float. |
294 if n.IsInt { | 294 if n.IsInt { |
295 n.IsFloat = true | 295 n.IsFloat = true |
296 n.Float64 = float64(n.Int64) | 296 n.Float64 = float64(n.Int64) |
297 } else if n.IsUint { | 297 } else if n.IsUint { |
298 n.IsFloat = true | 298 n.IsFloat = true |
299 n.Float64 = float64(n.Uint64) | 299 n.Float64 = float64(n.Uint64) |
300 } else { | 300 } else { |
301 » » f, err := strconv.Atof64(text) | 301 » » f, err := strconv.ParseFloat(text, 64) |
302 if err == nil { | 302 if err == nil { |
303 n.IsFloat = true | 303 n.IsFloat = true |
304 n.Float64 = f | 304 n.Float64 = f |
305 // If a floating-point extraction succeeded, extract the
int if needed. | 305 // If a floating-point extraction succeeded, extract the
int if needed. |
306 if !n.IsInt && float64(int64(f)) == f { | 306 if !n.IsInt && float64(int64(f)) == f { |
307 n.IsInt = true | 307 n.IsInt = true |
308 n.Int64 = int64(f) | 308 n.Int64 = int64(f) |
309 } | 309 } |
310 if !n.IsUint && float64(uint64(f)) == f { | 310 if !n.IsUint && float64(uint64(f)) == f { |
311 n.IsUint = true | 311 n.IsUint = true |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 func newTemplate(line int, name string, pipe *PipeNode) *TemplateNode { | 454 func newTemplate(line int, name string, pipe *PipeNode) *TemplateNode { |
455 return &TemplateNode{NodeType: NodeTemplate, Line: line, Name: name, Pip
e: pipe} | 455 return &TemplateNode{NodeType: NodeTemplate, Line: line, Name: name, Pip
e: pipe} |
456 } | 456 } |
457 | 457 |
458 func (t *TemplateNode) String() string { | 458 func (t *TemplateNode) String() string { |
459 if t.Pipe == nil { | 459 if t.Pipe == nil { |
460 return fmt.Sprintf("{{template %q}}", t.Name) | 460 return fmt.Sprintf("{{template %q}}", t.Name) |
461 } | 461 } |
462 return fmt.Sprintf("{{template %q %s}}", t.Name, t.Pipe) | 462 return fmt.Sprintf("{{template %q %s}}", t.Name, t.Pipe) |
463 } | 463 } |
OLD | NEW |