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