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