def run(self):
        print 'run'
        if self.source_filename == None or self.source_filename == '':
             QtGui.QMessageBox.information(self, "Empty Source File", "The source file can't be empty")
        else:
            try:
                self.machine = LexicalMachine(self.source_filename, self.style_filename)
                if  self.config_style_dict:
                    self.machine.style = self.config_style_dict
                self.machine.run()
            except Exception as e:
                QtGui.QMessageBox.information(self, "Failed!","Sorry, there is something wrong with the program." +
                                              "You can find information in the text browser")
                self.leftText.setText(e.message)
            else:
                QtGui.QMessageBox.information(self, "Success!",
                                              "The program runs successfully!\n" +
                                              "You can find the output file in {}\n".format(self.machine.output_filename) +
                                              "and the info file in {}".format(self.machine.output_info))
                self.leftText.setText(self.machine.info)
                file_object = open(self.machine.output_filename)
                try:
                    all_the_text = file_object.read( )
                    all_the_text = self.string2html(all_the_text)

                    self.bottomText.setHtml("""
                        <body>
                        %s
                        </body>
                    """ % all_the_text)
                finally:
                    file_object.close( )
    def open_a_source_file(self):
        self.source_filename = QtGui.QFileDialog.getOpenFileName(self, 'Open Source File', '', 'Source Code(*.c)')
        self.setWindowTitle( self.source_filename + ' - ' + LMConfig.APPLICATION_NAME)
        self.set_working_space()
        self.machine = LexicalMachine(self.source_filename, self.style_filename)

        file_object = open(self.source_filename)
        try:
            all_the_text = file_object.read( )
            all_the_text = self.string2html(all_the_text)

            self.upText.setHtml("""
                <body>
                %s
                </body>
            """ % all_the_text)

        finally:
            file_object.close( )
Ejemplo n.º 3
0
    else:
        for i in range(len(text1)):
            if text1[i] != text2[i]:
                status = False
                print("Line:{}\n->{}\n<-{}".format(i, text1[i], text2[i]))
    print("----------\n")


if __name__ == "__main__":

    file_path = "/Users/zhounaiding/Desktop/lexical/test/codeE1.c"
    # file_output_path = '/Users/zhounaiding/Desktop/lexical/test/codeE1_output.c'
    style_file_path = "/Users/zhounaiding/PycharmProjects/Python/Lexical-Machine-master/default.style"
    # original_file = '/Users/zhounaiding/Desktop/lexical/test/code.c'

    lexical = LexicalMachine(file_path, style_file_path)
    lexical.run()
    # file_diff(files_output, original_file)

    # files_with_error_list = ['/Users/zhounaiding/PycharmProjects/Python/Lexical-Machine/test/codeE{}.c'.format(x) for x in range(1,9)]
    # files_output = ['/Users/zhounaiding/PycharmProjects/Python/Lexical-Machine/test/codeE{}_output.c'.format(x) for x in range(1,9)]
    # original_file = '/Users/zhounaiding/PycharmProjects/Python/Lexical-Machine/test/code.c'

    # files_with_error_list = ['./test/codeE{}.c'.format(x) for x in range(1,9)]
    # files_output = ['./test/codeE{}_output.c'.format(x) for x in range(1,9)]
    # original_file = './test/code.c'
    # for filename in files_with_error_list:
    #     lexical = LexicalMachine(filename, None)
    #     lexical.run()
    # for i in range(8):
    #     file_diff(files_output[i], original_file)
 def open_a_style_file(self):
     self.style_filename = QtGui.QFileDialog.getOpenFileName(self, 'Open Style File', '', 'Source Code(*.style)')
     self.machine = LexicalMachine(self.source_filename, self.style_filename)
     self.config_style_dict = self.machine.style
