Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | package kati |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "io" |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 22 | "regexp" |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 23 | "strconv" |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 24 | "strings" |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 25 | |
| 26 | "github.com/golang/glog" |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
Fumitoshi Ukai | cba36c9 | 2015-07-07 16:04:18 +0900 | [diff] [blame] | 30 | errEndOfInput = errors.New("unexpected end of input") |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 31 | errNotLiteral = errors.New("valueNum: not literal") |
Fumitoshi Ukai | cdf55a0 | 2015-07-07 16:07:01 +0900 | [diff] [blame] | 32 | |
| 33 | errUnterminatedVariableReference = errors.New("*** unterminated variable reference.") |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 34 | ) |
| 35 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 36 | type evalWriter interface { |
| 37 | io.Writer |
| 38 | writeWord([]byte) |
| 39 | writeWordString(string) |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 40 | resetSep() |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 41 | } |
| 42 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 43 | // Value is an interface for value. |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 44 | type Value interface { |
| 45 | String() string |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 46 | Eval(w evalWriter, ev *Evaluator) error |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 47 | serialize() serializableVar |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 48 | dump(d *dumpbuf) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // literal is literal value. |
| 52 | type literal string |
| 53 | |
| 54 | func (s literal) String() string { return string(s) } |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 55 | func (s literal) Eval(w evalWriter, ev *Evaluator) error { |
Fumitoshi Ukai | 5541c7e | 2015-04-18 22:47:03 +0900 | [diff] [blame] | 56 | io.WriteString(w, string(s)) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 57 | return nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 58 | } |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 59 | func (s literal) serialize() serializableVar { |
| 60 | return serializableVar{Type: "literal", V: string(s)} |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 61 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 62 | func (s literal) dump(d *dumpbuf) { |
| 63 | d.Byte(valueTypeLiteral) |
| 64 | d.Bytes([]byte(s)) |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 65 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 66 | |
| 67 | // tmpval is temporary value. |
| 68 | type tmpval []byte |
| 69 | |
| 70 | func (t tmpval) String() string { return string(t) } |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 71 | func (t tmpval) Eval(w evalWriter, ev *Evaluator) error { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 72 | w.Write(t) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 73 | return nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 74 | } |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 75 | func (t tmpval) Value() []byte { return []byte(t) } |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 76 | func (t tmpval) serialize() serializableVar { |
| 77 | return serializableVar{Type: "tmpval", V: string(t)} |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 78 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 79 | func (t tmpval) dump(d *dumpbuf) { |
| 80 | d.Byte(valueTypeTmpval) |
| 81 | d.Bytes(t) |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 82 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 83 | |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 84 | // expr is a list of values. |
| 85 | type expr []Value |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 86 | |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 87 | func (e expr) String() string { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 88 | var s []string |
| 89 | for _, v := range e { |
| 90 | s = append(s, v.String()) |
| 91 | } |
| 92 | return strings.Join(s, "") |
| 93 | } |
| 94 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 95 | func (e expr) Eval(w evalWriter, ev *Evaluator) error { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 96 | for _, v := range e { |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 97 | w.resetSep() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 98 | err := v.Eval(w, ev) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 102 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 103 | return nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 106 | func (e expr) serialize() serializableVar { |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 107 | r := serializableVar{Type: "expr"} |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 108 | for _, v := range e { |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 109 | r.Children = append(r.Children, v.serialize()) |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 110 | } |
| 111 | return r |
| 112 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 113 | func (e expr) dump(d *dumpbuf) { |
| 114 | d.Byte(valueTypeExpr) |
| 115 | d.Int(len(e)) |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 116 | for _, v := range e { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 117 | v.dump(d) |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 118 | } |
| 119 | } |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 120 | |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 121 | func compactExpr(e expr) Value { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 122 | if len(e) == 1 { |
| 123 | return e[0] |
| 124 | } |
| 125 | // TODO(ukai): concat literal |
| 126 | return e |
| 127 | } |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 128 | func toExpr(v Value) expr { |
Fumitoshi Ukai | 201df42 | 2015-07-07 17:31:05 +0900 | [diff] [blame] | 129 | if v == nil { |
| 130 | return nil |
| 131 | } |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 132 | if e, ok := v.(expr); ok { |
| 133 | return e |
| 134 | } |
| 135 | return expr{v} |
| 136 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 137 | |
| 138 | // varref is variable reference. e.g. ${foo}. |
| 139 | type varref struct { |
| 140 | varname Value |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 141 | paren byte |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 142 | } |
| 143 | |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 144 | func (v *varref) String() string { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 145 | varname := v.varname.String() |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 146 | if len(varname) == 1 && v.paren == 0 { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 147 | return fmt.Sprintf("$%s", varname) |
| 148 | } |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 149 | paren := v.paren |
| 150 | if paren == 0 { |
| 151 | paren = '{' |
| 152 | } |
| 153 | return fmt.Sprintf("$%c%s%c", paren, varname, closeParen(paren)) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 154 | } |
| 155 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 156 | func (v *varref) Eval(w evalWriter, ev *Evaluator) error { |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 157 | te := traceEvent.begin("var", v, traceEventMain) |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 158 | buf := newEbuf() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 159 | err := v.varname.Eval(buf, ev) |
| 160 | if err != nil { |
| 161 | return err |
| 162 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 163 | vv := ev.LookupVar(buf.String()) |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 164 | buf.release() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 165 | err = vv.Eval(w, ev) |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 169 | traceEvent.end(te) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 170 | return nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 173 | func (v *varref) serialize() serializableVar { |
| 174 | return serializableVar{ |
Shinichiro Hamaji | 3d6d0aa | 2015-04-28 16:18:44 +0900 | [diff] [blame] | 175 | Type: "varref", |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 176 | V: string(v.paren), |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 177 | Children: []serializableVar{v.varname.serialize()}, |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 178 | } |
| 179 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 180 | func (v *varref) dump(d *dumpbuf) { |
| 181 | d.Byte(valueTypeVarref) |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 182 | d.Byte(v.paren) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 183 | v.varname.dump(d) |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 184 | } |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 185 | |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 186 | // paramref is parameter reference e.g. $1. |
| 187 | type paramref int |
| 188 | |
| 189 | func (p paramref) String() string { |
| 190 | return fmt.Sprintf("$%d", int(p)) |
| 191 | } |
| 192 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 193 | func (p paramref) Eval(w evalWriter, ev *Evaluator) error { |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 194 | te := traceEvent.begin("param", p, traceEventMain) |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 195 | n := int(p) |
| 196 | if n < len(ev.paramVars) { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 197 | err := ev.paramVars[n].Eval(w, ev) |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 201 | } else { |
Fumitoshi Ukai | e16c2b7 | 2015-06-19 00:15:59 +0900 | [diff] [blame] | 202 | vv := ev.LookupVar(fmt.Sprintf("%d", n)) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 203 | err := vv.Eval(w, ev) |
| 204 | if err != nil { |
| 205 | return err |
| 206 | } |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 207 | } |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 208 | traceEvent.end(te) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 209 | return nil |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 210 | } |
| 211 | |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 212 | func (p paramref) serialize() serializableVar { |
| 213 | return serializableVar{Type: "paramref", V: strconv.Itoa(int(p))} |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 214 | } |
| 215 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 216 | func (p paramref) dump(d *dumpbuf) { |
| 217 | d.Byte(valueTypeParamref) |
| 218 | d.Int(int(p)) |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 221 | // varsubst is variable substitutaion. e.g. ${var:pat=subst}. |
| 222 | type varsubst struct { |
| 223 | varname Value |
| 224 | pat Value |
| 225 | subst Value |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 226 | paren byte |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | func (v varsubst) String() string { |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 230 | paren := v.paren |
| 231 | if paren == 0 { |
| 232 | paren = '{' |
| 233 | } |
| 234 | return fmt.Sprintf("$%c%s:%s=%s%c", paren, v.varname, v.pat, v.subst, closeParen(paren)) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 235 | } |
| 236 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 237 | func (v varsubst) Eval(w evalWriter, ev *Evaluator) error { |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 238 | te := traceEvent.begin("varsubst", v, traceEventMain) |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 239 | buf := newEbuf() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 240 | params, err := ev.args(buf, v.varname, v.pat, v.subst) |
| 241 | if err != nil { |
| 242 | return err |
| 243 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 244 | vname := string(params[0]) |
| 245 | pat := string(params[1]) |
| 246 | subst := string(params[2]) |
| 247 | buf.Reset() |
| 248 | vv := ev.LookupVar(vname) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 249 | err = vv.Eval(buf, ev) |
| 250 | if err != nil { |
| 251 | return err |
| 252 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 253 | vals := splitSpaces(buf.String()) |
Fumitoshi Ukai | a4a0225 | 2015-07-09 14:25:18 +0900 | [diff] [blame] | 254 | buf.release() |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 255 | space := false |
| 256 | for _, val := range vals { |
| 257 | if space { |
Fumitoshi Ukai | 5541c7e | 2015-04-18 22:47:03 +0900 | [diff] [blame] | 258 | io.WriteString(w, " ") |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 259 | } |
Fumitoshi Ukai | b06cd9d | 2015-05-07 12:56:12 +0900 | [diff] [blame] | 260 | io.WriteString(w, substRef(pat, subst, val)) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 261 | space = true |
| 262 | } |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 263 | traceEvent.end(te) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 264 | return nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 265 | } |
| 266 | |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 267 | func (v varsubst) serialize() serializableVar { |
| 268 | return serializableVar{ |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 269 | Type: "varsubst", |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 270 | V: string(v.paren), |
Fumitoshi Ukai | a045ccb | 2015-06-25 12:57:25 +0900 | [diff] [blame] | 271 | Children: []serializableVar{ |
| 272 | v.varname.serialize(), |
| 273 | v.pat.serialize(), |
| 274 | v.subst.serialize(), |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 275 | }, |
| 276 | } |
| 277 | } |
| 278 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 279 | func (v varsubst) dump(d *dumpbuf) { |
| 280 | d.Byte(valueTypeVarsubst) |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 281 | d.Byte(v.paren) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 282 | v.varname.dump(d) |
| 283 | v.pat.dump(d) |
| 284 | v.subst.dump(d) |
Shinichiro Hamaji | 723f56a | 2015-05-15 17:12:55 +0900 | [diff] [blame] | 285 | } |
| 286 | |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 287 | func str(buf []byte, alloc bool) Value { |
| 288 | if alloc { |
| 289 | return literal(string(buf)) |
| 290 | } |
| 291 | return tmpval(buf) |
| 292 | } |
| 293 | |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 294 | func appendStr(exp expr, buf []byte, alloc bool) expr { |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 295 | if len(buf) == 0 { |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 296 | return exp |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 297 | } |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 298 | if len(exp) == 0 { |
| 299 | return append(exp, str(buf, alloc)) |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 300 | } |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 301 | switch v := exp[len(exp)-1].(type) { |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 302 | case literal: |
| 303 | v += literal(string(buf)) |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 304 | exp[len(exp)-1] = v |
| 305 | return exp |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 306 | case tmpval: |
| 307 | v = append(v, buf...) |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 308 | exp[len(exp)-1] = v |
| 309 | return exp |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 310 | } |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 311 | return append(exp, str(buf, alloc)) |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | func valueNum(v Value) (int, error) { |
| 315 | switch v := v.(type) { |
| 316 | case literal, tmpval: |
| 317 | n, err := strconv.ParseInt(v.String(), 10, 64) |
| 318 | return int(n), err |
| 319 | } |
| 320 | return 0, errNotLiteral |
| 321 | } |
| 322 | |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 323 | type parseOp struct { |
| 324 | // alloc indicates text will be allocated as literal (string) |
| 325 | alloc bool |
| 326 | |
| 327 | // matchParen matches parenthesis. |
| 328 | // note: required for func arg |
| 329 | matchParen bool |
| 330 | } |
| 331 | |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 332 | // parseExpr parses expression in `in` until it finds any byte in term. |
| 333 | // if term is nil, it will parse to end of input. |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 334 | // if term is not nil, and it reaches to end of input, return error. |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 335 | // it returns parsed value, and parsed length `n`, so in[n-1] is any byte of |
| 336 | // term, and in[n:] is next input. |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 337 | func parseExpr(in, term []byte, op parseOp) (Value, int, error) { |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 338 | var exp expr |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 339 | b := 0 |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 340 | i := 0 |
| 341 | var saveParen byte |
| 342 | parenDepth := 0 |
| 343 | Loop: |
| 344 | for i < len(in) { |
| 345 | ch := in[i] |
Fumitoshi Ukai | 96c79f1 | 2015-04-18 22:58:13 +0900 | [diff] [blame] | 346 | if term != nil && bytes.IndexByte(term, ch) >= 0 { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 347 | break Loop |
| 348 | } |
| 349 | switch ch { |
| 350 | case '$': |
| 351 | if i+1 >= len(in) { |
| 352 | break Loop |
| 353 | } |
| 354 | if in[i+1] == '$' { |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 355 | exp = appendStr(exp, in[b:i+1], op.alloc) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 356 | i += 2 |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 357 | b = i |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 358 | continue |
| 359 | } |
| 360 | if bytes.IndexByte(term, in[i+1]) >= 0 { |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 361 | exp = appendStr(exp, in[b:i], op.alloc) |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 362 | exp = append(exp, &varref{varname: literal("")}) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 363 | i++ |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 364 | b = i |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 365 | break Loop |
| 366 | } |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 367 | exp = appendStr(exp, in[b:i], op.alloc) |
| 368 | v, n, err := parseDollar(in[i:], op.alloc) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 369 | if err != nil { |
| 370 | return nil, 0, err |
| 371 | } |
| 372 | i += n |
Fumitoshi Ukai | 00178d1 | 2015-04-18 00:11:05 +0900 | [diff] [blame] | 373 | b = i |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 374 | exp = append(exp, v) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 375 | continue |
| 376 | case '(', '{': |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 377 | if !op.matchParen { |
| 378 | break |
| 379 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 380 | cp := closeParen(ch) |
| 381 | if i := bytes.IndexByte(term, cp); i >= 0 { |
| 382 | parenDepth++ |
| 383 | saveParen = cp |
| 384 | term[i] = 0 |
| 385 | } else if cp == saveParen { |
| 386 | parenDepth++ |
| 387 | } |
| 388 | case saveParen: |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 389 | if !op.matchParen { |
| 390 | break |
| 391 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 392 | parenDepth-- |
| 393 | if parenDepth == 0 { |
| 394 | i := bytes.IndexByte(term, 0) |
| 395 | term[i] = saveParen |
| 396 | saveParen = 0 |
| 397 | } |
| 398 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 399 | i++ |
| 400 | } |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 401 | exp = appendStr(exp, in[b:i], op.alloc) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 402 | if i == len(in) && term != nil { |
Fumitoshi Ukai | 6450d0f | 2015-07-10 16:34:06 +0900 | [diff] [blame] | 403 | glog.Warningf("parse: unexpected end of input: %q %d [%q]", in, i, term) |
Fumitoshi Ukai | cba36c9 | 2015-07-07 16:04:18 +0900 | [diff] [blame] | 404 | return exp, i, errEndOfInput |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 405 | } |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 406 | return compactExpr(exp), i, nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | func closeParen(ch byte) byte { |
| 410 | switch ch { |
| 411 | case '(': |
| 412 | return ')' |
| 413 | case '{': |
| 414 | return '}' |
| 415 | } |
| 416 | return 0 |
| 417 | } |
| 418 | |
| 419 | // parseDollar parses |
| 420 | // $(func expr[, expr...]) # func = literal SP |
| 421 | // $(expr:expr=expr) |
| 422 | // $(expr) |
| 423 | // $x |
| 424 | // it returns parsed value and parsed length. |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 425 | func parseDollar(in []byte, alloc bool) (Value, int, error) { |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 426 | if len(in) <= 1 { |
| 427 | return nil, 0, errors.New("empty expr") |
| 428 | } |
| 429 | if in[0] != '$' { |
| 430 | return nil, 0, errors.New("should starts with $") |
| 431 | } |
| 432 | if in[1] == '$' { |
| 433 | return nil, 0, errors.New("should handle $$ as literal $") |
| 434 | } |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 435 | oparen := in[1] |
| 436 | paren := closeParen(oparen) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 437 | if paren == 0 { |
| 438 | // $x case. |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 439 | if in[1] >= '0' && in[1] <= '9' { |
| 440 | return paramref(in[1] - '0'), 2, nil |
| 441 | } |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 442 | return &varref{varname: str(in[1:2], alloc)}, 2, nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 443 | } |
| 444 | term := []byte{paren, ':', ' '} |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 445 | var varname expr |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 446 | i := 2 |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 447 | op := parseOp{alloc: alloc} |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 448 | Again: |
| 449 | for { |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 450 | e, n, err := parseExpr(in[i:], term, op) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 451 | if err != nil { |
Fumitoshi Ukai | cdf55a0 | 2015-07-07 16:07:01 +0900 | [diff] [blame] | 452 | if err == errEndOfInput { |
Fumitoshi Ukai | f76eac8 | 2015-07-08 10:18:06 +0900 | [diff] [blame] | 453 | // unmatched_paren2.mk |
| 454 | varname = append(varname, toExpr(e)...) |
| 455 | if len(varname) > 0 { |
| 456 | for i, vn := range varname { |
| 457 | if vr, ok := vn.(*varref); ok { |
| 458 | if vr.paren == oparen { |
| 459 | varname = varname[:i+1] |
| 460 | varname[i] = expr{literal(fmt.Sprintf("$%c", oparen)), vr.varname} |
| 461 | return &varref{varname: varname, paren: oparen}, i + 1 + n + 1, nil |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
Fumitoshi Ukai | cdf55a0 | 2015-07-07 16:07:01 +0900 | [diff] [blame] | 466 | return nil, 0, errUnterminatedVariableReference |
| 467 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 468 | return nil, 0, err |
| 469 | } |
Fumitoshi Ukai | f76eac8 | 2015-07-08 10:18:06 +0900 | [diff] [blame] | 470 | varname = append(varname, toExpr(e)...) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 471 | i += n |
| 472 | switch in[i] { |
| 473 | case paren: |
| 474 | // ${expr} |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 475 | vname := compactExpr(varname) |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 476 | n, err := valueNum(vname) |
| 477 | if err == nil { |
| 478 | // ${n} |
| 479 | return paramref(n), i + 1, nil |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 480 | } |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 481 | return &varref{varname: vname, paren: oparen}, i + 1, nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 482 | case ' ': |
| 483 | // ${e ...} |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 484 | switch token := e.(type) { |
| 485 | case literal, tmpval: |
| 486 | funcName := intern(token.String()) |
Shinichiro Hamaji | 2216dd6 | 2015-04-11 13:44:39 +0900 | [diff] [blame] | 487 | if f, ok := funcMap[funcName]; ok { |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 488 | return parseFunc(f(), in, i+1, term[:1], funcName, op.alloc) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | term = term[:2] // drop ' ' |
| 492 | continue Again |
| 493 | case ':': |
| 494 | // ${varname:...} |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 495 | colon := in[i : i+1] |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 496 | var vterm []byte |
| 497 | vterm = append(vterm, term[:2]...) |
| 498 | vterm[1] = '=' // term={paren, '='}. |
| 499 | e, n, err := parseExpr(in[i+1:], vterm, op) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 500 | if err != nil { |
| 501 | return nil, 0, err |
| 502 | } |
| 503 | i += 1 + n |
| 504 | if in[i] == paren { |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 505 | varname = appendStr(varname, colon, op.alloc) |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 506 | return &varref{varname: varname, paren: oparen}, i + 1, nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 507 | } |
| 508 | // ${varname:xx=...} |
| 509 | pat := e |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 510 | subst, n, err := parseExpr(in[i+1:], term[:1], op) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 511 | if err != nil { |
| 512 | return nil, 0, err |
| 513 | } |
| 514 | i += 1 + n |
| 515 | // ${first:pat=e} |
| 516 | return varsubst{ |
| 517 | varname: compactExpr(varname), |
| 518 | pat: pat, |
| 519 | subst: subst, |
Fumitoshi Ukai | bbb0db5 | 2015-06-30 16:51:27 +0900 | [diff] [blame] | 520 | paren: oparen, |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 521 | }, i + 1, nil |
| 522 | default: |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 523 | return nil, 0, fmt.Errorf("unexpected char %c at %d in %q", in[i], i, string(in)) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // skipSpaces skips spaces at front of `in` before any bytes in term. |
| 529 | // in[n] will be the first non white space in in. |
| 530 | func skipSpaces(in, term []byte) int { |
| 531 | for i := 0; i < len(in); i++ { |
| 532 | if bytes.IndexByte(term, in[i]) >= 0 { |
| 533 | return i |
| 534 | } |
| 535 | switch in[i] { |
| 536 | case ' ', '\t': |
| 537 | default: |
| 538 | return i |
| 539 | } |
| 540 | } |
| 541 | return len(in) |
| 542 | } |
| 543 | |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 544 | // trimLiteralSpace trims literal space around v. |
| 545 | func trimLiteralSpace(v Value) Value { |
| 546 | switch v := v.(type) { |
| 547 | case literal: |
| 548 | return literal(strings.TrimSpace(string(v))) |
| 549 | case tmpval: |
| 550 | b := bytes.TrimSpace([]byte(v)) |
| 551 | if len(b) == 0 { |
| 552 | return literal("") |
| 553 | } |
| 554 | return tmpval(b) |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 555 | case expr: |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 556 | if len(v) == 0 { |
| 557 | return v |
| 558 | } |
| 559 | switch s := v[0].(type) { |
| 560 | case literal, tmpval: |
| 561 | t := trimLiteralSpace(s) |
| 562 | if t == literal("") { |
| 563 | v = v[1:] |
| 564 | } else { |
| 565 | v[0] = t |
| 566 | } |
| 567 | } |
| 568 | switch s := v[len(v)-1].(type) { |
| 569 | case literal, tmpval: |
| 570 | t := trimLiteralSpace(s) |
| 571 | if t == literal("") { |
| 572 | v = v[:len(v)-1] |
| 573 | } else { |
| 574 | v[len(v)-1] = t |
| 575 | } |
| 576 | } |
| 577 | return compactExpr(v) |
| 578 | } |
| 579 | return v |
| 580 | } |
| 581 | |
| 582 | // concatLine concatinates line with "\\\n" in function expression. |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 583 | // TODO(ukai): less alloc? |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 584 | func concatLine(v Value) Value { |
| 585 | switch v := v.(type) { |
| 586 | case literal: |
| 587 | for { |
| 588 | s := string(v) |
| 589 | i := strings.Index(s, "\\\n") |
| 590 | if i < 0 { |
| 591 | return v |
| 592 | } |
| 593 | v = literal(s[:i] + strings.TrimLeft(s[i+2:], " \t")) |
| 594 | } |
| 595 | case tmpval: |
| 596 | for { |
| 597 | b := []byte(v) |
| 598 | i := bytes.Index(b, []byte{'\\', '\n'}) |
| 599 | if i < 0 { |
| 600 | return v |
| 601 | } |
| 602 | var buf bytes.Buffer |
| 603 | buf.Write(b[:i]) |
| 604 | buf.Write(bytes.TrimLeft(b[i+2:], " \t")) |
Fumitoshi Ukai | d8d8425 | 2015-04-19 17:30:13 +0900 | [diff] [blame] | 605 | v = tmpval(buf.Bytes()) |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 606 | } |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 607 | case expr: |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 608 | for i := range v { |
| 609 | switch vv := v[i].(type) { |
| 610 | case literal, tmpval: |
| 611 | v[i] = concatLine(vv) |
| 612 | } |
| 613 | } |
| 614 | return v |
| 615 | } |
| 616 | return v |
| 617 | } |
| 618 | |
Fumitoshi Ukai | ebf945c | 2015-04-10 17:30:04 +0900 | [diff] [blame] | 619 | // parseFunc parses function arguments from in[s:] for f. |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 620 | // in[0] is '$' and in[s] is space just after func name. |
Fumitoshi Ukai | ebf945c | 2015-04-10 17:30:04 +0900 | [diff] [blame] | 621 | // in[:n] will be "${func args...}" |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 622 | func parseFunc(f mkFunc, in []byte, s int, term []byte, funcName string, alloc bool) (Value, int, error) { |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 623 | f.AddArg(str(in[1:s-1], alloc)) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 624 | arity := f.Arity() |
| 625 | term = append(term, ',') |
Fumitoshi Ukai | ebf945c | 2015-04-10 17:30:04 +0900 | [diff] [blame] | 626 | i := skipSpaces(in[s:], term) |
| 627 | i = s + i |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 628 | if i == len(in) { |
| 629 | return f, i, nil |
| 630 | } |
| 631 | narg := 1 |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 632 | op := parseOp{alloc: alloc, matchParen: true} |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 633 | for { |
| 634 | if arity != 0 && narg >= arity { |
| 635 | // final arguments. |
| 636 | term = term[:1] // drop ',' |
| 637 | } |
Fumitoshi Ukai | e9aa380 | 2015-07-03 11:33:23 +0900 | [diff] [blame] | 638 | v, n, err := parseExpr(in[i:], term, op) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 639 | if err != nil { |
Fumitoshi Ukai | cba36c9 | 2015-07-07 16:04:18 +0900 | [diff] [blame] | 640 | if err == errEndOfInput { |
| 641 | return nil, 0, fmt.Errorf("*** unterminated call to function `%s': missing `)'.", funcName) |
| 642 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 643 | return nil, 0, err |
| 644 | } |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 645 | v = concatLine(v) |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 646 | // TODO(ukai): do this in funcIf, funcAnd, or funcOr's compactor? |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 647 | if (narg == 1 && funcName == "if") || funcName == "and" || funcName == "or" { |
| 648 | v = trimLiteralSpace(v) |
| 649 | } |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 650 | f.AddArg(v) |
| 651 | i += n |
| 652 | narg++ |
| 653 | if in[i] == term[0] { |
| 654 | i++ |
| 655 | break |
| 656 | } |
| 657 | i++ // should be ',' |
| 658 | if i == len(in) { |
| 659 | break |
| 660 | } |
| 661 | } |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 662 | var fv Value |
| 663 | fv = f |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 664 | if compactor, ok := f.(compactor); ok { |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 665 | fv = compactor.Compact() |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 666 | } |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 667 | if EvalStatsFlag || traceEvent.enabled() { |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 668 | fv = funcstats{ |
| 669 | Value: fv, |
| 670 | str: fv.String(), |
| 671 | } |
| 672 | |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 673 | } |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 674 | return fv, i, nil |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 675 | } |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 676 | |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 677 | type compactor interface { |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 678 | Compact() Value |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 679 | } |
| 680 | |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 681 | type funcstats struct { |
Fumitoshi Ukai | 9f6b635 | 2015-04-16 16:25:09 +0900 | [diff] [blame] | 682 | Value |
Fumitoshi Ukai | f543f4d | 2015-06-15 15:21:47 +0900 | [diff] [blame] | 683 | str string |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 684 | } |
| 685 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 686 | func (f funcstats) Eval(w evalWriter, ev *Evaluator) error { |
Fumitoshi Ukai | 8341013 | 2015-06-15 14:50:07 +0900 | [diff] [blame] | 687 | te := traceEvent.begin("func", literal(f.str), traceEventMain) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 688 | err := f.Value.Eval(w, ev) |
| 689 | if err != nil { |
| 690 | return err |
| 691 | } |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 692 | // TODO(ukai): per functype? |
Fumitoshi Ukai | 432a242 | 2015-06-11 15:16:29 +0900 | [diff] [blame] | 693 | traceEvent.end(te) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 694 | return nil |
Fumitoshi Ukai | 6ac7f69 | 2015-04-15 17:13:51 +0900 | [diff] [blame] | 695 | } |
Fumitoshi Ukai | 4a70851 | 2015-06-11 17:15:49 +0900 | [diff] [blame] | 696 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 697 | type matcherValue struct{} |
Fumitoshi Ukai | 4a70851 | 2015-06-11 17:15:49 +0900 | [diff] [blame] | 698 | |
Fumitoshi Ukai | b44b12d | 2015-07-07 14:19:32 +0900 | [diff] [blame] | 699 | func (m matcherValue) Eval(w evalWriter, ev *Evaluator) error { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 700 | return fmt.Errorf("couldn't eval matcher") |
| 701 | } |
| 702 | func (m matcherValue) serialize() serializableVar { |
| 703 | return serializableVar{Type: ""} |
| 704 | } |
| 705 | |
| 706 | func (m matcherValue) dump(d *dumpbuf) { |
| 707 | d.err = fmt.Errorf("couldn't dump matcher") |
| 708 | } |
| 709 | |
| 710 | type matchVarref struct{ matcherValue } |
| 711 | |
| 712 | func (m matchVarref) String() string { return "$(match-any)" } |
Fumitoshi Ukai | 4a70851 | 2015-06-11 17:15:49 +0900 | [diff] [blame] | 713 | |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 714 | type literalRE struct { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 715 | matcherValue |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 716 | *regexp.Regexp |
| 717 | } |
| 718 | |
| 719 | func mustLiteralRE(s string) literalRE { |
| 720 | return literalRE{ |
| 721 | Regexp: regexp.MustCompile(s), |
| 722 | } |
| 723 | } |
| 724 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 725 | func (r literalRE) String() string { return r.Regexp.String() } |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 726 | |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 727 | func matchValue(exp, pat Value) bool { |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 728 | switch pat := pat.(type) { |
| 729 | case literal: |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 730 | return literal(exp.String()) == pat |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 731 | } |
| 732 | // TODO: other type match? |
| 733 | return false |
| 734 | } |
| 735 | |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 736 | func matchExpr(exp, pat expr) ([]Value, bool) { |
| 737 | if len(exp) != len(pat) { |
Fumitoshi Ukai | 4a70851 | 2015-06-11 17:15:49 +0900 | [diff] [blame] | 738 | return nil, false |
| 739 | } |
| 740 | var mv matchVarref |
| 741 | var matches []Value |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 742 | for i := range exp { |
Fumitoshi Ukai | 4a70851 | 2015-06-11 17:15:49 +0900 | [diff] [blame] | 743 | if pat[i] == mv { |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 744 | switch exp[i].(type) { |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 745 | case paramref, *varref: |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 746 | matches = append(matches, exp[i]) |
Fumitoshi Ukai | 4a70851 | 2015-06-11 17:15:49 +0900 | [diff] [blame] | 747 | continue |
| 748 | } |
| 749 | return nil, false |
| 750 | } |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 751 | if patre, ok := pat[i].(literalRE); ok { |
| 752 | re := patre.Regexp |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 753 | m := re.FindStringSubmatch(exp[i].String()) |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 754 | if m == nil { |
| 755 | return nil, false |
| 756 | } |
| 757 | for _, sm := range m[1:] { |
| 758 | matches = append(matches, literal(sm)) |
| 759 | } |
| 760 | continue |
| 761 | } |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 762 | if !matchValue(exp[i], pat[i]) { |
Fumitoshi Ukai | 4a70851 | 2015-06-11 17:15:49 +0900 | [diff] [blame] | 763 | return nil, false |
| 764 | } |
| 765 | } |
| 766 | return matches, true |
| 767 | } |