Newer
Older
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
/**
* Copyright (c) 2018 Mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { MonadicParser as P } from 'mol-util/monadic-parser'
import Expression from './expression'
import { MolScriptBuilder as B } from './builder'
export function parseMolScript(input: string) {
return Language.parse(input);
}
namespace Language {
type AST = ASTNode.Expression[]
namespace ASTNode {
export type Expression = Str | Symb | List | Comment
export interface Str {
kind: 'string',
value: string
}
export interface Symb {
kind: 'symbol',
value: string
}
export interface List {
kind: 'list',
bracket: '(' | '[' | '{',
nodes: Expression[]
}
export interface Comment {
kind: 'comment',
value: string
}
export function str(value: string): Str { return { kind: 'string', value }; }
export function symb(value: string): Symb { return { kind: 'symbol', value }; }
export function list(bracket: '(' | '[' | '{', nodes: Expression[]): List { return { kind: 'list', bracket, nodes }; }
export function comment(value: string): Comment { return { kind: 'comment', value } }
}
const ws = P.regexp(/[\n\r\s]*/);
const Expr: P<ASTNode.Expression> = P.lazy(() => (P.alt(Str, List, Symb, Comment).trim(ws)));
const Str = P.takeWhile(c => c !== '`').trim('`').map(ASTNode.str);
const Symb = P.regexp(/[^()\[\]{};`,\n\r\s]+/).map(ASTNode.symb);
const Comment = P.regexp(/\s*;+([^\n\r]*)\n/, 1).map(ASTNode.comment);
const Args = Expr.many();
const List1 = Args.wrap('(', ')').map(args => ASTNode.list('(', args));
const List2 = Args.wrap('[', ']').map(args => ASTNode.list('[', args));
const List3 = Args.wrap('{', '}').map(args => ASTNode.list('{', args));
const List = P.alt(List1, List2, List3);
const Expressions: P<AST> = Expr.many();
function getAST(input: string) { return Expressions.tryParse(input); }
function visitExpr(expr: ASTNode.Expression): Expression {
switch (expr.kind) {
case 'string': return expr.value;
case 'symbol': {
const value = expr.value;
if (value.length > 1) {
const fst = value.charAt(0);
switch (fst) {
case '.': return B.atomName(value.substr(1));
case '_': return B.struct.type.elementSymbol([value.substr(1)]);
}
}
if (value === 'true') return true;
if (value === 'false') return false;
if (isNumber(value)) return +value;
return Expression.Symbol(value);
}
case 'list': {
switch (expr.bracket) {
case '[': return B.core.type.list(withoutComments(expr.nodes).map(visitExpr));
case '{': return B.core.type.set(withoutComments(expr.nodes).map(visitExpr));
case '(': {
const head = visitExpr(expr.nodes[0]);
return Expression.Apply(head, getArgs(expr.nodes));
}
}
return 0 as any;
}
default: {
throw new Error('should not happen');
}
}
}
function getArgs(nodes: ASTNode.Expression[]): Expression.Arguments | undefined {
if (nodes.length <= 1) return void 0;
if (!hasNamedArgs(nodes)) {
const args: Expression[] = [];
for (let i = 1, _i = nodes.length; i < _i; i++) {
const n = nodes[i];
if (n.kind === 'comment') continue;
args[args.length] = visitExpr(n);
}
return args;
}
const args: { [name: string]: Expression } = {};
let allNumeric = true;
let pos = 0;
for (let i = 1, _i = nodes.length; i < _i; i++) {
const n = nodes[i];
if (n.kind === 'comment') continue;
if (n.kind === 'symbol' && n.value.length > 1 && n.value.charAt(0) === ':') {
const name = n.value.substr(1);
++i;
while (i < _i && nodes[i].kind === 'comment') { i++; }
if (i >= _i) throw new Error(`There must be a value foolowed a named arg ':${name}'.`);
args[name] = visitExpr(nodes[i]);
if (isNaN(+name)) allNumeric = false;
} else {
args[pos++] = visitExpr(n);
}
}
if (allNumeric) {
const keys = Object.keys(args).map(a => +a).sort((a, b) => a - b);
let isArray = true;
for (let i = 0, _i = keys.length; i < _i; i++) {
if (keys[i] !== i) {
isArray = false;
break;
}
}
if (isArray) {
const arrayArgs: Expression[] = [];
for (let i = 0, _i = keys.length; i < _i; i++) {
arrayArgs[i] = args[i];
}
return arrayArgs;
}
}
return args;
}
function hasNamedArgs(nodes: ASTNode.Expression[]) {
for (let i = 1, _i = nodes.length; i < _i; i++) {
const n = nodes[i];
if (n.kind === 'symbol' && n.value.length > 1 && n.value.charAt(0) === ':') return true;
}
return false;
}
function withoutComments(nodes: ASTNode.Expression[]) {
let hasComment = false;
for (let i = 0, _i = nodes.length; i < _i; i++) {
if (nodes[i].kind === 'comment') {
hasComment = true;
break;
}
}
if (!hasComment) return nodes;
return nodes.filter(n => n.kind !== 'comment');
}
function isNumber(value: string) {
David Sehnal
committed
return /-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?/.test(value) && !isNaN(+value);
}
export function parse(input: string): Expression[] {
const ast = getAST(input);
const ret: Expression[] = [];
for (const expr of ast) {
if (expr.kind === 'comment') continue;
ret[ret.length] = visitExpr(expr);
}
return ret;
}
}