update: commented the python code so it is more readable
This commit is contained in:
parent
285104a19a
commit
c284eb3786
3 changed files with 16 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import str_to_token
|
import str_to_token
|
||||||
import prettier
|
import concat
|
||||||
import ttoken
|
import ttoken
|
||||||
|
|
||||||
s = input("> ")
|
s = input("> ")
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,25 @@
|
||||||
from ttoken import *
|
from ttoken import *
|
||||||
|
|
||||||
|
|
||||||
|
# This function will make a "big" token that will represent a word in the shell sense
|
||||||
def concat(tokens: list[Token]):
|
def concat(tokens: list[Token]):
|
||||||
i = 0
|
i = 0
|
||||||
out = []
|
out = []
|
||||||
while i < len(tokens):
|
while i < len(tokens):
|
||||||
tok = tokens[i]
|
tok = tokens[i]
|
||||||
|
# if the token is a token that can be inside a word, then we start createing a WORD "metaToken"
|
||||||
if tok.is_word():
|
if tok.is_word():
|
||||||
word = Token(TokenType.WORD, subtokens=[])
|
word = Token(TokenType.WORD, subtokens=[])
|
||||||
word.subtokens.append(tok)
|
word.subtokens.append(tok)
|
||||||
j = 1
|
j = 1
|
||||||
|
# then we get every token after the first that is also a word and we push them
|
||||||
while i + j < len(tokens) and (tokens[i + j]).is_word():
|
while i + j < len(tokens) and (tokens[i + j]).is_word():
|
||||||
word.subtokens.append(tokens[i + j])
|
word.subtokens.append(tokens[i + j])
|
||||||
j += 1
|
j += 1
|
||||||
i += j
|
i += j
|
||||||
out.append(word)
|
out.append(word)
|
||||||
else:
|
else:
|
||||||
|
# otherwise we just push the token alone
|
||||||
out.append(tok)
|
out.append(tok)
|
||||||
i += 1
|
i += 1
|
||||||
return out
|
return out
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ def is_quote(c: chr):
|
||||||
return c == "'" or c == '"'
|
return c == "'" or c == '"'
|
||||||
|
|
||||||
|
|
||||||
|
# This function takes the string and seperate them into different tokens depending on the quotes
|
||||||
def str_to_token(s: str):
|
def str_to_token(s: str):
|
||||||
tokens = []
|
tokens = []
|
||||||
current_token = None
|
current_token = None
|
||||||
|
|
@ -15,15 +16,20 @@ def str_to_token(s: str):
|
||||||
while i < len(s):
|
while i < len(s):
|
||||||
c = s[i]
|
c = s[i]
|
||||||
if quote == 0:
|
if quote == 0:
|
||||||
|
# if we have a quote, juste push the current token if any, then switch the the correct quote token
|
||||||
if is_quote(c):
|
if is_quote(c):
|
||||||
if current_token != None:
|
if current_token != None:
|
||||||
tokens.append(current_token)
|
tokens.append(current_token)
|
||||||
quote = c
|
quote = c
|
||||||
current_token = Token(TT.DQUOTE if c == '"' else TT.SQUOTE, string="")
|
current_token = Token(TT.DQUOTE if c == '"' else TT.SQUOTE, string="")
|
||||||
else:
|
else:
|
||||||
|
# here we have no quote, so we first create a token if none exist, then handle special stuff
|
||||||
|
# like whitespace for example, or any character we want to spit in a single token of their own (; $ | &)
|
||||||
if current_token == None:
|
if current_token == None:
|
||||||
current_token = Token(TT.NQUOTE, string="")
|
current_token = Token(TT.NQUOTE, string="")
|
||||||
if c.isspace():
|
if c.isspace():
|
||||||
|
# we have a whitespace, then create a whitespace token, and push the current token
|
||||||
|
# if it isn't empty and not whitesace
|
||||||
if (
|
if (
|
||||||
len(current_token.string) != 0
|
len(current_token.string) != 0
|
||||||
and current_token.ty != TT.WHITESPACE
|
and current_token.ty != TT.WHITESPACE
|
||||||
|
|
@ -31,6 +37,7 @@ def str_to_token(s: str):
|
||||||
tokens.append(current_token)
|
tokens.append(current_token)
|
||||||
current_token = Token(TT.WHITESPACE, string="")
|
current_token = Token(TT.WHITESPACE, string="")
|
||||||
else:
|
else:
|
||||||
|
# we DON'T have a whitespace, then if the current token is a whitespace, just push it and set the new token to raw_string
|
||||||
if current_token.ty == TT.WHITESPACE:
|
if current_token.ty == TT.WHITESPACE:
|
||||||
tokens.append(current_token)
|
tokens.append(current_token)
|
||||||
current_token = Token(TT.NQUOTE, string="")
|
current_token = Token(TT.NQUOTE, string="")
|
||||||
|
|
@ -65,6 +72,7 @@ def str_to_token(s: str):
|
||||||
else:
|
else:
|
||||||
current_token.append_char(c)
|
current_token.append_char(c)
|
||||||
elif quote == "'":
|
elif quote == "'":
|
||||||
|
# we are in a single quotem basically we push until we have another single quote
|
||||||
if c == "'":
|
if c == "'":
|
||||||
tokens.append(current_token)
|
tokens.append(current_token)
|
||||||
current_token = None
|
current_token = None
|
||||||
|
|
@ -74,6 +82,7 @@ def str_to_token(s: str):
|
||||||
current_token = Token(TT.SQUOTE, string="")
|
current_token = Token(TT.SQUOTE, string="")
|
||||||
current_token.append_char(c)
|
current_token.append_char(c)
|
||||||
elif quote == '"':
|
elif quote == '"':
|
||||||
|
# we are in a double quotem basically we push until we have another double quote
|
||||||
if c == '"':
|
if c == '"':
|
||||||
tokens.append(current_token)
|
tokens.append(current_token)
|
||||||
current_token = None
|
current_token = None
|
||||||
|
|
@ -85,6 +94,7 @@ def str_to_token(s: str):
|
||||||
else:
|
else:
|
||||||
print("you fucked up you quote thingy")
|
print("you fucked up you quote thingy")
|
||||||
i += 1
|
i += 1
|
||||||
|
# if the current token is not none and the current token is "no quote" then we push it
|
||||||
if current_token != None and current_token.ty == TT.NQUOTE:
|
if current_token != None and current_token.ty == TT.NQUOTE:
|
||||||
tokens.append(current_token)
|
tokens.append(current_token)
|
||||||
return tokens
|
return tokens
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue