Skip to content

Commit f27d0d4

Browse files
committed
Fix imports above namespace in "namespace_first" rule. Few other misc tweaks.
1 parent 5752c05 commit f27d0d4

File tree

5 files changed

+130
-150
lines changed

5 files changed

+130
-150
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wapc/widl",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "waPC Interface Definition Language",
55
"keywords": [
66
"webassembly",

src/ast/definitions.ts

+114-129
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@ export interface Annotated {
2424
): Annotation | undefined;
2525
}
2626

27-
// DescribableNode are nodes that have descriptions associated with them.
28-
export interface DescribableNode {
29-
getDescription(): StringValue;
30-
}
31-
32-
export interface TypeDefinition extends DescribableNode {
33-
getKind(): Kind;
34-
getLoc(): Location;
35-
}
36-
37-
export interface TypeSystemDefinition {
38-
getKind(): Kind;
39-
getLoc(): Location;
40-
}
41-
4227
export class NamespaceDefinition extends AbstractNode implements Annotated {
4328
name: Name;
4429
description?: StringValue;
@@ -78,13 +63,13 @@ export class AliasDefinition extends AbstractNode implements Annotated {
7863
constructor(
7964
loc: Location | undefined,
8065
name: Name,
81-
desc: StringValue | undefined,
66+
description: StringValue | undefined,
8267
type: Type,
8368
annotations?: Annotation[]
8469
) {
8570
super(Kind.AliasDefinition, loc);
8671
this.name = name;
87-
this.description = desc;
72+
this.description = description;
8873
this.type = type;
8974
this.annotations = annotations || [];
9075
}
@@ -106,19 +91,19 @@ export class ImportDefinition extends AbstractNode implements Annotated {
10691
description?: StringValue;
10792
all: boolean;
10893
names: ImportName[];
109-
from: Name;
94+
from: StringValue;
11095
annotations?: Annotation[];
11196

11297
constructor(
11398
loc: Location | undefined,
114-
desc: StringValue | undefined,
99+
description: StringValue | undefined,
115100
all: boolean,
116101
names: ImportName[],
117-
from: Name,
102+
from: StringValue,
118103
annotations?: Annotation[]
119104
) {
120105
super(Kind.ImportDefinition, loc);
121-
this.description = desc;
106+
this.description = description;
122107
this.all = all;
123108
this.names = names;
124109
this.from = from;
@@ -184,80 +169,6 @@ export class TypeDefinition extends AbstractNode implements Annotated {
184169
}
185170
}
186171

187-
export class OperationDefinition extends AbstractNode implements Annotated {
188-
name: Name;
189-
description: StringValue | undefined;
190-
parameters: ParameterDefinition[];
191-
type: Type;
192-
annotations: Annotation[];
193-
unary: boolean;
194-
195-
constructor(
196-
loc: Location | undefined,
197-
name: Name,
198-
desc: StringValue | undefined,
199-
parameters: ParameterDefinition[],
200-
type: Type,
201-
annotations: Annotation[],
202-
unary: boolean
203-
) {
204-
super(Kind.OperationDefinition, loc);
205-
this.name = name;
206-
this.description = desc;
207-
this.parameters = parameters;
208-
this.type = type;
209-
this.annotations = annotations;
210-
this.unary = unary;
211-
}
212-
213-
public isUnary(): boolean {
214-
return this.unary && this.parameters && this.parameters.length == 1;
215-
}
216-
217-
public unaryOp(): ParameterDefinition {
218-
return this.parameters[0];
219-
}
220-
221-
mapTypeToTranslation(
222-
typeTranslation: (inp: Type) => string
223-
): Map<String, String> {
224-
const mp = new Map<String, String>();
225-
if (this.unary) {
226-
mp.set(this.unaryOp().name.value, typeTranslation(this.unaryOp().type));
227-
} else {
228-
this.parameters.forEach((arg) => {
229-
mp.set(arg.name.value, typeTranslation(arg.type));
230-
});
231-
}
232-
return mp;
233-
}
234-
235-
annotation(
236-
name: string,
237-
callback?: (annotation: Annotation) => void
238-
): Annotation | undefined {
239-
return getAnnotation(name, this.annotations, callback);
240-
}
241-
242-
public accept(context: Context, visitor: Visitor): void {
243-
visitor.visitOperationBefore(context);
244-
visitor.visitOperation(context);
245-
246-
context = context.clone({ parameters: context.operation!.parameters });
247-
visitor.visitParametersBefore(context);
248-
context.parameters!.map((parameter, index) => {
249-
parameter.accept(
250-
context.clone({ parameter: parameter, parameterIndex: index }),
251-
visitor
252-
);
253-
});
254-
visitor.visitParametersAfter(context);
255-
256-
visitAnnotations(context, visitor, this.annotations);
257-
visitor.visitOperationAfter(context);
258-
}
259-
}
260-
261172
export abstract class ValuedDefinition
262173
extends AbstractNode
263174
implements Annotated {
@@ -299,9 +210,9 @@ export class FieldDefinition extends ValuedDefinition {
299210
desc: StringValue | undefined,
300211
type: Type,
301212
defaultVal: Value | undefined,
302-
directives: Annotation[]
213+
annotations: Annotation[]
303214
) {
304-
super(Kind.FieldDefinition, loc, name, desc, type, defaultVal, directives);
215+
super(Kind.FieldDefinition, loc, name, desc, type, defaultVal, annotations);
305216
}
306217

307218
public accept(context: Context, visitor: Visitor): void {
@@ -310,36 +221,6 @@ export class FieldDefinition extends ValuedDefinition {
310221
}
311222
}
312223

313-
export class ParameterDefinition extends ValuedDefinition {
314-
constructor(
315-
loc: Location | undefined,
316-
name: Name,
317-
desc: StringValue | undefined,
318-
type: Type,
319-
defaultVal: Value | undefined,
320-
directives: Annotation[]
321-
) {
322-
super(
323-
Kind.ParameterDefinition,
324-
loc,
325-
name,
326-
desc,
327-
type,
328-
defaultVal,
329-
directives
330-
);
331-
}
332-
333-
public accept(context: Context, visitor: Visitor): void {
334-
if (context.operation != undefined) {
335-
visitor.visitParameter(context);
336-
} else if (context.directive != undefined) {
337-
visitor.visitDirectiveParameter(context);
338-
}
339-
visitAnnotations(context, visitor, this.annotations);
340-
}
341-
}
342-
343224
export class InterfaceDefinition
344225
extends AbstractNode
345226
implements Definition, Annotated {
@@ -427,20 +308,124 @@ export class RoleDefinition
427308
}
428309
}
429310

311+
export class OperationDefinition extends AbstractNode implements Annotated {
312+
name: Name;
313+
description: StringValue | undefined;
314+
parameters: ParameterDefinition[];
315+
type: Type;
316+
annotations: Annotation[];
317+
unary: boolean;
318+
319+
constructor(
320+
loc: Location | undefined,
321+
name: Name,
322+
desc: StringValue | undefined,
323+
type: Type,
324+
annotations: Annotation[],
325+
unary: boolean,
326+
parameters: ParameterDefinition[]
327+
) {
328+
super(Kind.OperationDefinition, loc);
329+
this.name = name;
330+
this.description = desc;
331+
this.type = type;
332+
this.annotations = annotations;
333+
this.parameters = parameters;
334+
this.unary = unary;
335+
}
336+
337+
public isUnary(): boolean {
338+
return this.unary && this.parameters && this.parameters.length == 1;
339+
}
340+
341+
public unaryOp(): ParameterDefinition {
342+
return this.parameters[0];
343+
}
344+
345+
mapTypeToTranslation(
346+
typeTranslation: (inp: Type) => string
347+
): Map<String, String> {
348+
const mp = new Map<String, String>();
349+
if (this.unary) {
350+
mp.set(this.unaryOp().name.value, typeTranslation(this.unaryOp().type));
351+
} else {
352+
this.parameters.forEach((arg) => {
353+
mp.set(arg.name.value, typeTranslation(arg.type));
354+
});
355+
}
356+
return mp;
357+
}
358+
359+
annotation(
360+
name: string,
361+
callback?: (annotation: Annotation) => void
362+
): Annotation | undefined {
363+
return getAnnotation(name, this.annotations, callback);
364+
}
365+
366+
public accept(context: Context, visitor: Visitor): void {
367+
visitor.visitOperationBefore(context);
368+
visitor.visitOperation(context);
369+
370+
context = context.clone({ parameters: context.operation!.parameters });
371+
visitor.visitParametersBefore(context);
372+
context.parameters!.map((parameter, index) => {
373+
parameter.accept(
374+
context.clone({ parameter: parameter, parameterIndex: index }),
375+
visitor
376+
);
377+
});
378+
visitor.visitParametersAfter(context);
379+
380+
visitAnnotations(context, visitor, this.annotations);
381+
visitor.visitOperationAfter(context);
382+
}
383+
}
384+
385+
export class ParameterDefinition extends ValuedDefinition {
386+
constructor(
387+
loc: Location | undefined,
388+
name: Name,
389+
desc: StringValue | undefined,
390+
type: Type,
391+
defaultVal: Value | undefined,
392+
annotations: Annotation[]
393+
) {
394+
super(
395+
Kind.ParameterDefinition,
396+
loc,
397+
name,
398+
desc,
399+
type,
400+
defaultVal,
401+
annotations
402+
);
403+
}
404+
405+
public accept(context: Context, visitor: Visitor): void {
406+
if (context.operation != undefined) {
407+
visitor.visitParameter(context);
408+
} else if (context.directive != undefined) {
409+
visitor.visitDirectiveParameter(context);
410+
}
411+
visitAnnotations(context, visitor, this.annotations);
412+
}
413+
}
414+
430415
export class UnionDefinition
431416
extends AbstractNode
432417
implements Definition, Annotated {
433418
name: Name;
434419
description?: StringValue;
435420
annotations: Annotation[];
436-
types: Named[];
421+
types: Type[];
437422

438423
constructor(
439424
loc: Location | undefined,
440425
name: Name,
441426
desc: StringValue | undefined,
442427
annotations: Annotation[],
443-
types: Named[]
428+
types: Type[]
444429
) {
445430
super(Kind.UnionDefinition, loc);
446431
this.name = name;

src/ast/nodes.ts

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export interface Node {
1010
accept(_context: Context, _visitor: Visitor): void;
1111
}
1212

13-
export type NodeArray = Node[];
14-
1513
export abstract class AbstractNode implements Node {
1614
kind: Kind;
1715
loc?: Location;

src/parser.ts

+5-16
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ class Parser {
330330
this.loc(start),
331331
name,
332332
description,
333-
parameters,
334333
type,
335334
annotations,
336-
isUnary
335+
isUnary,
336+
parameters
337337
);
338338
}
339339

@@ -651,18 +651,7 @@ class Parser {
651651

652652
this.expectKeyword("from");
653653

654-
start = this._lexer.token;
655-
if (
656-
start.kind == TokenKind.NS ||
657-
start.kind == TokenKind.NAME ||
658-
start.kind == TokenKind.STRING
659-
) {
660-
this._lexer.advance();
661-
} else {
662-
throw this.unexpected();
663-
}
664-
665-
const from = new Name(this.loc(start), start.value);
654+
const from = this.parseStringLiteral();
666655
const annotations = this.parseAnnotations();
667656
return new ImportDefinition(
668657
this.loc(start),
@@ -938,10 +927,10 @@ class Parser {
938927
* - NamedType
939928
* - UnionMemberTypes | NamedType
940929
*/
941-
parseUnionMembers(): Array<Named> {
930+
parseUnionMembers(): Array<Type> {
942931
const types = [];
943932
do {
944-
const member = this.parseNamed();
933+
const member = this.parseType();
945934
types.push(member);
946935
} while (this.expectOptionalToken(TokenKind.PIPE));
947936
return types;

0 commit comments

Comments
 (0)