def gen(self, custom_exceptions, ast): return generate(Node('module', definitions=[], dependencies=[], constants=[], custom_exceptions=custom_exceptions, main=ast if isinstance(ast, list) else [ast]), self._language).rstrip()
def gen(self, custom_exceptions, ast): return generate( Node('module', definitions=[], dependencies=[], constants=[], custom_exceptions=custom_exceptions, main=ast if isinstance(ast, list) else [ast]), self._language).rstrip()
def main(): if len(sys.argv) == 1: print(USAGE) return filename = sys.argv[1] with open(filename, 'r') as f: source = f.read() base, _ = os.path.splitext(filename) try: if len(sys.argv) == 2: clj = pseudo_java.translate_to_yaml(filename,source) with open('%s.pseudo.yaml' % base, 'w') as f: f.write(clj) print(colored('OK\nsaved pseudo ast as %s.pseudo.yaml' % base, 'green')) else: arg = sys.argv[2] if '.' in arg: base, language = os.path.splitext(arg) language = language[1:] else: language = arg if language not in pseudo.SUPPORTED_FORMATS: print(colored('%s is not supported' % language, 'red')) exit(1) if '%s.%s' % (base, pseudo.FILE_EXTENSIONS[language]) == filename: print(colored('this would overwrite the input file, please choose another name', 'red')) exit(1) node = pseudo_java.translate(filename,source,sys.argv[2:]) output = pseudo.generate(node, language) with open('%s.%s' % (base, pseudo.FILE_EXTENSIONS[language]), 'w') as f: f.write(output) print(colored('OK\nsaved as %s.%s' % (base, pseudo.FILE_EXTENSIONS[language]), 'green')) except pseudo_java.errors.PseudoError as e: print(colored(e, 'red')) if e.suggestions: print(colored(e.suggestions, 'green')) if e.right: print(colored('\nright:\n%s' % e.right, 'green')) if e.wrong: print(colored('\nwrong:\n%s' % e.wrong, 'red')) exit(1) except pseudo.errors.PseudoError as e: print(colored('Pseudo error:\n%s' % e, 'red')) exit(1)
def main(): if len(sys.argv) == 1: print(USAGE) return filename = sys.argv[1] with open(filename, 'r') as f: source = f.read() base, _ = os.path.splitext(filename) try: if len(sys.argv) == 2: clj = pseudo_python.translate_to_yaml(source) with open('%s.pseudo.yaml' % base, 'w') as f: f.write(clj) print(colored('OK\nsaved pseudo ast as %s.pseudo.yaml' % base, 'green')) else: arg = sys.argv[2] if '.' in arg: base, language = os.path.splitext(arg) language = language[1:] else: language = arg if language not in pseudo.SUPPORTED_FORMATS: print(colored('%s is not supported' % language, 'red')) exit(1) if '%s.%s' % (base, pseudo.FILE_EXTENSIONS[language]) == filename: print(colored('this would overwrite the input file, please choose another name', 'red')) exit(1) node = pseudo_python.translate(source) output = pseudo.generate(node, language) with open('%s.%s' % (base, pseudo.FILE_EXTENSIONS[language]), 'w') as f: f.write(output) print(colored('OK\nsaved as %s.%s' % (base, pseudo.FILE_EXTENSIONS[language]), 'green')) except pseudo_python.errors.PseudoError as e: print(colored(e, 'red')) if e.suggestions: print(colored(e.suggestions, 'green')) if e.right: print(colored('\nright:\n%s' % e.right, 'green')) if e.wrong: print(colored('\nwrong:\n%s' % e.wrong, 'red')) exit(1) except pseudo.errors.PseudoError as e: print(colored('Pseudo error:\n%s' % e, 'red')) exit(1)
def gen_with_imports(self, custom_exceptions, ast): if isinstance(ast, Node): if ast.type == 'block': e = ast.block else: e = [ast] elif isinstance(ast, list): e = ast else: e = [ast] definitions, main = [], [] for node in e: if node.type.endswith('_definition'): definitions.append(node) else: main.append(node) result = generate( Node('module', definitions=definitions, custom_exceptions=custom_exceptions, dependencies=[], constants=[], main=main), self._language) if hasattr(self, 'gen_special'): # input(result) return self.gen_special(result) ls = result.split('\n') l = 0 imports = [] while ls[l].startswith(self._import): imports.append(self._parse_import(ls[l]).strip()) l += 1 if not ls[l].strip(): l += 1 source = '\n'.join(ls[l:]) if not hasattr(self, '_no_strip'): source = source.strip() if hasattr(self, '_ignore_import'): imports.remove(self._ignore_import) return imports, source
def gen_with_imports(self, custom_exceptions, ast): result = generate(Node('module', definitions=[], dependencies=[], custom_exceptions=custom_exceptions, constants=[], main=ast if isinstance(ast, list) else [ast]), self._language).rstrip() l = 0 lines = result.split('\n') imports = [] while l < len(lines) and lines[l].startswith("var ") and 'require' in lines[l]: require_index = lines[l].index('require') imports.append(lines[l][require_index + 9:-3]) l += 1 source = lines[l:] if len(source) == 1 and source[0][-1] == ';': return imports, source[0][:-1] else: return imports, '\n'.join(source).strip()
def gen_with_imports(self, custom_exceptions, ast): result = generate( Node('module', definitions=[], dependencies=[], custom_exceptions=custom_exceptions, constants=[], main=ast if isinstance(ast, list) else [ast]), self._language).rstrip() l = 0 lines = result.split('\n') imports = [] while l < len(lines) and lines[l].startswith( "var ") and 'require' in lines[l]: require_index = lines[l].index('require') imports.append(lines[l][require_index + 9:-3]) l += 1 source = lines[l:] if len(source) == 1 and source[0][-1] == ';': return imports, source[0][:-1] else: return imports, '\n'.join(source).strip()
def gen_with_imports(self, custom_exceptions, ast): if isinstance(ast, Node): if ast.type == 'block': e = ast.block else: e = [ast] elif isinstance(ast, list): e = ast else: e = [ast] definitions, main = [], [] for node in e: if node.type.endswith('_definition'): definitions.append(node) else: main.append(node) result = generate(Node('module', definitions=definitions, custom_exceptions=custom_exceptions, dependencies=[], constants=[], main=main), self._language) if hasattr(self, 'gen_special'): # input(result) return self.gen_special(result) ls = result.split('\n') l = 0 imports = [] while ls[l].startswith(self._import): imports.append(self._parse_import(ls[l]).strip()) l += 1 if not ls[l].strip(): l += 1 source = '\n'.join(ls[l:]) if not hasattr(self, '_no_strip'): source = source.strip() if hasattr(self, '_ignore_import'): imports.remove(self._ignore_import) return imports, source