Skip to content

Instantly share code, notes, and snippets.

@dsblank
Created November 8, 2014 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsblank/17b27a3d219766a6993e to your computer and use it in GitHub Desktop.
Save dsblank/17b27a3d219766a6993e to your computer and use it in GitHub Desktop.
Some code to split a Unix-like command-line
def splitParts(text, stack):
# OOP, needed to protect raw-objects
if debug: print("splitParts:", text, stack)
try:
length = len(text)
except:
return [text]
retval = []
i = 0
current = makeAnnotatedString("")
mode = "start"
while i < length:
if mode == "start":
if text[i] == '\\': ## remove special chars
i += 1
current = addAnnotatedStrings(current, escaped(text[i], remove_special=True), i)
#if text[i] == "\\":
# if text[i+1] == "n":
# current = addAnnotatedStrings(current, "\n", i)
# elif text[i+1] == "t":
# current = addAnnotatedStrings(current, "\t", i)
# else:
# current = addAnnotatedStrings(current, "\\" + text[i+1], i)
# i += 1
elif text[i].startswith("#"):
# ignore here to end of line
break
elif text[i] == " ":
if current:
current.end = i
retval.extend(expand(current, stack))
current = makeAnnotatedString("")
else:
pass ## skip
elif text[i] == "=":
if current:
current.end = i
retval.append(current)
current = makeAnnotatedString("")
retval.append(makeAnnotatedString('=', i, i + 1))
elif text[i] == "'":
if current:
current.end = i
retval.append(current)
current = makeAnnotatedString("", i)
mode = "quote"
elif text[i] == '"':
if current:
current.end = i
retval.append(current)
current = makeAnnotatedString("", i)
mode = "double-quote"
elif text[i] == '`':
if current:
current.end = i
retval.append(current)
# signal expand that this is an expr:
current = makeAnnotatedString("`", i)
mode = "back-quote"
else:
current = addAnnotatedStrings(current, text[i], i)
elif mode == "quote":
if text[i] == "'":
current.end = i
retval.append(current)
current = makeAnnotatedString("", -1)
mode = "start"
elif text[i] == '\\': ## replace with special char
i += 1
current = addAnnotatedStrings(current, escaped(text[i]), i)
#elif text[i] == '\\':
# if text[i+1] == "n":
# current = addAnnotatedStrings(current, "\n", i)
# i += 1
# elif text[i+1] == "t":
# current = addAnnotatedStrings(current, "\t", i)
# i += 1
# else:
# current = addAnnotatedStrings(current, text[i], i)
else:
current = addAnnotatedStrings(current, text[i], i)
elif mode == "double-quote":
if text[i] == '"':
current.end = i
retval.append(current)
current = makeAnnotatedString("", -1)
mode = "start"
elif text[i] == '\\': ## replace with special char
i += 1
current = addAnnotatedStrings(current, escaped(text[i]), i)
# if text[i+1] == "n":
# current = addAnnotatedStrings(current, "\n", i)
# i += 1
# elif text[i+1] == "t":
# current = addAnnotatedStrings(current, "\t", i)
# i += 1
# else:
# current = addAnnotatedStrings(current, text[i], i)
else:
current = addAnnotatedStrings(current, text[i], i)
elif mode == "back-quote":
if text[i] == '`':
current.end = i
retval.extend(expand(current, stack))
current = makeAnnotatedString("", -1)
mode = "start"
elif text[i] == '\\': ## replace with special char
i += 1
current = addAnnotatedStrings(current, escaped(text[i]), i)
#elif text[i] == '\\':
# if text[i+1] == "n":
# current = addAnnotatedStrings(current, "\n", i)
# i += 1
# elif text[i+1] == "t":
# current = addAnnotatedStrings(current, "\t", i)
# i += 1
# else:
# current = addAnnotatedStrings(current, text[i], i)
else:
current = addAnnotatedStrings(current, text[i], i)
i += 1
if current: # some still left:
if mode == "start":
current.end = i
retval.extend(expand(current, stack))
elif mode == "quote":
raise ConsoleException("Console: unended quote", stack)
elif mode == "double-quote":
raise ConsoleException("Console: unended double-quote", stack)
elif mode == "back-quote":
raise ConsoleException("Console: unended back-quote: leftover: '%s'" % current, stack)
return retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment