parseInt parses a string and returns an integer literal.
(min, max int)
| 43 | |
| 44 | // parseInt parses a string and returns an integer literal. |
| 45 | func (p *Parser) parseInt(min, max int) (int, error) { |
| 46 | tok, pos, lit := p.scanIgnoreWhitespace() |
| 47 | if tok != NUMBER { |
| 48 | return 0, newParseError(tokstr(tok, lit), []string{"number"}, pos) |
| 49 | } |
| 50 | |
| 51 | // Return an error if the number has a fractional part. |
| 52 | if strings.Contains(lit, ".") { |
| 53 | return 0, &ParseError{Message: "number must be an integer", Pos: pos} |
| 54 | } |
| 55 | |
| 56 | // Convert string to int. |
| 57 | n, err := strconv.Atoi(lit) |
| 58 | if err != nil { |
| 59 | return 0, &ParseError{Message: err.Error(), Pos: pos} |
| 60 | } else if min > n || n > max { |
| 61 | return 0, &ParseError{ |
| 62 | Message: fmt.Sprintf("invalid value %d: must be %d <= n <= %d", n, min, max), |
| 63 | Pos: pos, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return n, nil |
| 68 | } |
| 69 | |
| 70 | // parseUInt32 parses a string and returns a 32-bit unsigned integer literal. |
| 71 | func (p *Parser) parseUInt32() (uint32, error) { |
nothing calls this directly
no test coverage detected