Skip to content

Commit

Permalink
Fix bug when there is a comma in GET SUBS
Browse files Browse the repository at this point in the history
  • Loading branch information
enekomartinmartinez committed Jul 8, 2024
1 parent 00cc755 commit 3e2ecf5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
4 changes: 1 addition & 3 deletions pysd/translators/vensim/parsing_grammars/lookups.peg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

lookup = _ "(" _ (regularLookup / excelLookup) _ ")"
regularLookup = limits? _ ( "(" _ number _ "," _ number _ ")" _ ","? _ )+
excelLookup = ~"GET( |_)(XLS|DIRECT)( |_)LOOKUPS"I _ "(" (args _ ","? _)+ ")"

args = ~r"[^,()]*"
excelLookup = ~"GET( |_)(XLS|DIRECT)( |_)LOOKUPS"I _ "(" _ (string _ ","? _)+ ")"
35 changes: 21 additions & 14 deletions pysd/translators/vensim/vensim_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def __init__(self, ast):
self.subscripts = []
self.subscripts_except = []
self.subscripts_except_groups = []
self.qargs = []
self.name = None
self.expression = None
self.keyword = None
Expand Down Expand Up @@ -166,14 +167,17 @@ def visit_keyword(self, n, vc):
self.keyword = n.text.strip()[1:-1].lower().replace(" ", "_")

def visit_imported_subscript(self, n, vc):
self.subscripts = {
arg_name: argument.strip().strip("'")
for arg_name, argument
in zip(
("file", "tab", "firstcell", "lastcell", "prefix"),
vc[4].split(",")
)
}
self.subscripts = dict(
file=self.qargs[0],
tab=self.qargs[1],
firstcell=self.qargs[2],
lastcell=self.qargs[3],
prefix=self.qargs[4]
)

def visit_string(self, n, vc):
self.qargs.append(vc[1])
return vc[1]

def visit_subscript_copy(self, n, vc):
self.component = SubscriptRange(self.name, vc[4].strip())
Expand Down Expand Up @@ -635,6 +639,7 @@ class LookupsVisitor(parsimonious.NodeVisitor):
"""Visit the elements of a lookups to get the AST"""
def __init__(self, ast):
self.translation = None
self.qargs = []
self.visit(ast)

def visit_limits(self, n, vc):
Expand All @@ -658,15 +663,17 @@ def visit_regularLookup(self, n, vc):
)

def visit_excelLookup(self, n, vc):
arglist = vc[3].split(",")

self.translation = structures["get_xls_lookups"](
file=eval(arglist[0]),
tab=eval(arglist[1]),
x_row_or_col=eval(arglist[2]),
cell=eval(arglist[3])
file=self.qargs[0],
tab=self.qargs[1],
x_row_or_col=self.qargs[2],
cell=self.qargs[3]
)

def visit_string(self, n, vc):
self.qargs.append(vc[1])
return vc[1]

def generic_visit(self, n, vc):
return "".join(filter(None, vc)) or n.text

Expand Down

0 comments on commit 3e2ecf5

Please sign in to comment.