Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "io" |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 8 | "strconv" |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 9 | "strings" |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 10 | "sync" |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 11 | "time" |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | errEndOfInput = errors.New("parse: unexpected end of input") |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 16 | |
| 17 | bufFree = sync.Pool{ |
Fumitoshi Ukai | d0bf88c | 2015-05-08 10:54:32 +0900 | [diff] [blame] | 18 | New: func() interface{} { return new(buffer) }, |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 19 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 20 | ) |
| 21 | |
Fumitoshi Ukai | d0bf88c | 2015-05-08 10:54:32 +0900 | [diff] [blame] | 22 | type buffer struct { |
| 23 | bytes.Buffer |
| 24 | args [][]byte |
| 25 | } |
| 26 | |
| 27 | func newBuf() *buffer { |
| 28 | buf := bufFree.Get().(*buffer) |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 29 | return buf |
| 30 | } |
| 31 | |
Fumitoshi Ukai | d0bf88c | 2015-05-08 10:54:32 +0900 | [diff] [blame] | 32 | func freeBuf(buf *buffer) { |
Fumitoshi Ukai | 7ff5b23 | 2015-05-08 00:44:12 +0900 | [diff] [blame] | 33 | if cap(buf.Bytes()) > 1024 { |
| 34 | return |
| 35 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 36 | buf.Reset() |
Fumitoshi Ukai | d0bf88c | 2015-05-08 10:54:32 +0900 | [diff] [blame] | 37 | buf.args = buf.args[:0] |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 38 | bufFree.Put(buf) |
| 39 | } |
| 40 | |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 41 | type Value interface { |
| 42 | String() string |
| 43 | Eval(w io.Writer, ev *Evaluator) |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 44 | Serialize() SerializableVar |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 45 | Dump(w io.Writer) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 48 | type Valuer interface { |
| 49 | Value() []byte |
| 50 | } |
| 51 | |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 52 | // literal is literal value. |
Fumitoshi Ukai | d2bcf66 | 2015-04-11 01:02:27 +0900 | [diff] [blame] | 53 | // TODO(ukai): always use []byte? |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 54 | type literal string |
| 55 | |
| 56 | func (s literal) String() string { return string(s) } |
| 57 | func (s literal) Eval(w io.Writer, ev *Evaluator) { |
Fumitoshi Ukai | 5541c7e | 2015-04-18 22:47:03 +0900 | [diff] [blame] | 58 | io.WriteString(w, string(s)) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 59 | } |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 60 | func (s literal) Serialize() SerializableVar { |
| 61 | return SerializableVar{Type: "literal", V: string(s)} |
| 62 | } |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 63 | func (s literal) Dump(w io.Writer) { |
| 64 | dumpByte(w, VALUE_TYPE_LITERAL) |
| 65 | dumpBytes(w, []byte(s)) |
| 66 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 67 | |
| 68 | // tmpval is temporary value. |
Fumitoshi Ukai | d2bcf66 | 2015-04-11 01:02:27 +0900 | [diff] [blame] | 69 | // TODO(ukai): Values() returns []Value? (word list?) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 70 | type tmpval []byte |
| 71 | |
| 72 | func (t tmpval) String() string { return string(t) } |
| 73 | func (t tmpval) Eval(w io.Writer, ev *Evaluator) { |
| 74 | w.Write(t) |
| 75 | } |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 76 | func (t tmpval) Value() []byte { return []byte(t) } |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 77 | func (t tmpval) Serialize() SerializableVar { |
| 78 | return SerializableVar{Type: "tmpval", V: string(t)} |
| 79 | } |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 80 | func (t tmpval) Dump(w io.Writer) { |
| 81 | dumpByte(w, VALUE_TYPE_TMPVAL) |
| 82 | dumpBytes(w, t) |
| 83 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 84 | |
| 85 | // Expr is a list of values. |
| 86 | type Expr []Value |
| 87 | |
| 88 | func (e Expr) String() string { |
| 89 | var s []string |
| 90 | for _, v := range e { |
| 91 | s = append(s, v.String()) |
| 92 | } |
| 93 | return strings.Join(s, "") |
| 94 | } |
| 95 | |
| 96 | func (e Expr) Eval(w io.Writer, ev *Evaluator) { |
| 97 | for _, v := range e { |
| 98 | v.Eval(w, ev) |
| 99 | } |
| 100 | } |
| 101 | |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 102 | func (e Expr) Serialize() SerializableVar { |
| 103 | r := SerializableVar{Type: "expr"} |
| 104 | for _, v := range e { |
| 105 | r.Children = append(r.Children, v.Serialize()) |
| 106 | } |
| 107 | return r |
| 108 | } |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 109 | func (e Expr) Dump(w io.Writer) { |
| 110 | dumpByte(w, VALUE_TYPE_EXPR) |
| 111 | dumpInt(w, len(e)) |
| 112 | for _, v := range e { |
| 113 | v.Dump(w) |
| 114 | } |
| 115 | } |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 116 | |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 117 | func compactExpr(e Expr) Value { |
| 118 | if len(e) == 1 { |
| 119 | return e[0] |
| 120 | } |
| 121 | // TODO(ukai): concat literal |
| 122 | return e |
| 123 | } |
| 124 | |
| 125 | // varref is variable reference. e.g. ${foo}. |
| 126 | type varref struct { |
| 127 | varname Value |
| 128 | } |
| 129 | |
| 130 | func (v varref) String() string { |
| 131 | varname := v.varname.String() |
| 132 | if len(varname) == 1 { |
| 133 | return fmt.Sprintf("$%s", varname) |
| 134 | } |
| 135 | return fmt.Sprintf("${%s}", varname) |
| 136 | } |
| 137 | |
| 138 | func (v varref) Eval(w io.Writer, ev *Evaluator) { |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 139 | t := time.Now() |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 140 | buf := newBuf() |
| 141 | v.varname.Eval(buf, ev) |
| 142 | vv := ev.LookupVar(buf.String()) |
| 143 | freeBuf(buf) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 144 | vv.Eval(w, ev) |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 145 | addStats("var", v, t) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 146 | } |
| 147 | |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 148 | func (v varref) Serialize() SerializableVar { |
| 149 | return SerializableVar{ |
Shinichiro Hamaji | 3d6d0aa | 2015-04-28 16:18:44 +0900 | [diff] [blame] | 150 | Type: "varref", |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 151 | Children: []SerializableVar{v.varname.Serialize()}, |
| 152 | } |
| 153 | } |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 154 | func (v varref) Dump(w io.Writer) { |
| 155 | dumpByte(w, VALUE_TYPE_VARREF) |
| 156 | v.varname.Dump(w) |
| 157 | } |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 158 | |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 159 | // paramref is parameter reference e.g. $1. |
| 160 | type paramref int |
| 161 | |
| 162 | func (p paramref) String() string { |
| 163 | return fmt.Sprintf("$%d", int(p)) |
| 164 | } |
| 165 | |
| 166 | func (p paramref) Eval(w io.Writer, ev *Evaluator) { |
| 167 | t := time.Now() |
| 168 | n := int(p) |
| 169 | if n < len(ev.paramVars) { |
| 170 | ev.paramVars[n].Eval(w, ev) |
| 171 | } else { |
| 172 | // out of range? |
| 173 | // panic(fmt.Sprintf("out of range %d: %d", n, len(ev.paramVars))) |
| 174 | } |
| 175 | addStats("param", p, t) |
| 176 | } |
| 177 | |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 178 | func (p paramref) Serialize() SerializableVar { |
| 179 | return SerializableVar{Type: "paramref", V: strconv.Itoa(int(p))} |
| 180 | } |
| 181 | |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 182 | func (p paramref) Dump(w io.Writer) { |
| 183 | dumpByte(w, VALUE_TYPE_PARAMREF) |
| 184 | dumpInt(w, int(p)) |
| 185 | } |
| 186 | |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 187 | // varsubst is variable substitutaion. e.g. ${var:pat=subst}. |
| 188 | type varsubst struct { |
| 189 | varname Value |
| 190 | pat Value |
| 191 | subst Value |
| 192 | } |
| 193 | |
| 194 | func (v varsubst) String() string { |
| 195 | return fmt.Sprintf("${%s:%s=%s}", v.varname, v.pat, v.subst) |
| 196 | } |
| 197 | |
| 198 | func (v varsubst) Eval(w io.Writer, ev *Evaluator) { |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 199 | t := time.Now() |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 200 | buf := newBuf() |
| 201 | params := ev.args(buf, v.varname, v.pat, v.subst) |
| 202 | vname := string(params[0]) |
| 203 | pat := string(params[1]) |
| 204 | subst := string(params[2]) |
| 205 | buf.Reset() |
| 206 | vv := ev.LookupVar(vname) |
| 207 | vv.Eval(buf, ev) |
| 208 | vals := splitSpaces(buf.String()) |
| 209 | freeBuf(buf) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 210 | space := false |
| 211 | for _, val := range vals { |
| 212 | if space { |
Fumitoshi Ukai | 5541c7e | 2015-04-18 22:47:03 +0900 | [diff] [blame] | 213 | io.WriteString(w, " ") |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 214 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 215 | io.WriteString(w, substRef(pat, subst, val)) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 216 | space = true |
| 217 | } |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 218 | addStats("varsubst", v, t) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 221 | func (v varsubst) Serialize() SerializableVar { |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 222 | return SerializableVar{ |
| 223 | Type: "varsubst", |
| 224 | Children: []SerializableVar{ |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 225 | v.varname.Serialize(), |
| 226 | v.pat.Serialize(), |
| 227 | v.subst.Serialize(), |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 228 | }, |
| 229 | } |
| 230 | } |
| 231 | |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 232 | func (v varsubst) Dump(w io.Writer) { |
| 233 | dumpByte(w, VALUE_TYPE_VARSUBST) |
| 234 | v.varname.Dump(w) |
| 235 | v.pat.Dump(w) |
| 236 | v.subst.Dump(w) |
| 237 | } |
| 238 | |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 239 | // parseExpr parses expression in `in` until it finds any byte in term. |
| 240 | // if term is nil, it will parse to end of input. |
| 241 | // if term is not nil, and it reaches to end of input, return errEndOfInput. |
| 242 | // it returns parsed value, and parsed length `n`, so in[n-1] is any byte of |
| 243 | // term, and in[n:] is next input. |
| 244 | func parseExpr(in, term []byte) (Value, int, error) { |
| 245 | var expr Expr |
Fumitoshi Ukai | d8d8425 | 2015-04-19 17:30:13 +0900 | [diff] [blame] | 246 | buf := make([]byte, 0, len(in)) |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 247 | b := 0 |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 248 | i := 0 |
| 249 | var saveParen byte |
| 250 | parenDepth := 0 |
| 251 | Loop: |
| 252 | for i < len(in) { |
| 253 | ch := in[i] |
Fumitoshi Ukai | 96c79f1 | 2015-04-18 22:58:13 +0900 | [diff] [blame] | 254 | if term != nil && bytes.IndexByte(term, ch) >= 0 { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 255 | break Loop |
| 256 | } |
| 257 | switch ch { |
| 258 | case '$': |
| 259 | if i+1 >= len(in) { |
| 260 | break Loop |
| 261 | } |
| 262 | if in[i+1] == '$' { |
Fumitoshi Ukai | d8d8425 | 2015-04-19 17:30:13 +0900 | [diff] [blame] | 263 | buf = append(buf, in[b:i+1]...) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 264 | i += 2 |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 265 | b = i |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 266 | continue |
| 267 | } |
| 268 | if bytes.IndexByte(term, in[i+1]) >= 0 { |
Fumitoshi Ukai | d8d8425 | 2015-04-19 17:30:13 +0900 | [diff] [blame] | 269 | buf = append(buf, in[b:i]...) |
| 270 | if len(buf) > 0 { |
| 271 | expr = append(expr, literal(string(buf))) |
| 272 | buf = buf[:0] |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 273 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 274 | expr = append(expr, varref{varname: literal("")}) |
| 275 | i++ |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 276 | b = i |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 277 | break Loop |
| 278 | } |
Fumitoshi Ukai | d8d8425 | 2015-04-19 17:30:13 +0900 | [diff] [blame] | 279 | buf = append(buf, in[b:i]...) |
| 280 | if len(buf) > 0 { |
| 281 | expr = append(expr, literal(string(buf))) |
| 282 | buf = buf[:0] |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 283 | } |
| 284 | v, n, err := parseDollar(in[i:]) |
| 285 | if err != nil { |
| 286 | return nil, 0, err |
| 287 | } |
| 288 | i += n |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 289 | b = i |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 290 | expr = append(expr, v) |
| 291 | continue |
| 292 | case '(', '{': |
| 293 | cp := closeParen(ch) |
| 294 | if i := bytes.IndexByte(term, cp); i >= 0 { |
| 295 | parenDepth++ |
| 296 | saveParen = cp |
| 297 | term[i] = 0 |
| 298 | } else if cp == saveParen { |
| 299 | parenDepth++ |
| 300 | } |
| 301 | case saveParen: |
| 302 | parenDepth-- |
| 303 | if parenDepth == 0 { |
| 304 | i := bytes.IndexByte(term, 0) |
| 305 | term[i] = saveParen |
| 306 | saveParen = 0 |
| 307 | } |
| 308 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 309 | i++ |
| 310 | } |
Fumitoshi Ukai | d8d8425 | 2015-04-19 17:30:13 +0900 | [diff] [blame] | 311 | buf = append(buf, in[b:i]...) |
| 312 | if len(buf) > 0 { |
| 313 | expr = append(expr, literal(string(buf))) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 314 | } |
| 315 | if i == len(in) && term != nil { |
| 316 | return expr, i, errEndOfInput |
| 317 | } |
| 318 | return compactExpr(expr), i, nil |
| 319 | } |
| 320 | |
| 321 | func closeParen(ch byte) byte { |
| 322 | switch ch { |
| 323 | case '(': |
| 324 | return ')' |
| 325 | case '{': |
| 326 | return '}' |
| 327 | } |
| 328 | return 0 |
| 329 | } |
| 330 | |
| 331 | // parseDollar parses |
| 332 | // $(func expr[, expr...]) # func = literal SP |
| 333 | // $(expr:expr=expr) |
| 334 | // $(expr) |
| 335 | // $x |
| 336 | // it returns parsed value and parsed length. |
| 337 | func parseDollar(in []byte) (Value, int, error) { |
| 338 | if len(in) <= 1 { |
| 339 | return nil, 0, errors.New("empty expr") |
| 340 | } |
| 341 | if in[0] != '$' { |
| 342 | return nil, 0, errors.New("should starts with $") |
| 343 | } |
| 344 | if in[1] == '$' { |
| 345 | return nil, 0, errors.New("should handle $$ as literal $") |
| 346 | } |
| 347 | paren := closeParen(in[1]) |
| 348 | if paren == 0 { |
| 349 | // $x case. |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 350 | if in[1] >= '0' && in[1] <= '9' { |
| 351 | return paramref(in[1] - '0'), 2, nil |
| 352 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 353 | return varref{varname: literal(string(in[1]))}, 2, nil |
| 354 | } |
| 355 | term := []byte{paren, ':', ' '} |
| 356 | var varname Expr |
| 357 | i := 2 |
| 358 | Again: |
| 359 | for { |
| 360 | e, n, err := parseExpr(in[i:], term) |
| 361 | if err != nil { |
| 362 | return nil, 0, err |
| 363 | } |
| 364 | varname = append(varname, e) |
| 365 | i += n |
| 366 | switch in[i] { |
| 367 | case paren: |
| 368 | // ${expr} |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 369 | vname := compactExpr(varname) |
| 370 | if vname, ok := vname.(literal); ok { |
| 371 | n, err := strconv.ParseInt(string(vname), 10, 64) |
| 372 | if err == nil { |
| 373 | // ${n} |
| 374 | return paramref(n), i + 1, nil |
| 375 | } |
| 376 | } |
| 377 | return varref{varname: vname}, i + 1, nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 378 | case ' ': |
| 379 | // ${e ...} |
| 380 | if token, ok := e.(literal); ok { |
Shinichiro Hamaji | 2216dd6 | 2015-04-11 13:44:39 +0900 | [diff] [blame] | 381 | funcName := string(token) |
| 382 | if f, ok := funcMap[funcName]; ok { |
| 383 | return parseFunc(f(), in, i+1, term[:1], funcName) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | term = term[:2] // drop ' ' |
| 387 | continue Again |
| 388 | case ':': |
| 389 | // ${varname:...} |
| 390 | term = term[:2] |
| 391 | term[1] = '=' // term={paren, '='}. |
| 392 | e, n, err := parseExpr(in[i+1:], term) |
| 393 | if err != nil { |
| 394 | return nil, 0, err |
| 395 | } |
| 396 | i += 1 + n |
| 397 | if in[i] == paren { |
| 398 | varname = append(varname, literal(string(":")), e) |
| 399 | return varref{varname: varname}, i + 1, nil |
| 400 | } |
| 401 | // ${varname:xx=...} |
| 402 | pat := e |
| 403 | subst, n, err := parseExpr(in[i+1:], term[:1]) |
| 404 | if err != nil { |
| 405 | return nil, 0, err |
| 406 | } |
| 407 | i += 1 + n |
| 408 | // ${first:pat=e} |
| 409 | return varsubst{ |
| 410 | varname: compactExpr(varname), |
| 411 | pat: pat, |
| 412 | subst: subst, |
| 413 | }, i + 1, nil |
| 414 | default: |
| 415 | panic(fmt.Sprintf("unexpected char")) |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // skipSpaces skips spaces at front of `in` before any bytes in term. |
| 421 | // in[n] will be the first non white space in in. |
| 422 | func skipSpaces(in, term []byte) int { |
| 423 | for i := 0; i < len(in); i++ { |
| 424 | if bytes.IndexByte(term, in[i]) >= 0 { |
| 425 | return i |
| 426 | } |
| 427 | switch in[i] { |
| 428 | case ' ', '\t': |
| 429 | default: |
| 430 | return i |
| 431 | } |
| 432 | } |
| 433 | return len(in) |
| 434 | } |
| 435 | |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 436 | // trimLiteralSpace trims literal space around v. |
| 437 | func trimLiteralSpace(v Value) Value { |
| 438 | switch v := v.(type) { |
| 439 | case literal: |
| 440 | return literal(strings.TrimSpace(string(v))) |
| 441 | case tmpval: |
| 442 | b := bytes.TrimSpace([]byte(v)) |
| 443 | if len(b) == 0 { |
| 444 | return literal("") |
| 445 | } |
| 446 | return tmpval(b) |
| 447 | case Expr: |
| 448 | if len(v) == 0 { |
| 449 | return v |
| 450 | } |
| 451 | switch s := v[0].(type) { |
| 452 | case literal, tmpval: |
| 453 | t := trimLiteralSpace(s) |
| 454 | if t == literal("") { |
| 455 | v = v[1:] |
| 456 | } else { |
| 457 | v[0] = t |
| 458 | } |
| 459 | } |
| 460 | switch s := v[len(v)-1].(type) { |
| 461 | case literal, tmpval: |
| 462 | t := trimLiteralSpace(s) |
| 463 | if t == literal("") { |
| 464 | v = v[:len(v)-1] |
| 465 | } else { |
| 466 | v[len(v)-1] = t |
| 467 | } |
| 468 | } |
| 469 | return compactExpr(v) |
| 470 | } |
| 471 | return v |
| 472 | } |
| 473 | |
| 474 | // concatLine concatinates line with "\\\n" in function expression. |
| 475 | func concatLine(v Value) Value { |
| 476 | switch v := v.(type) { |
| 477 | case literal: |
| 478 | for { |
| 479 | s := string(v) |
| 480 | i := strings.Index(s, "\\\n") |
| 481 | if i < 0 { |
| 482 | return v |
| 483 | } |
| 484 | v = literal(s[:i] + strings.TrimLeft(s[i+2:], " \t")) |
| 485 | } |
| 486 | case tmpval: |
| 487 | for { |
| 488 | b := []byte(v) |
| 489 | i := bytes.Index(b, []byte{'\\', '\n'}) |
| 490 | if i < 0 { |
| 491 | return v |
| 492 | } |
| 493 | var buf bytes.Buffer |
| 494 | buf.Write(b[:i]) |
| 495 | buf.Write(bytes.TrimLeft(b[i+2:], " \t")) |
Fumitoshi Ukai | d8d8425 | 2015-04-19 17:30:13 +0900 | [diff] [blame] | 496 | v = tmpval(buf.Bytes()) |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 497 | } |
| 498 | case Expr: |
| 499 | for i := range v { |
| 500 | switch vv := v[i].(type) { |
| 501 | case literal, tmpval: |
| 502 | v[i] = concatLine(vv) |
| 503 | } |
| 504 | } |
| 505 | return v |
| 506 | } |
| 507 | return v |
| 508 | } |
| 509 | |
Fumitoshi Ukai | ebf945c | 2015-04-10 17:30:04 +0900 | [diff] [blame] | 510 | // parseFunc parses function arguments from in[s:] for f. |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 511 | // in[0] is '$' and in[s] is space just after func name. |
Fumitoshi Ukai | ebf945c | 2015-04-10 17:30:04 +0900 | [diff] [blame] | 512 | // in[:n] will be "${func args...}" |
Shinichiro Hamaji | 2216dd6 | 2015-04-11 13:44:39 +0900 | [diff] [blame] | 513 | func parseFunc(f Func, in []byte, s int, term []byte, funcName string) (Value, int, error) { |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 514 | f.AddArg(literal(string(in[1 : s-1]))) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 515 | arity := f.Arity() |
| 516 | term = append(term, ',') |
Fumitoshi Ukai | ebf945c | 2015-04-10 17:30:04 +0900 | [diff] [blame] | 517 | i := skipSpaces(in[s:], term) |
| 518 | i = s + i |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 519 | if i == len(in) { |
| 520 | return f, i, nil |
| 521 | } |
| 522 | narg := 1 |
| 523 | for { |
| 524 | if arity != 0 && narg >= arity { |
| 525 | // final arguments. |
| 526 | term = term[:1] // drop ',' |
| 527 | } |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 528 | v, n, err := parseExpr(in[i:], term) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 529 | if err != nil { |
| 530 | return nil, 0, err |
| 531 | } |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 532 | v = concatLine(v) |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 533 | // TODO(ukai): do this in funcIf, funcAnd, or funcOr's compactor? |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 534 | if (narg == 1 && funcName == "if") || funcName == "and" || funcName == "or" { |
| 535 | v = trimLiteralSpace(v) |
| 536 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 537 | f.AddArg(v) |
| 538 | i += n |
| 539 | narg++ |
| 540 | if in[i] == term[0] { |
| 541 | i++ |
| 542 | break |
| 543 | } |
| 544 | i++ // should be ',' |
| 545 | if i == len(in) { |
| 546 | break |
| 547 | } |
| 548 | } |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 549 | var fv Value |
| 550 | fv = f |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 551 | if compactor, ok := f.(Compactor); ok { |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 552 | fv = compactor.Compact() |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 553 | } |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 554 | if katiStatsFlag { |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 555 | fv = funcstats{fv} |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 556 | } |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 557 | return fv, i, nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 558 | } |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 559 | |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 560 | type Compactor interface { |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 561 | Compact() Value |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 562 | } |
| 563 | |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 564 | type funcstats struct { |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 565 | Value |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | func (f funcstats) Eval(w io.Writer, ev *Evaluator) { |
| 569 | t := time.Now() |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 570 | f.Value.Eval(w, ev) |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 571 | // TODO(ukai): per functype? |
| 572 | addStats("func", f, t) |
| 573 | } |