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 | @group, # current group |
---|
14 | %groups, # stored groups of fields |
---|
15 | $type, |
---|
16 | @options, |
---|
17 | $required, |
---|
18 | $list_var, |
---|
19 | $size, |
---|
20 | $rows, |
---|
21 | $cols, |
---|
22 | ); |
---|
23 | $context = 'line'; |
---|
24 | } |
---|
25 | |
---|
26 | form_spec: (list_def | description_def | group_def | line)(s) |
---|
27 | { |
---|
28 | # grab the last section, if there is any |
---|
29 | if (@lines) { |
---|
30 | push @sections, |
---|
31 | { |
---|
32 | id => $section_id, |
---|
33 | head => $section_head, |
---|
34 | lines => [ @lines ], |
---|
35 | }; |
---|
36 | } |
---|
37 | |
---|
38 | $section_id = $item{identifier}; |
---|
39 | $section_head = $item[3]; |
---|
40 | @lines = (); |
---|
41 | $return = { |
---|
42 | title => $title, |
---|
43 | author => $author, |
---|
44 | description => $description, |
---|
45 | lists => \%lists, |
---|
46 | patterns => \%patterns, |
---|
47 | groups => \%groups, |
---|
48 | sections => \@sections, |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | list_def: '!list' var_name (static_list | dynamic_list) |
---|
53 | { $lists{$item{var_name}} = [ @options ]; @options = () } |
---|
54 | |
---|
55 | static_list: '{' option(s /,\s*/) /,?/ '}' |
---|
56 | |
---|
57 | dynamic_list: '&' <perl_codeblock> |
---|
58 | { |
---|
59 | my @results = (eval $item[2]); |
---|
60 | if (ref $results[0] eq 'HASH') { |
---|
61 | @options = @results; |
---|
62 | } else { |
---|
63 | @options = map { { $_ => $_ } } @results; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | description_def: '!description' <perl_codeblock> |
---|
68 | { warn "[Text::FormBuilder] Description redefined at input text line $thisline\n" if defined $description; |
---|
69 | |
---|
70 | $description = $item[2]; |
---|
71 | $description =~ s/^{\s*|\s*}$//g; |
---|
72 | } |
---|
73 | |
---|
74 | group_def: '!group' { $context = 'group' } var_name '{' field_line(s) '}' { $context = 'line' } |
---|
75 | { |
---|
76 | #warn "$item{var_name} group; context $context\n" |
---|
77 | $groups{$item{var_name}} = [ @group ]; |
---|
78 | @group = (); |
---|
79 | } |
---|
80 | |
---|
81 | field_line: <skip:'[ \t]*'> ( field | comment | blank ) "\n" |
---|
82 | line: <skip:'[ \t]*'> ( title | author | pattern_def | section_head | heading | group_field | unknown_directive | field | comment | blank ) "\n" |
---|
83 | |
---|
84 | title: '!title' /.*/ |
---|
85 | { |
---|
86 | warn "[Text::Formbuilder] Title redefined at input text line $thisline\n" if defined $title; |
---|
87 | $title = $item[2]; |
---|
88 | } |
---|
89 | |
---|
90 | author: '!author' /.*/ |
---|
91 | { |
---|
92 | warn "[Text::Formbuilder] Author redefined at input text line $thisline\n" if defined $author; |
---|
93 | $author = $item[2]; |
---|
94 | } |
---|
95 | |
---|
96 | pattern_def: '!pattern' var_name pattern |
---|
97 | { $patterns{$item{var_name}} = $item{pattern} } |
---|
98 | |
---|
99 | pattern: /.*/ |
---|
100 | |
---|
101 | section_head: '!section' identifier /.*/ |
---|
102 | { |
---|
103 | #warn "starting section $item{identifier}\n"; |
---|
104 | #warn " with heading $item[3]\n" if $item[3]; |
---|
105 | |
---|
106 | if (@lines) { |
---|
107 | push @sections, |
---|
108 | { |
---|
109 | id => $section_id, |
---|
110 | head => $section_head, |
---|
111 | lines => [ @lines ], |
---|
112 | }; |
---|
113 | } |
---|
114 | |
---|
115 | $section_id = $item{identifier}; |
---|
116 | $section_head = $item[3]; |
---|
117 | @lines = (); |
---|
118 | } |
---|
119 | |
---|
120 | heading: '!head' /.*/ { push @lines, [ 'head', $item[2] ] } |
---|
121 | |
---|
122 | group_field: '!field' group_name name label(?) |
---|
123 | { |
---|
124 | push @lines, [ 'group', { name => $item{name}, label => $item{'label(?)'}[0], group => $item{group_name} } ]; |
---|
125 | } |
---|
126 | |
---|
127 | group_name: /%[A-Z_]+/ |
---|
128 | |
---|
129 | field: name field_size(?) label(?) hint(?) type(?) default(?) option_list(?) validate(?) |
---|
130 | { |
---|
131 | my $field = { |
---|
132 | name => $item{name}, |
---|
133 | label => $item{'label(?)'}[0], |
---|
134 | comment => $item{'hint(?)'}[0], |
---|
135 | type => $item{'type(?)'}[0], |
---|
136 | value => $item{'default(?)'}[0], |
---|
137 | list => $list_var, |
---|
138 | validate => $item{'validate(?)'}[0], |
---|
139 | required => $required || 0, |
---|
140 | }; |
---|
141 | |
---|
142 | $$field{options} = [ @options ] if @options; |
---|
143 | |
---|
144 | $$field{rows} = $rows if defined $rows; |
---|
145 | $$field{cols} = $cols if defined $cols; |
---|
146 | $$field{size} = $size if defined $size; |
---|
147 | |
---|
148 | #warn "[$thisline] field $item{name}; context $context\n"; |
---|
149 | if ($context eq 'group') { |
---|
150 | push @group, $field; |
---|
151 | } else { |
---|
152 | push @lines, [ 'field', $field ]; |
---|
153 | } |
---|
154 | |
---|
155 | $type = undef; |
---|
156 | $required = 0; |
---|
157 | $list_var = undef; |
---|
158 | $size = undef; |
---|
159 | $rows = undef; |
---|
160 | $cols = undef; |
---|
161 | @options = (); |
---|
162 | |
---|
163 | } |
---|
164 | |
---|
165 | name: identifier |
---|
166 | |
---|
167 | var_name: /[A-Z_]+/ |
---|
168 | |
---|
169 | field_size: '[' ( row_col | size ) ']' |
---|
170 | |
---|
171 | size: /\d+/ |
---|
172 | { $size = $item[1] } |
---|
173 | |
---|
174 | row_col: /\d+/ /,\s*/ /\d+/ |
---|
175 | { $rows = $item[1]; $cols = $item[3] } |
---|
176 | |
---|
177 | label: '|' (simple_multiword | quoted_string) { $item[2] } |
---|
178 | |
---|
179 | hint: '[' /[^\]]+/ ']' { $item[2] } |
---|
180 | |
---|
181 | type: ':' /textarea|text|password|file|checkbox|radio|select|hidden|static/ |
---|
182 | |
---|
183 | default: '=' (simple_multiword | quoted_string) { $item[2] } |
---|
184 | |
---|
185 | # for simple multiword values not involving punctuation |
---|
186 | simple_multiword: <skip:''> /[\w\t ]+/ { $item[2] } |
---|
187 | |
---|
188 | # my attempt at a single-quoted, non-interpolating string |
---|
189 | # where the backslash can escape literal single quotes |
---|
190 | quoted_string: <skip:''> "'" /(\\'|[^'])*/ "'" |
---|
191 | { $item[3] =~ s/\\'/'/g; $item[3] } |
---|
192 | |
---|
193 | option_list: options | list_var |
---|
194 | |
---|
195 | options: '{' option(s /,\s*/) '}' |
---|
196 | |
---|
197 | list_var: /@[A-Z_]+/ { $list_var = $item[1] } |
---|
198 | |
---|
199 | option: (simple_multiword | value | quoted_string) display_text(?) |
---|
200 | { push @options, { $item[1] => $item{'display_text(?)'}[0] } } |
---|
201 | |
---|
202 | value: identifier |
---|
203 | |
---|
204 | display_text: '[' /[^\]]+/i ']' { $item[2] } |
---|
205 | |
---|
206 | validate: '//' (optional_pattern | required_pattern) { $item[2] } |
---|
207 | |
---|
208 | optional_pattern: /[A-Z_]+/ '?' { $required = 0; $item[1] } |
---|
209 | |
---|
210 | required_pattern: /[A-Z_]+/ { $required = 1; $item[1] } |
---|
211 | |
---|
212 | comment: '#' /.*/ |
---|
213 | blank: |
---|
214 | |
---|
215 | identifier: /\w+/ |
---|
216 | |
---|
217 | # skip unknown directives with a warning |
---|
218 | unknown_directive: /\!\S*/ /.*/ |
---|
219 | { warn "[Text::Formbuilder] Skipping unknown directive '$item[1]' at input text line $thisline\n"; } |
---|