| 13 | # literal grammar: |
| 14 | # [access]<order>?type' |
| 15 | class Query(object): |
| 16 | def __init__(self, literal : str): |
| 17 | self.all = [] |
| 18 | self.any = [] |
| 19 | self.none = [] |
| 20 | self.components = [] |
| 21 | self.accesses = [] |
| 22 | self.literal = literal |
| 23 | #parse literal |
| 24 | pattern = re.compile(r'\s+') |
| 25 | literal = re.sub(pattern, '', literal) |
| 26 | parts = literal.split(",") |
| 27 | for part in parts: |
| 28 | #[access] |
| 29 | endpos = part.find("]") |
| 30 | access = part[1:endpos] |
| 31 | part = part[endpos+1:] |
| 32 | optional = False |
| 33 | #<order> |
| 34 | order = "seq" |
| 35 | if part[0] == "<": |
| 36 | endpos = part.find(">") |
| 37 | order = part[1:endpos] |
| 38 | part = part[endpos+1:] |
| 39 | if part[0] == "!": |
| 40 | cat = self.none |
| 41 | part = part[1:] |
| 42 | elif part[0] == "?": |
| 43 | cat = None |
| 44 | part = part[1:] |
| 45 | optional = True |
| 46 | elif part[0] == "|": |
| 47 | cat = self.any |
| 48 | part = part[1:] |
| 49 | else: |
| 50 | cat = self.all |
| 51 | #type |
| 52 | endpos = part.find("@") |
| 53 | if(endpos == -1): |
| 54 | type = part |
| 55 | else: |
| 56 | type = part[:endpos] |
| 57 | #add to list |
| 58 | if access == "out": |
| 59 | count = 0 |
| 60 | if access != "has" and cat is not self.none: |
| 61 | self.components.append(type) |
| 62 | acc = Access(access == "in", access == "atomic", order, optional) |
| 63 | self.accesses.append(acc) |
| 64 | if cat is not None: |
| 65 | cat.append(type) |
| 66 | def sequence(self): |
| 67 | return [(i, c) for i, c in enumerate(self.components) if self.accesses[i].order != "unseq"] |
| 68 | def unsequence(self): |
| 69 | return [(i, c) for i, c in enumerate(self.components) if self.accesses[i].order != "seq"] |
| 70 | |
| 71 | class Generator(object): |
| 72 | def __init__(self): |