def re_transpile(self): set_config("parsedlines", 0) set_config("exportedlines", 0) set_config("readfiles", 0) set_config("skippedfiles", 0) print() if os.path.isdir(arg): scan_directory(self.path, output_directory=get_config("output"), file_replacement=get_config("filereplace")) else: transpile_file(self.path, output_directory=get_config("output"), file_replacement=get_config("filereplace")) parsed_lines = get_config("parsedlines") exported_lines = get_config("exportedlines") number_of_files = get_config("readfiles") number_of_skipped_files = get_config("skippedfiles") print( f"Read {number_of_files} script files, parsed {parsed_lines} lines and exported {exported_lines} lines in {time_taken} seconds\a" )
def to_script(self): newline = "\n" space = " " tab = "\t" if get_config("minify") == True: newline = "" space = "" tab = "" output = "" for class_expression in self.class_expressions: output = output + class_expression.to_script() full_output = "new " + output output = "" for argument_expression in self.argument_expressions: output = output + argument_expression.to_script() full_output = full_output + "(" + output + ")" if len(self.expressions) > 0: output = "" for expression in self.expressions: output = output + (tab * self.get_indent_level() ) + expression.to_script() + newline full_output = full_output + space + "{" + newline + output + ( tab * (self.get_indent_level() - 1)) + "}" return full_output + self.handle_semicolon()
def to_script(self): space = " " if get_config("minify") == True and text_operators.match( self.operator) == None: space = "" return f"{space}{self.operator.strip()}{space}"
def to_script(self): full_output = "" newline = "\n" space = " " tab = "\t" if get_config("minify") == True: newline = "" space = "" tab = "" if self.type != "else": output = "" for conditional_expression in self.conditional_expressions: output = output + conditional_expression.to_script() full_output = self.type + "(" + output + ")" + space + "{" + newline else: full_output = self.type + space + "{" + newline output = "" for expression in self.expressions: output = output + (tab * self.get_indent_level() ) + expression.to_script() + newline full_output = full_output + output + ( tab * (self.get_indent_level() - 1)) + "}" return full_output
def to_script(self): full_output = "" newline = "\n" space = " " tab = "\t" if get_config("minify") == True: newline = "" space = "" tab = "" output = " " for argument_expression in self.argument_expressions: output = output + argument_expression.to_script() + "," + space full_output = "function " + self.name_symbol.to_script( ) + "(" + re.sub(r',$', '', output.strip()) + ")" + space + "{" + newline output = "" for expression in self.expressions: output = output + (tab * self.get_indent_level() ) + expression.to_script() + newline full_output = full_output + output + ( tab * (self.get_indent_level() - 1)) + "}" return full_output
def to_script(self): newline = "\n" if get_config("minify") == True: newline = "" output = "" for expression in self.expressions: output = output + expression.to_script() + newline return output
def to_script(self): space = " " if get_config("minify") == True: space = "" value = " " for argument in self.argument_expressions: value = (value + argument.to_script() + "," + space) return f"{self.method_symbol.to_script()}({re.sub(r',$', '', value.strip())}){self.handle_semicolon()}"
def transpile_file(filename, output_directory="./", file_replacement="{}.cs"): path = Path(filename) if has_been_modified(path.absolute().__str__()): if get_config("verbose") == True: print(f"Parsing '{path.absolute()}'") file = File(filename) script_file = ScriptFile(filename) tokenizer = Tokenizer(file) tokenizer.tokenize(tree=script_file) script = script_file.to_script() output_filename = "" try: output_filename = file_replacement.format(path.stem) except: print(f"Could not format file using '{file_replacement}'") return output = Path(f"{output_directory}/{output_filename}") try: os.makedirs(output.parent.absolute().__str__()) except: pass add_to_cache(path.absolute().__str__()) file = open(output.absolute(), "w") file.write(script) add_exported_lines(script.count("\n") + 1) add_read_file() file.close() else: if get_config("verbose") == True: print(f"Skipped '{path.absolute()}'") add_skipped_file()
def scan_directory(directory, output_directory="./", file_replacement="{}.cs"): original_path = Path(directory) for root, subdirs, files in os.walk(directory): root_path = Path(root) for filename in files: path = Path(f"{root}/{filename}") if path.suffix == ".egg" or (get_config("includecs") and path.suffix == ".cs"): true_output_directory = output_directory + root_path.absolute( ).__str__().replace(original_path.absolute().__str__(), "") transpile_file(f"{root}/{filename}", output_directory=true_output_directory, file_replacement=file_replacement)
def to_script(self): space = " " if get_config("minify") == True: space = "" output = "" for index in range(0, len(self.strings) - 1): expression_output = "" for expression in self.templates[index]: expression_output = expression_output + expression.to_script() output = output + self.strings[ index] + f'"{space}@{space}({expression_output}){space}@{space}"' output = output + self.strings[len(self.strings) - 1] return f'"{output}"'
def load_cache(): try: os.mkdir("./.eggscript") except: pass try: global cache cache = json.load(open("./.eggscript/file_cache.json", "r")) except: print("Could not open cache.") if should_reset_cache(): if get_config("verbose") == True: print("Reset cache b/c arguments not the same") cache = {}
def to_script(self): newline = "\n" space = " " tab = "\t" if get_config("minify") == True: newline = "" space = "" tab = "" full_output = f"default:" + newline output = "" for expression in self.expressions: output = output + (tab * self.get_indent_level() ) + expression.to_script() + newline full_output = full_output + output return full_output
def should_reset_cache(): global cache_options if "config" not in cache: cache["config"] = [] if get_config("force") == True: return True if len(cache["config"]) != len(cache_options): return True else: list1 = sorted(cache["config"]) list2 = sorted(cache_options) # go though cache options and make sure we have everything for option_name, option_value in list1: if get_option_from_tuple(option_name, list2) != option_value: print(list2[option_name], "!=", option_value) return True return False
def to_script(self): full_output = "" newline = "\n" space = " " tab = "\t" if get_config("minify") == True: newline = "" space = "" tab = "" initiation_output = "" for initiation_expression in self.initiation_expressions: initiation_output = initiation_output + initiation_expression.to_script( ) conditional_output = "" for conditional_expression in self.conditional_expressions: conditional_output = conditional_output + conditional_expression.to_script( ) increment_output = "" for increment_expression in self.increment_expressions: increment_output = increment_output + increment_expression.to_script( ) full_output = f"for({initiation_output};{space}{conditional_output};{space}{increment_output})" full_output = full_output + space + "{" + newline output = "" for expression in self.expressions: output = output + (tab * self.get_indent_level() ) + expression.to_script() + newline full_output = full_output + output + ( tab * (self.get_indent_level() - 1)) + "}" return full_output
def to_script(self): full_output = "" newline = "\n" space = " " tab = "\t" if get_config("minify") == True: newline = "" space = "" tab = "" full_output = "package " + self.name_symbol.to_script( ) + space + "{" + newline output = "" for expression in self.expressions: output = output + (tab * self.get_indent_level() ) + expression.to_script() + newline full_output = full_output + output + ( tab * (self.get_indent_level() - 1)) + "};" return full_output
def warn(expression, message): if get_config("no-warnings") != True: print( f"\033[93m{message} at line #{expression.current_line_index} character #{expression.current_index} file '{expression.current_file_name}'\033[0m" )
def add_cache_option(config_option): global cache_options cache_options.append((config_option, get_config(config_option)))
def to_script(self): space = " " if get_config("minify") == True: space = "" return f"{self.child_class.to_script()}{space}:{space}{self.super_class.to_script()}"
def tokenize(self, stop_ats=[], give_back_stop_ats=[], buffer_give_back_stop_at=[], inheritable_give_back_stop_at=[], tree=None, read_spaces=False, vector_mode=False): self.buffer = "" while True: char = '' try: char = self.file.read_character( ignore_whitespace=not read_spaces) except: break try: # handle keyword matching (function, for, if, etc) if (vector_mode == False and regex.keywords.match(self.buffer) and regex.keywords.match(self.buffer + char) == None and (regex.valid_symbol.match(self.buffer + char) == None or self.file.skipped_space) and tree.no_keywords_in_code_block == False): for keyword_regex, expression_class in keyword_regexes.items( ): if keyword_regex.match(self.buffer): self.add_expression( tree, expression_class.read_expression(self, tree)) self.buffer = "" break continue for stop_at in buffer_give_back_stop_at: if stop_at.match(self.buffer): self.file.current_index = self.file.current_index - len( self.buffer) - 2 self.buffer = "" return tree for stop_at in give_back_stop_ats: if stop_at.match(char): self.absorb_buffer(tree) self.file.give_character_back() return tree for stop_at in stop_ats: if stop_at.match(char): self.absorb_buffer(tree) return tree if ((regex.operator_token.match(char) and (self.file.current_line_index > self.operator_ban[0] or self.file.current_index > self.operator_ban[1])) or (vector_mode and regex.vector_cross_token.match(char)) ): # handle operators if regex.comma_token.match( char): # handle commas in special case if tree.has_arguments: self.absorb_buffer(tree) tree.convert_expressions_to_arguments() continue elif vector_mode == False: operator = OperatorExpression.read_expression(self) if operator != None: self.absorb_buffer(tree) if regex.valid_comment.match(operator.operator): comment = Comment.read_expression(self) if get_config("nocomments") != True: self.add_expression(tree, comment) elif regex.valid_assignment.match( operator.operator): # take last expression and use that as left hand for variable assignment last_expression = tree.expressions.pop() new_expression = VariableAssignmentExpression.read_expression( self, operator, last_expression, stop_ats) last_expression.parent = new_expression self.add_expression(tree, new_expression) elif regex.valid_postfix.match(operator.operator): # take last expression and use that for postfix operation last_expression = tree.expressions.pop() new_expression = PostfixExpression( last_expression, operator, tokenizer=self) last_expression.parent = new_expression self.add_expression(tree, new_expression) elif regex.valid_template_string.match( operator.operator): self.add_expression( tree, StringLiteral.read_expression( self, is_template=True)) else: self.add_expression(tree, operator) continue elif vector_mode == True: # when in vector mode, don't attempt to read a multi-character operator and instead dump what we find straight to expression if regex.valid_vector_operators.match(char): self.absorb_buffer(tree) self.add_expression( tree, OperatorExpression(char, tokenizer=self)) continue if (regex.chaining_token.match(char) and regex.valid_symbol.match(self.buffer) and regex.valid_symbol.match( self.file.peek_next_character()) or (len(tree.expressions) > 0 and tree.expressions[-1].is_chainable and regex.valid_symbol.match(self.buffer) == None)): # handle chaining (%test.test.test.test...) self.add_expression( tree, ChainingExpression.read_expression( self, tree, inheritable_give_back_stop_at, vector_mode=vector_mode)) self.buffer = "" # absorb buffer elif regex.namespace_token.match( char): # handle namespaces ($Test::frog:egg...) if regex.valid_symbol.match(self.buffer): # if we have a valid symbol, then build chaining expression from remaining valid symbols self.add_expression( tree, NamespaceExpression.read_expression( self, inheritable_give_back_stop_at, vector_mode=vector_mode)) self.buffer = "" # absorb buffer else: raise Exception( f"Invalid symbol for namespace expression '{self.buffer}'" ) elif regex.semicolon_token.match(char): # just absorb the character and move on, we have automatic semicolon placement self.absorb_buffer(tree) elif regex.string_token.match( char): # handle strings (except for template strings) self.add_expression(tree, StringLiteral.read_expression(self)) elif regex.vector_token.match( char): # handle vector operations self.add_expression(tree, VectorExpression.read_expression(self)) elif regex.parentheses_token.match(char): if regex.opening_parenthesis_token.match( char) and regex.valid_symbol.match( self.buffer ) == None: # handle math parentheses self.add_expression( tree, ParenthesesExpression.read_expression( self, vector_mode=vector_mode)) elif regex.opening_parenthesis_token.match( char ) and regex.valid_symbol.match( self.buffer) != None: # handle method parentheses self.add_expression( tree, MethodExpression.read_expression( self, vector_mode=vector_mode)) elif regex.opening_bracket_token.match( char): # handle array accessing self.add_expression( tree, ArrayAccessExpression.read_expression(self)) self.buffer = "" elif (vector_mode and regex.vector_dot_token.match(char) ): # handle vector dots in special case self.add_expression( tree, OperatorExpression(char, tokenizer=self)) self.buffer = "" elif (vector_mode and regex.vector_escape_token.match(char)): self.add_expression( tree, VectorEscapeExpression.read_expression(self)) self.buffer = "" elif (vector_mode and regex.vector_length_token.match(char)): self.add_expression( tree, VectorLengthExpression.read_expression(self)) self.buffer = "" else: # when in doubt, add to buffer self.buffer = self.buffer + char except Exception as error: if get_config("verbose") == True: traceback.print_exc() print( f"\033[91mEncountered exception '{error.__str__()}' at line #{self.file.current_line_index} character #{self.file.current_index}\033[0m" ) return None return tree
add_cache_option("nocomments") elif option == "-w" or option == "--watch": set_config("watch", True) elif option == "--no-warnings": set_config("no-warnings", True) load_cache() # go through args and figure out what to do with them for arg in args: if os.path.exists(arg): start = time() if os.path.isdir(arg): scan_directory(arg, output_directory=get_config("output"), file_replacement=get_config("filereplace")) else: transpile_file(arg, output_directory=get_config("output"), file_replacement=get_config("filereplace")) parsed_lines = get_config("parsedlines") exported_lines = get_config("exportedlines") number_of_files = get_config("readfiles") number_of_skipped_files = get_config("skippedfiles") time_taken = "{:.2f}".format(round(time() - start, 2)) save_cache() print(
def to_script(self): if get_config( "nocomments") != True and self.parent.is_code_block == False: return self.comment + "\n" else: return self.comment
def process_IN_CLOSE_WRITE(self, event): if get_config("verbose") == True: print(f"IN_CLOSE_WRITE for watcher '{self.path}'") self.handle_event(event)
def process_IN_MOVED_TO(self, event): if get_config("verbose") == True: print(f"IN_MOVED_TO for watcher '{self.path}'") self.handle_event(event)