Created
August 10, 2014 13:52
-
-
Save ciiqr/563007f162e33dbf8b85 to your computer and use it in GitHub Desktop.
Eats up swift style (recursive multiline) comments
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
# Simulated just matched /* | |
stream = " /* /* /* /* */ */ */ */ /* */ /* */ */ /**/" | |
# stream = "/*/*/*/**/*/*/*//**//**/*/ /**/" | |
# stream = """ | |
# /* | |
# Some random comment | |
# */ | |
# // More text | |
# var i = "5"; | |
# """ | |
# Simulates my input stream | |
def input(): | |
global stream | |
nextChar = stream[0] | |
stream = stream[1:] | |
return nextChar | |
# Constants | |
SLASH = "/" | |
STAR = "*" | |
# Variables | |
previousChar = None | |
commentLevel = 1 # Change to 0 to match entire comments "/* */", otherwise assumes we've already matched /* with the lexer | |
# Go until no more input | |
while True: | |
try: # Next Character | |
char = input() | |
except Exception, e: | |
break # No More Input | |
# If a / | |
if char in SLASH: | |
# If last char was a *, we just found a */ | |
if previousChar is STAR: | |
# Don't match the opposite rule right away ie. */* | |
previousChar = None | |
# Rising up | |
commentLevel -= 1 | |
# If commentLevel is 0, break completly, we're done | |
if commentLevel is 0: | |
print "we're done and this is left in the input stream, '" + stream + "'" | |
break | |
else: | |
previousChar = char | |
# Else a * | |
elif char in STAR: | |
# If last char was a /, we just found a /* | |
if previousChar is SLASH: | |
# Don't match the opposite rule right away ie. /*/ | |
previousChar = None | |
# Going deeper | |
commentLevel += 1 | |
else: | |
previousChar = char | |
else: # Any old character | |
previousChar = char | |
continue | |
if commentLevel is not 0: | |
print "the comment was not finished within our available input" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment