/
lama_strings.go
134 lines (124 loc) · 3.26 KB
/
lama_strings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package parser
import (
"fmt"
"math/big"
"strconv"
"strings"
"unicode/utf16"
"github.com/HexmosTech/gabs/v2"
"github.com/HexmosTech/lama2/utils"
)
func (p *Lama2Parser) readUnicodeHexStr() ([]string, error) {
codePoint := make([]string, 0)
for i := 0; i < 4; i++ {
r, e := p.CharClass("0-9a-fA-F")
if e == nil {
codePoint = append(codePoint, string(r))
} else {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Invalid code point", []string{})
}
}
return codePoint, nil
}
// QuotedString accepts both single-quoted and double-quoted
// types of strings. Moreover, it can deal with unicode
// escape characters, control characters appropriately
// Ultimately, we get a string wrapped in a gabs container
func (p *Lama2Parser) QuotedString() (*gabs.Container, error) {
temp := gabs.New()
chars := []string{}
quote, e := p.CharClass("\"'")
if e != nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1,
"Couldn't find quoted string", []string{})
}
escapeSequences := map[string]string{
"b": "\b",
"f": "\f",
"n": "\n",
"r": "\r",
"t": "\t",
"\\": "\\",
"/": "/",
}
// TODO: The following is slightly different from
// the original; may require rework
escapeSequences[string(quote)] = string(quote)
for {
c, _ := p.Char()
if c == quote {
break
} else if c == '\\' {
escape, _ := p.Char()
var codePoint []string
var e error
if escape == 'u' {
codePoint, e = p.readUnicodeHexStr()
if e != nil {
return nil, e
}
hex := strings.Join(codePoint, "")
n := new(big.Int)
n.SetString(hex, 16)
rune1 := rune(n.Int64())
if utf16.IsSurrogate(rune1) {
p.CharClass("\\")
p.CharClass("u")
codePoint, e = p.readUnicodeHexStr()
if e != nil {
return nil, e
}
hex2 := strings.Join(codePoint, "")
n2 := new(big.Int)
n2.SetString(hex2, 16)
rune2 := rune(n2.Int64())
res2 := utf16.DecodeRune(rune1, rune2)
chars = append(chars, string(res2))
} else {
r2 := strconv.FormatInt(n.Int64(), 16)
r3 := fmt.Sprintf("\\u%04s", r2)
r4, _ := strconv.Unquote(`"` + r3 + `"`)
// Uncomment to read the string slice:
// r4Rune := []rune(r4)
// fmt.Println(r4Rune)
chars = append(chars, r4)
}
} else {
if val, ok := escapeSequences[string(escape)]; ok {
chars = append(chars, val)
} else {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Invalid escape sequence", []string{})
}
}
} else {
if utils.UnicodeCategory(c) == "Cc" {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Unescaped control character", []string{})
}
chars = append(chars, string(c))
}
}
res := strings.Join(chars, "")
temp.Set(res)
return temp, nil
}
func (p *Lama2Parser) Unquoted() (*gabs.Container, error) {
acceptableChars := "0-9A-Za-z \t!$%&()*+./;<=>?^_`|~-"
chars := make([]string, 0)
c, e := p.CharClass(acceptableChars)
if e != nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1,
"No match for Unquoted string", []string{})
}
chars = append(chars, string(c))
for {
c, e = p.CharClass(acceptableChars)
if e != nil {
break
}
chars = append(chars, string(c))
}
s := strings.TrimSpace(strings.Join(chars, ""))
temp := gabs.New()
temp.Set(s)
return temp, nil
}