class MainWindow(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.machine = None
        self.config_style_dict = {}
        self.source_filename = None
        self.style_filename = None

        # Window Configuration
        screen = QtGui.QDesktopWidget().screenGeometry()
        self.resize(screen.width(), screen.height())
        font = QFont(self.tr("Arial"),10)  
        QApplication.setFont(font)
        self.setWindowTitle(LMConfig.APPLICATION_NAME)
        self.setWindowIcon(QtGui.QIcon(LMConfig.APPLICATION_ICON))
        self.statusBar().showMessage('Ready')
        # QThread.sleep(1)

        # MenuBar Configuration
        self.configurate_menubar()

        self.set_opening_space()

    def configurate_menubar(self):
        self.configurate_file_menu()
        self.configurate_view_menu()
        self.configurate_run_menu()
        self.configurate_help_menu()

    def set_opening_space(self):
        # Launch Window Configuration
        self.OpenLabel = QtGui.QLabel( "Open a C Source File To Begin :)" )
        self.OpenLabel.setAlignment( QtCore.Qt.AlignCenter )
        self.OpenLabel.setFont(QFont("Arvo",13, QFont.Light))
        self.setCentralWidget(self.OpenLabel) 

    def set_working_space(self):
        # MainWindow Configuration
        self.mainSplitter = QSplitter(Qt.Horizontal,self)
        self.leftText = QTextBrowser(self.mainSplitter)  
        self.leftText.setAlignment(Qt.AlignCenter)  
        self.rightSplitter = QSplitter(Qt.Horizontal, self.mainSplitter)
        self.rightSplitter.setOpaqueResize(False)  
        self.upText = QTextBrowser(self.rightSplitter)  
        self.upText.setAlignment(Qt.AlignCenter)  
        self.bottomText = QTextBrowser(self.rightSplitter)  
        self.bottomText.setAlignment(Qt.AlignCenter)  
        self.mainSplitter.setStretchFactor(1,1)  
        self.mainSplitter.setWindowTitle(self.tr("分割窗口")) 
        self.setCentralWidget(self.mainSplitter)
        self.leftText.setFont(QFont("Consolas",12, QFont.Light))
        self.upText.setFont(QFont("Consolas",10, QFont.Light))
        self.bottomText.setFont(QFont("Consolas",10, QFont.Light))

    def configurate_file_menu(self):

        # OPEN SOURCE FILE
        open_source_file = QtGui.QAction(QtGui.QIcon(LMConfig.OPEN_SOURCE_FILE_ICON), 
                                                     LMConfig.OPEN_SOURCE_FILE_NAME, self)
        open_source_file.setShortcut(LMConfig.OPEN_SOURCE_FILE_SHORTCUT)
        open_source_file.setStatusTip(LMConfig.OPEN_SOURCE_FILE_STATUS)
        open_source_file.triggered.connect(self.open_a_source_file)

        # OPEN STYLE FILE
        open_style_file = QtGui.QAction(QtGui.QIcon(LMConfig.OPEN_STYLE_FILE_ICON), 
                                                     LMConfig.OPEN_STYLE_FILE_NAME, self)
        open_style_file.setShortcut(LMConfig.OPEN_STYLE_FILE_SHORTCUT)
        open_style_file.setStatusTip(LMConfig.OPEN_STYLE_FILE_STATUS)
        open_style_file.triggered.connect(self.open_a_style_file)

        # CLOSE SOURCE FILE
        close_source_file = QtGui.QAction(QtGui.QIcon(LMConfig.CLOSE_SOURCE_FILE_ICON), 
                                                     LMConfig.CLOSE_SOURCE_FILE_NAME, self)
        close_source_file.setShortcut(LMConfig.CLOSE_SOURCE_FILE_SHORTCUT)
        close_source_file.setStatusTip(LMConfig.CLOSE_SOURCE_FILE_STATUS)
        close_source_file.triggered.connect(self.close_a_source_file)

        # EXIT
        exit = QtGui.QAction(QtGui.QIcon(LMConfig.EXIT_APPLICATION_ICON),
                                         LMConfig.EXIT_APPLICATION_NAME, self)
        exit.setShortcut(LMConfig.EXIT_APPLICATION_SHORTCUT)
        exit.setStatusTip(LMConfig.EXIT_APPLICATION_STATUS)
        self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

        menubar = self.menuBar()
        menubar_file = menubar.addMenu('&File')
        menubar_file.addAction(open_source_file)
        menubar_file.addAction(open_style_file)
        menubar_file.addSeparator()
        menubar_file.addSeparator()
        menubar_file.addAction(close_source_file)
        menubar_file.addAction(exit)  

    def open_a_source_file(self):
        self.source_filename = QtGui.QFileDialog.getOpenFileName(self, 'Open Source File', '', 'Source Code(*.c)')
        self.setWindowTitle( self.source_filename + ' - ' + LMConfig.APPLICATION_NAME)
        self.set_working_space()
        self.machine = LexicalMachine(self.source_filename, self.style_filename)

        file_object = open(self.source_filename)
        try:
            all_the_text = file_object.read( )
            all_the_text = self.string2html(all_the_text)

            self.upText.setHtml("""
                <body>
                %s
                </body>
            """ % all_the_text)

        finally:
            file_object.close( )

    def string2html(self, all_the_text):
        all_the_text = all_the_text.replace("<", "&lt;")
        all_the_text = all_the_text.replace(">", "&gt;")
        all_the_text = all_the_text.replace("\n","<br>")
        all_the_text = all_the_text.replace(" ","&nbsp;")
        all_the_text = all_the_text.replace("//","""<font color=#2E8B57>//</font>""")
        all_the_text = all_the_text.replace("{","""<font color=#DC143C>{</font>""")
        all_the_text = all_the_text.replace("}","""<font color=#DC143C>}</font>""")

        all_the_text = re.sub(r"(\binclude\b)","""<font color=#800080>include</font>""",all_the_text)
        all_the_text = re.sub(r"(\bint\b)","""<font color=#000080>int</font>""",all_the_text)
        all_the_text = re.sub(r"(\blong\b)","""<font color=#000080>long</font>""",all_the_text)
        all_the_text = re.sub(r"(\bdouble\b)","""<font color=#000080>double</font>""",all_the_text)
        all_the_text = re.sub(r"(\bfloat\b)","""<font color=#000080>float</font>""",all_the_text)
        all_the_text = re.sub(r"(\bchar\b)","""<font color=#000080>char</font>""",all_the_text)
        all_the_text = re.sub(r"(\bshort\b)","""<font color=#000080>short</font>""",all_the_text)
        all_the_text = re.sub(r"(\bif\b)","""<font color=#6A5ACD>if</font>""",all_the_text)
        all_the_text = re.sub(r"(\belse\b)","""<font color=#6A5ACD>else</font>""",all_the_text)
        all_the_text = re.sub(r"(\bprintf\b)","""<font color=#663300>printf</font>""",all_the_text)
        all_the_text = re.sub(r"(\bscanf\b)","""<font color=#663300>scanf</font>""",all_the_text)

        all_the_text = re.sub(r"(\bfor\b)","""<font color=#800000>for</font>""",all_the_text)
        all_the_text = re.sub(r"(\bwhile\b)","""<font color=#800000>while</font>""",all_the_text)
        all_the_text = re.sub(r"(\bswitch\b)","""<font color=#800000>switch</font>""",all_the_text)

        all_the_text = re.sub(r"(\breturn\b)","""<font color=#FF1493>return</font>""",all_the_text)
        all_the_text = re.sub(r"(\bvoid\b)","""<font color=#FF1493>void</font>""",all_the_text)

        return all_the_text

    def open_a_style_file(self):
        self.style_filename = QtGui.QFileDialog.getOpenFileName(self, 'Open Style File', '', 'Source Code(*.style)')
        self.machine = LexicalMachine(self.source_filename, self.style_filename)
        self.config_style_dict = self.machine.style


    def close_a_source_file(self):
        self.set_opening_space()
        print 'close'

    def configurate_view_menu(self):
        menubar = self.menuBar()
        menubar_view = menubar.addMenu('&View')

    def configurate_run_menu(self):

        # Run
        # CLOSE SOURCE FILE
        run_lexical_machine = QtGui.QAction(QtGui.QIcon(LMConfig.RUN_LEXICAL_MACHINCE_ICON), 
                                                     LMConfig.RUN_LEXICAL_MACHINCE_NAME, self)
        run_lexical_machine.setShortcut(LMConfig.RUN_LEXICAL_MACHINCE_SHORTCUT)
        run_lexical_machine.setStatusTip(LMConfig.RUN_LEXICAL_MACHINCE_STATUS)
        run_lexical_machine.triggered.connect(self.run)

        config_style = QtGui.QAction(QtGui.QIcon(LMConfig.CONFIG_STYLE_ICON),
                                                     LMConfig.CONFIG_STYLE_NAME, self)
        config_style.setShortcut(LMConfig.CONFIG_STYLE_SHORTCUT)
        config_style.setStatusTip(LMConfig.CONFIG_STYLE_STATUS)
        config_style.triggered.connect(self.config_style)

        menubar = self.menuBar()
        menubar_run = menubar.addMenu('&Run')
        menubar_run.addAction(run_lexical_machine)
        menubar_run.addAction(config_style)

    def configurate_help_menu(self):

        about = QtGui.QAction(QtGui.QIcon(LMConfig.ABOUT_ICON),
                                                     LMConfig.ABOUT_NAME, self)
        about.setShortcut(LMConfig.ABOUT_SHORTCUT)
        about.setStatusTip(LMConfig.ABOUT_STATUS)
        about.triggered.connect(self.about)

        menubar_help = self.menuBar()
        menubar_help = menubar_help.addMenu('&Help')
        menubar_help.addAction(about)

    def config_style(self):
        self.select_style_dialog = selectStyleWidget(self, self.config_style_dict)
        self.select_style_dialog.show()

    def about(self):
        self.about_us = QMessageBox()
        self.about_us.setWindowTitle('About us')
        self.about_us.resize(320, 240)
        self.about_us.setText("""Naiding Zhou : U201313768   \nXuanyu Zheng: U201313731   \nChenchen Xu : U201313768\n   """)
        self.about_us.show()

    def run(self):
        print 'run'
        if self.source_filename == None or self.source_filename == '':
             QtGui.QMessageBox.information(self, "Empty Source File", "The source file can't be empty")
        else:
            try:
                self.machine = LexicalMachine(self.source_filename, self.style_filename)
                if  self.config_style_dict:
                    self.machine.style = self.config_style_dict
                self.machine.run()
            except Exception as e:
                QtGui.QMessageBox.information(self, "Failed!","Sorry, there is something wrong with the program." +
                                              "You can find information in the text browser")
                self.leftText.setText(e.message)
            else:
                QtGui.QMessageBox.information(self, "Success!",
                                              "The program runs successfully!\n" +
                                              "You can find the output file in {}\n".format(self.machine.output_filename) +
                                              "and the info file in {}".format(self.machine.output_info))
                self.leftText.setText(self.machine.info)
                file_object = open(self.machine.output_filename)
                try:
                    all_the_text = file_object.read( )
                    all_the_text = self.string2html(all_the_text)

                    self.bottomText.setHtml("""
                        <body>
                        %s
                        </body>
                    """ % all_the_text)
                finally:
                    file_object.close( )