| 142 | var sprintf_cache = Object.create(null) |
| 143 | |
| 144 | function sprintf_parse(fmt) { |
| 145 | if (sprintf_cache[fmt]) { |
| 146 | return sprintf_cache[fmt] |
| 147 | } |
| 148 | |
| 149 | var _fmt = fmt, match, parse_tree = [], arg_names = 0 |
| 150 | while (_fmt) { |
| 151 | if ((match = re.text.exec(_fmt)) !== null) { |
| 152 | parse_tree.push(match[0]) |
| 153 | } |
| 154 | else if ((match = re.modulo.exec(_fmt)) !== null) { |
| 155 | parse_tree.push('%') |
| 156 | } |
| 157 | else if ((match = re.placeholder.exec(_fmt)) !== null) { |
| 158 | if (match[2]) { |
| 159 | arg_names |= 1 |
| 160 | var field_list = [], replacement_field = match[2], field_match = [] |
| 161 | if ((field_match = re.key.exec(replacement_field)) !== null) { |
| 162 | field_list.push(field_match[1]) |
| 163 | while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') { |
| 164 | if ((field_match = re.key_access.exec(replacement_field)) !== null) { |
| 165 | field_list.push(field_match[1]) |
| 166 | } |
| 167 | else if ((field_match = re.index_access.exec(replacement_field)) !== null) { |
| 168 | field_list.push(field_match[1]) |
| 169 | } |
| 170 | else { |
| 171 | throw new SyntaxError('[sprintf] failed to parse named argument key') |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | else { |
| 176 | throw new SyntaxError('[sprintf] failed to parse named argument key') |
| 177 | } |
| 178 | match[2] = field_list |
| 179 | } |
| 180 | else { |
| 181 | arg_names |= 2 |
| 182 | } |
| 183 | if (arg_names === 3) { |
| 184 | throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported') |
| 185 | } |
| 186 | |
| 187 | parse_tree.push( |
| 188 | { |
| 189 | placeholder: match[0], |
| 190 | param_no: match[1], |
| 191 | keys: match[2], |
| 192 | sign: match[3], |
| 193 | pad_char: match[4], |
| 194 | align: match[5], |
| 195 | width: match[6], |
| 196 | precision: match[7], |
| 197 | type: match[8] |
| 198 | } |
| 199 | ) |
| 200 | } |
| 201 | else { |