def skyComp(update, context): try: msg = update.message.text lexer = SkylineLexer(InputStream(msg)) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() numError = parser.getNumberOfSyntaxErrors() if numError > 0: raise Exception('Error de sintaxis!') visitor = EvalVisitor(context.user_data) a = visitor.visit(tree) if isinstance(a, Skyline): user_id = update.effective_chat.id fitxer = str(user_id) + "_tmp.png" area, top = a.plot() plt.savefig(fitxer, bbox_inches='tight') context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(fitxer, 'rb')) os.remove(fitxer) context.bot.send_message(chat_id=update.effective_chat.id, text='area: %d\nalçada: %d' % (area, top)) else: raise Exception('Cap Skyline detectat!') except Exception as e: print(e) context.bot.send_message(chat_id=update.effective_chat.id, text='💣' + " *" + str(e) + "*", parse_mode=ParseMode.MARKDOWN)
def message_txt(update, context): try: # agafa el text i el visitor de l'usuari. txt = update.message.text visitor = context.user_data[VISITOR] # tokenitza, parseja i visita input_stream = InputStream(txt) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() # dibuixa l'skyline resultant s = visitor.visit(tree) s.plot().savefig('tmp.png', bbox_inches='tight') # contesta amb la imatge i la info de l'skyline context.bot.send_photo( chat_id=update.effective_chat.id, photo=open('tmp.png', 'rb')) context.bot.send_message( chat_id=update.effective_chat.id, text=TEXT_SKYLINE_INFO.format(s.area(), s.alçada())) os.remove('tmp.png') except Exception: context.bot.send_message( chat_id=update.effective_chat.id, text=TEXT_ERROR_MESSAGE)
def parse_text(text, skylines): input_stream = InputStream(text) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() visitor = EvalVisitor(skylines) return visitor.visit(tree)
def evalMsg(msg, t): input_stream = InputStream(msg) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) visitor = SkylineVisitor(t) tree = parser.root() result = visitor.visit(tree) errors = parser.getNumberOfSyntaxErrors() if errors == 0: return result
def user_operation(update, context): try: input_stream = InputStream(update.message.text) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() visitor = EvalVisitor(context.user_data['ts']) m_output = visitor.visit(tree) draw_graph(update, context, m_output) except Exception as e: context.bot.send_message(chat_id=update.effective_chat.id, text=str(e))
def compile_command(update, context): command = update.message.text input_stream = InputStream(command) print("Command [", command, "] received.") lexer = Lexer(input_stream) token_stream = CommonTokenStream(lexer) parser = Parser(token_stream) tree = parser.root() visitor = Visitor(context.user_data['skylines']) skln = visitor.visit(tree) send_info(update, context, skln)
def commandManagement(update, context): missatge = update.message.text print(missatge) input_stream = InputStream(missatge) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() visitor = EvalVisitor(context.user_data['skylines']) actSkln = visitor.visit(tree) response(update, context, actSkln)
def entrada(update, context): """Parseja el missatge d'entrada, crea l'skyline i l'envia.""" input_stream = InputStream(update.message.text) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) parser.addErrorListener(MyErrorListener()) try: tree = parser.root() if 'taula_simbols' not in context.user_data: context.user_data['taula_simbols'] = {} visitor = EvalVisitor(context.user_data['taula_simbols']) (var, sk) = visitor.visit(tree) if sk.edificis: genera_grafic(sk) context.bot.send_photo(chat_id=update.message.chat_id, photo=open('tmp.png', 'rb')) os.remove("tmp.png") sortida(update, context, "Àrea: " + str(sk.area())) sortida(update, context, "Alçada: " + str(sk.alcada())) else: sortida(update, context, "El resultat és l'skyline buit.") if var is not None: context.user_data['taula_simbols'][var] = sk except ParseError: text = "Error en parsejar l'expressió." except KeyError: text = "Aquest identificador no existeix." except TypeError: text = "Error de tipus: operació no suportada." except Exception as err: text = "Hi ha hagut una excepció. Comprova la terminal." print("Excepció: " + str(err)) sortida(update, context, text)
def parseExpr(update, context): input_stream = InputStream(update.message.text) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() # print(tree.toStringTree(recog=parser)) uid = str(update.effective_chat.id) if uid not in context.user_data: context.user_data[uid] = EvalVisitor() visitor = context.user_data[uid] s = visitor.visit(tree) fileName = str(update.effective_chat.id) + '.png' s.draw(fileName) context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(fileName, 'rb')) context.bot.send_message(chat_id=update.effective_chat.id, text=s.stats()) os.remove(fileName)
# encoding: utf-8 import sys from antlr4 import * from cl.SkylineLexer import SkylineLexer from cl.SkylineParser import SkylineParser from cl.EvalVisitor import EvalVisitor import matplotlib input_stream = InputStream(input('? ')) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() visitor = EvalVisitor() matplotlib.pyplot.show(visitor.visit(tree).plot()) print(tree.toStringTree(recog=parser))