]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/zclconf/go-cty/cty/function/stdlib/format_fsm.rl
deps: github.com/hashicorp/terraform@sdk-v0.11-with-go-modules
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / zclconf / go-cty / cty / function / stdlib / format_fsm.rl
1 // This file is generated from format_fsm.rl. DO NOT EDIT.
2 %%{
3 # (except you are actually in scan_tokens.rl here, so edit away!)
4 machine formatfsm;
5 }%%
6
7 package stdlib
8
9 import (
10 "bytes"
11 "fmt"
12 "unicode/utf8"
13
14 "github.com/zclconf/go-cty/cty"
15 )
16
17 %%{
18 write data;
19 }%%
20
21 func formatFSM(format string, a []cty.Value) (string, error) {
22 var buf bytes.Buffer
23 data := format
24 nextArg := 1 // arg numbers are 1-based
25 var verb formatVerb
26
27 %%{
28
29 action begin {
30 verb = formatVerb{
31 ArgNum: nextArg,
32 Prec: -1,
33 Width: -1,
34 }
35 ts = p
36 }
37
38 action emit {
39 buf.WriteByte(fc);
40 }
41
42 action finish_ok {
43 }
44
45 action finish_err {
46 return buf.String(), fmt.Errorf("invalid format string starting at offset %d", p)
47 }
48
49 action err_char {
50 // We'll try to slurp a whole UTF-8 sequence here, to give the user
51 // better feedback.
52 r, _ := utf8.DecodeRuneInString(data[p:])
53 return buf.String(), fmt.Errorf("unrecognized format character %q at offset %d", r, p)
54 }
55
56 action flag_sharp {
57 verb.Sharp = true
58 }
59 action flag_zero {
60 verb.Zero = true
61 }
62 action flag_minus {
63 verb.Minus = true
64 }
65 action flag_plus {
66 verb.Plus = true
67 }
68 action flag_space {
69 verb.Space = true
70 }
71
72 action argidx_reset {
73 verb.ArgNum = 0
74 }
75 action argidx_num {
76 verb.ArgNum = (10 * verb.ArgNum) + (int(fc) - '0')
77 }
78
79 action has_width {
80 verb.HasWidth = true
81 }
82 action width_reset {
83 verb.Width = 0
84 }
85 action width_num {
86 verb.Width = (10 * verb.Width) + (int(fc) - '0')
87 }
88
89 action has_prec {
90 verb.HasPrec = true
91 }
92 action prec_reset {
93 verb.Prec = 0
94 }
95 action prec_num {
96 verb.Prec = (10 * verb.Prec) + (int(fc) - '0')
97 }
98
99 action mode {
100 verb.Mode = rune(fc)
101 te = p+1
102 verb.Raw = data[ts:te]
103 verb.Offset = ts
104
105 err := formatAppend(&verb, &buf, a)
106 if err != nil {
107 return buf.String(), err
108 }
109 nextArg = verb.ArgNum + 1
110 }
111
112 # a number that isn't zero and doesn't have a leading zero
113 num = [1-9] [0-9]*;
114
115 flags = (
116 '0' @flag_zero |
117 '#' @flag_sharp |
118 '-' @flag_minus |
119 '+' @flag_plus |
120 ' ' @flag_space
121 )*;
122
123 argidx = ((
124 '[' (num $argidx_num) ']'
125 ) >argidx_reset)?;
126
127 width = (
128 ( num $width_num ) >width_reset %has_width
129 )?;
130
131 precision = (
132 ('.' ( digit* $prec_num )) >prec_reset %has_prec
133 )?;
134
135 # We accept any letter here, but will be more picky in formatAppend
136 mode = ('a'..'z' | 'A'..'Z') @mode;
137
138 fmt_verb = (
139 '%' @begin
140 flags
141 width
142 precision
143 argidx
144 mode
145 );
146
147 main := (
148 [^%] @emit |
149 '%%' @emit |
150 fmt_verb
151 )* @/finish_err %/finish_ok $!err_char;
152
153 }%%
154
155 // Ragel state
156 p := 0 // "Pointer" into data
157 pe := len(data) // End-of-data "pointer"
158 cs := 0 // current state (will be initialized by ragel-generated code)
159 ts := 0
160 te := 0
161 eof := pe
162
163 // Keep Go compiler happy even if generated code doesn't use these
164 _ = ts
165 _ = te
166 _ = eof
167
168 %%{
169 write init;
170 write exec;
171 }%%
172
173 // If we fall out here without being in a final state then we've
174 // encountered something that the scanner can't match, which should
175 // be impossible (the scanner matches all bytes _somehow_) but we'll
176 // flag it anyway rather than just losing data from the end.
177 if cs < formatfsm_first_final {
178 return buf.String(), fmt.Errorf("extraneous characters beginning at offset %i", p)
179 }
180
181 return buf.String(), nil
182 }