3 # PSPP - a program for statistical analysis.
4 # Copyright (C) 2017, 2018, 2019 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
29 sys.stderr.write("%s:%d: %s\n" % (file_name, line_number, msg))
41 line = input_file.readline()
42 line = re.sub('//.*', '\n', line)
48 fatal("syntax error at %s expecting %s" % (token[0], type))
65 if token == ('id', id_):
73 return c.isalnum() or c in '-_#%'
82 if token == ('eof', ):
83 fatal("unexpected end of input")
96 if line.startswith('=>'):
99 elif line[0] in '[]()?|*+=:':
102 elif is_idchar(line[0]):
104 while n < len(line) and is_idchar(line[n]):
110 fatal("unknown character %c" % line[0])
115 argv0 = os.path.basename(sys.argv[0])
117 %(argv0)s, parser generator for SPV XML members
118 usage: %(argv0)s GRAMMAR header PREFIX
119 %(argv0)s GRAMMAR code PREFIX HEADER_NAME
120 where GRAMMAR contains grammar definitions\
121 ''' % {"argv0": argv0})
127 sub = parse_alternation()
131 member_name, nonterminal_name = parse_name()
132 if member_name.isupper():
133 fatal('%s; unknown terminal' % member_name)
135 return {'type': 'nonterminal',
136 'nonterminal_name': nonterminal_name,
137 'member_name': member_name}
140 def parse_quantified():
142 if token[0] in ['*', '+', '?']:
143 item = {'type': token[0], 'item': item}
147 item['seq_name'] = token[1]
153 def parse_sequence():
154 if match_id('EMPTY'):
155 return {'type': 'empty'}
158 sub = parse_quantified()
159 if sub['type'] == 'sequence':
160 items.extend(sub[1:])
163 if token[0] in ('|', ';', ')', 'eof'):
165 return {'type': 'sequence', 'items': items} if len(items) > 1 else items[0]
168 def parse_alternation():
169 items = [parse_sequence()]
171 items.append(parse_sequence())
173 return {'type': '|', 'items': items}
179 # The name used in XML for the attribute or element always comes
185 # If a different name is needed to disambiguate when the same name
186 # is used in different contexts in XML, it comes later, in
190 unique_name = token[1]
194 unique_name = xml_name
196 return unique_name, xml_name
200 def parse_production():
201 unique_name, xml_name = parse_name()
203 attr_xml_names = set()
206 attr_unique_name, attr_xml_name = parse_name()
210 while not match(')'):
212 attr_value.add(token[1])
217 if attr_unique_name not in enums:
218 enums[attr_unique_name] = attr_value
219 elif enums[attr_unique_name] != attr_value:
220 sys.stderr.write('%s: different enums with same name\n'
223 elif match_id('bool'):
224 attr_value = set(('true', 'false'))
225 elif match_id('dimension'):
226 attr_value = 'dimension'
227 elif match_id('real'):
229 elif match_id('int'):
231 elif match_id('color'):
233 elif match_id('ref'):
236 attr_value = ('ref', ref_type)
240 while not match(')'):
242 ref_types.add(token[1])
245 attr_value = ('ref', ref_types)
247 attr_value = ('ref', None)
249 fatal("unknown attribute value type")
251 attr_value = 'string'
252 attr_required = not match('?')
254 if attr_xml_name == 'id':
255 if attr_value != 'string':
256 fatal("id attribute must have string type")
259 if attr_unique_name in attributes:
260 fatal("production %s has two attributes %s" % (unique_name,
262 if attr_xml_name in attr_xml_names:
263 fatal("production %s has two attributes %s" % (unique_name,
265 attr_xml_names.add(attr_xml_name)
266 attributes[attr_unique_name] = (attr_xml_name,
267 attr_value, attr_required)
268 if 'id' not in attributes:
269 attributes["id"] = ('id', 'id', False)
274 rhs = {'type': 'text'}
275 elif match_id('ETC'):
276 rhs = {'type': 'etc'}
278 rhs = parse_alternation()
281 for a in rhs['items'] if rhs['type'] == '|' else (rhs,):
282 for term in a['items'] if a['type'] == 'sequence' else (a,):
283 if term['type'] == 'empty':
285 elif term['type'] == 'nonterminal':
287 elif term['type'] == '?' and term['item']['type'] == 'nonterminal':
289 elif (term['type'] in ('*', '+')
290 and term['item']['type'] == 'nonterminal'):
292 elif 'seq_name' not in term:
294 term['seq_name'] = 'seq' if n == 1 else 'seq%d' % n
296 return unique_name, xml_name, attributes, rhs
300 def print_members(attributes, rhs, indent):
303 for unique_name, (xml_name, value, required) in attributes.items():
304 c_name = name_to_id(unique_name)
305 if type(value) is set:
308 attrs += [('bool %s_present;' % c_name,
309 'True if attribute present')]
310 elif value == set(('true', 'false')):
312 attrs += [('bool %s;' % c_name, None)]
314 attrs += [('int %s;' % c_name,
315 '-1 if not present, otherwise 0 or 1')]
317 attrs += [('enum %s%s %s;' % (prefix, c_name, c_name),
318 'Always nonzero' if required else
319 'Zero if not present')]
322 if unique_name not in used_enums:
323 new_enums.add(unique_name)
324 elif value == 'dimension' or value == 'real':
325 attrs += [('double %s;' % c_name,
326 'In inches. ' + ('Always present' if required else
327 'DBL_MAX if not present'))]
329 attrs += [('int %s;' % c_name,
330 'Always present' if required
331 else 'INT_MIN if not present')]
332 elif value == 'color':
333 attrs += [('int %s;' % c_name,
334 'Always present' if required
335 else '-1 if not present')]
336 elif value == 'string':
337 attrs += [('char *%s;' % c_name,
338 'Always nonnull' if required else 'Possibly null')]
339 elif value[0] == 'ref':
340 struct = ('spvxml_node'
341 if value[1] is None or type(value[1]) is set
342 else '%s%s' % (prefix, name_to_id(value[1])))
343 attrs += [('struct %s *%s;' % (struct, c_name),
344 'Always nonnull' if required else 'Possibly null')]
350 for enum_name in new_enums:
351 used_enums.add(enum_name)
352 c_name = name_to_id(enum_name)
353 print('\nenum %s%s {' % (prefix, c_name))
355 for value in sorted(enums[enum_name]):
356 print(' %s%s_%s%s,' % (prefix.upper(),
358 name_to_id(value).upper(),
359 ' = 1' if i == 0 else ''))
362 print('const char *%s%s_to_string (enum %s%s);' % (
363 prefix, c_name, prefix, c_name))
365 print('\nstruct %s%s {' % (prefix, name_to_id(name)))
366 print('%sstruct spvxml_node node_;' % indent)
369 print('\n%s/* Attributes. */' % indent)
370 for decl, comment in attrs:
371 line = '%s%s' % (indent, decl)
373 n_spaces = max(35 - len(line), 1)
374 line += '%s/* %s. */' % (' ' * n_spaces, comment)
377 if rhs['type'] == 'etc' or rhs['type'] == 'empty':
380 print('\n%s/* Content. */' % indent)
381 if rhs['type'] == 'text':
382 print('%schar *text; /* Always nonnull. */' % indent)
385 for a in rhs['items'] if rhs['type'] == '|' else (rhs,):
386 for term in a['items'] if a['type'] == 'sequence' else (a,):
387 if term['type'] == 'empty':
389 elif term['type'] == 'nonterminal':
390 nt_name = name_to_id(term['nonterminal_name'])
391 member_name = name_to_id(term['member_name'])
392 print('%sstruct %s%s *%s; /* Always nonnull. */' % (
393 indent, prefix, nt_name, member_name))
394 elif term['type'] == '?' and term['item']['type'] == 'nonterminal':
395 nt_name = name_to_id(term['item']['nonterminal_name'])
396 member_name = name_to_id(term['item']['member_name'])
397 print('%sstruct %s%s *%s; /* Possibly null. */' % (
398 indent, prefix, nt_name, member_name))
399 elif (term['type'] in ('*', '+')
400 and term['item']['type'] == 'nonterminal'):
401 nt_name = name_to_id(term['item']['nonterminal_name'])
402 member_name = name_to_id(term['item']['member_name'])
403 print('%sstruct %s%s **%s;' % (indent, prefix,
404 nt_name, member_name))
405 print('%ssize_t n_%s;' % (indent, member_name))
407 seq_name = term['seq_name']
408 print('%sstruct spvxml_node **%s;' % (indent, seq_name))
409 print('%ssize_t n_%s;' % (indent, seq_name))
413 return ''.join(['"'] + ["\\x%02x" % ord(x) for x in s] + ['"'])
416 class Parser_Context(object):
417 def __init__(self, function_name, productions):
420 self.need_error_handler = False
422 self.parser_index = 0
423 self.productions = productions
425 self.function_name = function_name
427 def gen_name(self, prefix):
428 n = self.suffixes.get(prefix, 0) + 1
429 self.suffixes[prefix] = n
430 return '%s%d' % (prefix, n) if n > 1 else prefix
431 def new_function(self, type_name):
432 f = Function('%s_%d' % (self.function_name, len(self.functions) + 1),
434 self.functions += [f]
438 def print_attribute_decls(name, attributes):
441 for unique_name, (xml_name, value, required) in sorted(attributes.items()):
442 c_name = name_to_id(unique_name)
443 print(' ATTR_%s,' % c_name.upper())
445 print(' struct spvxml_attribute attrs[] = {')
446 for unique_name, (xml_name, value, required) in sorted(attributes.items()):
447 c_name = name_to_id(unique_name)
448 print(' [ATTR_%s] = { "%s", %s, NULL },'
449 % (c_name.upper(), xml_name, 'true' if required else 'false'))
451 print(' enum { N_ATTRS = sizeof attrs / sizeof *attrs };')
454 def print_parser_for_attributes(name, attributes):
455 print(' /* Parse attributes. */')
456 print(' spvxml_parse_attributes (&nctx);')
461 for unique_name, (xml_name, value, required) in sorted(attributes.items()):
462 c_name = name_to_id(unique_name)
463 params = '&nctx, &attrs[ATTR_%s]' % c_name.upper()
464 if type(value) is set:
467 print(' spvxml_attr_parse_fixed (%s, "%s");'
468 % (params, tuple(value)[0]))
470 print(' p->%s_present = spvxml_attr_parse_fixed (\n'
472 % (c_name, params, tuple(value)[0]))
473 elif value == set(('true', 'false')):
474 print(' p->%s = spvxml_attr_parse_bool (%s);'
477 map_name = '%s%s_map' % (prefix, c_name)
478 print(' p->%s = spvxml_attr_parse_enum (\n'
480 % (c_name, params, map_name))
481 elif value in ('real', 'dimension', 'int', 'color'):
482 print(' p->%s = spvxml_attr_parse_%s (%s);'
483 % (c_name, value, params))
484 elif value == 'string':
485 print(' p->%s = attrs[ATTR_%s].value;\n'
486 ' attrs[ATTR_%s].value = NULL;'
487 % (c_name, c_name.upper(),
490 print(' p->node_.id = attrs[ATTR_%s].value;\n'
491 ' attrs[ATTR_%s].value = NULL;'
492 % (c_name.upper(), c_name.upper()))
493 elif value[0] == 'ref':
499 spvxml_node_context_uninit (&nctx);
500 ctx->hard_error = true;
504 % (prefix, name_to_id(name)))
506 class Function(object):
507 def __init__(self, function_name, type_name):
508 self.function_name = function_name
509 self.type_name = type_name
512 def gen_name(self, prefix):
513 n = self.suffixes.get(prefix, 0) + 1
514 self.suffixes[prefix] = n
515 return '%s%d' % (prefix, n) if n > 1 else prefix
519 %s (struct spvxml_node_context *nctx, xmlNode **input, struct %s *p)
521 % (self.function_name, self.type_name))
522 while self.code and self.code[0] == '':
523 self.code = self.code[1:]
524 for line in self.code:
525 print(' %s' % line if line else '')
526 print(' return true;')
530 STATE_ALTERNATION = 1
536 def generate_content_parser(nonterminal, rhs, function, ctx, state, seq_name):
537 seq_name = seq_name if seq_name else rhs.get('seq_name')
538 ctx.parser_index += 1
540 if rhs['type'] == 'etc':
541 function.code += ['spvxml_content_parse_etc (input);']
542 elif rhs['type'] == 'text':
543 function.code += ['if (!spvxml_content_parse_text (nctx, input, &p->text))',
545 elif rhs['type'] == '|':
546 for i in range(len(rhs['items'])):
547 choice = rhs['items'][i]
548 subfunc = ctx.new_function(function.type_name)
549 generate_content_parser(nonterminal, choice, subfunc, ctx,
551 if state == STATE_START
552 else STATE_GENERAL, seq_name)
553 function.code += ['%(start)s!%(tryfunc)s (nctx, input, p, %(subfunc)s)%(end)s'
554 % {'start': 'if (' if i == 0 else ' && ',
555 'subfunc': subfunc.function_name,
556 'tryfunc': '%stry_parse_%s'
557 % (prefix, name_to_id(nonterminal)),
558 'end': ')' if i == len(rhs['items']) - 1 else ''}]
559 function.code += [' {',
560 ' spvxml_content_error (nctx, *input, "Syntax error.");',
563 elif rhs['type'] == 'sequence':
564 for element in rhs['items']:
565 generate_content_parser(nonterminal, element, function, ctx,
567 if state in (STATE_START,
569 else STATE_GENERAL, seq_name)
570 elif rhs['type'] == 'empty':
571 function.code += ['(void) nctx;']
572 function.code += ['(void) input;']
573 function.code += ['(void) p;']
574 elif rhs['type'] in ('*', '+', '?'):
575 subfunc = ctx.new_function(function.type_name)
576 generate_content_parser(nonterminal, rhs['item'], subfunc, ctx,
578 if rhs['type'] == '?'
579 else STATE_REPETITION)
580 if state in (STATE_START,
583 else STATE_GENERAL, seq_name)
584 next_name = function.gen_name('next')
585 args = {'subfunc': subfunc.function_name,
586 'tryfunc': '%stry_parse_%s' % (prefix,
587 name_to_id (nonterminal))}
588 if rhs['type'] == '?':
590 '%(tryfunc)s (nctx, input, p, %(subfunc)s);' % args]
592 if rhs['type'] == '+':
593 function.code += ['if (!%(subfunc)s (nctx, input, p))' % args,
596 'while (%(tryfunc)s (nctx, input, p, %(subfunc)s))' % args,
598 elif rhs['type'] == 'nonterminal':
599 node_name = function.gen_name('node')
602 'xmlNode *%s;' % node_name,
603 'if (!spvxml_content_parse_element (nctx, input, "%s", &%s))'
604 % (ctx.productions[rhs['nonterminal_name']][0], node_name),
606 if state in (STATE_START,
610 target = '&p->%s' % name_to_id(rhs['member_name'])
612 assert state in (STATE_REPETITION, STATE_GENERAL)
613 member = name_to_id(rhs['member_name']) if state == STATE_REPETITION else seq_name
614 function.code += ['struct %s%s *%s;' % (
615 prefix, name_to_id(rhs['nonterminal_name']), member)]
616 target = '&%s' % member
618 'if (!%sparse_%s (nctx->up, %s, %s))'
619 % (prefix, name_to_id(rhs['nonterminal_name']), node_name, target),
621 if state in (STATE_REPETITION, STATE_GENERAL):
623 'p->%s = xrealloc (p->%s, sizeof *p->%s * (p->n_%s + 1));'
624 % (member, member, member, member),
625 'p->%s[p->n_%s++] = %s;' % (member, member,
626 '&%s->node_' % member
627 if state == STATE_GENERAL
632 def print_parser(name, production, productions, indent):
633 xml_name, attributes, rhs = production
637 %(prefix)stry_parse_%(name)s (
638 struct spvxml_node_context *nctx, xmlNode **input,
639 struct %(prefix)s%(name)s *p,
640 bool (*sub) (struct spvxml_node_context *,
642 struct %(prefix)s%(name)s *))
644 xmlNode *next = *input;
645 bool ok = sub (nctx, &next, p);
648 else if (!nctx->up->hard_error) {
649 free (nctx->up->error);
650 nctx->up->error = NULL;
655 'name': name_to_id(name)})
657 ctx = Parser_Context('%sparse_%s' % (prefix, name_to_id(name)),
659 if rhs['type'] not in ('empty', 'etc'):
660 function = ctx.new_function('%s%s' % (prefix, name_to_id(name)))
661 generate_content_parser(name, rhs, function, ctx, 0, None)
662 for f in reversed(ctx.functions):
667 %(prefix)sparse_%(name)s (
668 struct spvxml_context *ctx, xmlNode *input,
669 struct %(prefix)s%(name)s **p_)
672 'name': name_to_id(name)})
674 print_attribute_decls(name, attributes)
676 print(' struct spvxml_node_context nctx = {')
678 print(' .parent = input,')
679 print(' .attrs = attrs,')
680 print(' .n_attrs = N_ATTRS,')
683 print(' *p_ = NULL;')
684 print(' struct %(prefix)s%(name)s *p = xzalloc (sizeof *p);'
686 'name': name_to_id(name)})
687 print(' p->node_.raw = input;')
688 print(' p->node_.class_ = &%(prefix)s%(name)s_class;'
690 'name': name_to_id(name)})
693 print_parser_for_attributes(name, attributes)
695 if rhs['type'] == 'empty':
698 if (!spvxml_content_parse_end (&nctx, input->children)) {
699 ctx->hard_error = true;
700 spvxml_node_context_uninit (&nctx);
704 % (prefix, name_to_id(name)))
705 elif rhs['type'] == 'etc':
707 /* Ignore content. */
712 input = input->children;
713 if (!%s (&nctx, &input, p)
714 || !spvxml_content_parse_end (&nctx, input)) {
715 ctx->hard_error = true;
716 spvxml_node_context_uninit (&nctx);
720 % (function.function_name,
721 prefix, name_to_id(name)))
724 spvxml_node_context_uninit (&nctx);
731 def print_free_members(attributes, rhs, indent):
732 for unique_name, (xml_name, value, required) in attributes.items():
733 c_name = name_to_id(unique_name)
734 if (type(value) is set
735 or value in ('dimension', 'real', 'int', 'color', 'id')
736 or value[0] == 'ref'):
738 elif value == 'string':
739 print(' free (p->%s);' % c_name);
743 if rhs['type'] in ('etc', 'empty'):
745 elif rhs['type'] == 'text':
746 print(' free (p->text);')
749 for a in rhs['items'] if rhs['type'] == '|' else (rhs,):
750 for term in a['items'] if a['type'] == 'sequence' else (a,):
751 if term['type'] == 'empty':
753 elif (term['type'] == 'nonterminal'
754 or (term['type'] == '?'
755 and term['item']['type'] == 'nonterminal')):
756 if term['type'] == '?':
758 nt_name = name_to_id(term['nonterminal_name'])
759 member_name = name_to_id(term['member_name'])
760 print(' %sfree_%s (p->%s);' % (prefix, nt_name,
762 elif (term['type'] in ('*', '+')
763 and term['item']['type'] == 'nonterminal'):
764 nt_name = name_to_id(term['item']['nonterminal_name'])
765 member_name = name_to_id(term['item']['member_name'])
767 for (size_t i = 0; i < p->n_%s; i++)
768 %sfree_%s (p->%s[i]);
771 prefix, nt_name, member_name,
774 seq_name = term['seq_name']
776 for (size_t i = 0; i < p->n_%s; i++)
777 p->%s[i]->class_->spvxml_node_free (p->%s[i]);
782 print(' free (p->node_.id);')
786 def print_free(name, production, indent):
787 xml_name, attributes, rhs = production
791 %(prefix)sfree_%(name)s (struct %(prefix)s%(name)s *p)
795 ''' % {'prefix': prefix,
796 'name': name_to_id(name)})
798 print_free_members(attributes, rhs, ' ' * 4)
803 return ((s[0].lower()
804 + ''.join(['_%c' % x.lower() if x.isupper() else x
805 for x in s[1:]]).replace('-', '_'))
811 def print_recurse_members(attributes, rhs, function):
812 if rhs['type'] == 'etc' or rhs['type'] == 'empty':
814 elif rhs['type'] == 'text':
818 for a in rhs['items'] if rhs['type'] == '|' else (rhs,):
819 for term in a['items'] if a['type'] == 'sequence' else (a,):
820 if term['type'] == 'empty':
822 elif (term['type'] == 'nonterminal'
823 or (term['type'] == '?'
824 and term['item']['type'] == 'nonterminal')):
825 if term['type'] == '?':
827 nt_name = name_to_id(term['nonterminal_name'])
828 member_name = name_to_id(term['member_name'])
829 print(' %s%s_%s (ctx, p->%s);'
830 % (prefix, function, nt_name, member_name))
831 elif (term['type'] in ('*', '+')
832 and term['item']['type'] == 'nonterminal'):
833 nt_name = name_to_id(term['item']['nonterminal_name'])
834 member_name = name_to_id(term['item']['member_name'])
836 for (size_t i = 0; i < p->n_%s; i++)
837 %s%s_%s (ctx, p->%s[i]);'''
839 prefix, function, nt_name, member_name))
841 seq_name = term['seq_name']
843 for (size_t i = 0; i < p->n_%s; i++)
844 p->%s[i]->class_->spvxml_node_%s (ctx, p->%s[i]);'''
846 seq_name, function, seq_name))
849 def print_collect_ids(name, production):
850 xml_name, attributes, rhs = production
854 %(prefix)scollect_ids_%(name)s (struct spvxml_context *ctx, struct %(prefix)s%(name)s *p)
859 spvxml_node_collect_id (ctx, &p->node_);
860 ''' % {'prefix': prefix,
861 'name': name_to_id(name)})
863 print_recurse_members(attributes, rhs, 'collect_ids')
868 def print_resolve_refs(name, production):
869 xml_name, attributes, rhs = production
873 %(prefix)sis_%(name)s (const struct spvxml_node *node)
875 return node->class_ == &%(prefix)s%(name)s_class;
878 struct %(prefix)s%(name)s *
879 %(prefix)scast_%(name)s (const struct spvxml_node *node)
881 return (node && %(prefix)sis_%(name)s (node)
882 ? UP_CAST (node, struct %(prefix)s%(name)s, node_)
887 %(prefix)sresolve_refs_%(name)s (struct spvxml_context *ctx UNUSED, struct %(prefix)s%(name)s *p UNUSED)
891 ''' % {'prefix': prefix,
892 'name': name_to_id(name)})
895 for unique_name, (xml_name, value, required) in sorted(attributes.items()):
896 c_name = name_to_id(unique_name)
897 if type(value) is set or value[0] != 'ref':
901 print(' p->%s = spvxml_node_resolve_ref (ctx, p->node_.raw, \"%s\", NULL, 0);'
902 % (c_name, xml_name))
908 if type(value[1]) is set:
909 print(' static const struct spvxml_node_class *const %s[] = {' % name)
910 for ref_type in value[1]:
911 print(' &%(prefix)s%(ref_type)s_class,'
913 'ref_type': name_to_id(ref_type)})
915 print(' const size_t n_%s = sizeof %s / sizeof *%s;'
916 % (name, name, name))
917 print(' p->%(member)s = spvxml_node_resolve_ref (ctx, p->node_.raw, \"%(attr)s\", %(name)s, n_%(name)s);'
924 print(' static const struct spvxml_node_class *const %s' % name)
925 print(' = &%(prefix)s%(ref_type)s_class;'
927 'ref_type': name_to_id(value[1])})
928 print(' p->%(member)s = %(prefix)scast_%(ref_type)s (spvxml_node_resolve_ref (ctx, p->node_.raw, \"%(attr)s\", &%(name)s, 1));'
933 'ref_type': name_to_id(value[1])})
936 print_recurse_members(attributes, rhs, 'resolve_refs')
941 if __name__ == "__main__":
944 options, args = getopt.gnu_getopt(sys.argv[1:], 'h', ['help'])
945 except getopt.GetoptError as e:
946 sys.stderr.write("%s: %s\n" % (argv0, e.msg))
949 for key, value in options:
950 if key in ['-h', '--help']:
956 sys.stderr.write("%s: bad usage (use --help for help)\n" % argv0)
961 file_name, output_type, prefix = args[:3]
962 input_file = open(file_name)
964 prefix = '%s_' % prefix
979 if token[0] == 'eof':
982 name, xml_name, attributes, rhs = parse_production()
983 if name in productions:
984 fatal("%s: duplicate production" % name)
985 productions[name] = (xml_name, attributes, rhs)
987 print('/* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */')
988 if output_type == 'code' and len(args) == 4:
989 header_name = args[3]
997 #include "libpspp/cast.h"
998 #include "libpspp/str.h"
999 #include "gl/xalloc.h"
1002 for enum_name, values in sorted(enums.items()):
1003 if len(values) <= 1:
1006 c_name = name_to_id(enum_name)
1007 print('\nstatic const struct spvxml_enum %s%s_map[] = {'
1009 for value in sorted(values):
1010 print(' { "%s", %s%s_%s },' % (value, prefix.upper(),
1012 name_to_id(value).upper()))
1013 print(' { NULL, 0 },')
1015 print('\nconst char *')
1016 print('%s%s_to_string (enum %s%s %s)'
1017 % (prefix, c_name, prefix, c_name, c_name))
1019 print(' switch (%s) {' % c_name)
1020 for value in sorted(values):
1021 print(' case %s%s_%s: return "%s";'
1022 % (prefix.upper(), c_name.upper(),
1023 name_to_id(value).upper(), value))
1024 print(' default: return NULL;')
1028 for name, (xml_name, attributes, rhs) in sorted(productions.items()):
1029 print('static void %(prefix)scollect_ids_%(name)s (struct spvxml_context *, struct %(prefix)s%(name)s *);\n'
1030 'static void %(prefix)sresolve_refs_%(name)s (struct spvxml_context *ctx UNUSED, struct %(prefix)s%(name)s *p UNUSED);\n'
1031 % {'prefix': prefix,
1032 'name': name_to_id(name)})
1033 for name, production in sorted(productions.items()):
1034 print_parser(name, production, productions, ' ' * 4)
1035 print_free(name, production, ' ' * 4)
1036 print_collect_ids(name, production)
1037 print_resolve_refs(name, production)
1040 %(prefix)sdo_free_%(name)s (struct spvxml_node *node)
1042 %(prefix)sfree_%(name)s (UP_CAST (node, struct %(prefix)s%(name)s, node_));
1046 %(prefix)sdo_collect_ids_%(name)s (struct spvxml_context *ctx, struct spvxml_node *node)
1048 %(prefix)scollect_ids_%(name)s (ctx, UP_CAST (node, struct %(prefix)s%(name)s, node_));
1052 %(prefix)sdo_resolve_refs_%(name)s (struct spvxml_context *ctx, struct spvxml_node *node)
1054 %(prefix)sresolve_refs_%(name)s (ctx, UP_CAST (node, struct %(prefix)s%(name)s, node_));
1057 struct spvxml_node_class %(prefix)s%(name)s_class = {
1059 %(prefix)sdo_free_%(name)s,
1060 %(prefix)sdo_collect_ids_%(name)s,
1061 %(prefix)sdo_resolve_refs_%(name)s,
1064 % {'prefix': prefix,
1065 'name': name_to_id(name),
1066 'class': (name if name == production[0]
1067 else '%s (%s)' % (name, production[0]))})
1068 elif output_type == 'header' and len(args) == 3:
1070 #ifndef %(PREFIX)sPARSER_H
1071 #define %(PREFIX)sPARSER_H
1075 #include <stdbool.h>
1076 #include "output/spv/spvxml-helpers.h"\
1077 """ % {'PREFIX': prefix.upper()})
1078 for name, (xml_name, attributes, rhs) in sorted(productions.items()):
1079 print_members(attributes, rhs, ' ' * 4)
1082 extern struct spvxml_node_class %(prefix)s%(name)s_class;
1084 bool %(prefix)sparse_%(name)s (struct spvxml_context *, xmlNode *input, struct %(prefix)s%(name)s **);
1085 void %(prefix)sfree_%(name)s (struct %(prefix)s%(name)s *);
1086 bool %(prefix)sis_%(name)s (const struct spvxml_node *);
1087 struct %(prefix)s%(name)s *%(prefix)scast_%(name)s (const struct spvxml_node *);'''
1088 % {'prefix': prefix,
1089 'name': name_to_id(name)})
1092 #endif /* %(PREFIX)sPARSER_H */""" % {'PREFIX': prefix.upper()})
1094 sys.stderr.write("%s: bad usage (use --help for help)" % argv0)