Skip to content

Commit

Permalink
Fixed parenthesis logic
Browse files Browse the repository at this point in the history
parenthesis are now closed with the right order
  • Loading branch information
maxpat78 committed Oct 25, 2024
1 parent b1645c2 commit 89423a9
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions w32lex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
COPYRIGHT = '''Copyright (C)2024, by maxpat78.'''

__version__ = '1.0.5'
__version__ = '1.0.6'

import os

Expand Down Expand Up @@ -163,8 +163,7 @@ def cmd_parse(s, mode=SPLIT_SHELL32|CMD_VAREXPAND):
quoted = 0
percent = 0
exclamation = 0
leftP = [] # opened-closed parenthesis (position)
rightP = []
parenthesis = [] # opened parenthesis (argv position)
arg = ''
argv = []

Expand Down Expand Up @@ -200,13 +199,22 @@ def cmd_parse(s, mode=SPLIT_SHELL32|CMD_VAREXPAND):
else:
escaped = 1
continue
if c in '()' and not (escaped or quoted):
if c == '(' and not (escaped or quoted):
if arg:
argv += [arg]
arg = ''
argv += [c]
o = (leftP,rightP)[c==')']
o += [len(argv)-1]
parenthesis += [len(argv)-1]
continue
if c == ')' and not (escaped or quoted):
if arg:
argv += [arg]
arg = ''
if not parenthesis:
raise NotExpected(')')
last_opened = parenthesis.pop()
# replaces parenthesized trait with a single argument
argv[last_opened:] = [''.join(argv[last_opened:])+')']
continue
# %VAR% -> replace with os.environ['VAR'] *if set* and even if quoted
# ^%VAR% -> same as above
Expand Down Expand Up @@ -281,19 +289,9 @@ def cmd_parse(s, mode=SPLIT_SHELL32|CMD_VAREXPAND):
arg += c
escaped = 0
if arg: argv += [arg]
# if parenthesis are not balanced
if len(rightP) != len(leftP):
raise NotExpected(('(',')')[len(rightP)>len(leftP)])
if leftP:
# replace from innermost couple
leftP = list(reversed(leftP))
rightP = list(reversed(rightP))
l = len(leftP)-1
for i in range(l+1):
j = leftP[i]
k = rightP[l-i]
# replace list items with their concatenation
argv[j:k+1] = [''.join(argv[j:k+1])]
# if any unclosed parenthesis
if parenthesis:
raise NotExpected('(')
return argv

def cmd_split(s, mode=SPLIT_SHELL32):
Expand Down

0 comments on commit 89423a9

Please sign in to comment.