1 // This file is generated from format_fsm.rl. DO NOT EDIT.
3 # (except you are actually in scan_tokens.rl here, so edit away!)
14 "github.com/zclconf/go-cty/cty"
15 "github.com/zclconf/go-cty/cty/function"
22 func formatFSM(format string, a []cty.Value) (string, error) {
25 nextArg := 1 // arg numbers are 1-based
27 highestArgIdx := 0 // zero means "none", since arg numbers are 1-based
48 return buf.String(), fmt.Errorf("invalid format string starting at offset %d", p)
52 // We'll try to slurp a whole UTF-8 sequence here, to give the user
54 r, _ := utf8.DecodeRuneInString(data[p:])
55 return buf.String(), fmt.Errorf("unrecognized format character %q at offset %d", r, p)
78 verb.ArgNum = (10 * verb.ArgNum) + (int(fc) - '0')
88 verb.Width = (10 * verb.Width) + (int(fc) - '0')
98 verb.Prec = (10 * verb.Prec) + (int(fc) - '0')
104 verb.Raw = data[ts:te]
107 if verb.ArgNum > highestArgIdx {
108 highestArgIdx = verb.ArgNum
111 err := formatAppend(&verb, &buf, a)
113 return buf.String(), err
115 nextArg = verb.ArgNum + 1
118 # a number that isn't zero and doesn't have a leading zero
130 '[' (num $argidx_num) ']'
134 ( num $width_num ) >width_reset %has_width
138 ('.' ( digit* $prec_num )) >prec_reset %has_prec
141 # We accept any letter here, but will be more picky in formatAppend
142 mode = ('a'..'z' | 'A'..'Z') @mode;
157 )* @/finish_err %/finish_ok $!err_char;
162 p := 0 // "Pointer" into data
163 pe := len(data) // End-of-data "pointer"
164 cs := 0 // current state (will be initialized by ragel-generated code)
169 // Keep Go compiler happy even if generated code doesn't use these
179 // If we fall out here without being in a final state then we've
180 // encountered something that the scanner can't match, which should
181 // be impossible (the scanner matches all bytes _somehow_) but we'll
182 // flag it anyway rather than just losing data from the end.
183 if cs < formatfsm_first_final {
184 return buf.String(), fmt.Errorf("extraneous characters beginning at offset %d", p)
187 if highestArgIdx < len(a) {
188 // Extraneous args are an error, to more easily detect mistakes
189 firstBad := highestArgIdx+1
190 if highestArgIdx == 0 {
191 // Custom error message for this case
192 return buf.String(), function.NewArgErrorf(firstBad, "too many arguments; no verbs in format string")
194 return buf.String(), function.NewArgErrorf(firstBad, "too many arguments; only %d used by format string", highestArgIdx)
197 return buf.String(), nil