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

Last change on this file since 42 was 42, checked in by peter, 19 years ago

added a create_form exported method to "do the right thing" in simple cases
added parse_array method to parse an array of lines making up a formspec
some internal cleanup of the lines structure

File size: 5.4 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        $rows,
22        $cols,
23    );
24    $context = 'line';
25}
26
27form_spec: (list_def | description_def | validate_def | group_def | line)(s)
28    {
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 = ();
42        $return = {
43            title    => $title,
44            author   => $author,
45            description => $description,
46            lists    => \%lists,
47            patterns => \%patterns,
48            subs     => \%subs,
49            groups   => \%groups,
50            sections => \@sections,
51        }
52    }
53
54list_def: '!list' var_name (static_list | dynamic_list)
55    { $lists{$item{var_name}} = [ @options ]; @options = () }
56
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
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
76validate_def: '!validate' var_name <perl_codeblock>
77    { $subs{$item{var_name}} = eval "sub $item[3]" }
78
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    }
85
86field_line: <skip:'[ \t]*'> ( field | comment | blank ) "\n"
87line: <skip:'[ \t]*'> ( title | author | pattern_def | section_head | heading | group_field | unknown_directive | field | comment | blank ) "\n"
88
89title: '!title' /.*/
90    {
91        warn "[Text::Formbuilder] Title redefined at input text line $thisline\n" if defined $title;
92        $title = $item[2];
93    }
94
95author: '!author' /.*/
96    {
97        warn "[Text::Formbuilder] Author redefined at input text line $thisline\n" if defined $author;
98        $author = $item[2];
99    }
100
101pattern_def: '!pattern' var_name pattern
102    { $patterns{$item{var_name}} = $item{pattern} }
103
104pattern: /.*/
105
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
125heading: '!head' /.*/    { push @lines, [ 'head', $item[2] ] }
126
127group_field: '!field' group_name name label(?)
128    {
129        push @lines, [ 'group', { name => $item{name}, label => $item{'label(?)'}[0], group => $item{group_name} } ];
130    }
131
132group_name: /%[A-Z_]+/
133
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],
144            required => $required || 0,
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       
153        #warn "[$thisline] field $item{name}; context $context\n";
154        if ($context eq 'group') {
155            push @group, $field;
156        } else {
157            push @lines, [ 'field', $field ];
158        }
159       
160        $type = undef;
161        $required = 0;
162        $list_var = undef;
163        $size = undef;
164        $rows = undef;
165        $cols = undef;
166        @options = ();
167       
168    }
169   
170name: identifier
171
172var_name: /[A-Z_]+/
173
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
182label: '|' (simple_multiword | quoted_string) { $item[2] }
183
184hint: '[' /[^\]]+/ ']'    { $item[2] }
185
186type: ':' /textarea|text|password|file|checkbox|radio|select|hidden|static/
187
188default: '=' (simple_multiword | quoted_string) { $item[2] }
189
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
198option_list: options | list_var
199   
200options: '{' option(s /,\s*/) '}'
201
202list_var: /@[A-Z_]+/ { $list_var = $item[1] }
203
204option: (simple_multiword | value | quoted_string) display_text(?)
205    { push @options, { $item[1] => $item{'display_text(?)'}[0] } }
206
207value: identifier
208
209display_text: '[' /[^\]]+/i ']'    { $item[2] }
210
211validate: '//' (optional_pattern | required_pattern)    { $item[2] }
212
213optional_pattern: /[A-Z_]+/ '?' { $required = 0; $item[1] }
214
215required_pattern: /[A-Z_]+/ { $required = 1; $item[1] }
216
217comment: '#' /.*/
218blank:
219
220identifier: /\w+/
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.