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