Created
November 8, 2014 15:36
-
-
Save dsblank/17b27a3d219766a6993e to your computer and use it in GitHub Desktop.
Some code to split a Unix-like command-line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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