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