Skip to content

Commit

Permalink
Removendo busca de codigo fonte na pasta 'code'
Browse files Browse the repository at this point in the history
  • Loading branch information
willian-rosa committed Mar 15, 2019
1 parent 05c8c69 commit 21a272b
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 10 deletions.
16 changes: 10 additions & 6 deletions analizador_lexico.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ def add_codigo(self, path_file, code):
})

def analize(self):

for code in self.codes:
self._analize_code(code['arquivo'], code['code'])
print('Finalizado anazlize lexica de: '+code['arquivo'])
for token in self.tabela_tokens:
print(token)
sys.exit()
return self.tabela_tokens

def clear(self):
self.tabela_tokens = []
self.codes = []


def _analize_code(self, arquivo, code):
self.linha = 1
Expand Down Expand Up @@ -121,14 +124,15 @@ def _analize_caracter(self, arquivo, caracter):
# Literal
elif self.estado == self.ESTADO_LITERAL:

self.coletor = self.coletor + caracter

if caracter == '"':
self.estado = self.ESTADO_INICIAL
if len(self.coletor) > 255:
self.add_error('Literal excedeu 255 caracteres.', arquivo)
else:
self.add_token(21, self.coletor, 'Literal')
else:
self.coletor = self.coletor + caracter

# Identificador
elif self.estado == self.ESTADO_IDENTIFICADOR:

Expand Down
148 changes: 148 additions & 0 deletions interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
from tkinter import *
from analizador_lexico import AnalizadorLexico

class Application:

analizador_lexico = None
root = None
container_esquerdo = None

btn_analise = None
txt = None

items_result = []

def __init__(self, analizador_lexico: AnalizadorLexico):
self.analizador_lexico = analizador_lexico

def start(self):

self.root = Tk()
w, h = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
self.root.geometry("%dx%d+0+0" % (w, h))

self.root.update_idletasks()
self.root.update()

container_principal = Frame(self.root)
container_principal["padx"] = 50
container_principal.pack()

titulo = Label(container_principal, text="Analisador Léxico")
titulo["font"] = ("Arial", "15", "bold")
titulo.pack()

container_direito = Frame(container_principal)
container_direito["pady"] = 10
container_direito.pack(side=LEFT)

container_meio = Frame(container_principal)
container_meio["padx"] = 20
container_meio.pack(side=LEFT)

container_esquerdo = Frame(container_principal)
container_esquerdo["padx"] = 20
container_esquerdo.pack(side=LEFT)

self.container_esquerdo = container_esquerdo

self.txt = self.create_textarea(container_direito)

self.btn_analise = Button(container_meio, text="Analisar >>>", fg="black", command=self.click_analisar)
self.btn_analise.pack()

self.create_grid(container_esquerdo, [])

self.refresh()

def refresh(self):
self.root.update_idletasks()
self.root.update()

def create_textarea(self, container):
scroll = Scrollbar(container)
textarea = Text(container, height=50, width=80)

scroll.pack(side=RIGHT, fill=Y)
textarea.pack(side=LEFT, fill=Y)
scroll.config(command=textarea.yview)
textarea.config(yscrollcommand=scroll.set)

return textarea

def create_grid(self, container, tokens:list):
# {'codigo': 19, 'token': 'dsadas', 'descricao': 'Identificador'}
# {'codigo': 2, 'token': '=', 'descricao': 'Sinal de Igualdade'}
# {'codigo': 19, 'token': 'dasdas', 'descricao': 'Identificador'}
self.rows = []

for widget in self.items_result:
widget.destroy()

items = []
label = Label(container, text="Código")
label["font"] = ("Arial", "11")
label.grid(row=0, column=0)

items.append(label)

label = Label(container, text="Token")
label["font"] = ("Arial", "11")
label.grid(row=0, column=1)

items.append(label)

label = Label(container, text="Descrição")
label["font"] = ("Arial", "11")
label.grid(row=0, column=2)

items.append(label)

for i, token in enumerate(tokens):

r = i + 1

for j, valor in enumerate(token): # Columns
b = Entry(container)
b.insert(0, token[valor])
items.append(b)
b.grid(row=r, column=j)

self.items_result = items

def create_grid_erro(self, container, erro):

self.rows = []

for widget in self.items_result:
widget.destroy()

items = []
label = Label(container, text=erro, fg="red")
label["font"] = ("Arial", "11")
label.grid(row=0, column=0)

items.append(label)

self.items_result = items


def click_analisar(self):

self.analizador_lexico.clear()

code = self.txt.get("1.0", "end-1c")
self.analizador_lexico.add_codigo('', code)

try:
tokens = self.analizador_lexico.analize()
print(tokens)
print('---------------------')
self.create_grid(self.container_esquerdo, tokens)
except Exception as e:
self.create_grid_erro(self.container_esquerdo, e)





20 changes: 16 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
from analizador_lexico import AnalizadorLexico
import glob
import time
from interface import Application


al = AnalizadorLexico()

app = Application(al)

for path_file in glob.glob("code/*.pas"):
file = open(path_file, 'r')
al.add_codigo(path_file, file.read())
# Abrindo diretamente o arquivos
# for path_file in glob.glob("code/*.pas"):
# file = open(path_file, 'r')
# al.add_codigo(path_file, file.read())

al.analize()
# al.analize()


app.start()



app.root.mainloop()

time.sleep(3)


0 comments on commit 21a272b

Please sign in to comment.