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

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

single-line textfields can be given a maxlength

File size: 5.9 KB
Line 
1{
2    my (
3        $context,      # line or group
4        @sections,     # master data structure
5        $section_head,
6        $section_id,
7        @lines,        # lines in each section
8        $title,
9        $author,
10        $description,
11        %lists,
12        %patterns,
13        %subs,         # validation subs
14        @group,        # current group
15        %groups,       # stored groups of fields
16        $type,
17        @options,
18        $required,
19        $list_var,
20        $size,
21        $maxlength,
22        $rows,
23        $cols,
24    );
25    $context = 'line';
26}
27
28form_spec: (list_def | description_def | validate_def | group_def | note | line)(s)
29    {
30        # grab the last section, if there is any
31        if (@lines) {
32            push @sections,
33                {
34                    id   => $section_id,
35                    head => $section_head,
36                    lines => [ @lines ],
37                };
38        }
39       
40        $section_id = $item{identifier};
41        $section_head = $item[3];
42        @lines = ();
43        $return = {
44            title    => $title,
45            author   => $author,
46            description => $description,
47            lists    => \%lists,
48            patterns => \%patterns,
49            subs     => \%subs,
50            groups   => \%groups,
51            sections => \@sections,
52        }
53    }
54
55list_def: '!list' var_name (static_list | dynamic_list)
56    { $lists{$item{var_name}} = [ @options ]; @options = () }
57
58static_list: '{' option(s /,\s*/) /,?/ '}'
59
60dynamic_list: '&' <perl_codeblock>
61    {
62        my @results = (eval $item[2]);
63        if (ref $results[0] eq 'HASH') {
64            @options = @results;
65        } else {   
66            @options = map { { $_ => $_ } } @results;
67        }
68    }
69
70description_def: '!description' <perl_codeblock>
71    { warn "[Text::FormBuilder] Description redefined at input text line $thisline\n" if defined $description;
72   
73    $description = $item[2];
74    $description =~ s/^{\s*|\s*}$//g;
75    }
76
77validate_def: '!validate' var_name <perl_codeblock>
78    { $subs{$item{var_name}} = eval "sub $item[3]" }
79
80group_def: '!group' { $context = 'group' } var_name '{' field_line(s) '}' { $context = 'line' }
81    {
82        #warn "$item{var_name} group; context $context\n"
83        $groups{$item{var_name}} = [ @group ];
84        @group = ();
85    }
86
87note: '!note' <perl_codeblock>
88    {   
89        (my $note = $item[2]) =~ s/^{\s*|\s*}$//g;
90        push @lines, [ 'note', $note ];
91    }
92
93
94field_line: <skip:'[ \t]*'> ( field | comment | blank ) "\n"
95line: <skip:'[ \t]*'> ( title | author | pattern_def | section_head | heading | group_field | unknown_directive | field | comment | blank ) "\n"
96
97title: '!title' /.*/
98    {
99        warn "[Text::Formbuilder] Title redefined at input text line $thisline\n" if defined $title;
100        $title = $item[2];
101    }
102
103author: '!author' /.*/
104    {
105        warn "[Text::Formbuilder] Author redefined at input text line $thisline\n" if defined $author;
106        $author = $item[2];
107    }
108
109pattern_def: '!pattern' var_name pattern
110    { $patterns{$item{var_name}} = $item{pattern} }
111
112pattern: /.*/
113
114section_head: '!section' identifier /.*/
115    {
116        #warn "starting section $item{identifier}\n";
117        #warn "  with heading $item[3]\n" if $item[3];
118       
119        if (@lines) {
120            push @sections,
121                {
122                    id   => $section_id,
123                    head => $section_head,
124                    lines => [ @lines ],
125                };
126        }
127       
128        $section_id = $item{identifier};
129        $section_head = $item[3];
130        @lines = ();
131    }
132
133heading: '!head' /.*/    { push @lines, [ 'head', $item[2] ] }
134
135group_field: '!field' group_name name label(?)
136    {
137        push @lines, [ 'group', { name => $item{name}, label => $item{'label(?)'}[0], group => $item{group_name} } ];
138    }
139
140group_name: /%[A-Z_]+/
141
142field: name field_size(?) label(?) hint(?) type(?) default(?) option_list(?) validate(?)
143    {
144        my $field = {
145            name     => $item{name},
146            label    => $item{'label(?)'}[0],
147            comment  => $item{'hint(?)'}[0],
148            type     => $item{'type(?)'}[0],
149            value    => $item{'default(?)'}[0],
150            list     => $list_var,
151            validate => $item{'validate(?)'}[0],
152            required => $required || 0,
153        };
154       
155        $$field{options} = [ @options ] if @options;
156       
157        $$field{rows} = $rows if defined $rows;
158        $$field{cols} = $cols if defined $cols;
159        $$field{size} = $size if defined $size;
160        $$field{maxlength} = $maxlength if defined $maxlength;
161       
162        #warn "[$thisline] field $item{name}; context $context\n";
163        if ($context eq 'group') {
164            push @group, $field;
165        } else {
166            push @lines, [ 'field', $field ];
167        }
168       
169        $type = undef;
170        $required = 0;
171        $list_var = undef;
172        $size = undef;
173        $rows = undef;
174        $cols = undef;
175        $maxlength = undef;
176        @options = ();
177       
178    }
179   
180name: identifier
181
182var_name: /[A-Z_]+/
183
184field_size: '[' ( row_col | size ) ']'
185
186size: /\d+/ bang(?)
187    { $maxlength = $item[1] if $item[2][0]; $size = $item[1] }
188
189bang: '!'
190
191row_col: /\d+/ /,\s*/ /\d+/
192    { $rows = $item[1]; $cols = $item[3] }
193
194label: '|' (simple_multiword | quoted_string) { $item[2] }
195
196hint: '[' /[^\]]+/ ']'    { $item[2] }
197
198# TODO: differentiate between builtins and custom field groups
199type: ':' ( builtin_field | var_name { warn "[Text::FormBuilder] Using field group names directly is not (yet) supported (input line $thisline)\n"; 'text' } )
200
201builtin_field: /textarea|text|password|file|checkbox|radio|select|hidden|static/
202
203
204default: '=' (simple_multiword | quoted_string) { $item[2] }
205
206# for simple multiword values not involving punctuation
207simple_multiword: <skip:''> /[\w\t ]+/ { $item[2] }
208
209# my attempt at a single-quoted, non-interpolating string
210# where the backslash can escape literal single quotes
211quoted_string: <skip:''> "'" /(\\'|[^'])*/ "'"
212    { $item[3] =~ s/\\'/'/g; $item[3] }
213
214option_list: options | list_var
215   
216options: '{' option(s /,\s*/) '}'
217
218list_var: /@[A-Z_]+/ { $list_var = $item[1] }
219
220option: (simple_multiword | value | quoted_string) display_text(?)
221    { push @options, { $item[1] => $item{'display_text(?)'}[0] } }
222
223value: identifier
224
225display_text: '[' /[^\]]+/i ']'    { $item[2] }
226
227validate: '//' (optional_pattern | required_pattern)    { $item[2] }
228
229optional_pattern: /[A-Z_]+/ '?' { $required = 0; $item[1] }
230
231required_pattern: /[A-Z_]+/ { $required = 1; $item[1] }
232
233comment: '#' /.*/
234blank:
235
236identifier: /\w+/
237
238# skip unknown directives with a warning
239unknown_directive: /\!\S*/ /.*/
240    { warn "[Text::Formbuilder] Skipping unknown directive '$item[1]' at input text line $thisline\n"; }
Note: See TracBrowser for help on using the repository browser.