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