-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.ts
173 lines (149 loc) · 5.18 KB
/
parser.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2017 MolQL contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <[email protected]>
*/
// https://pymol.org/dokuwiki/doku.php?id=selection:alpha
// https://github.com/evonove/pymol/blob/master/pymol/layer3/Selector.cpp
import * as P from 'parsimmon'
import * as h from '../helper'
import { AtomGroupArgs } from '../types'
import properties from './properties'
import operators from './operators'
import keywords from './keywords'
import Transpiler from '../transpiler'
import B from '../../molql/builder'
const propertiesDict = h.getPropertyRules(properties)
const slash = P.string('/')
function orNull(rule: P.Parser<any>) {
return rule.or(P.of(null))
}
function atomSelectionQuery(x: any) {
const tests: AtomGroupArgs = {}
const props: {[k: string]: any[]} = {}
for (let k in x) {
const ps = properties[k]
if (!ps) {
throw new Error(`property '${k}' not supported, value '${x[k]}'`)
}
if (x[k] === null) continue
if (!props[ps.level]) props[ps.level] = []
props[ps.level].push(x[k])
}
for (let p in props) {
tests[p] = h.andExpr(props[p])
}
return B.struct.generator.atomGroups(tests)
}
const lang = P.createLanguage({
Parens: function (r) {
return P.alt(
r.Parens,
r.Operator,
r.Expression
).wrap(P.string('('), P.string(')'))
},
Expression: function(r) {
return P.alt(
r.AtomSelectionMacro.map(atomSelectionQuery),
r.NamedAtomProperties,
r.Pepseq,
r.Rep,
r.Keywords,
r.Object
)
},
AtomSelectionMacro: function(r) {
return P.alt(
slash.then(P.alt(
P.seq(
orNull(r.ObjectProperty).skip(slash),
orNull(propertiesDict.segi).skip(slash),
orNull(propertiesDict.chain).skip(slash),
orNull(propertiesDict.resi).skip(slash),
orNull(propertiesDict.name)
).map(x => {return {object: x[0], segi: x[1], chain: x[2], resi: x[3], name: x[4]}}),
P.seq(
orNull(r.ObjectProperty).skip(slash),
orNull(propertiesDict.segi).skip(slash),
orNull(propertiesDict.chain).skip(slash),
orNull(propertiesDict.resi)
).map(x => {return {object: x[0], segi: x[1], chain: x[2], resi: x[3]}}),
P.seq(
orNull(r.ObjectProperty).skip(slash),
orNull(propertiesDict.segi).skip(slash),
orNull(propertiesDict.chain)
).map(x => {return {object: x[0], segi: x[1], chain: x[2]}}),
P.seq(
orNull(r.ObjectProperty).skip(slash),
orNull(propertiesDict.segi)
).map(x => {return {object: x[0], segi: x[1]}}),
P.seq(
orNull(r.ObjectProperty)
).map(x => {return {object: x[0]}}),
)),
P.alt(
P.seq(
orNull(r.ObjectProperty).skip(slash),
orNull(propertiesDict.segi).skip(slash),
orNull(propertiesDict.chain).skip(slash),
orNull(propertiesDict.resi).skip(slash),
orNull(propertiesDict.name)
).map(x => {return {object: x[0], segi: x[1], chain: x[2], resi: x[3], name: x[4]}}),
P.seq(
orNull(propertiesDict.segi).skip(slash),
orNull(propertiesDict.chain).skip(slash),
orNull(propertiesDict.resi).skip(slash),
orNull(propertiesDict.name)
).map(x => {return {segi: x[0], chain: x[1], resi: x[2], name: x[3]}}),
P.seq(
orNull(propertiesDict.chain).skip(slash),
orNull(propertiesDict.resi).skip(slash),
orNull(propertiesDict.name)
).map(x => {return {chain: x[0], resi: x[1], name: x[2]}}),
P.seq(
orNull(propertiesDict.resi).skip(slash),
orNull(propertiesDict.name)
).map(x => {return {resi: x[0], name: x[1]}}),
)
)
},
NamedAtomProperties: function() {
return P.alt(...h.getNamedPropertyRules(properties))
},
Keywords: () => P.alt(...h.getKeywordRules(keywords)),
ObjectProperty: () => {
const w = h.getReservedWords(properties, keywords, operators)
.sort(h.strLenSortFn).map(h.escapeRegExp).join('|')
return P.regex(new RegExp(`(?!(${w}))[A-Z0-9_]+`, 'i'))
},
Object: (r) => {
return r.ObjectProperty.notFollowedBy(slash)
.map(x => { throw new Error(`property 'object' not supported, value '${x}'`) })
},
// Selects peptide sequence matching upper-case one-letter
// sequence SEQ (see also FindSeq).
// PEPSEQ seq
Pepseq: () => {
return P.regex(/(PEPSEQ|ps\.)\s+([a-z]+)/i, 2)
.map(h.makeError(`operator 'pepseq' not supported`))
},
// Selects atoms which show representation rep.
// REP rep
Rep: () => {
return P.regex(/REP\s+(lines|spheres|mesh|ribbon|cartoon|sticks|dots|surface|labels|extent|nonbonded|nb_spheres|slice|extent|slice|dashes|angles|dihedrals|cgo|cell|callback|everything)/i, 1)
.map(h.makeError(`operator 'rep' not supported`))
},
Operator: function(r) {
return h.combineOperators(operators, P.alt(r.Parens, r.Expression, r.Operator))
},
Query: function(r) {
return P.alt(
r.Operator,
r.Parens,
r.Expression
).trim(P.optWhitespace)
}
})
const transpiler: Transpiler = str => lang.Query.tryParse(str)
export default transpiler