source: text-formbuilder/trunk/lib/Text/FormBuilder/grammar @ 40

Last change on this file since 40 was 39, checked in by peter, 20 years ago

added write_script function to write a complete CGI script using the form
abstracted some of the internal code generating and writing code to be easily reused by write_* functions
started work on allowing validation coderefs in the formspec

File size: 5.4 KB
RevLine 
[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]27form_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]54list_def: '!list' var_name (static_list | dynamic_list)
55    { $lists{$item{var_name}} = [ @options ]; @options = () }
[1]56
[10]57static_list: '{' option(s /,\s*/) /,?/ '}'
58
59dynamic_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]69description_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]76validate_def: '!validate' var_name <perl_codeblock>
77    { $subs{$item{var_name}} = $item[3] }
78
[21]79group_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]86field_line: <skip:'[ \t]*'> ( field | comment | blank ) "\n"
[29]87line: <skip:'[ \t]*'> ( title | author | pattern_def | section_head | heading | group_field | unknown_directive | field | comment | blank ) "\n"
[21]88
[1]89title: '!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
95author: '!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]101pattern_def: '!pattern' var_name pattern
102    { $patterns{$item{var_name}} = $item{pattern} }
[1]103
104pattern: /.*/
105
[29]106section_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]125heading: '!head' /.*/    { push @lines, [ 'head', $item[2] ] }
[11]126
[21]127group_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
132group_name: /%[A-Z_]+/
133
[1]134field: 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   
170name: identifier
171
[16]172var_name: /[A-Z_]+/
173
[1]174field_size: '[' ( row_col | size ) ']'
175
176size: /\d+/
177    { $size = $item[1] }
178
179row_col: /\d+/ /,\s*/ /\d+/
180    { $rows = $item[1]; $cols = $item[3] }
181
[22]182label: '|' (simple_multiword | quoted_string) { $item[2] }
[1]183
184hint: '[' /[^\]]+/ ']'    { $item[2] }
185
186type: ':' /textarea|text|password|file|checkbox|radio|select|hidden|static/
187
[22]188default: '=' (simple_multiword | quoted_string) { $item[2] }
[1]189
[22]190# for simple multiword values not involving punctuation
191simple_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
195quoted_string: <skip:''> "'" /(\\'|[^'])*/ "'"
196    { $item[3] =~ s/\\'/'/g; $item[3] }
197
[1]198option_list: options | list_var
199   
200options: '{' option(s /,\s*/) '}'
201
202list_var: /@[A-Z_]+/ { $list_var = $item[1] }
203
[25]204option: (simple_multiword | value | quoted_string) display_text(?)
[23]205    { push @options, { $item[1] => $item{'display_text(?)'}[0] } }
[1]206
207value: identifier
208
209display_text: '[' /[^\]]+/i ']'    { $item[2] }
210
[24]211validate: '//' (optional_pattern | required_pattern)    { $item[2] }
[1]212
[24]213optional_pattern: /[A-Z_]+/ '?' { $required = 0; $item[1] }
214
215required_pattern: /[A-Z_]+/ { $required = 1; $item[1] }
216
[1]217comment: '#' /.*/
218blank:
219
220identifier: /\w+/
[16]221
222# skip unknown directives with a warning
223unknown_directive: /\!\S*/ /.*/
224    { warn "[Text::Formbuilder] Skipping unknown directive '$item[1]' at input text line $thisline\n"; }
Note: See TracBrowser for help on using the repository browser.