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

Last change on this file since 86 was 84, checked in by peichman, 20 years ago

added support for the 'multiple' attribute on fields

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