| 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 |         $rows, | 
|---|
| 22 |         $cols, | 
|---|
| 23 |     ); | 
|---|
| 24 |     $context = 'line'; | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | form_spec: (list_def | description_def | validate_def | group_def | line)(s) | 
|---|
| 28 |     { | 
|---|
| 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 = (); | 
|---|
| 42 |         $return = { | 
|---|
| 43 |             title    => $title, | 
|---|
| 44 |             author   => $author, | 
|---|
| 45 |             description => $description, | 
|---|
| 46 |             lists    => \%lists, | 
|---|
| 47 |             patterns => \%patterns, | 
|---|
| 48 |             subs     => \%subs, | 
|---|
| 49 |             groups   => \%groups, | 
|---|
| 50 |             sections => \@sections, | 
|---|
| 51 |         } | 
|---|
| 52 |     } | 
|---|
| 53 |  | 
|---|
| 54 | list_def: '!list' var_name (static_list | dynamic_list) | 
|---|
| 55 |     { $lists{$item{var_name}} = [ @options ]; @options = () } | 
|---|
| 56 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 76 | validate_def: '!validate' var_name <perl_codeblock> | 
|---|
| 77 |     { $subs{$item{var_name}} = $item[3] } | 
|---|
| 78 |  | 
|---|
| 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 |     } | 
|---|
| 85 |  | 
|---|
| 86 | field_line: <skip:'[ \t]*'> ( field | comment | blank ) "\n" | 
|---|
| 87 | line: <skip:'[ \t]*'> ( title | author | pattern_def | section_head | heading | group_field | unknown_directive | field | comment | blank ) "\n" | 
|---|
| 88 |  | 
|---|
| 89 | title: '!title' /.*/ | 
|---|
| 90 |     { | 
|---|
| 91 |         warn "[Text::Formbuilder] Title redefined at input text line $thisline\n" if defined $title; | 
|---|
| 92 |         $title = $item[2]; | 
|---|
| 93 |     } | 
|---|
| 94 |  | 
|---|
| 95 | author: '!author' /.*/ | 
|---|
| 96 |     { | 
|---|
| 97 |         warn "[Text::Formbuilder] Author redefined at input text line $thisline\n" if defined $author; | 
|---|
| 98 |         $author = $item[2]; | 
|---|
| 99 |     } | 
|---|
| 100 |  | 
|---|
| 101 | pattern_def: '!pattern' var_name pattern | 
|---|
| 102 |     { $patterns{$item{var_name}} = $item{pattern} } | 
|---|
| 103 |  | 
|---|
| 104 | pattern: /.*/ | 
|---|
| 105 |  | 
|---|
| 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 |  | 
|---|
| 125 | heading: '!head' /.*/    { push @lines, [ 'head', $item[2] ] } | 
|---|
| 126 |  | 
|---|
| 127 | group_field: '!field' group_name name label(?) | 
|---|
| 128 |     {  | 
|---|
| 129 |         push @lines, [ 'group', { name => $item{name}, label => $item{'label(?)'}[0], group => $item{group_name} } ]; | 
|---|
| 130 |     } | 
|---|
| 131 |  | 
|---|
| 132 | group_name: /%[A-Z_]+/ | 
|---|
| 133 |  | 
|---|
| 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], | 
|---|
| 144 |             required => $required || 0, | 
|---|
| 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 |          | 
|---|
| 153 |         #warn "[$thisline] field $item{name}; context $context\n"; | 
|---|
| 154 |         if ($context eq 'group') { | 
|---|
| 155 |             push @group, $field; | 
|---|
| 156 |         } else { | 
|---|
| 157 |             push @lines, [ 'field', $field ]; | 
|---|
| 158 |         } | 
|---|
| 159 |          | 
|---|
| 160 |         $type = undef; | 
|---|
| 161 |         $required = 0; | 
|---|
| 162 |         $list_var = undef; | 
|---|
| 163 |         $size = undef; | 
|---|
| 164 |         $rows = undef; | 
|---|
| 165 |         $cols = undef; | 
|---|
| 166 |         @options = (); | 
|---|
| 167 |          | 
|---|
| 168 |     } | 
|---|
| 169 |      | 
|---|
| 170 | name: identifier | 
|---|
| 171 |  | 
|---|
| 172 | var_name: /[A-Z_]+/ | 
|---|
| 173 |  | 
|---|
| 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 |  | 
|---|
| 182 | label: '|' (simple_multiword | quoted_string) { $item[2] } | 
|---|
| 183 |  | 
|---|
| 184 | hint: '[' /[^\]]+/ ']'    { $item[2] } | 
|---|
| 185 |  | 
|---|
| 186 | type: ':' /textarea|text|password|file|checkbox|radio|select|hidden|static/ | 
|---|
| 187 |  | 
|---|
| 188 | default: '=' (simple_multiword | quoted_string) { $item[2] } | 
|---|
| 189 |  | 
|---|
| 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 |  | 
|---|
| 198 | option_list: options | list_var | 
|---|
| 199 |      | 
|---|
| 200 | options: '{' option(s /,\s*/) '}' | 
|---|
| 201 |  | 
|---|
| 202 | list_var: /@[A-Z_]+/ { $list_var = $item[1] } | 
|---|
| 203 |  | 
|---|
| 204 | option: (simple_multiword | value | quoted_string) display_text(?) | 
|---|
| 205 |     { push @options, { $item[1] => $item{'display_text(?)'}[0] } } | 
|---|
| 206 |  | 
|---|
| 207 | value: identifier | 
|---|
| 208 |  | 
|---|
| 209 | display_text: '[' /[^\]]+/i ']'    { $item[2] } | 
|---|
| 210 |  | 
|---|
| 211 | validate: '//' (optional_pattern | required_pattern)    { $item[2] } | 
|---|
| 212 |  | 
|---|
| 213 | optional_pattern: /[A-Z_]+/ '?' { $required = 0; $item[1] } | 
|---|
| 214 |  | 
|---|
| 215 | required_pattern: /[A-Z_]+/ { $required = 1; $item[1] } | 
|---|
| 216 |  | 
|---|
| 217 | comment: '#' /.*/ | 
|---|
| 218 | blank: | 
|---|
| 219 |  | 
|---|
| 220 | identifier: /\w+/ | 
|---|
| 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"; } | 
|---|