geometry_tools.automata.gap_parse
1import re 2 3WHITESPACE = " \n\t" 4MAX_ERRLEN = 100 5 6class GAPInputException(Exception): 7 pass 8 9def parse_record(text): 10 record = {} 11 identifier_name = "" 12 13 i = 0 14 while i < len(text): 15 c = text[i] 16 if c == ')': 17 i += 1 18 break 19 elif c == ',': 20 identifier_name = "" 21 elif c == ":" and text[i+1] == "=": 22 value, offset = parse_contents(text[i + 2:]) 23 record[identifier_name] = value 24 i += offset 25 elif c not in WHITESPACE: 26 identifier_name += c 27 i += 1 28 return (record, i + 1) 29 30 31def literal_contents(content_str): 32 if re.match(r"\d*\.\d+", content_str): 33 return float(content_str) 34 elif re.match(r"\d+", content_str): 35 return int(content_str) 36 else: 37 return content_str 38 39def parse_contents(text): 40 content = "" 41 for i, c in enumerate(text): 42 if c == '"': 43 content, offset = parse_quote(text[i + 1:]) 44 return (content, offset + i + 1) 45 elif c == '[': 46 content, offset = parse_list(text[i + 1:]) 47 return (content, offset + i + 1) 48 elif c in WHITESPACE: 49 continue 50 elif c == 'r': 51 if len(text) > i + 4 and text[i:i+4] == 'rec(': 52 content, offset = parse_record(text[i+4:]) 53 return (content, offset + i + 4) 54 else: 55 content += c 56 elif c in ",)": 57 return (literal_contents(content), i + 1) 58 else: 59 content += c 60 return (literal_contents(content), len(text)) 61 62def parse_list(text): 63 64 interval = re.match(r"((-?\d+)\.\.(-?\d+)\])", text) 65 66 if interval and int(interval.group(2)) <= int(interval.group(3)): 67 current_list = range(int(interval.group(2)), 68 int(interval.group(3)) + 1) 69 return (current_list, len(interval.group(1))) 70 71 current_list = [] 72 content = "" 73 i = 0 74 while i < len(text): 75 c = text[i] 76 if c == '"': 77 content, offset = parse_quote(text[i+1:]) 78 current_list.append(content) 79 content = "" 80 i += offset 81 elif c == ',': 82 if len(content) > 0: 83 current_list.append(literal_contents(content)) 84 content = "" 85 elif c == '[': 86 newlist, offset = parse_list(text[i+1:]) 87 current_list.append(newlist) 88 i += offset 89 elif c == ']': 90 if len(content) > 0: 91 current_list.append(literal_contents(content)) 92 i += 1 93 return (current_list, i) 94 elif c not in WHITESPACE: 95 content += c 96 97 i += 1 98 99 raise GAPInputException('Unclosed [: [%s'%text[:MAX_ERRLEN]) 100 101def parse_quote(text): 102 #no escaped quotations 103 close_quote = text.find('"') 104 if close_quote != -1: 105 return (text[:close_quote], close_quote + 1) 106 else: 107 raise GAPInputException('Unclosed ": "%s'%text[:MAX_ERRLEN]) 108 109def load_record_file(filename): 110 with open(filename, 'r') as recfile: 111 record, length = parse_record(recfile.read()) 112 return record
WHITESPACE =
' \n\t'
MAX_ERRLEN =
100
class
GAPInputException(builtins.Exception):
Common base class for all non-exit exceptions.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- add_note
- args
def
parse_record(text):
10def parse_record(text): 11 record = {} 12 identifier_name = "" 13 14 i = 0 15 while i < len(text): 16 c = text[i] 17 if c == ')': 18 i += 1 19 break 20 elif c == ',': 21 identifier_name = "" 22 elif c == ":" and text[i+1] == "=": 23 value, offset = parse_contents(text[i + 2:]) 24 record[identifier_name] = value 25 i += offset 26 elif c not in WHITESPACE: 27 identifier_name += c 28 i += 1 29 return (record, i + 1)
def
literal_contents(content_str):
def
parse_contents(text):
40def parse_contents(text): 41 content = "" 42 for i, c in enumerate(text): 43 if c == '"': 44 content, offset = parse_quote(text[i + 1:]) 45 return (content, offset + i + 1) 46 elif c == '[': 47 content, offset = parse_list(text[i + 1:]) 48 return (content, offset + i + 1) 49 elif c in WHITESPACE: 50 continue 51 elif c == 'r': 52 if len(text) > i + 4 and text[i:i+4] == 'rec(': 53 content, offset = parse_record(text[i+4:]) 54 return (content, offset + i + 4) 55 else: 56 content += c 57 elif c in ",)": 58 return (literal_contents(content), i + 1) 59 else: 60 content += c 61 return (literal_contents(content), len(text))
def
parse_list(text):
63def parse_list(text): 64 65 interval = re.match(r"((-?\d+)\.\.(-?\d+)\])", text) 66 67 if interval and int(interval.group(2)) <= int(interval.group(3)): 68 current_list = range(int(interval.group(2)), 69 int(interval.group(3)) + 1) 70 return (current_list, len(interval.group(1))) 71 72 current_list = [] 73 content = "" 74 i = 0 75 while i < len(text): 76 c = text[i] 77 if c == '"': 78 content, offset = parse_quote(text[i+1:]) 79 current_list.append(content) 80 content = "" 81 i += offset 82 elif c == ',': 83 if len(content) > 0: 84 current_list.append(literal_contents(content)) 85 content = "" 86 elif c == '[': 87 newlist, offset = parse_list(text[i+1:]) 88 current_list.append(newlist) 89 i += offset 90 elif c == ']': 91 if len(content) > 0: 92 current_list.append(literal_contents(content)) 93 i += 1 94 return (current_list, i) 95 elif c not in WHITESPACE: 96 content += c 97 98 i += 1 99 100 raise GAPInputException('Unclosed [: [%s'%text[:MAX_ERRLEN])
def
parse_quote(text):
def
load_record_file(filename):