|
|
@@ -4,6 +4,8 @@ package token
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
+ "strconv"
|
|
|
+ "unicode/utf8"
|
|
|
)
|
|
|
|
|
|
type Token struct {
|
|
|
@@ -57,6 +59,64 @@ func (m TokenMap) StringType(typ Type) string {
|
|
|
return fmt.Sprintf("%s(%d)", m.Id(typ), typ)
|
|
|
}
|
|
|
|
|
|
+// CharLiteralValue returns the string value of the char literal.
|
|
|
+func (t *Token) CharLiteralValue() string {
|
|
|
+ return string(t.Lit[1 : len(t.Lit)-1])
|
|
|
+}
|
|
|
+
|
|
|
+// Float32Value returns the float32 value of the token or an error if the token literal does not
|
|
|
+// denote a valid float32.
|
|
|
+func (t *Token) Float32Value() (float32, error) {
|
|
|
+ if v, err := strconv.ParseFloat(string(t.Lit), 32); err != nil {
|
|
|
+ return 0, err
|
|
|
+ } else {
|
|
|
+ return float32(v), nil
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Float64Value returns the float64 value of the token or an error if the token literal does not
|
|
|
+// denote a valid float64.
|
|
|
+func (t *Token) Float64Value() (float64, error) {
|
|
|
+ return strconv.ParseFloat(string(t.Lit), 64)
|
|
|
+}
|
|
|
+
|
|
|
+// IDValue returns the string representation of an identifier token.
|
|
|
+func (t *Token) IDValue() string {
|
|
|
+ return string(t.Lit)
|
|
|
+}
|
|
|
+
|
|
|
+// Int32Value returns the int32 value of the token or an error if the token literal does not
|
|
|
+// denote a valid float64.
|
|
|
+func (t *Token) Int32Value() (int32, error) {
|
|
|
+ if v, err := strconv.ParseInt(string(t.Lit), 10, 64); err != nil {
|
|
|
+ return 0, err
|
|
|
+ } else {
|
|
|
+ return int32(v), nil
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Int64Value returns the int64 value of the token or an error if the token literal does not
|
|
|
+// denote a valid float64.
|
|
|
+func (t *Token) Int64Value() (int64, error) {
|
|
|
+ return strconv.ParseInt(string(t.Lit), 10, 64)
|
|
|
+}
|
|
|
+
|
|
|
+// UTF8Rune decodes the UTF8 rune in the token literal. It returns utf8.RuneError if
|
|
|
+// the token literal contains an invalid rune.
|
|
|
+func (t *Token) UTF8Rune() (rune, error) {
|
|
|
+ r, _ := utf8.DecodeRune(t.Lit)
|
|
|
+ if r == utf8.RuneError {
|
|
|
+ err := fmt.Errorf("Invalid rune")
|
|
|
+ return r, err
|
|
|
+ }
|
|
|
+ return r, nil
|
|
|
+}
|
|
|
+
|
|
|
+// StringValue returns the string value of the token literal.
|
|
|
+func (t *Token) StringValue() string {
|
|
|
+ return string(t.Lit[1 : len(t.Lit)-1])
|
|
|
+}
|
|
|
+
|
|
|
var TokMap = TokenMap{
|
|
|
typeMap: []string{
|
|
|
"INVALID",
|