aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty/cty/function/stdlib/format_fsm.rl
blob: 85d43bb77f9aec5d210c975b20a2886e57113871 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// This file is generated from format_fsm.rl. DO NOT EDIT.
%%{
	# (except you are actually in scan_tokens.rl here, so edit away!)
	machine formatfsm;
}%%

package stdlib

import (
	"bytes"
	"fmt"
	"unicode/utf8"

	"github.com/zclconf/go-cty/cty"
)

%%{
	write data;
}%%

func formatFSM(format string, a []cty.Value) (string, error) {
	var buf bytes.Buffer
	data := format
	nextArg := 1 // arg numbers are 1-based
	var verb formatVerb

	%%{

	action begin {
		verb = formatVerb{
			ArgNum: nextArg,
			Prec:   -1,
			Width:  -1,
		}
		ts = p
	}

	action emit {
		buf.WriteByte(fc);
	}

	action finish_ok {
	}

	action finish_err {
		return buf.String(), fmt.Errorf("invalid format string starting at offset %d", p)
	}

	action err_char {
		// We'll try to slurp a whole UTF-8 sequence here, to give the user
		// better feedback.
		r, _ := utf8.DecodeRuneInString(data[p:])
		return buf.String(), fmt.Errorf("unrecognized format character %q at offset %d", r, p)
	}

	action flag_sharp {
		verb.Sharp = true
	}
	action flag_zero {
		verb.Zero = true
	}
	action flag_minus {
		verb.Minus = true
	}
	action flag_plus {
		verb.Plus = true
	}
	action flag_space {
		verb.Space = true
	}

	action argidx_reset {
		verb.ArgNum = 0
	}
	action argidx_num {
		verb.ArgNum = (10 * verb.ArgNum) + (int(fc) - '0')
	}

	action has_width {
		verb.HasWidth = true
	}
	action width_reset {
		verb.Width = 0
	}
	action width_num {
		verb.Width = (10 * verb.Width) + (int(fc) - '0')
	}

	action has_prec {
		verb.HasPrec = true
	}
	action prec_reset {
		verb.Prec = 0
	}
	action prec_num {
		verb.Prec = (10 * verb.Prec) + (int(fc) - '0')
	}

	action mode {
		verb.Mode = rune(fc)
		te = p+1
		verb.Raw = data[ts:te]
		verb.Offset = ts

		err := formatAppend(&verb, &buf, a)
		if err != nil {
			return buf.String(), err
		}
		nextArg = verb.ArgNum + 1
	}

	# a number that isn't zero and doesn't have a leading zero
	num = [1-9] [0-9]*;

	flags = (
		'0' @flag_zero |
		'#' @flag_sharp |
		'-' @flag_minus |
		'+' @flag_plus |
		' ' @flag_space
	)*;

	argidx = ((
		'[' (num $argidx_num) ']'
	) >argidx_reset)?;

	width = (
		( num $width_num ) >width_reset %has_width
	)?;

	precision = (
		('.' ( digit* $prec_num )) >prec_reset %has_prec
	)?;

	# We accept any letter here, but will be more picky in formatAppend
	mode = ('a'..'z' | 'A'..'Z') @mode;

	fmt_verb = (
		'%' @begin
		flags
		width
		precision
		argidx
		mode
	);

	main := (
		[^%] @emit |
		'%%' @emit |
		fmt_verb
	)* @/finish_err %/finish_ok $!err_char;

	}%%

	// Ragel state
	p := 0  // "Pointer" into data
	pe := len(data) // End-of-data "pointer"
	cs := 0 // current state (will be initialized by ragel-generated code)
	ts := 0
	te := 0
	eof := pe

	// Keep Go compiler happy even if generated code doesn't use these
	_ = ts
	_ = te
	_ = eof

	%%{
		write init;
		write exec;
	}%%

	// If we fall out here without being in a final state then we've
	// encountered something that the scanner can't match, which should
	// be impossible (the scanner matches all bytes _somehow_) but we'll
	// flag it anyway rather than just losing data from the end.
	if cs < formatfsm_first_final {
		return buf.String(), fmt.Errorf("extraneous characters beginning at offset %i", p)
	}

	return buf.String(), nil
}