source: trunk/tools/Writing Editor/scripts/parser.py @ 242

Revision 234, 5.2 KB checked in by bretzel_parpg, 10 years ago (diff)

Started on the parser and 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, document):
27        """
28        Initialize the parser
29        @type document: QtGui.QDocument
30        @param document: The document generated from a QTextEdit
31        @return: None
32        """
33        self.document = document
34        self.makeFunctions()
35        self.funcs = ["NPC", "PC", "CALLSECTION", "ENDSECTION", "SECTION", 
36                      "SCRIPTNAME", "ENDOPTION", "OPTION", "PLAYSOUND", 
37                      "SAY", "ATTACK", "RETURN", "ELIF", "IF", "ELSE"]
38
39        self.func_by_name = {"NPC":self.npc, "PC":self.pc, "CALLSECTION":self.callsection,
40                             "ENDSECTION":self.endsection, "SECTION": self.section,
41                             "SCRIPTNAME":self.scriptname, "ENDOPTION":self.endoption,
42                             "OPTION":self.option, "PLAYSOUND":self.playsound,
43                             "SAY":self.say, "ATTACK":self.attack, "RETURN":self.return_,
44                             "ELIF":self.elif_, "IF":self.if_, "ELSE":self.else_}
45
46
47    def makeFunctions(self):
48        """
49        Setup all the matching functions
50        @return: None
51        """
52        self.text = Word(alphas)
53        self.space = Literal(" ")
54        self.colon = Literal(":")
55        self.quote = Literal("\"")
56
57        self.npc = Combine(Word("NPC") + self.space + self.text)
58        self.pc = Word("PC")
59        self.section = Combine(Word("SECTION") + self.space + self.text)
60        self.endsection = Word("ENDSECTION")
61        self.callsection = Combine(Word("CALLSECTION") + self.space + self.text)
62        self.scriptname = Combine(Word("SCRIPTNAME") + self.space + self.text)
63        self.option = Combine(Word("OPTION") + self.space + self.text)
64        self.endoption = Combine(Word("ENDOPTION") + self.space + self.text)
65        self.playsound = Combine(Word("PLAYSOUND") + self.space + self.quote + self.text + self.quote)
66        self.say = Combine(self.text + self.space + Word("SAY") + self.space + self.quote
67                           + self.text + self.quote)
68        self.attack = Combine(self.text + self.space + Word("ATTACK") + self.space + self.text)
69        self.return_ = Combine(Word("RETURN") + self.space + self.text)
70        self.if_ = Combine(Word("IF") + self.space + self.text + self.colon)
71        self.elif_ = Combine(Word("ELIF") + self.space + self.text + self.colon)
72        self.else_ = Combine(Word("ELSE") + self.colon)
73
74       
75    def findType(self, string):
76        """
77        Find the type of command that is in the string given
78        @type string: string
79        @param string: the string to find the type of
80        @return: the type
81        """
82        type_ = None
83        for func in self.funcs:
84            regex = re.compile(func + "{0,1}")
85            if (regex.search(str(string)) != None):
86                type_ = func
87                print type_
88                break
89
90        return type_
91
92    def parse(self):
93        """
94        Parse the text
95        @return: the parsed text
96        """       
97        doc = self.document.toPlainText()
98        if (doc == ""):
99            return
100
101        for line in doc.split('\n'):
102            line_type = self.findType(line)
103
104            try:
105                command = self.func_by_name[line_type]
106
107            except KeyError, e:
108                self.createErrorBox(e)
109                return
110
111            parse = command.scanString(line)
112            for result in parse:
113                self.handleResult(result[0][0], line_type)
114
115
116    def handleResult(self, result, type_):
117        """
118        Take the result, determine where to put it in the dialog map, then put it there
119        @type result: string
120        @param result: the result to be handled
121        @type type_: string
122        @param type_: the type of result
123        @return: None
124        """
125        print "Result: " + result
126        print "Type: " + type_
127
128
129    def createErrorBox(self, error):
130        """
131        Create an error box saying that the text couldn't be parsed
132        @type error: KeyError
133        @param error: The error that was generated
134        @return: None
135        """
136        msg_box = QtGui.QMessageBox()
137        msg_box.setText("Error while parsing")
138        msg_box.setInformativeText("Could not find the type \"%s\" in self.func_by_name" % str(error))
139        msg_box.setStandardButtons(QtGui.QMessageBox.Ok)
140        msg_box.setWindowTitle("Error")
141        msg_box.setWindowIcon(QtGui.QIcon("data/images/error.png"))
142       
143        ret = msg_box.exec_()
144        if (ret == QtGui.QMessageBox.Ok):
145            msg_box.close()
Note: See TracBrowser for help on using the repository browser.