2017-06-12 09:03:14 +00:00
|
|
|
/* WIP Under Construction */
|
2017-06-13 07:12:58 +00:00
|
|
|
package qgen
|
2017-06-12 09:03:14 +00:00
|
|
|
|
|
|
|
//import "fmt"
|
|
|
|
import "strings"
|
|
|
|
import "os"
|
|
|
|
|
|
|
|
func _process_columns(colstr string) (columns []DB_Column) {
|
|
|
|
if colstr == "" {
|
|
|
|
return columns
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
colstr = strings.Replace(colstr, " as ", " AS ", -1)
|
|
|
|
for _, segment := range strings.Split(colstr, ",") {
|
2017-06-12 09:03:14 +00:00
|
|
|
var outcol DB_Column
|
2017-09-03 04:50:31 +00:00
|
|
|
dothalves := strings.Split(strings.TrimSpace(segment), ".")
|
|
|
|
|
2017-06-12 09:03:14 +00:00
|
|
|
var halves []string
|
|
|
|
if len(dothalves) == 2 {
|
|
|
|
outcol.Table = dothalves[0]
|
2017-09-03 04:50:31 +00:00
|
|
|
halves = strings.Split(dothalves[1], " AS ")
|
2017-06-12 09:03:14 +00:00
|
|
|
} else {
|
2017-09-03 04:50:31 +00:00
|
|
|
halves = strings.Split(dothalves[0], " AS ")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-06-12 09:03:14 +00:00
|
|
|
halves[0] = strings.TrimSpace(halves[0])
|
|
|
|
if len(halves) == 2 {
|
|
|
|
outcol.Alias = strings.TrimSpace(halves[1])
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
if halves[0][len(halves[0])-1] == ')' {
|
2017-06-12 09:03:14 +00:00
|
|
|
outcol.Type = "function"
|
2017-06-19 08:06:54 +00:00
|
|
|
} else if halves[0] == "?" {
|
|
|
|
outcol.Type = "substitute"
|
2017-06-12 09:03:14 +00:00
|
|
|
} else {
|
|
|
|
outcol.Type = "column"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-06-12 09:03:14 +00:00
|
|
|
outcol.Left = halves[0]
|
2017-09-03 04:50:31 +00:00
|
|
|
columns = append(columns, outcol)
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
return columns
|
|
|
|
}
|
|
|
|
|
|
|
|
func _process_orderby(orderstr string) (order []DB_Order) {
|
|
|
|
if orderstr == "" {
|
|
|
|
return order
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
for _, segment := range strings.Split(orderstr, ",") {
|
2017-06-12 09:03:14 +00:00
|
|
|
var outorder DB_Order
|
2017-09-03 04:50:31 +00:00
|
|
|
halves := strings.Split(strings.TrimSpace(segment), " ")
|
2017-06-12 09:03:14 +00:00
|
|
|
if len(halves) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
outorder.Column = halves[0]
|
|
|
|
outorder.Order = strings.ToLower(halves[1])
|
2017-09-03 04:50:31 +00:00
|
|
|
order = append(order, outorder)
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
return order
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _processJoiner(joinstr string) (joiner []DB_Joiner) {
|
2017-06-12 09:03:14 +00:00
|
|
|
if joinstr == "" {
|
|
|
|
return joiner
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
joinstr = strings.Replace(joinstr, " on ", " ON ", -1)
|
|
|
|
joinstr = strings.Replace(joinstr, " and ", " AND ", -1)
|
|
|
|
for _, segment := range strings.Split(joinstr, " AND ") {
|
2017-06-12 09:03:14 +00:00
|
|
|
var outjoin DB_Joiner
|
|
|
|
var parseOffset int
|
|
|
|
var left, right string
|
2017-09-03 04:50:31 +00:00
|
|
|
|
|
|
|
left, parseOffset = _getIdentifier(segment, parseOffset)
|
|
|
|
outjoin.Operator, parseOffset = _getOperator(segment, parseOffset+1)
|
|
|
|
right, parseOffset = _getIdentifier(segment, parseOffset+1)
|
|
|
|
|
|
|
|
left_column := strings.Split(left, ".")
|
|
|
|
right_column := strings.Split(right, ".")
|
2017-06-12 09:03:14 +00:00
|
|
|
outjoin.LeftTable = strings.TrimSpace(left_column[0])
|
|
|
|
outjoin.RightTable = strings.TrimSpace(right_column[0])
|
|
|
|
outjoin.LeftColumn = strings.TrimSpace(left_column[1])
|
|
|
|
outjoin.RightColumn = strings.TrimSpace(right_column[1])
|
2017-09-03 04:50:31 +00:00
|
|
|
|
|
|
|
joiner = append(joiner, outjoin)
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
return joiner
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _processWhere(wherestr string) (where []DB_Where) {
|
2017-06-12 09:03:14 +00:00
|
|
|
if wherestr == "" {
|
|
|
|
return where
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
wherestr = strings.Replace(wherestr, " and ", " AND ", -1)
|
|
|
|
|
2017-06-19 08:06:54 +00:00
|
|
|
var buffer string
|
|
|
|
var optype int // 0: None, 1: Number, 2: Column, 3: Function, 4: String, 5: Operator
|
2017-09-03 04:50:31 +00:00
|
|
|
for _, segment := range strings.Split(wherestr, " AND ") {
|
2017-06-19 08:06:54 +00:00
|
|
|
var tmp_where DB_Where
|
|
|
|
segment += ")"
|
|
|
|
for i := 0; i < len(segment); i++ {
|
|
|
|
char := segment[i]
|
|
|
|
//fmt.Println("optype",optype)
|
2017-09-03 04:50:31 +00:00
|
|
|
switch optype {
|
2017-06-19 08:06:54 +00:00
|
|
|
case 0: // unknown
|
|
|
|
//fmt.Println("case 0:",char,string(char))
|
2017-09-03 04:50:31 +00:00
|
|
|
if '0' <= char && char <= '9' {
|
2017-06-19 08:06:54 +00:00
|
|
|
optype = 1
|
|
|
|
buffer = string(char)
|
|
|
|
} else if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char == '_' {
|
|
|
|
optype = 2
|
|
|
|
buffer = string(char)
|
|
|
|
} else if char == '\'' {
|
|
|
|
optype = 4
|
|
|
|
buffer = ""
|
|
|
|
} else if _is_op_byte(char) {
|
|
|
|
optype = 5
|
|
|
|
buffer = string(char)
|
|
|
|
} else if char == '?' {
|
|
|
|
//fmt.Println("Expr:","?")
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_where.Expr = append(tmp_where.Expr, DB_Token{"?", "substitute"})
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
|
|
|
case 1: // number
|
2017-09-03 04:50:31 +00:00
|
|
|
if '0' <= char && char <= '9' {
|
2017-06-19 08:06:54 +00:00
|
|
|
buffer += string(char)
|
|
|
|
} else {
|
|
|
|
optype = 0
|
|
|
|
i--
|
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_where.Expr = append(tmp_where.Expr, DB_Token{buffer, "number"})
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
|
|
|
case 2: // column
|
|
|
|
if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char == '.' || char == '_' {
|
|
|
|
buffer += string(char)
|
|
|
|
} else if char == '(' {
|
|
|
|
optype = 3
|
|
|
|
i--
|
|
|
|
} else {
|
|
|
|
optype = 0
|
|
|
|
i--
|
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_where.Expr = append(tmp_where.Expr, DB_Token{buffer, "column"})
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
|
|
|
case 3: // function
|
2017-09-03 04:50:31 +00:00
|
|
|
var preI = i
|
2017-06-19 08:06:54 +00:00
|
|
|
//fmt.Println("buffer",buffer)
|
|
|
|
//fmt.Println("len(halves)",len(halves[1]))
|
2017-09-03 04:50:31 +00:00
|
|
|
//fmt.Println("preI",string(halves[1][preI]))
|
|
|
|
//fmt.Println("msg prior to preI",halves[1][0:preI])
|
|
|
|
i = _skipFunctionCall(segment, i-1)
|
2017-06-19 08:06:54 +00:00
|
|
|
//fmt.Println("i",i)
|
|
|
|
//fmt.Println("msg prior to i-1",halves[1][0:i-1])
|
|
|
|
//fmt.Println("string(i-1)",string(halves[1][i-1]))
|
|
|
|
//fmt.Println("string(i)",string(halves[1][i]))
|
2017-09-03 04:50:31 +00:00
|
|
|
buffer += segment[preI:i] + string(segment[i])
|
2017-06-19 08:06:54 +00:00
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_where.Expr = append(tmp_where.Expr, DB_Token{buffer, "function"})
|
2017-06-19 08:06:54 +00:00
|
|
|
optype = 0
|
|
|
|
case 4: // string
|
|
|
|
if char != '\'' {
|
|
|
|
buffer += string(char)
|
|
|
|
} else {
|
|
|
|
optype = 0
|
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_where.Expr = append(tmp_where.Expr, DB_Token{buffer, "string"})
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
|
|
|
case 5: // operator
|
|
|
|
if _is_op_byte(char) {
|
|
|
|
buffer += string(char)
|
|
|
|
} else {
|
|
|
|
optype = 0
|
|
|
|
i--
|
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_where.Expr = append(tmp_where.Expr, DB_Token{buffer, "operator"})
|
2017-06-19 08:06:54 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("Bad optype in _process_where")
|
|
|
|
}
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
where = append(where, tmp_where)
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
return where
|
|
|
|
}
|
|
|
|
|
|
|
|
func _process_set(setstr string) (setter []DB_Setter) {
|
|
|
|
if setstr == "" {
|
|
|
|
return setter
|
|
|
|
}
|
|
|
|
//fmt.Println("setstr",setstr)
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-06-12 09:03:14 +00:00
|
|
|
// First pass, splitting the string by commas while ignoring the innards of functions
|
|
|
|
var setset []string
|
|
|
|
var buffer string
|
2017-09-03 04:50:31 +00:00
|
|
|
var lastItem int
|
2017-06-12 09:03:14 +00:00
|
|
|
setstr += ","
|
2017-09-03 04:50:31 +00:00
|
|
|
for i := 0; i < len(setstr); i++ {
|
2017-06-12 09:03:14 +00:00
|
|
|
if setstr[i] == '(' {
|
2017-09-03 04:50:31 +00:00
|
|
|
i = _skipFunctionCall(setstr, i-1)
|
|
|
|
setset = append(setset, setstr[lastItem:i+1])
|
2017-06-12 09:03:14 +00:00
|
|
|
buffer = ""
|
2017-09-03 04:50:31 +00:00
|
|
|
lastItem = i + 2
|
2017-06-12 09:03:14 +00:00
|
|
|
} else if setstr[i] == ',' && buffer != "" {
|
2017-09-03 04:50:31 +00:00
|
|
|
setset = append(setset, buffer)
|
2017-06-12 09:03:14 +00:00
|
|
|
buffer = ""
|
2017-09-03 04:50:31 +00:00
|
|
|
lastItem = i + 1
|
2017-06-12 09:03:14 +00:00
|
|
|
} else if (setstr[i] > 32) && setstr[i] != ',' && setstr[i] != ')' {
|
|
|
|
buffer += string(setstr[i])
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-06-12 09:03:14 +00:00
|
|
|
// Second pass. Break this setitem into manageable chunks
|
|
|
|
buffer = ""
|
|
|
|
for _, setitem := range setset {
|
|
|
|
var tmp_setter DB_Setter
|
2017-09-03 04:50:31 +00:00
|
|
|
halves := strings.Split(setitem, "=")
|
2017-06-12 09:03:14 +00:00
|
|
|
if len(halves) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tmp_setter.Column = strings.TrimSpace(halves[0])
|
2017-09-03 04:50:31 +00:00
|
|
|
|
2017-06-12 09:03:14 +00:00
|
|
|
halves[1] += ")"
|
|
|
|
var optype int // 0: None, 1: Number, 2: Column, 3: Function, 4: String, 5: Operator
|
|
|
|
//fmt.Println("halves[1]",halves[1])
|
|
|
|
for i := 0; i < len(halves[1]); i++ {
|
|
|
|
char := halves[1][i]
|
|
|
|
//fmt.Println("optype",optype)
|
2017-09-03 04:50:31 +00:00
|
|
|
switch optype {
|
2017-06-12 09:03:14 +00:00
|
|
|
case 0: // unknown
|
2017-09-03 04:50:31 +00:00
|
|
|
if '0' <= char && char <= '9' {
|
2017-06-12 09:03:14 +00:00
|
|
|
optype = 1
|
|
|
|
buffer = string(char)
|
2017-06-19 08:06:54 +00:00
|
|
|
} else if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char == '_' {
|
2017-06-12 09:03:14 +00:00
|
|
|
optype = 2
|
|
|
|
buffer = string(char)
|
|
|
|
} else if char == '\'' {
|
|
|
|
optype = 4
|
2017-06-19 08:06:54 +00:00
|
|
|
buffer = ""
|
2017-06-12 09:03:14 +00:00
|
|
|
} else if _is_op_byte(char) {
|
|
|
|
optype = 5
|
|
|
|
buffer = string(char)
|
|
|
|
} else if char == '?' {
|
|
|
|
//fmt.Println("Expr:","?")
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_setter.Expr = append(tmp_setter.Expr, DB_Token{"?", "substitute"})
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
case 1: // number
|
2017-09-03 04:50:31 +00:00
|
|
|
if '0' <= char && char <= '9' {
|
2017-06-12 09:03:14 +00:00
|
|
|
buffer += string(char)
|
|
|
|
} else {
|
|
|
|
optype = 0
|
|
|
|
i--
|
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_setter.Expr = append(tmp_setter.Expr, DB_Token{buffer, "number"})
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
case 2: // column
|
2017-06-19 08:06:54 +00:00
|
|
|
if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char == '_' {
|
2017-06-12 09:03:14 +00:00
|
|
|
buffer += string(char)
|
|
|
|
} else if char == '(' {
|
|
|
|
optype = 3
|
|
|
|
i--
|
|
|
|
} else {
|
|
|
|
optype = 0
|
|
|
|
i--
|
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_setter.Expr = append(tmp_setter.Expr, DB_Token{buffer, "column"})
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
case 3: // function
|
2017-09-03 04:50:31 +00:00
|
|
|
var preI = i
|
2017-06-12 09:03:14 +00:00
|
|
|
//fmt.Println("buffer",buffer)
|
|
|
|
//fmt.Println("len(halves)",len(halves[1]))
|
2017-09-03 04:50:31 +00:00
|
|
|
//fmt.Println("preI",string(halves[1][preI]))
|
|
|
|
//fmt.Println("msg prior to preI",halves[1][0:preI])
|
|
|
|
i = _skipFunctionCall(halves[1], i-1)
|
2017-06-12 09:03:14 +00:00
|
|
|
//fmt.Println("i",i)
|
|
|
|
//fmt.Println("msg prior to i-1",halves[1][0:i-1])
|
|
|
|
//fmt.Println("string(i-1)",string(halves[1][i-1]))
|
|
|
|
//fmt.Println("string(i)",string(halves[1][i]))
|
2017-09-03 04:50:31 +00:00
|
|
|
buffer += halves[1][preI:i] + string(halves[1][i])
|
2017-06-12 09:03:14 +00:00
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_setter.Expr = append(tmp_setter.Expr, DB_Token{buffer, "function"})
|
2017-06-12 09:03:14 +00:00
|
|
|
optype = 0
|
|
|
|
case 4: // string
|
|
|
|
if char != '\'' {
|
|
|
|
buffer += string(char)
|
|
|
|
} else {
|
|
|
|
optype = 0
|
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_setter.Expr = append(tmp_setter.Expr, DB_Token{buffer, "string"})
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
case 5: // operator
|
|
|
|
if _is_op_byte(char) {
|
|
|
|
buffer += string(char)
|
|
|
|
} else {
|
|
|
|
optype = 0
|
|
|
|
i--
|
|
|
|
//fmt.Println("Expr:",buffer)
|
2017-09-03 04:50:31 +00:00
|
|
|
tmp_setter.Expr = append(tmp_setter.Expr, DB_Token{buffer, "operator"})
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
2017-06-19 08:06:54 +00:00
|
|
|
default:
|
|
|
|
panic("Bad optype in _process_set")
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
setter = append(setter, tmp_setter)
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|
|
|
|
//fmt.Println("setter",setter)
|
|
|
|
return setter
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _processLimit(limitstr string) (limiter DB_Limit) {
|
|
|
|
halves := strings.Split(limitstr, ",")
|
2017-06-15 11:40:35 +00:00
|
|
|
if len(halves) == 2 {
|
|
|
|
limiter.Offset = halves[0]
|
|
|
|
limiter.MaxCount = halves[1]
|
|
|
|
} else {
|
|
|
|
limiter.MaxCount = halves[0]
|
|
|
|
}
|
|
|
|
return limiter
|
|
|
|
}
|
|
|
|
|
2017-06-12 09:03:14 +00:00
|
|
|
func _is_op_byte(char byte) bool {
|
|
|
|
return char == '<' || char == '>' || char == '=' || char == '!' || char == '*' || char == '%' || char == '+' || char == '-' || char == '/'
|
|
|
|
}
|
|
|
|
|
2017-06-19 08:06:54 +00:00
|
|
|
func _is_op_rune(char rune) bool {
|
|
|
|
return char == '<' || char == '>' || char == '=' || char == '!' || char == '*' || char == '%' || char == '+' || char == '-' || char == '/'
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _processFields(fieldstr string) (fields []DB_Field) {
|
2017-06-12 09:03:14 +00:00
|
|
|
if fieldstr == "" {
|
|
|
|
return fields
|
|
|
|
}
|
|
|
|
var buffer string
|
2017-09-03 04:50:31 +00:00
|
|
|
var lastItem int
|
2017-06-12 09:03:14 +00:00
|
|
|
fieldstr += ","
|
2017-09-03 04:50:31 +00:00
|
|
|
for i := 0; i < len(fieldstr); i++ {
|
2017-06-12 09:03:14 +00:00
|
|
|
if fieldstr[i] == '(' {
|
2017-09-03 04:50:31 +00:00
|
|
|
i = _skipFunctionCall(fieldstr, i-1)
|
|
|
|
fields = append(fields, DB_Field{Name: fieldstr[lastItem : i+1], Type: _getIdentifierType(fieldstr[lastItem : i+1])})
|
2017-06-12 09:03:14 +00:00
|
|
|
buffer = ""
|
2017-09-03 04:50:31 +00:00
|
|
|
lastItem = i + 2
|
2017-06-12 09:03:14 +00:00
|
|
|
} else if fieldstr[i] == ',' && buffer != "" {
|
2017-09-03 04:50:31 +00:00
|
|
|
fields = append(fields, DB_Field{Name: buffer, Type: _getIdentifierType(buffer)})
|
2017-06-12 09:03:14 +00:00
|
|
|
buffer = ""
|
2017-09-03 04:50:31 +00:00
|
|
|
lastItem = i + 1
|
2017-06-12 09:03:14 +00:00
|
|
|
} else if (fieldstr[i] > 32) && fieldstr[i] != ',' && fieldstr[i] != ')' {
|
|
|
|
buffer += string(fieldstr[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fields
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _getIdentifierType(identifier string) string {
|
2017-06-12 09:03:14 +00:00
|
|
|
if ('a' <= identifier[0] && identifier[0] <= 'z') || ('A' <= identifier[0] && identifier[0] <= 'Z') {
|
2017-09-03 04:50:31 +00:00
|
|
|
if identifier[len(identifier)-1] == ')' {
|
2017-06-12 09:03:14 +00:00
|
|
|
return "function"
|
|
|
|
}
|
|
|
|
return "column"
|
|
|
|
}
|
|
|
|
if identifier[0] == '\'' || identifier[0] == '"' {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
return "literal"
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _getIdentifier(segment string, startOffset int) (out string, i int) {
|
2017-06-12 09:03:14 +00:00
|
|
|
segment = strings.TrimSpace(segment)
|
|
|
|
segment += " " // Avoid overflow bugs with slicing
|
|
|
|
for i = startOffset; i < len(segment); i++ {
|
|
|
|
if segment[i] == '(' {
|
2017-09-03 04:50:31 +00:00
|
|
|
i = _skipFunctionCall(segment, i)
|
2017-06-12 09:03:14 +00:00
|
|
|
return strings.TrimSpace(segment[startOffset:i]), (i - 1)
|
|
|
|
}
|
|
|
|
if (segment[i] == ' ' || _is_op_byte(segment[i])) && i != startOffset {
|
|
|
|
return strings.TrimSpace(segment[startOffset:i]), (i - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(segment[startOffset:]), (i - 1)
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _getOperator(segment string, startOffset int) (out string, i int) {
|
2017-06-12 09:03:14 +00:00
|
|
|
segment = strings.TrimSpace(segment)
|
|
|
|
segment += " " // Avoid overflow bugs with slicing
|
|
|
|
for i = startOffset; i < len(segment); i++ {
|
|
|
|
if !_is_op_byte(segment[i]) && i != startOffset {
|
|
|
|
return strings.TrimSpace(segment[startOffset:i]), (i - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(segment[startOffset:]), (i - 1)
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func _skipFunctionCall(data string, index int) int {
|
|
|
|
var braceCount int
|
|
|
|
for ; index < len(data); index++ {
|
2017-06-12 09:03:14 +00:00
|
|
|
char := data[index]
|
|
|
|
if char == '(' {
|
2017-09-03 04:50:31 +00:00
|
|
|
braceCount++
|
2017-06-12 09:03:14 +00:00
|
|
|
} else if char == ')' {
|
2017-09-03 04:50:31 +00:00
|
|
|
braceCount--
|
|
|
|
if braceCount == 0 {
|
2017-06-12 09:03:14 +00:00
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func writeFile(name string, content string) (err error) {
|
2017-06-12 09:03:14 +00:00
|
|
|
f, err := os.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
err = f.Sync()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return f.Close()
|
2017-06-12 09:03:14 +00:00
|
|
|
}
|