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

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

fixed some typos in the docs
rearranged internals in the grammar, plus a minor bugfix for the skip pattern of inline lists (now allows padding space around the comma)
added new test scripts to the MANIFEST

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