Debian Codegraph @

internal/util/litconv.go

raw symbols SCIP
1 //Copyright 2013 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package util
16
17 import (
18 "fmt"
19 "strconv"
20 "unicode"
21 "unicode/utf8"
22 )
23
24 // Interface.
25
26 // LitToRun will attempt to convert the literal value of a scanned token to a rune.
27 func LitToRune(lit []byte) rune {
28 if lit[1] == '\\' {
29 return escapeCharVal(lit)
30 }
31 r, size := utf8.DecodeRune(lit[1:])
32 if size != len(lit)-2 {
33 panic(fmt.Sprintf("Error decoding rune. Lit: %s, rune: %d, size%d\n", lit, r, size))
34 }
35 return r
36 }
37
38 // IntValue will attempt to parse a byte-slice as a signed base-10 64-bit integer.
39 func IntValue(lit []byte) (int64, error) {
40 return strconv.ParseInt(string(lit), 10, 64)
41 }
42
43 // UintValue will attempt to parse a byte-slice as an unsigned base-10 64-bit integer.
44 func UintValue(lit []byte) (uint64, error) {
45 return strconv.ParseUint(string(lit), 10, 64)
46 }
47
48 // Helpers.
49 func escapeCharVal(lit []byte) rune {
50 var i, base, max uint32
51 offset := 2
52 switch lit[offset] {
53 case 'a':
54 return '\a'
55 case 'b':
56 return '\b'
57 case 'f':
58 return '\f'
59 case 'n':
60 return '\n'
61 case 'r':
62 return '\r'
63 case 't':
64 return '\t'
65 case 'v':
66 return '\v'
67 case '\\':
68 return '\\'
69 case '\'':
70 return '\''
71 case '0', '1', '2', '3', '4', '5', '6', '7':
72 i, base, max = 3, 8, 255
73 case 'x':
74 i, base, max = 2, 16, 255
75 offset++
76 case 'u':
77 i, base, max = 4, 16, unicode.MaxRune
78 offset++
79 case 'U':
80 i, base, max = 8, 16, unicode.MaxRune
81 offset++
82 default:
83 panic(fmt.Sprintf("Error decoding character literal: %s\n", lit))
84 }
85
86 var x uint32
87 for ; i > 0 && offset < len(lit)-1; i-- {
88 ch, size := utf8.DecodeRune(lit[offset:])
89 offset += size
90 d := uint32(digitVal(ch))
91 if d >= base {
92 panic(fmt.Sprintf("charVal(%s): illegal character (%c) in escape sequence. size=%d, offset=%d", lit, ch, size, offset))
93 }
94 x = x*base + d
95 }
96 if x > max || 0xD800 <= x && x < 0xE000 {
97 panic(fmt.Sprintf("Error decoding escape char value. Lit:%s, offset:%d, escape sequence is invalid Unicode code point\n", lit, offset))
98 }
99
100 return rune(x)
101 }
102
103 func digitVal(ch rune) int {
104 switch {
105 case '0' <= ch && ch <= '9':
106 return int(ch) - '0'
107 case 'a' <= ch && ch <= 'f':
108 return int(ch) - 'a' + 10
109 case 'A' <= ch && ch <= 'F':
110 return int(ch) - 'A' + 10
111 }
112 return 16 // larger than any legal digit val
113 }