Select Git revision
parser.ts 9.05 KiB
/**
* Copyright (c) 2017-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Panagiotis Tourlas <panagiot_tourlov@hotmail.com>
* @author Koya Sakuma <koya.sakuma.work@gmail.com>
*/
import * as P from '../../../mol-util/monadic-parser';
import * as h from '../helper';
import { MolScriptBuilder } from '../../../mol-script/language/builder';
const B = MolScriptBuilder;
import { sstrucMap, sstrucDict, properties } from './properties';
import { operators } from './operators';
import { keywords } from './keywords';
import { functions } from './functions';
import { OperatorList } from '../types';
import { Transpiler } from '../transpiler';
// <, <=, = or ==, >=, >, and !=
// lt, le, eq, ge, gt, and ne, =~
const valueOperators: OperatorList = [
{
'@desc': 'multiplication, division',
'@examples': [],
name: 'mul-div',
type: h.binaryLeft,
rule: P.MonadicParser.regexp(/\s*(\*|\/)\s*/, 1),
map: (op, e1, e2) => {
switch (op) {
case '*': return B.core.math.mult([e1, e2]);
case '/': return B.core.math.div([e1, e2]);
default: throw new Error(`value operator '${op}' not supported`);
}
}
},
{
'@desc': 'addition, substraction',
'@examples': [],
name: 'add-sub',
type: h.binaryLeft,
rule: P.MonadicParser.regexp(/\s*(-|\+)\s*/, 1),
map: (op, e1, e2) => {
switch (op) {
case '-': return B.core.math.sub([e1, e2]);
case '+': return B.core.math.add([e1, e2]);
default: throw new Error(`value operator '${op}' not supported`);
}
}
},
{
'@desc': 'value comparisons',
'@examples': [],
name: 'comparison',
type: h.binaryLeft,
rule: P.MonadicParser.alt(P.MonadicParser.regexp(/\s*(=~|==|>=|<=|=|!=|>|<)\s*/, 1), P.MonadicParser.whitespace.result('=')),
map: (op, e1, e2) => {
let expr;
if (e1.head !== undefined) {
if (e1.head.name === 'structure-query.atom-property.macromolecular.secondary-structure-flags') {
expr = B.core.flags.hasAny([e1, sstrucMap(e2)]);
}
if (e1.head.name === 'core.type.regex') {
expr = B.core.str.match([e1, B.core.type.str([e2])]);
}
} else if (e2.head !== undefined) {
if (e2.head.name === 'structure-query.atom-property.macromolecular.secondary-structure-flags') {
expr = B.core.flags.hasAny([e2, sstrucMap(e1)]);
}
if (e2.head.name === 'core.type.regex') {