[21] | 1 | { |
---|
| 2 | my ( |
---|
| 3 | $context, # line or group |
---|
[29] | 4 | @sections, # master data structure |
---|
| 5 | $section_head, |
---|
| 6 | $section_id, |
---|
| 7 | @lines, # lines in each section |
---|
[21] | 8 | $title, |
---|
| 9 | $author, |
---|
| 10 | $description, |
---|
| 11 | %lists, |
---|
| 12 | %patterns, |
---|
[39] | 13 | %subs, # validation subs |
---|
[21] | 14 | @group, # current group |
---|
| 15 | %groups, # stored groups of fields |
---|
| 16 | $type, |
---|
| 17 | @options, |
---|
[24] | 18 | $required, |
---|
[21] | 19 | $list_var, |
---|
| 20 | $size, |
---|
| 21 | $rows, |
---|
| 22 | $cols, |
---|
| 23 | ); |
---|
| 24 | $context = 'line'; |
---|
| 25 | } |
---|
[1] | 26 | |
---|
[39] | 27 | form_spec: (list_def | description_def | validate_def | group_def | line)(s) |
---|
[1] | 28 | { |
---|
[29] | 29 | # grab the last section, if there is any |
---|
| 30 | if (@lines) { |
---|
| 31 | push @sections, |
---|
| 32 | { |
---|
| 33 | id => $section_id, |
---|
| 34 | head => $section_head, |
---|
| 35 | lines => [ @lines ], |
---|
| 36 | }; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | $section_id = $item{identifier}; |
---|
| 40 | $section_head = $item[3]; |
---|
| 41 | @lines = (); |
---|
[1] | 42 | $return = { |
---|
| 43 | title => $title, |
---|
| 44 | author => $author, |
---|
[14] | 45 | description => $description, |
---|
[28] | 46 | lists => \%lists, |
---|
| 47 | patterns => \%patterns, |
---|
[39] | 48 | subs => \%subs, |
---|
[28] | 49 | groups => \%groups, |
---|
[29] | 50 | sections => \@sections, |
---|
[1] | 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
[16] | 54 | list_def: '!list' var_name (static_list | dynamic_list) |
---|
| 55 | { $lists{$item{var_name}} = [ @options ]; @options = () } |
---|
[1] | 56 | |
---|
[10] | 57 | static_list: '{' option(s /,\s*/) /,?/ '}' |
---|
| 58 | |
---|
| 59 | dynamic_list: '&' <perl_codeblock> |
---|
| 60 | { |
---|
| 61 | my @results = (eval $item[2]); |
---|
| 62 | if (ref $results[0] eq 'HASH') { |
---|
| 63 | @options = @results; |
---|
| 64 | } else { |
---|
| 65 | @options = map { { $_ => $_ } } @results; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
[14] | 69 | description_def: '!description' <perl_codeblock> |
---|
| 70 | { warn "[Text::FormBuilder] Description redefined at input text line $thisline\n" if defined $description; |
---|
| 71 | |
---|
| 72 | $description = $item[2]; |
---|
| 73 | $description =~ s/^{\s*|\s*}$//g; |
---|
| 74 | } |
---|
| 75 | |
---|
[39] | 76 | validate_def: '!validate' var_name <perl_codeblock> |
---|
[42] | 77 | { $subs{$item{var_name}} = eval "sub $item[3]" } |
---|
[39] | 78 | |
---|
[21] | 79 | group_def: '!group' { $context = 'group' } var_name '{' field_line(s) '}' { $context = 'line' } |
---|
| 80 | { |
---|
| 81 | #warn "$item{var_name} group; context $context\n" |
---|
| 82 | $groups{$item{var_name}} = [ @group ]; |
---|
| 83 | @group = (); |
---|
| 84 | } |
---|
[1] | 85 | |
---|
[21] | 86 | field_line: <skip:'[ \t]*'> ( field | comment | blank ) "\n" |
---|
[29] | 87 | line: <skip:'[ \t]*'> ( title | author | pattern_def | section_head | heading | group_field | unknown_directive | field | comment | blank ) "\n" |
---|
[21] | 88 | |
---|
[1] | 89 | title: '!title' /.*/ |
---|
[28] | 90 | { |
---|
| 91 | warn "[Text::Formbuilder] Title redefined at input text line $thisline\n" if defined $title; |
---|
| 92 | $title = $item[2]; |
---|
| 93 | } |
---|
[1] | 94 | |
---|
| 95 | author: '!author' /.*/ |
---|
[28] | 96 | { |
---|
| 97 | warn "[Text::Formbuilder] Author redefined at input text line $thisline\n" if defined $author; |
---|
| 98 | $author = $item[2]; |
---|
| 99 | } |
---|
[1] | 100 | |
---|
[16] | 101 | pattern_def: '!pattern' var_name pattern |
---|
| 102 | { $patterns{$item{var_name}} = $item{pattern} } |
---|
[1] | 103 | |
---|
| 104 | pattern: /.*/ |
---|
| 105 | |
---|
[29] | 106 | section_head: '!section' identifier /.*/ |
---|
| 107 | { |
---|
| 108 | #warn "starting section $item{identifier}\n"; |
---|
| 109 | #warn " with heading $item[3]\n" if $item[3]; |
---|
| 110 | |
---|
| 111 | if (@lines) { |
---|
| 112 | push @sections, |
---|
| 113 | { |
---|
| 114 | id => $section_id, |
---|
| 115 | head => $section_head, |
---|
| 116 | lines => [ @lines ], |
---|
| 117 | }; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | $section_id = $item{identifier}; |
---|
| 121 | $section_head = $item[3]; |
---|
| 122 | @lines = (); |
---|
| 123 | } |
---|
| 124 | |
---|
[28] | 125 | heading: '!head' /.*/ { push @lines, [ 'head', $item[2] ] } |
---|
[11] | 126 | |
---|
[21] | 127 | group_field: '!field' group_name name label(?) |
---|
[28] | 128 | { |
---|
| 129 | push @lines, [ 'group', { name => $item{name}, label => $item{'label(?)'}[0], group => $item{group_name} } ]; |
---|
[21] | 130 | } |
---|
| 131 | |
---|
| 132 | group_name: /%[A-Z_]+/ |
---|
| 133 | |
---|
[1] | 134 | field: name field_size(?) label(?) hint(?) type(?) default(?) option_list(?) validate(?) |
---|
| 135 | { |
---|
| 136 | my $field = { |
---|
| 137 | name => $item{name}, |
---|
| 138 | label => $item{'label(?)'}[0], |
---|
| 139 | comment => $item{'hint(?)'}[0], |
---|
| 140 | type => $item{'type(?)'}[0], |
---|
| 141 | value => $item{'default(?)'}[0], |
---|
| 142 | list => $list_var, |
---|
| 143 | validate => $item{'validate(?)'}[0], |
---|
[24] | 144 | required => $required || 0, |
---|
[1] | 145 | }; |
---|
| 146 | |
---|
| 147 | $$field{options} = [ @options ] if @options; |
---|
| 148 | |
---|
| 149 | $$field{rows} = $rows if defined $rows; |
---|
| 150 | $$field{cols} = $cols if defined $cols; |
---|
| 151 | $$field{size} = $size if defined $size; |
---|
| 152 | |
---|
[28] | 153 | #warn "[$thisline] field $item{name}; context $context\n"; |
---|
[21] | 154 | if ($context eq 'group') { |
---|
| 155 | push @group, $field; |
---|
| 156 | } else { |
---|
| 157 | push @lines, [ 'field', $field ]; |
---|
| 158 | } |
---|
[1] | 159 | |
---|
| 160 | $type = undef; |
---|
[24] | 161 | $required = 0; |
---|
[1] | 162 | $list_var = undef; |
---|
| 163 | $size = undef; |
---|
| 164 | $rows = undef; |
---|
| 165 | $cols = undef; |
---|
| 166 | @options = (); |
---|
| 167 | |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | name: identifier |
---|
| 171 | |
---|
[16] | 172 | var_name: /[A-Z_]+/ |
---|
| 173 | |
---|
[1] | 174 | field_size: '[' ( row_col | size ) ']' |
---|
| 175 | |
---|
| 176 | size: /\d+/ |
---|
| 177 | { $size = $item[1] } |
---|
| 178 | |
---|
| 179 | row_col: /\d+/ /,\s*/ /\d+/ |
---|
| 180 | { $rows = $item[1]; $cols = $item[3] } |
---|
| 181 | |
---|
[22] | 182 | label: '|' (simple_multiword | quoted_string) { $item[2] } |
---|
[1] | 183 | |
---|
| 184 | hint: '[' /[^\]]+/ ']' { $item[2] } |
---|
| 185 | |
---|
| 186 | type: ':' /textarea|text|password|file|checkbox|radio|select|hidden|static/ |
---|
| 187 | |
---|
[22] | 188 | default: '=' (simple_multiword | quoted_string) { $item[2] } |
---|
[1] | 189 | |
---|
[22] | 190 | # for simple multiword values not involving punctuation |
---|
| 191 | simple_multiword: <skip:''> /[\w\t ]+/ { $item[2] } |
---|
| 192 | |
---|
| 193 | # my attempt at a single-quoted, non-interpolating string |
---|
| 194 | # where the backslash can escape literal single quotes |
---|
| 195 | quoted_string: <skip:''> "'" /(\\'|[^'])*/ "'" |
---|
| 196 | { $item[3] =~ s/\\'/'/g; $item[3] } |
---|
| 197 | |
---|
[1] | 198 | option_list: options | list_var |
---|
| 199 | |
---|
| 200 | options: '{' option(s /,\s*/) '}' |
---|
| 201 | |
---|
| 202 | list_var: /@[A-Z_]+/ { $list_var = $item[1] } |
---|
| 203 | |
---|
[25] | 204 | option: (simple_multiword | value | quoted_string) display_text(?) |
---|
[23] | 205 | { push @options, { $item[1] => $item{'display_text(?)'}[0] } } |
---|
[1] | 206 | |
---|
| 207 | value: identifier |
---|
| 208 | |
---|
| 209 | display_text: '[' /[^\]]+/i ']' { $item[2] } |
---|
| 210 | |
---|
[24] | 211 | validate: '//' (optional_pattern | required_pattern) { $item[2] } |
---|
[1] | 212 | |
---|
[24] | 213 | optional_pattern: /[A-Z_]+/ '?' { $required = 0; $item[1] } |
---|
| 214 | |
---|
| 215 | required_pattern: /[A-Z_]+/ { $required = 1; $item[1] } |
---|
| 216 | |
---|
[1] | 217 | comment: '#' /.*/ |
---|
| 218 | blank: |
---|
| 219 | |
---|
| 220 | identifier: /\w+/ |
---|
[16] | 221 | |
---|
| 222 | # skip unknown directives with a warning |
---|
| 223 | unknown_directive: /\!\S*/ /.*/ |
---|
| 224 | { warn "[Text::Formbuilder] Skipping unknown directive '$item[1]' at input text line $thisline\n"; } |
---|