Skip to content

Commit 3c5f87e

Browse files
committed
Merge branch 'imports'
2 parents 0a17055 + 34cb049 commit 3c5f87e

File tree

13 files changed

+161
-32
lines changed

13 files changed

+161
-32
lines changed

Examples/imports.owpy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import "lib/child"
2+
3+
Rule "Debug Import"
4+
Actions
5+
debug_func("Hello!")

Examples/lib/child.owpy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import 'lib2/child2.owpy'
2+
3+
%debug_func(text)
4+
Msg(Everyone, text)
5+
nested(Lucio)

Examples/lib/lib2/child2.owpy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%nested(hero_)
2+
Set Hero(Event Player, hero_)

OWScript.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import os
23
import re
34
import sys
45
import time
@@ -7,7 +8,7 @@
78
from OWScript.Parser import Parser
89
from OWScript.Transpiler import Transpiler
910

10-
def transpile(text, args):
11+
def transpile(text, path, args):
1112
"""Transpiles an OWScript code into Overwatch Workshop rules."""
1213
start = time.time()
1314
Errors.TEXT = text
@@ -23,7 +24,7 @@ def transpile(text, args):
2324
tree = parser.script()
2425
if args.tree:
2526
print(tree.string())
26-
transpiler = Transpiler(tree=tree)
27+
transpiler = Transpiler(tree=tree, path=path)
2728
code = transpiler.run()
2829
if args.min:
2930
code = re.sub(r'[\s\n]*', '', code)
@@ -50,10 +51,15 @@ def transpile(text, args):
5051
parser.add_argument('--tokens', action='store_true', help='Debug: shows the tokens created by the lexer')
5152
parser.add_argument('--tree', action='store_true', help='Debug: visualizes the AST generated by the parser')
5253
args = parser.parse_args()
53-
file_input = args.input[0] if args.input else sys.stdin
54-
with open(file_input) as f:
55-
text = f.read()
54+
if args.input:
55+
file_input = args.input[0]
56+
path = os.path.abspath(file_input)
57+
with open(path) as f:
58+
text = f.read()
59+
else:
60+
text = sys.stdin.read()
61+
path = os.getcwd()
5662
try:
57-
transpile(text, args=args)
63+
transpile(text, path=path, args=args)
5864
except Errors.OWSError as ex:
5965
print('Error:', ex)

OWScript/AST.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ def __init__(self, code):
9494
def __repr__(self):
9595
return '<Raw {}>'.format(len(self.code))
9696

97+
class Import(AST):
98+
def __init__(self, path):
99+
self.path = path
100+
101+
def __repr__(self):
102+
return '#import {}'.format(self.path)
103+
97104
# Workshop Types
98105
class WorkshopType(AST):
99106
@classmethod

OWScript/Errors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class LexError(OWSError):
1616
class ParseError(OWSError):
1717
pass
1818

19+
class ImportError(OWSError):
20+
pass
21+
1922
class SyntaxError(OWSError):
2023
pass
2124

OWScript/Importer.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from . import Errors
2+
from .Lexer import Lexer
3+
from .Parser import Parser
4+
5+
def import_file(path):
6+
error_text = Errors.TEXT
7+
with open(path) as f:
8+
text = f.read()
9+
try:
10+
Errors.TEXT = text
11+
lexer = Lexer(text=text)
12+
tokens = lexer.lex()
13+
parser = Parser(tokens=tokens)
14+
tree = parser.script()
15+
Errors.TEXT = error_text
16+
return tree
17+
except Exception as ex:
18+
Errors.TEXT = error_text
19+
raise ex

OWScript/Lexer.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import functools
22
import re
33

4-
try:
5-
from . import Errors
6-
from .Tokens import Token, Tokens
7-
except ImportError:
8-
import Errors
9-
from Tokens import Token, Tokens
4+
from . import Errors
5+
from .Tokens import Token, Tokens
106

117
class Lexer:
128
IGNORE = ('WHITESPACE', 'SEMI', 'COMMENT', 'ANNOTATION')

OWScript/Parser.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
from collections import deque
33
from functools import partial
44

5-
try:
6-
from . import Errors
7-
from .AST import *
8-
from .Tokens import ALIASES
9-
from .Workshop import *
10-
except ImportError:
11-
import Errors
12-
from AST import *
5+
from . import Errors
6+
from .AST import *
7+
from .Tokens import ALIASES
8+
from .Workshop import *
139

1410
class Parser:
1511
def __init__(self, tokens):
@@ -105,11 +101,13 @@ def script(self):
105101
return node
106102

107103
def stmt(self):
108-
"""stmt : (funcdef | ruledef | line)"""
104+
"""stmt : (funcdef | ruledef | importdef | line)"""
109105
if self.curvalue == '%':
110106
return self.funcdef()
111107
elif self.curtype in ('DISABLED', 'RULE'):
112108
return self.ruledef()
109+
elif self.curtype == 'IMPORT':
110+
return self.importdef()
113111
else:
114112
return self.line()
115113

@@ -207,6 +205,16 @@ def block(self):
207205
node.children.append(line)
208206
return node
209207

208+
def importdef(self):
209+
"""importdef : #import STRING"""
210+
self.eat('IMPORT')
211+
path = self.curvalue.strip('\'').strip('"').replace('/', '\\').rstrip('.owpy')
212+
pos = self.curpos
213+
self.eat('STRING')
214+
node = Import(path=path)
215+
node._pos = pos
216+
return node
217+
210218
def line(self):
211219
"""line :
212220
( if_stmt

OWScript/Tokens.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Tokens:
8585
RBRACK : r'\]'
8686
STRING : r'("[^\\\r\n\f]*?"|\'[^\\\r\n\f]*?\')'
8787
F_STRING : r'(`[^\\\r\n\f]*?`)'
88+
IMPORT : r'#IMPORT\b'
8889
IF : r'IF\b'
8990
ELIF : r'ELIF\b'
9091
ELSE : r'ELSE\b'

0 commit comments

Comments
 (0)