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

Last change on this file since 15 was 14, checked in by peter, 20 years ago

added a !description directive
enabled showing checkboxes/radio groups as ulists when there are 3 or more options

File size: 3.0 KB
Line 
1{ my ($title, $author, $description, %lists, %patterns, @fields, @headings, $type, @options, $list_var, $size, $rows, $cols); }
2
3form_spec: (list_def | description_def | line)(s)
4    {
5        $return = {
6            title    => $title,
7            author   => $author,
8            description => $description,
9            lists    => \%lists,
10            patterns => \%patterns,
11            headings => \@headings,
12            fields   => \@fields,
13        }
14    }
15
16list_def: '!list' list_name (static_list | dynamic_list)
17    { $lists{$item{list_name}} = [ @options ]; @options = () }
18
19static_list: '{' option(s /,\s*/) /,?/ '}'
20
21dynamic_list: '&' <perl_codeblock>
22    {
23        my @results = (eval $item[2]);
24        if (ref $results[0] eq 'HASH') {
25            @options = @results;
26        } else {   
27            @options = map { { $_ => $_ } } @results;
28        }
29    }
30
31list_name: /[A-Z_]+/
32
33description_def: '!description' <perl_codeblock>
34    { warn "[Text::FormBuilder] Description redefined at input text line $thisline\n" if defined $description;
35   
36    $description = $item[2];
37    $description =~ s/^{\s*|\s*}$//g;
38    }
39
40line: <skip:'[ \t]*'> ( title | author | pattern_def | heading | field | comment | blank ) "\n"
41
42title: '!title' /.*/
43    { warn "[Text::Formbuilder] Title redefined at input text line $thisline\n" if defined $title;
44    $title = $item[2] }
45
46author: '!author' /.*/
47    { $author = $item[2] }
48
49pattern_def: '!pattern' pattern_name pattern
50    { $patterns{$item{pattern_name}} = $item{pattern} }
51
52pattern_name: /[A-Z_]+/
53pattern: /.*/
54
55heading: '!head' /.*/
56    { warn "[Text::FormBuilder] Header before field " . scalar(@fields) . " redefined at input text line $thisline\n" if defined $headings[@fields];
57    $headings[@fields] = $item[2] }
58
59field: name field_size(?) label(?) hint(?) type(?) default(?) option_list(?) validate(?)
60    {
61        my $field = {
62            name     => $item{name},
63            label    => $item{'label(?)'}[0],
64            comment  => $item{'hint(?)'}[0],
65            type     => $item{'type(?)'}[0],
66            value    => $item{'default(?)'}[0],
67            list     => $list_var,
68            validate => $item{'validate(?)'}[0],
69        };
70       
71        $$field{options} = [ @options ] if @options;
72       
73        $$field{rows} = $rows if defined $rows;
74        $$field{cols} = $cols if defined $cols;
75        $$field{size} = $size if defined $size;
76       
77        push @fields, $field;
78       
79        $type = undef;
80        $list_var = undef;
81        $size = undef;
82        $rows = undef;
83        $cols = undef;
84        @options = ();
85       
86    }
87   
88name: identifier
89
90field_size: '[' ( row_col | size ) ']'
91
92size: /\d+/
93    { $size = $item[1] }
94
95row_col: /\d+/ /,\s*/ /\d+/
96    { $rows = $item[1]; $cols = $item[3] }
97
98label: '|' /[^:\[\{\/]+/i
99
100hint: '[' /[^\]]+/ ']'    { $item[2] }
101
102type: ':' /textarea|text|password|file|checkbox|radio|select|hidden|static/
103
104default: '=' /[^\@\{\s]+/
105
106option_list: options | list_var
107   
108options: '{' option(s /,\s*/) '}'
109
110list_var: /@[A-Z_]+/ { $list_var = $item[1] }
111
112option: value display_text(?)
113    { push @options, { $item{value} => $item{'display_text(?)'}[0] } }
114
115value: identifier
116
117display_text: '[' /[^\]]+/i ']'    { $item[2] }
118
119validate: '//' value
120
121comment: '#' /.*/
122blank:
123
124identifier: /\w+/
Note: See TracBrowser for help on using the repository browser.