-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbaserules.py
54 lines (47 loc) · 1.82 KB
/
baserules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# This file is part of Predicate Wars.
# Predicate Wars is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# Predicate Wars is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with Predicate Wars. If not, see <https://www.gnu.org/licenses/>.
from dataclasses import dataclass
from typing import List
import re
def _countRepeatedChars(text: str, target: str) -> int:
count = 0
for char in text:
if char == target:
count += 1
else:
break
return count
@dataclass
class BRulesParseResult:
statement: str
titles: List[str] #Empty for now
def parse(text: str) -> List[BRulesParseResult]:
rules = []
remaining = text
while remaining:
stateSearch = re.search(r'~(.*?)~', remaining, re.MULTILINE | re.DOTALL)
titleSearch = re.search(r'>+.*', remaining, re.MULTILINE)
if titleSearch and (not stateSearch or (titleSearch.start() < stateSearch.start())):
... #Just in case when we need titles
remaining = remaining[titleSearch.end():]
elif stateSearch:
statement = stateSearch.group(1)
rules.append(BRulesParseResult(statement, []))
remaining = remaining[stateSearch.end():]
else:
break
return rules
def getBaseRules():
with open('baserules.txt', 'r', encoding='utf-8') as f:
text = f.read()
results = parse(text)
return results