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