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

Last change on this file was 88, checked in by peichman, 19 years ago

BUGFIX: stopped crosstalk of data from one object to another (references were getting shared in the parser grammar code)

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