11import argparse
2- import os
3- import re
42import sys
5- from antlr4 import *
6- from src .OWScriptLexer import OWScriptLexer
7- from src .OWScriptParser import OWScriptParser
8- from src .ASTBuilder import ASTBuilder
9- from src .Transpiler import Transpiler
3+ from OWScript .Lexer import Lexer
4+ from OWScript .Parser import Parser
5+ from OWScript .Transpiler import Transpiler
6+
7+ class DEBUG :
8+ """Bit flags used for debug code."""
9+ TOKENS = 1
10+ TREE = 2
11+
12+ def transpile (text , minify = False , save = None , debug = 0 ):
13+ """Transpiles an OWScript code into Overwatch Workshop rules."""
14+ lexer = Lexer (text = text )
15+ tokens = lexer .lex ()
16+ if debug & DEBUG .TOKENS :
17+ lexer .print_tokens ()
18+ parser = Parser (tokens = tokens )
19+ tree = parser .script ()
20+ if debug & DEBUG .TREE :
21+ print (tree .string ())
22+ transpiler = Transpiler (tree = tree )
23+ code = transpiler .run ()
24+ print (code )
25+
1026
11- class UppercaseStream (FileStream ):
12- def _loadString (self ):
13- self ._index = 0
14- self .data = [ord (c .upper ()) for c in self .strdata ]
15- self ._size = len (self .data )
16-
17- def process (code , minify = False , save = None ):
18- input_stream = UppercaseStream (code , 'utf-8' )
19- lexer = OWScriptLexer (input_stream )
20- stream = CommonTokenStream (lexer )
21- parser = OWScriptParser (stream )
22- ast = ASTBuilder ().run (parser .script ())
23- # print(ast)
24- output = Transpiler ().run (ast )
25- if minify :
26- output = re .sub (r'[\s\n]*' , '' , output )
27- if not save :
28- sys .stdout .write (output )
29- else :
30- with open (save , 'w' ) as f :
31- f .write (output )
32-
3327if __name__ == '__main__' :
34- sys .path .append (os .path .join (os .getcwd (), 'src' ))
3528 parser = argparse .ArgumentParser (description = 'Generate Overwatch Workshop code from OWScript' )
3629 parser .add_argument ('input' , nargs = '*' , type = str , help = 'Standard input to process' )
3730 parser .add_argument ('-m' , '--min' , action = 'store_true' , help = 'Minifies the output by removing whitespace' )
3831 parser .add_argument ('-s' , '--save' , help = 'Save the output to a file instead of printing it' )
32+ parser .add_argument ('-d' , '--debug' , type = int , help = 'Debugging tool used for development' )
3933 args = parser .parse_args ()
40- if not args .input :
41- process (sys .stdin )
42- for file in args .input :
43- process (file , minify = args .min , save = args .save )
34+ file_input = args .input [0 ] if args .input else sys .stdin
35+ with open (file_input ) as f :
36+ text = f .read ()
37+ transpile (text , minify = args .min , save = args .save , debug = args .debug )
38+
0 commit comments