1 | # line directives |
---|
2 | # block directives |
---|
3 | # field lines |
---|
4 | # comments |
---|
5 | |
---|
6 | { |
---|
7 | #$::RD_TRACE = 1; |
---|
8 | my ( |
---|
9 | @sections, # master data structure |
---|
10 | $section_head, |
---|
11 | $section_id, |
---|
12 | @lines, # lines in each section |
---|
13 | %lists, |
---|
14 | %patterns, |
---|
15 | %subs, # validation subs |
---|
16 | @group, # current group |
---|
17 | %groups, # stored groups of fields |
---|
18 | $type, |
---|
19 | @options, |
---|
20 | $required, |
---|
21 | $multiple, |
---|
22 | $list_var, |
---|
23 | $size, |
---|
24 | $maxlength, |
---|
25 | $rows, |
---|
26 | $cols, |
---|
27 | @submit, |
---|
28 | ); |
---|
29 | my $context = 'line'; # start in line context by default |
---|
30 | my %formspec; |
---|
31 | |
---|
32 | # TODO: helper sub? |
---|
33 | sub alert ($) { |
---|
34 | warn '[' . (split(/::/, (caller(1))[3]))[-1] . '] ' . shift() . "\n"; |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | form_spec: |
---|
39 | { |
---|
40 | # clear out old data, so we don't end up with old data in new objects |
---|
41 | @lines = (); |
---|
42 | @sections = (); |
---|
43 | %formspec = (); |
---|
44 | } |
---|
45 | (list_def | description_def | group_def | note | fb_params | unknown_block_directive | line)(s) |
---|
46 | { |
---|
47 | # grab the last section, if there is any |
---|
48 | if (@lines) { |
---|
49 | push @sections, |
---|
50 | { |
---|
51 | id => $section_id, |
---|
52 | head => $section_head, |
---|
53 | lines => [ @lines ], |
---|
54 | }; |
---|
55 | } |
---|
56 | |
---|
57 | # make copies instead of taking references, again so we |
---|
58 | # don't end up with connections between objects |
---|
59 | $return = { |
---|
60 | fb_params => $formspec{fb_params}, |
---|
61 | title => $formspec{title}, |
---|
62 | author => $formspec{author}, |
---|
63 | description => $formspec{description}, |
---|
64 | lists => { %lists }, |
---|
65 | patterns => { %patterns }, |
---|
66 | subs => { %subs }, |
---|
67 | groups => { %groups }, |
---|
68 | sections => [ @sections ], |
---|
69 | ( @submit ? (submit => @submit == 1 ? $submit[0] : [ @submit ]) : () ), |
---|
70 | reset => $formspec{reset}, |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | list_def: '!list' var_name (static_list | dynamic_list) |
---|
75 | { $lists{$item{var_name}} = [ @options ]; @options = () } |
---|
76 | |
---|
77 | static_list: '{' /\s*/ option(s /\s*,\s*/) /,?/ /\s*/ '}' |
---|
78 | |
---|
79 | dynamic_list: '&' <perl_codeblock> |
---|
80 | { warn "[Text::FormBuilder] Dynamic lists have been removed from the formspec grammar"; } |
---|
81 | |
---|
82 | description_def: '!description' block |
---|
83 | { |
---|
84 | warn "[Text::FormBuilder] Description redefined at input text line $thisline\n" if defined $formspec{description}; |
---|
85 | $formspec{description} = $item{block}; |
---|
86 | } |
---|
87 | |
---|
88 | group_def: '!group' { $context = 'group' } var_name '{' field_line(s) '}' { $context = 'line' } |
---|
89 | { |
---|
90 | #warn "$item{var_name} group; context $context\n" |
---|
91 | $groups{$item{var_name}} = [ @group ]; |
---|
92 | @group = (); |
---|
93 | } |
---|
94 | |
---|
95 | note: '!note' block { push @lines, [ 'note', $item{block} ]; } |
---|
96 | |
---|
97 | |
---|
98 | # curly-brace delimited block, that can contain properly |
---|
99 | # nested curly braces, along with any other characters |
---|
100 | # inner blocks return with the '{...}' so that nested |
---|
101 | # blocks get the braces treated as literals |
---|
102 | block: '{' <skip:''> block_content(s) '}' { join('', @{ $item[3] }) } |
---|
103 | inner_block: '{' <skip:''> block_content(s) '}' { '{' . join('', @{ $item[3] }) . '}' } |
---|
104 | block_content: /[^\{\}]+?/ | inner_block |
---|
105 | |
---|
106 | # square brace delimited block, that can contain properly |
---|
107 | # nested square brackets, along with any other characters |
---|
108 | # inner bracket blocks return with the '[...]' so that nested |
---|
109 | # blocks get the braces treated as literals |
---|
110 | bracket_block: '[' <skip:''> bracket_block_content(s) ']' { join('', @{ $item[3] }) } |
---|
111 | inner_bracket_block: '[' <skip:''> bracket_block_content(s) ']' { '[' . join('', @{ $item[3] }) . ']'; } |
---|
112 | bracket_block_content: /[^\[\]]+?/ | inner_bracket_block |
---|
113 | |
---|
114 | |
---|
115 | # field lines are the subset of lines that are allowed in a !group directive |
---|
116 | field_line: <skip:'[ \t]*'> ( field | comment | blank ) "\n" |
---|
117 | |
---|
118 | line: <skip:'[ \t]*'> ( title | author | pattern_def | section_head | heading | submit | reset | group_field | field_group | unknown_directive | field | comment | blank ) /\n+/ |
---|
119 | |
---|
120 | title: '!title' /.*/ |
---|
121 | { |
---|
122 | warn "[Text::FormBuilder] Title redefined at input text line $thisline\n" if defined $formspec{title}; |
---|
123 | $formspec{title} = $item[2]; |
---|
124 | } |
---|
125 | |
---|
126 | author: '!author' /.*/ |
---|
127 | { |
---|
128 | warn "[Text::FormBuilder] Author redefined at input text line $thisline\n" if defined $formspec{author}; |
---|
129 | $formspec{author} = $item[2]; |
---|
130 | } |
---|
131 | |
---|
132 | pattern_def: '!pattern' var_name pattern |
---|
133 | { $patterns{$item{var_name}} = $item{pattern} } |
---|
134 | |
---|
135 | pattern: /.*/ |
---|
136 | |
---|
137 | section_head: '!section' identifier /.*/ |
---|
138 | { |
---|
139 | #warn "starting section $item{identifier}\n"; |
---|
140 | #warn " with heading $item[3]\n" if $item[3]; |
---|
141 | |
---|
142 | if (@lines) { |
---|
143 | push @sections, |
---|
144 | { |
---|
145 | id => $section_id, |
---|
146 | head => $section_head, |
---|
147 | lines => [ @lines ], |
---|
148 | }; |
---|
149 | } |
---|
150 | |
---|
151 | $section_id = $item{identifier}; |
---|
152 | $section_head = $item[3]; |
---|
153 | @lines = (); |
---|
154 | } |
---|
155 | |
---|
156 | heading: '!head' /.*/ { push @lines, [ 'head', $item[2] ] } |
---|
157 | |
---|
158 | submit: '!submit' string(s /\s*,\s*/) |
---|
159 | { |
---|
160 | #warn scalar(@{ $item[2] }) . ' submit button(s)'; |
---|
161 | push @submit, @{ $item[2] }; |
---|
162 | } |
---|
163 | |
---|
164 | reset: '!reset' string |
---|
165 | { |
---|
166 | warn "[Text::FormBuilder] Reset button redefined at input text line $thisline\n" if defined $formspec{reset}; |
---|
167 | $formspec{reset} = $item{string}; |
---|
168 | } |
---|
169 | |
---|
170 | group_field: '!field' group_name name label(?) |
---|
171 | { |
---|
172 | warn "WARNING line $thisline: The '!field' directive has been DEPRECATED. Use the 'name:GROUP' style instead.\n"; |
---|
173 | push @lines, [ 'group', { name => $item{name}, label => $item{'label(?)'}[0], group => $item{group_name} } ]; |
---|
174 | } |
---|
175 | |
---|
176 | group_name: /%[A-Z_]+/ |
---|
177 | |
---|
178 | # parameters that get passed to the FB constructor; these are serialized in YAML |
---|
179 | fb_params: '!fb' block |
---|
180 | { $formspec{fb_params} .= $item{block}; } |
---|
181 | |
---|
182 | field_group: name label(?) hint(?) group_type comment(?) |
---|
183 | { |
---|
184 | #warn "[$thisline] comment = $item{'hint(?)'}[0]\n" if $item{'hint(?)'}[0]; |
---|
185 | #warn "[$thisline] field $item{name} is $item{group_type}\n"; |
---|
186 | push @lines, [ 'group', { |
---|
187 | name => $item{name}, |
---|
188 | label => $item{'label(?)'}[0], |
---|
189 | comment => $item{'hint(?)'}[0], |
---|
190 | group => $item{group_type}, |
---|
191 | } ]; |
---|
192 | } |
---|
193 | |
---|
194 | group_type: ':' var_name |
---|
195 | |
---|
196 | # this is the real heart of the thing |
---|
197 | field: name field_size(?) growable(?) label(?) hint(?) type(?) multi(?) other(?) default(?) option_list(?) validate(?) comment(?) |
---|
198 | { |
---|
199 | my $field = { |
---|
200 | name => $item{name}, |
---|
201 | growable => $item{'growable(?)'}[0], |
---|
202 | label => $item{'label(?)'}[0], |
---|
203 | comment => $item{'hint(?)'}[0], |
---|
204 | multiple => $item{'multi(?)'}[0], |
---|
205 | type => $item{'type(?)'}[0], |
---|
206 | other => $item{'other(?)'}[0], |
---|
207 | value => $item{'default(?)'}[0], |
---|
208 | list => $list_var, |
---|
209 | validate => $item{'validate(?)'}[0], |
---|
210 | required => $required, |
---|
211 | }; |
---|
212 | |
---|
213 | $$field{options} = [ @options ] if @options; |
---|
214 | |
---|
215 | $$field{rows} = $rows if defined $rows; |
---|
216 | $$field{cols} = $cols if defined $cols; |
---|
217 | $$field{size} = $size if defined $size; |
---|
218 | $$field{maxlength} = $maxlength if defined $maxlength; |
---|
219 | |
---|
220 | #warn "[$thisline] field $item{name}; context $context\n"; |
---|
221 | if ($context eq 'group') { |
---|
222 | push @group, $field; |
---|
223 | } else { |
---|
224 | push @lines, [ 'field', $field ]; |
---|
225 | } |
---|
226 | |
---|
227 | $type = undef; |
---|
228 | $required = undef; |
---|
229 | $multiple = undef; |
---|
230 | $list_var = undef; |
---|
231 | $size = undef; |
---|
232 | $rows = undef; |
---|
233 | $cols = undef; |
---|
234 | $maxlength = undef; |
---|
235 | @options = (); |
---|
236 | |
---|
237 | #warn "$$field{name}: $field"; |
---|
238 | |
---|
239 | $field; |
---|
240 | } |
---|
241 | |
---|
242 | name: identifier |
---|
243 | |
---|
244 | var_name: /[A-Z_]+/ |
---|
245 | |
---|
246 | field_size: '[' ( row_col | size ) ']' |
---|
247 | |
---|
248 | size: /\d+/ bang(?) |
---|
249 | { $maxlength = $item[1] if $item[2][0]; $size = $item[1] } |
---|
250 | |
---|
251 | bang: '!' |
---|
252 | |
---|
253 | row_col: /\d+/ /,\s*/ /\d+/ |
---|
254 | { $rows = $item[1]; $cols = $item[3] } |
---|
255 | |
---|
256 | growable: '*' limit(?) { $item{'limit(?)'}[0] || 1 } |
---|
257 | |
---|
258 | limit: /\d+/ |
---|
259 | |
---|
260 | label: '|' string { $item[2] } |
---|
261 | |
---|
262 | hint: bracket_block |
---|
263 | |
---|
264 | type: ':' builtin_field |
---|
265 | |
---|
266 | builtin_field: /textarea|text|password|file|checkbox|radio|select|hidden|static/ |
---|
267 | |
---|
268 | multi: '*' { 1 } |
---|
269 | |
---|
270 | other: '+' 'other' { 1 } |
---|
271 | |
---|
272 | default: '=' string { $item[2] } |
---|
273 | |
---|
274 | string: simple_multiword | quoted_string |
---|
275 | |
---|
276 | # for simple multiword values not involving punctuation |
---|
277 | simple_multiword: /\w/ <skip:''> /[\w\t ]*/ { $item[1] . $item[3] } |
---|
278 | |
---|
279 | # my attempt at a single-quoted, non-interpolating string |
---|
280 | # where the backslash can escape literal single quotes |
---|
281 | quoted_string: "'" <skip:''> /(\\'|[^'])*/ "'" |
---|
282 | { $item[3] =~ s/\\'/'/g; $item[3] } |
---|
283 | |
---|
284 | option_list: options | list_var |
---|
285 | |
---|
286 | options: '{' option(s /\s*,\s*/) '}' |
---|
287 | |
---|
288 | list_var: /@[A-Z_]+/ { $list_var = $item[1] } |
---|
289 | |
---|
290 | option: string display_text(?) |
---|
291 | { push @options, { $item[1] => $item{'display_text(?)'}[0] } } |
---|
292 | |
---|
293 | value: identifier |
---|
294 | |
---|
295 | display_text: bracket_block |
---|
296 | |
---|
297 | |
---|
298 | validate: '//' (optional_pattern | required_pattern) |
---|
299 | |
---|
300 | optional_pattern: var_name '?' { $required = 0; $item[1] } |
---|
301 | |
---|
302 | required_pattern: var_name { $required = 1; $item[1] } |
---|
303 | |
---|
304 | comment: '#' /.*/ |
---|
305 | blank: |
---|
306 | |
---|
307 | identifier: /\w+/ |
---|
308 | |
---|
309 | # skip unknown directives with a warning |
---|
310 | unknown_directive: /\!\S*/ /.*/ |
---|
311 | { warn "[Text::Formbuilder] Skipping unknown directive '$item[1]' at input text line $thisline\n"; } |
---|
312 | |
---|
313 | unknown_block_directive: /\!\S*/ var_name(?) block |
---|
314 | { warn "[Text::Formbuilder] Skipping unknown block directive '$item[1]' at input text line $thisline\n"; } |
---|