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

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

catch, warn about, and skip unknown directives (lines beginning with !)
optionally use PerlTidy to clean up generated module code
updated version number and docs
write, write_module, and form automatically call build if necessary

File size: 3.2 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' var_name (static_list | dynamic_list)
17    { $lists{$item{var_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
31description_def: '!description' <perl_codeblock>
32    { warn "[Text::FormBuilder] Description redefined at input text line $thisline\n" if defined $description;
33   
34    $description = $item[2];
35    $description =~ s/^{\s*|\s*}$//g;
36    }
37
38line: <skip:'[ \t]*'> ( title | author | pattern_def | heading | unknown_directive | field | comment | blank ) "\n"
39
40title: '!title' /.*/
41    { warn "[Text::Formbuilder] Title redefined at input text line $thisline\n" if defined $title;
42    $title = $item[2] }
43
44author: '!author' /.*/
45    { $author = $item[2] }
46
47pattern_def: '!pattern' var_name pattern
48    { $patterns{$item{var_name}} = $item{pattern} }
49
50pattern: /.*/
51
52heading: '!head' /.*/
53    { warn "[Text::FormBuilder] Header before field " . scalar(@fields) . " redefined at input text line $thisline\n" if defined $headings[@fields];
54    $headings[@fields] = $item[2] }
55
56field: name field_size(?) label(?) hint(?) type(?) default(?) option_list(?) validate(?)
57    {
58        my $field = {
59            name     => $item{name},
60            label    => $item{'label(?)'}[0],
61            comment  => $item{'hint(?)'}[0],
62            type     => $item{'type(?)'}[0],
63            value    => $item{'default(?)'}[0],
64            list     => $list_var,
65            validate => $item{'validate(?)'}[0],
66        };
67       
68        $$field{options} = [ @options ] if @options;
69       
70        $$field{rows} = $rows if defined $rows;
71        $$field{cols} = $cols if defined $cols;
72        $$field{size} = $size if defined $size;
73       
74        push @fields, $field;
75       
76        $type = undef;
77        $list_var = undef;
78        $size = undef;
79        $rows = undef;
80        $cols = undef;
81        @options = ();
82       
83    }
84   
85name: identifier
86
87var_name: /[A-Z_]+/
88
89field_size: '[' ( row_col | size ) ']'
90
91size: /\d+/
92    { $size = $item[1] }
93
94row_col: /\d+/ /,\s*/ /\d+/
95    { $rows = $item[1]; $cols = $item[3] }
96
97label: '|' /[^:\[\{\/]+/i
98
99hint: '[' /[^\]]+/ ']'    { $item[2] }
100
101type: ':' /textarea|text|password|file|checkbox|radio|select|hidden|static/
102
103default: '=' /[^\@\{\s]+/
104
105option_list: options | list_var
106   
107options: '{' option(s /,\s*/) '}'
108
109list_var: /@[A-Z_]+/ { $list_var = $item[1] }
110
111option: value display_text(?)
112    { push @options, { $item{value} => $item{'display_text(?)'}[0] } }
113
114value: identifier
115
116display_text: '[' /[^\]]+/i ']'    { $item[2] }
117
118validate: '//' value
119
120comment: '#' /.*/
121blank:
122
123identifier: /\w+/
124
125# skip unknown directives with a warning
126unknown_directive: /\!\S*/ /.*/
127    { warn "[Text::Formbuilder] Skipping unknown directive '$item[1]' at input text line $thisline\n"; }
Note: See TracBrowser for help on using the repository browser.