-
Notifications
You must be signed in to change notification settings - Fork 21
/
syntax_api.ts
161 lines (144 loc) · 5.76 KB
/
syntax_api.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
// -*- mode: typescript; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingTalk
//
// Copyright 2017-2020 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
import assert from 'assert';
import * as semver from 'semver';
import type * as Ast from './ast';
// the old grammar
import * as Grammar from './grammar';
// the old nnsyntax
import * as LegacyNNSyntax from './nn-syntax';
// the new grammar
import Parser from './new-syntax/parser';
import { surfaceLexer } from './new-syntax/lexer';
import { nnLexer } from './new-syntax/nn-lexer';
import { prettyprint } from './new-syntax/pretty';
import { prettyprint as legacyPrettyprint } from './legacy-prettyprint';
import { nnSerialize } from './new-syntax/nn-serializer';
import { KEYWORDS, CONTEXTUAL_KEYWORDS } from './new-syntax/keywords';
import {
EntityMap,
EntityResolver,
MeasureEntity,
LocationEntity,
TimeEntity,
GenericEntity,
DateEntity,
AnyEntity
} from './entities';
import {
AbstractEntityRetriever,
EntityRetriever,
SequentialEntityAllocator,
} from './entity-retriever';
/**
* APIs to parse and serialize ThingTalk code.
*
* @namespace
*/
export enum SyntaxType {
Legacy,
LegacyNN,
Normal,
Tokenized,
}
/**
* Parse a string into a ThingTalk {@link Ast}
*
* @param {string} code - the ThingTalk code to parse
* @return {Ast.Input} the parsed program, library or permission rule
*/
export function parse(code : string|string[], syntaxType : SyntaxType.Tokenized|SyntaxType.LegacyNN, entities : EntityMap|EntityResolver) : Ast.Input;
export function parse(code : string, syntaxType ?: SyntaxType.Normal|SyntaxType.Legacy) : Ast.Input;
export function parse(code : string|string[], syntaxType : SyntaxType = SyntaxType.Normal, entities ?: EntityMap|EntityResolver) : Ast.Input {
let input : Ast.Input;
if (syntaxType === SyntaxType.Tokenized) {
input = new Parser().parse(nnLexer(code, entities!));
} else if (syntaxType === SyntaxType.LegacyNN) {
input = LegacyNNSyntax.fromNN(code, entities!);
} else if (syntaxType === SyntaxType.Normal) {
assert(typeof code === 'string');
input = new Parser().parse(surfaceLexer(code as string));
} else {
// workaround grammar bug with // comments at the end of input
input = Grammar.parse(code + '\n') as any;
}
return input.optimize();
}
export interface SerializeOptions {
typeAnnotations ?: boolean;
includeEntityValue ?: boolean;
compatibility ?: string;
}
/**
* Serialize a ThingTalk AST node to a surface form, either in human-readable
* syntax or in tokenized syntax suitable for machine prediction.
*
* @param node - the program to serialize
* @param entityRetriever - object to use to retrieve entities
* @param [options={}] - additional options
* @param options.typeAnnotations - include type annotations for parameters
* (only meaningful to legacy NN syntax)
*/
export function serialize(node : Ast.Input, syntaxType : SyntaxType.LegacyNN, entities : AbstractEntityRetriever, options ?: SerializeOptions) : string[];
export function serialize(node : Ast.Node, syntaxType : SyntaxType.Tokenized, entities : AbstractEntityRetriever, options ?: SerializeOptions) : string[];
export function serialize(node : Ast.Node, syntaxType ?: SyntaxType.Normal|SyntaxType.Legacy, entities ?: undefined, options ?: SerializeOptions) : string;
export function serialize(node : Ast.Node,
syntaxType : SyntaxType = SyntaxType.Normal,
entityRetriever ?: AbstractEntityRetriever,
options : SerializeOptions = {}) : string|string[] {
if (syntaxType === SyntaxType.Tokenized && options.compatibility &&
semver.satisfies(options.compatibility, '1.*')) {
syntaxType = SyntaxType.LegacyNN;
entityRetriever!.setSyntaxType(syntaxType);
const serialized = LegacyNNSyntax.toNN(node as Ast.Input, entityRetriever!, options);
LegacyNNSyntax.applyCompatibility(serialized, options.compatibility);
return serialized;
// if we introduce compatibility fixes for new syntax, they will go here as well
} else if (syntaxType === SyntaxType.Tokenized || syntaxType === SyntaxType.LegacyNN) {
entityRetriever!.setSyntaxType(syntaxType);
if (syntaxType === SyntaxType.Tokenized)
return nnSerialize(node.toSource(), entityRetriever!, options);
else
return LegacyNNSyntax.toNN(node as Ast.Input, entityRetriever!, options);
} else if (syntaxType === SyntaxType.Normal && options.compatibility &&
semver.satisfies(options.compatibility, '1.*')) {
return legacyPrettyprint(node as Ast.Input);
} else if (syntaxType === SyntaxType.Normal) {
return prettyprint(node.toSource());
} else {
return legacyPrettyprint(node as Ast.Input);
}
}
// reexport entity API so Genie can subclass
export {
KEYWORDS,
CONTEXTUAL_KEYWORDS,
AbstractEntityRetriever,
SequentialEntityAllocator,
EntityRetriever,
EntityMap,
EntityResolver,
MeasureEntity,
LocationEntity,
TimeEntity,
GenericEntity,
DateEntity,
AnyEntity
};