source: trunk/tools/writing_editor/scripts/parser.py @ 250

Revision 250, 5.1 KB checked in by bretzel_parpg, 10 years ago (diff)

Added some functionality to the dialog map

Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18import re
19from lib.pyparsing import *
20from PyQt4 import QtGui, QtCore
21
22class Parser():
23    """
24    The parser
25    """
26    def __init__(self, widget, result_function):
27        """
28        Initialize the parser
29        @type document: QtGui.QTextEdit
30        @param document: The QTextEdit
31        @type result_function: function
32        @param result_function: the function that handles the results
33        @return: None
34        """
35        self.widget = widget
36        self.resultFunction = result_function
37        self.makeFunctions()
38        self.funcs = ["SCRIPTNAME", "NPC", "PC", "CALLSECTION", "ENDSECTION", "SECTION", 
39                      "SCRIPTNAME", "ENDOPTION", "OPTION", "PLAYSOUND", 
40                      "SAY", "ATTACK", "RETURN", "ELIF", "IF", "ELSE"]
41
42        self.func_by_name = {"NPC":self.npc, "PC":self.pc, "CALLSECTION":self.callsection,
43                             "ENDSECTION":self.endsection, "SECTION": self.section,
44                             "SCRIPTNAME":self.scriptname, "ENDOPTION":self.endoption,
45                             "OPTION":self.option, "PLAYSOUND":self.playsound,
46                             "SAY":self.say, "ATTACK":self.attack, "RETURN":self.return_,
47                             "ELIF":self.elif_, "IF":self.if_, "ELSE":self.else_}
48
49
50    def makeFunctions(self):
51        """
52        Setup all the matching functions
53        @return: None
54        """
55        self.text = Word(alphas)
56        self.space = Literal(" ")
57        self.colon = Literal(":")
58        self.quote = Literal("\"")
59
60        self.npc = Combine(Word("NPC") + self.space + self.text)
61        self.pc = Word("PC")
62        self.section = Combine(Word("SECTION") + self.space + self.text)
63        self.endsection = Word("ENDSECTION")
64        self.callsection = Combine(Word("CALLSECTION") + self.space + self.text)
65        self.scriptname = Combine(Word("SCRIPTNAME") + self.space + self.text)
66        self.option = Combine(Word("OPTION") + self.space + self.text)
67        self.endoption = Combine(Word("ENDOPTION") + self.space + self.text)
68        self.playsound = Combine(Word("PLAYSOUND") + self.space + self.quote + self.text + self.quote)
69        self.say = Combine(self.text + self.space + Word("SAY") + self.space + self.quote
70                           + self.text + self.quote)
71        self.attack = Combine(self.text + self.space + Word("ATTACK") + self.space + self.text)
72        self.return_ = Combine(Word("RETURN") + self.space + self.text)
73        self.if_ = Combine(Word("IF") + self.space + self.text + self.colon)
74        self.elif_ = Combine(Word("ELIF") + self.space + self.text + self.colon)
75        self.else_ = Combine(Word("ELSE") + self.colon)
76
77       
78    def findType(self, string):
79        """
80        Find the type of command that is in the string given
81        @type string: string
82        @param string: the string to find the type of
83        @return: the type
84        """
85        type_ = None
86        for func in self.funcs:
87            regex = re.compile(func + "{0,1}")
88            if (regex.search(str(string)) != None):
89                type_ = func
90                break
91
92        return type_
93
94    def parse(self):
95        """
96        Parse the text
97        @return: the parsed text
98        """       
99        doc = self.widget.document().toPlainText()
100        if (doc == ""):
101            return
102
103        for line in doc.split('\n'):
104            print line
105            if (line == ""):
106                continue
107
108            line_type = self.findType(line)
109            try:
110                command = self.func_by_name[line_type]
111            except KeyError, e:
112                self.createErrorBox(e)
113                return
114
115            parse = command.scanString(line)
116            for result in parse:
117                self.resultFunction(result[0][0], line_type)
118
119
120    def createErrorBox(self, error):
121        """
122        Create an error box saying that the text couldn't be parsed
123        @type error: KeyError
124        @param error: The error that was generated
125        @return: None
126        """
127        msg_box = QtGui.QMessageBox()
128        msg_box.setText("Error while parsing")
129        msg_box.setInformativeText("Could not find the type \"%s\" in self.func_by_name" % str(error))
130        msg_box.setStandardButtons(QtGui.QMessageBox.Ok)
131        msg_box.setWindowTitle("Error")
132        msg_box.setWindowIcon(QtGui.QIcon("data/images/error.png"))
133       
134        ret = msg_box.exec_()
135        if (ret == QtGui.QMessageBox.Ok):
136            msg_box.close()
Note: See TracBrowser for help on using the repository browser.