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

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

added dynamic lists (evaled perl code blocks) to !list def command

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