github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/mapper/exprmapper/expression/gocc/util/litconv.go (about)

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