def send(self, text, user_id): """Executes code 'text' sent bu the user 'user_id', and returns the resulting skyline measures""" if text[0] == '/': if text == '/clear': return False, 'Command not valid\. Did you mean /clean\?' return False, 'Command not valid' try: input_stream = InputStream(text) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() ret = self.visitor.visit(tree) ret.saveImage(user_id) return True, [ret.getMeasures()] except SkyilineNotAssigned: return False, 'Variable not initialized' except WrongDimensions: return False, 'Wrong dimensions' except AttributeError: return False, 'Command not valid' except Exception as e: return False, 'Unknown error'
def calc(input_stream): """ Operació de crida i retorn al visitor amb les comandes com un strig input_stream""" lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() visitor = EvalVisitor() return visitor.visit(tree)
def main(): visitor = EvalVisitor() while True: input_stream = InputStream(input('? ')) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() visitor.visit(tree)
def calcInput(update, inp): """ Mitjançant la entrada String (inp) crida a l'interpret de llenguatge per resoldre el càlcul """ if debug: print('calcInput: ', inp) first_name = update.effective_chat.first_name last_name = update.effective_chat.last_name if not (first_name + " " + last_name) in data: data[first_name + " " + last_name] = {} input_stream = InputStream(inp) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() if debug: print('EvalVisitor para ', first_name + " " + last_name) visitor = EvalVisitor(data[first_name + " " + last_name]) result = visitor.visit(tree) if debug: print('Result: ', result) return result
import sys from antlr4 import * from SkylineLexer import SkylineLexer from SkylineParser import SkylineParser from TreeVisitor import TreeVisitor from EvalVisitor import EvalVisitor input_stream = InputStream(input('? ')) lexer = SkylineLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = SkylineParser(token_stream) tree = parser.root() print(tree.toStringTree(recog=parser)) visitor = TreeVisitor() visitor.visit(tree) #visitor = EvalVisitor() #visitor.visit(tree)