[77] | 1 | # line directives |
---|
| 2 | # block directives |
---|
| 3 | # field lines |
---|
| 4 | # comments |
---|
| 5 | |
---|
[66] | 6 | { |
---|
| 7 | #$::RD_TRACE = 1; |
---|
[21] | 8 | my ( |
---|
[29] | 9 | @sections, # master data structure |
---|
| 10 | $section_head, |
---|
| 11 | $section_id, |
---|
| 12 | @lines, # lines in each section |
---|
[21] | 13 | %lists, |
---|
| 14 | %patterns, |
---|
[39] | 15 | %subs, # validation subs |
---|
[21] | 16 | @group, # current group |
---|
| 17 | %groups, # stored groups of fields |
---|
| 18 | $type, |
---|
| 19 | @options, |
---|
[24] | 20 | $required, |
---|
[21] | 21 | $list_var, |
---|
| 22 | $size, |
---|
[56] | 23 | $maxlength, |
---|
[21] | 24 | $rows, |
---|
| 25 | $cols, |
---|
[80] | 26 | @submit, |
---|
[21] | 27 | ); |
---|
[77] | 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 | } |
---|
[21] | 35 | } |
---|
[1] | 36 | |
---|
[77] | 37 | form_spec: |
---|
| 38 | { |
---|
| 39 | %formspec = (); # clear the old formspec data |
---|
| 40 | } |
---|
[80] | 41 | (list_def | description_def | group_def | note | unknown_block_directive | line)(s) |
---|
[1] | 42 | { |
---|
[29] | 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 | |
---|
[1] | 53 | $return = { |
---|
[77] | 54 | title => $formspec{title}, |
---|
| 55 | author => $formspec{author}, |
---|
| 56 | description => $formspec{description}, |
---|
[28] | 57 | lists => \%lists, |
---|
| 58 | patterns => \%patterns, |
---|
[39] | 59 | subs => \%subs, |
---|
[28] | 60 | groups => \%groups, |
---|
[29] | 61 | sections => \@sections, |
---|
[80] | 62 | ( @submit ? |
---|
| 63 | (submit => @submit == 1 ? $submit[0] : \@submit) : |
---|
| 64 | () |
---|
| 65 | ), |
---|
[81] | 66 | reset => $formspec{reset}, |
---|
[1] | 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
[16] | 70 | list_def: '!list' var_name (static_list | dynamic_list) |
---|
| 71 | { $lists{$item{var_name}} = [ @options ]; @options = () } |
---|
[1] | 72 | |
---|
[66] | 73 | static_list: '{' /\s*/ option(s /\s*,\s*/) /,?/ /\s*/ '}' |
---|
[10] | 74 | |
---|
| 75 | dynamic_list: '&' <perl_codeblock> |
---|
[77] | 76 | { warn "[Text::FormBuilder] Dynamic lists have been removed from the formspec grammar"; } |
---|
[10] | 77 | |
---|
[57] | 78 | description_def: '!description' block |
---|
[64] | 79 | { |
---|
[77] | 80 | warn "[Text::FormBuilder] Description redefined at input text line $thisline\n" if defined $formspec{description}; |
---|
| 81 | $formspec{description} = $item{block}; |
---|
[14] | 82 | } |
---|
| 83 | |
---|
[21] | 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 | } |
---|
[1] | 90 | |
---|
[64] | 91 | note: '!note' block { push @lines, [ 'note', $item{block} ]; } |
---|
[50] | 92 | |
---|
[64] | 93 | |
---|
[57] | 94 | # curly-brace delimited block, that can contain properly |
---|
[64] | 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 |
---|
[50] | 101 | |
---|
[64] | 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 |
---|
[57] | 109 | |
---|
| 110 | |
---|
[64] | 111 | # field lines are the subset of lines that are allowed in a !group directive |
---|
[21] | 112 | field_line: <skip:'[ \t]*'> ( field | comment | blank ) "\n" |
---|
[64] | 113 | |
---|
[81] | 114 | line: <skip:'[ \t]*'> ( title | author | pattern_def | section_head | heading | submit | reset | group_field | field_group | unknown_directive | field | comment | blank ) /\n+/ |
---|
[21] | 115 | |
---|
[1] | 116 | title: '!title' /.*/ |
---|
[28] | 117 | { |
---|
[77] | 118 | warn "[Text::FormBuilder] Title redefined at input text line $thisline\n" if defined $formspec{title}; |
---|
| 119 | $formspec{title} = $item[2]; |
---|
[28] | 120 | } |
---|
[1] | 121 | |
---|
| 122 | author: '!author' /.*/ |
---|
[28] | 123 | { |
---|
[77] | 124 | warn "[Text::FormBuilder] Author redefined at input text line $thisline\n" if defined $formspec{author}; |
---|
| 125 | $formspec{author} = $item[2]; |
---|
[28] | 126 | } |
---|
[1] | 127 | |
---|
[16] | 128 | pattern_def: '!pattern' var_name pattern |
---|
| 129 | { $patterns{$item{var_name}} = $item{pattern} } |
---|
[1] | 130 | |
---|
| 131 | pattern: /.*/ |
---|
| 132 | |
---|
[29] | 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 | |
---|
[28] | 152 | heading: '!head' /.*/ { push @lines, [ 'head', $item[2] ] } |
---|
[11] | 153 | |
---|
[80] | 154 | submit: '!submit' string(s /\s*,\s*/) |
---|
| 155 | { |
---|
| 156 | #warn scalar(@{ $item[2] }) . ' submit button(s)'; |
---|
| 157 | push @submit, @{ $item[2] }; |
---|
| 158 | } |
---|
| 159 | |
---|
[81] | 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 | |
---|
[21] | 166 | group_field: '!field' group_name name label(?) |
---|
[74] | 167 | { |
---|
[77] | 168 | warn "WARNING line $thisline: The '!field' directive has been DEPRECATED. Use the 'name:GROUP' style instead.\n"; |
---|
[28] | 169 | push @lines, [ 'group', { name => $item{name}, label => $item{'label(?)'}[0], group => $item{group_name} } ]; |
---|
[21] | 170 | } |
---|
| 171 | |
---|
| 172 | group_name: /%[A-Z_]+/ |
---|
| 173 | |
---|
[73] | 174 | field_group: name label(?) hint(?) group_type comment(?) |
---|
| 175 | { |
---|
[74] | 176 | #warn "[$thisline] comment = $item{'hint(?)'}[0]\n" if $item{'hint(?)'}[0]; |
---|
[63] | 177 | #warn "[$thisline] field $item{name} is $item{group_type}\n"; |
---|
[73] | 178 | push @lines, [ 'group', { |
---|
| 179 | name => $item{name}, |
---|
| 180 | label => $item{'label(?)'}[0], |
---|
| 181 | comment => $item{'hint(?)'}[0], |
---|
| 182 | group => $item{group_type}, |
---|
| 183 | } ]; |
---|
[63] | 184 | } |
---|
| 185 | |
---|
| 186 | group_type: ':' var_name |
---|
| 187 | |
---|
[64] | 188 | # this is the real heart of the thing |
---|
[66] | 189 | field: name field_size(?) growable(?) label(?) hint(?) type(?) other(?) default(?) option_list(?) validate(?) comment(?) |
---|
[1] | 190 | { |
---|
| 191 | my $field = { |
---|
| 192 | name => $item{name}, |
---|
[66] | 193 | growable => $item{'growable(?)'}[0], |
---|
[1] | 194 | label => $item{'label(?)'}[0], |
---|
| 195 | comment => $item{'hint(?)'}[0], |
---|
| 196 | type => $item{'type(?)'}[0], |
---|
[66] | 197 | other => $item{'other(?)'}[0], |
---|
[1] | 198 | value => $item{'default(?)'}[0], |
---|
[63] | 199 | list => $list_var, |
---|
| 200 | validate => $item{'validate(?)'}[0], |
---|
[68] | 201 | required => $required, |
---|
[1] | 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; |
---|
[56] | 209 | $$field{maxlength} = $maxlength if defined $maxlength; |
---|
[1] | 210 | |
---|
[28] | 211 | #warn "[$thisline] field $item{name}; context $context\n"; |
---|
[21] | 212 | if ($context eq 'group') { |
---|
| 213 | push @group, $field; |
---|
| 214 | } else { |
---|
| 215 | push @lines, [ 'field', $field ]; |
---|
| 216 | } |
---|
[1] | 217 | |
---|
| 218 | $type = undef; |
---|
[68] | 219 | $required = undef; |
---|
[1] | 220 | $list_var = undef; |
---|
| 221 | $size = undef; |
---|
| 222 | $rows = undef; |
---|
| 223 | $cols = undef; |
---|
[56] | 224 | $maxlength = undef; |
---|
[1] | 225 | @options = (); |
---|
[63] | 226 | |
---|
[62] | 227 | $field; |
---|
[1] | 228 | } |
---|
| 229 | |
---|
| 230 | name: identifier |
---|
| 231 | |
---|
[16] | 232 | var_name: /[A-Z_]+/ |
---|
| 233 | |
---|
[1] | 234 | field_size: '[' ( row_col | size ) ']' |
---|
| 235 | |
---|
[56] | 236 | size: /\d+/ bang(?) |
---|
| 237 | { $maxlength = $item[1] if $item[2][0]; $size = $item[1] } |
---|
[1] | 238 | |
---|
[56] | 239 | bang: '!' |
---|
| 240 | |
---|
[1] | 241 | row_col: /\d+/ /,\s*/ /\d+/ |
---|
| 242 | { $rows = $item[1]; $cols = $item[3] } |
---|
| 243 | |
---|
[66] | 244 | growable: '*' limit(?) { $item{'limit(?)'}[0] || 1 } |
---|
[61] | 245 | |
---|
[66] | 246 | limit: /\d+/ |
---|
| 247 | |
---|
[77] | 248 | label: '|' string { $item[2] } |
---|
[1] | 249 | |
---|
[64] | 250 | hint: bracket_block |
---|
[1] | 251 | |
---|
[63] | 252 | type: ':' builtin_field |
---|
[1] | 253 | |
---|
[55] | 254 | builtin_field: /textarea|text|password|file|checkbox|radio|select|hidden|static/ |
---|
| 255 | |
---|
[66] | 256 | other: '+' 'other' { 1 } |
---|
[55] | 257 | |
---|
[77] | 258 | default: '=' string { $item[2] } |
---|
[1] | 259 | |
---|
[77] | 260 | string: simple_multiword | quoted_string |
---|
| 261 | |
---|
[22] | 262 | # for simple multiword values not involving punctuation |
---|
[80] | 263 | simple_multiword: /\w/ <skip:''> /[\w\t ]*/ { $item[1] . $item[3] } |
---|
[22] | 264 | |
---|
| 265 | # my attempt at a single-quoted, non-interpolating string |
---|
| 266 | # where the backslash can escape literal single quotes |
---|
[80] | 267 | quoted_string: "'" <skip:''> /(\\'|[^'])*/ "'" |
---|
[22] | 268 | { $item[3] =~ s/\\'/'/g; $item[3] } |
---|
| 269 | |
---|
[1] | 270 | option_list: options | list_var |
---|
| 271 | |
---|
[77] | 272 | options: '{' option(s /\s*,\s*/) '}' |
---|
[1] | 273 | |
---|
| 274 | list_var: /@[A-Z_]+/ { $list_var = $item[1] } |
---|
| 275 | |
---|
[77] | 276 | option: string display_text(?) |
---|
[23] | 277 | { push @options, { $item[1] => $item{'display_text(?)'}[0] } } |
---|
[1] | 278 | |
---|
| 279 | value: identifier |
---|
| 280 | |
---|
[64] | 281 | display_text: bracket_block |
---|
[1] | 282 | |
---|
[59] | 283 | |
---|
[64] | 284 | validate: '//' (optional_pattern | required_pattern) |
---|
[59] | 285 | |
---|
[64] | 286 | optional_pattern: var_name '?' { $required = 0; $item[1] } |
---|
[1] | 287 | |
---|
[64] | 288 | required_pattern: var_name { $required = 1; $item[1] } |
---|
[24] | 289 | |
---|
[1] | 290 | comment: '#' /.*/ |
---|
| 291 | blank: |
---|
| 292 | |
---|
| 293 | identifier: /\w+/ |
---|
[16] | 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"; } |
---|
[80] | 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"; } |
---|