def gen_transform_code_java(self, node_name, field_name, trans_class_name, trans_method_name, trans_arg_name, transform_ancestors, pass_source): if not self.visitable or not transform_ancestors and self.ref_type != trans_class_name \ or transform_ancestors and not is_subnode(trans_class_name, self.ref_type): return [] getter_name = "get" + first_to_cap(field_name) setter_name = "set" + first_to_cap(field_name) transform_stmt_fmt = "{0}.{1}({2}({0}.{3}, {0}, {4}));" if pass_source \ else "{0}.{1}({2}({0}.{3}(), {4}));" return [transform_stmt_fmt.format(node_name, setter_name, trans_method_name, getter_name, trans_arg_name)]
def gen_printer(self, lang): res = None if lang == DST_LANGUAGE.CPP: res = 'printf("%s: {{\\n", "{0}");\n' res += tab * 2 + "elem->get{0}()->accept(this);\n" res += tab * 2 + 'printf("}}\\n");' if lang == DST_LANGUAGE.JAVA: if not self.optional: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'System.out.printf("%s: {{", "{0}");\n' if self.ref_type in ast_nodes.keys(): res += tab * 3 + 'elem.get{0}().accept(this, arg);\n' else: res += tab * 3 + 'System.out.printf("%s; ", elem.get{0}().toString());\n' res += tab * 3 + 'System.out.printf("}}; ");\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' else: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'if (elem.get{0}().isPresent()) {{\n' res += tab * 4 + 'System.out.printf("%s: {{", "{0}");\n' if self.ref_type in ast_nodes.keys(): res += tab * 4 + 'elem.get{0}().get().accept(this, arg);\n' else: res += tab * 4 + 'System.out.printf(elem.get{0}().get().toString());\n' res += tab * 4 + 'System.out.printf("}}; ");\n' res += tab * 3 + '}} else {{\n' res += tab * 4 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 3 + '}}\n' res += tab * 2 + '}}' return res.format(first_to_cap(self.name))
def get_type(self, lang, full=False): res = None if self.width == FLOAT_TYPE.SINGLE: res = "float" if self.width == FLOAT_TYPE.DOUBLE: res = "double" if lang == DST_LANGUAGE.CPP: if self.const and full: res = "const " + res if lang == DST_LANGUAGE.JAVA: res = first_to_cap(res) if self.optional: res = "Optional<" if self.width == FLOAT_TYPE.SINGLE: res += "Float" if self.width == FLOAT_TYPE.DOUBLE: res += "Double" res += ">" if self.const and full: res = "final " + res return res
def gen_printer(self, lang): width = "d" res = None if lang == DST_LANGUAGE.CPP: res = 'printf("%s: [", "{0}");'.format(self.name) res += tab * 2 + 'for(int i=0; i<elem->get{0}.size(); ++i) {{\n'.format(self.name) if self.width == INT_TYPE.BYTE: width = "hhd" if self.width == INT_TYPE.SHORT: width = "hd" if self.width == INT_TYPE.NORMAL: width = "ld" if self.width == INT_TYPE.LONG: width = "lld" res += tab * 3 res += 'printf("%{1} ", elem->get{0}()[i] ? "true":"false");\n'.format(first_to_cap(self.name), width) res += tab * 2 + '}}\n' res += tab * 2 + 'printf("]\\n");' if lang == DST_LANGUAGE.JAVA: if not self.optional: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'System.out.printf("%s: [", "{0}");\n' res += tab * 3 + 'for({1} ival : elem.get{0}()) {{\n' res += tab * 4 + 'System.out.printf("%d ", ival.longValue());\n' res += tab * 3 + '}}\n' res += tab * 3 + 'System.out.printf("]; ");\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' else: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'if (elem.get{0}().isPresent()) {{\n' res += tab * 4 + 'System.out.printf("%s: [", "{0}");\n' res += tab * 4 + 'for({1} ival : elem.get{0}().get()) {{\n' res += tab * 5 + 'System.out.printf("%d ", ival.longValue());\n' res += tab * 4 + '}}\n' res += tab * 4 + 'System.out.printf("]; ");\n' res += tab * 3 + '}} else {{\n' res += tab * 4 + 'System.out.printf("%s: %s", "{0}", elem.get{0}());\n' res += tab * 3 + '}}\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' return res.format(first_to_cap(self.name), "Long" if self.width == INT_TYPE.LONG else "Integer")
def __init__(self, ref_type, name=None, const=False, *args, **kwargs): super(ReferenceField, self).__init__(*args, **kwargs) self.name = name self.const = const if not isinstance(ref_type, str): self.ref_type = ref_type().__class__.__name__ else: self.ref_type = ref_type self.ref_type = first_to_cap(self.ref_type)
def gen_transform_code_java(self, node_name, field_name, trans_class_name, trans_method_name, trans_arg_name, transform_ancestors, pass_source): if not self.visitable or not transform_ancestors and self.ref_type != trans_class_name \ or transform_ancestors and not is_subnode(trans_class_name, self.ref_type): return [] transform_stmt_fmt = "{0}s({1}.get{2}(), {1}, {3});" if pass_source \ else "{0}s({1}.get{2}(), {3});" return [transform_stmt_fmt.format(trans_method_name, node_name, first_to_cap(field_name), trans_arg_name)]
def __init__(self, enum_type, name=None, const=False, *args, **kwargs): super(EnumField, self).__init__(*args, **kwargs) self.name = name self.const = const if not isinstance(enum_type, str): self.enum_type = enum_type().__class__.__name__ else: self.enum_type = enum_type self.enum_type = first_to_cap(self.enum_type)
def __init__(self, ref_type, name=None, const=False, deep_copy_mode=DEEP_COPY_MODE.ASSIGN_LIST_DEEP_COPY, *args, **kwargs): super(ReferenceListField, self).__init__(deep_copy_mode=deep_copy_mode, *args, **kwargs) self.name = name self.const = const if not isinstance(ref_type, str): self.ref_type = ref_type().__class__.__name__ else: self.ref_type = ref_type self.ref_type = first_to_cap(self.ref_type)
def gen_getter_code(self, lang, type): code = None if lang == DST_LANGUAGE.CPP: code = type + " get" + first_to_cap(self.name) + "() {\n" code += tab + "return this->" + self.name + ";\n" code += "}\n" if not self.const: code += "\nvoid set" + first_to_cap(self.name) + "(" + type + " val) {\n" code += tab + "this->" + self.name + " = val;\n" code += "}\n" if lang == DST_LANGUAGE.JAVA: code = "public " + type + " get" + first_to_cap(self.name) + "() {\n" code += tab + "return this." + self.name + ";\n" code += "}\n" if not self.const: code += "\npublic void set" + first_to_cap(self.name) + "(" + type + " val) {\n" code += tab + "this." + self.name + " = val;\n" code += "}\n" return code
def gen_printer(self, lang): res = None if lang == DST_LANGUAGE.CPP: res = 'printf("%s: \\"%s\\"\\n", "{0}", elem->get{0}().c_str());' if lang == DST_LANGUAGE.JAVA: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'System.out.printf("%s: %s; ", "{0}", elem.get{0}());\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' return res.format(first_to_cap(self.name))
def gen_printer(self, lang): res = None if lang == DST_LANGUAGE.CPP: res = 'printf("%s: [", "{0}");' res += tab * 2 + 'for(int i=0; i<elem->get{0}.size(); ++i) {{\n' res += tab * 3 + 'elem->get{0}()[i]->accept(this);\n' res += tab * 2 + '}}\n' res += tab * 2 + 'printf("]\\n");' if lang == DST_LANGUAGE.JAVA: if not self.optional: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'System.out.printf("%s: [", "{0}");\n' res += tab * 3 + 'for({1} rval : elem.get{0}()) {{\n' res += tab * 4 + 'if (rval != null) {{\n' if self.ref_type in ast_nodes.keys(): res += tab * 5 + 'rval.accept(this, arg);\n' else: res += tab * 5 + 'System.out.printf(rval.toString());\n' res += tab * 4 + '}} else {{\n' res += tab * 5 + 'System.out.printf("null ");\n' res += tab * 4 + '}}\n' res += tab * 3 + '}}\n' res += tab * 3 + 'System.out.printf("]; ");\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' else: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'if (elem.get{0}().isPresent()) {{\n' res += tab * 4 + 'System.out.printf("%s: [", "{0}");\n' res += tab * 4 + 'for({1} rval : elem.get{0}().get()) {{\n' res += tab * 5 + 'if (rval != null) {{\n' if self.ref_type in ast_nodes.keys(): res += tab * 6 + 'rval.accept(this, arg);\n' else: res += tab * 6 + 'System.out.printf(rval.toString());\n' res += tab * 5 + '}} else {{\n' res += tab * 6 + 'System.out.printf("null ");\n' res += tab * 5 + '}}\n' res += tab * 4 + '}}\n' res += tab * 4 + 'System.out.printf("]; ");\n' res += tab * 3 + '}} else {{\n' res += tab * 4 + 'System.out.printf("%s", elem.get{0}());\n' res += tab * 3 + '}}\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' return res.format(first_to_cap(self.name), self.ref_type)
def gen_printer(self, lang): res = None if lang == DST_LANGUAGE.CPP: res = 'enum_printer_' + self.enum_type + '(elem->get{0}())\n' if lang == DST_LANGUAGE.JAVA: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'System.out.printf("%s: %s; ", "{0}", elem.get{0}());' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", {0});\n' res += tab * 2 + '}}' return res.format(first_to_cap(self.name))
def gen_subst_code_helper(self, obj_expr, obj_type, manager_name, assign_fun): code = ["if ({0}.get{1}() == null || !{0}.get{1}()) {{" .format(obj_expr, first_to_cap("isPasted"))] code.append(tab + "final Optional<{0}> replacement = {1}.substitute({2});" .format(obj_type, manager_name, obj_expr)) code.append(tab + "if (replacement.isPresent()) {") code.append(2 * tab + assign_fun("replacement.get()")) code.append(tab + "} else {") code.append(2 * tab + "{0}.substitute({1});".format(obj_expr, manager_name)) code.append(tab + "}") code.append("} else {") code.append(tab + "{0}.substitute({1});".format(obj_expr, manager_name)) code.append("}") return code
def gen_mangling_code_java(self, field_name, nodes_names, node_ref_name): if not self.visitable or self.ref_type not in nodes_names: return [] getter_name = "get{0}".format(first_to_cap(field_name)) if not self.optional: code = ["if ({0}.{1}() != null) {{".format(node_ref_name, getter_name)] node_expr = "{0}.{1}()".format(node_ref_name, getter_name) else: code = ["if ({0}.{1}() != null && {0}.{1}().isPresent()) {{".format(node_ref_name, getter_name)] node_expr = "{0}.{1}().get()".format(node_ref_name, getter_name) code.append(tab + "{0}.accept(this, null);".format(node_expr)) code.append("}") return code
def gen_printer(self, lang): res = None if lang == DST_LANGUAGE.CPP: res = 'printf("%s: %' if self.width == INT_TYPE.BYTE: res += "hhd" if self.width == INT_TYPE.SHORT: res += "hd" if self.width == INT_TYPE.NORMAL: res += "ld" if self.width == INT_TYPE.LONG: res += "lld" res += '\\n", "{0}", elem->get{0}());' if lang == DST_LANGUAGE.JAVA: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'System.out.printf("%s: %s; ", "{0}", elem.get{0}());\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' return res.format(first_to_cap(self.name))
def gen_printer(self, lang): res = None if lang == DST_LANGUAGE.CPP: res = 'printf("%s: [", "{0}");' res += tab * 2 + 'for(int i=0; i<elem->get{0}.size(); ++i) {{\n' res += tab * 3 + 'printf("%s ", elem->get{0}()[i].c_str());\n' res += tab * 2 + '}}\n' res += tab * 2 + 'printf("]\\n");' if lang == DST_LANGUAGE.JAVA: if not self.optional: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'System.out.printf("%s: [", "{0}");\n' res += tab * 3 + 'for(String sval : elem.get{0}()) {{\n' res += tab * 4 + 'System.out.printf("%s ", sval);\n' res += tab * 3 + '}}\n' res += tab * 3 + 'System.out.printf("]; ");\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' else: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'if (elem.get{0}().isPresent()) {{\n' res += tab * 4 + 'System.out.printf("%s: [", "{0}");\n' res += tab * 4 + 'for(String sval : elem.get{0}().get()) {{\n' res += tab * 5 + 'System.out.printf("%s ", sval);\n' res += tab * 4 + '}}\n' res += tab * 4 + 'System.out.printf("]; ");\n' res += tab * 3 + '}} else {{\n' res += tab * 4 + 'System.out.printf("%s: %s", "{0}", elem.get{0}());\n' res += tab * 3 + '}}\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' return res.format(first_to_cap(self.name))
def gen_printer(self, lang): res = None if lang == DST_LANGUAGE.CPP: res = 'printf("%s: [", "{0}");' res += tab * 2 + 'for(int i=0; i<elem->get{0}.size(); ++i) {{\n' res += tab * 3 + 'printf("%f ", elem->get{0}()[i]);\n' res += tab * 2 + '}}\n' res += tab * 2 + 'printf("]\\n");' if lang == DST_LANGUAGE.JAVA: if not self.optional: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'System.out.printf("%s: [", "{0}");\n' res += tab * 3 + 'for({1} fval : elem.get{0}()) {{\n' res += tab * 4 + 'System.out.printf("%s ", fval);\n' res += tab * 3 + '}}\n' res += tab * 3 + 'System.out.printf("]; ");\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' else: res = 'if (elem.get{0}() != null) {{\n' res += tab * 3 + 'if (elem.get{0}().isPresent()) {{\n' res += tab * 4 + 'System.out.printf("%s: [", "{0}");\n' res += tab * 4 + 'for({1} fval : elem.get{0}().get()) {{\n' res += tab * 5 + 'System.out.printf("%s ", fval);\n' res += tab * 4 + '}}\n' res += tab * 4 + 'System.out.printf("]; ");\n' res += tab * 3 + '}} else {{\n' res += tab * 4 + 'System.out.printf("%s: %s", "{0}", elem.get{0}());\n' res += tab * 3 + '}}\n' res += tab * 2 + '}} else {{\n' res += tab * 3 + 'System.out.printf("%s: null; ", "{0}");\n' res += tab * 2 + '}}' return res.format(first_to_cap(self.name), "Float" if self.width == FLOAT_TYPE.SINGLE else "Double")