| 1 | package golang |
| 2 | |
| 3 | import ( |
| 4 | "path" |
| 5 | |
| 6 | "github.com/goccmack/gocc/internal/io" |
| 7 | ) |
| 8 | |
| 9 | func GenLitConv(outDir string) { |
| 10 | io.WriteFileString(path.Join(outDir, "util", "litconv.go"), litConvSrc[1:]) |
| 11 | } |
| 12 | |
| 13 | const litConvSrc = ` |
| 14 | // Code generated by gocc; DO NOT EDIT. |
| 15 | |
| 16 | package util |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "strconv" |
| 21 | "unicode" |
| 22 | "unicode/utf8" |
| 23 | ) |
| 24 | |
| 25 | // Interface. |
| 26 | |
| 27 | // RuneValue will convert the literal value of a scanned token to a rune. |
| 28 | func RuneValue(lit []byte) rune { |
| 29 | if lit[1] == '\\' { |
| 30 | return escapeCharVal(lit) |
| 31 | } |
| 32 | r, size := utf8.DecodeRune(lit[1:]) |
| 33 | if size != len(lit)-2 { |
| 34 | panic(fmt.Sprintf("Error decoding rune. Lit: %s, rune: %d, size%d\n", lit, r, size)) |
| 35 | } |
| 36 | return r |
| 37 | } |
| 38 | |
| 39 | // UintValue will attempt to parse a byte-slice as a signed base-10 64-bit integer. |
| 40 | func IntValue(lit []byte) (int64, error) { |
| 41 | return strconv.ParseInt(string(lit), 10, 64) |
| 42 | } |
| 43 | |
| 44 | // UintValue will attempt to parse a byte-slice as an unsigned base-10 64-bit integer. |
| 45 | func UintValue(lit []byte) (uint64, error) { |
| 46 | return strconv.ParseUint(string(lit), 10, 64) |
| 47 | } |
| 48 | |
| 49 | // Helpers. |
| 50 | func escapeCharVal(lit []byte) rune { |
| 51 | var i, base, max uint32 |
| 52 | offset := 2 |
| 53 | switch lit[offset] { |
| 54 | case 'a': |
| 55 | return '\a' |
| 56 | case 'b': |
| 57 | return '\b' |
| 58 | case 'f': |
| 59 | return '\f' |
| 60 | case 'n': |
| 61 | return '\n' |
| 62 | case 'r': |
| 63 | return '\r' |
| 64 | case 't': |
| 65 | return '\t' |
| 66 | case 'v': |
| 67 | return '\v' |
| 68 | case '\\': |
| 69 | return '\\' |
| 70 | case '\'': |
| 71 | return '\'' |
| 72 | case '0', '1', '2', '3', '4', '5', '6', '7': |
| 73 | i, base, max = 3, 8, 255 |
| 74 | case 'x': |
| 75 | i, base, max = 2, 16, 255 |
| 76 | offset++ |
| 77 | case 'u': |
| 78 | i, base, max = 4, 16, unicode.MaxRune |
| 79 | offset++ |
| 80 | case 'U': |
| 81 | i, base, max = 8, 16, unicode.MaxRune |
| 82 | offset++ |
| 83 | default: |
| 84 | panic(fmt.Sprintf("Error decoding character literal: %s\n", lit)) |
| 85 | } |
| 86 | |
| 87 | var x uint32 |
| 88 | for ; i > 0 && offset < len(lit)-1; i-- { |
| 89 | ch, size := utf8.DecodeRune(lit[offset:]) |
| 90 | offset += size |
| 91 | d := uint32(digitVal(ch)) |
| 92 | if d >= base { |
| 93 | panic(fmt.Sprintf("charVal(%s): illegal character (%c) in escape sequence. size=%d, offset=%d", lit, ch, size, offset)) |
| 94 | } |
| 95 | x = x*base + d |
| 96 | } |
| 97 | if x > max || 0xD800 <= x && x < 0xE000 { |
| 98 | panic(fmt.Sprintf("Error decoding escape char value. Lit:%s, offset:%d, escape sequence is invalid Unicode code point\n", lit, offset)) |
| 99 | } |
| 100 | |
| 101 | return rune(x) |
| 102 | } |
| 103 | |
| 104 | func digitVal(ch rune) int { |
| 105 | switch { |
| 106 | case '0' <= ch && ch <= '9': |
| 107 | return int(ch) - '0' |
| 108 | case 'a' <= ch && ch <= 'f': |
| 109 | return int(ch) - 'a' + 10 |
| 110 | case 'A' <= ch && ch <= 'F': |
| 111 | return int(ch) - 'A' + 10 |
| 112 | } |
| 113 | return 16 // larger than any legal digit val |
| 114 | } |
| 115 | ` |