Skip to content

Commit

Permalink
Add a logic to process dictionary and partial dictionary (#222)
Browse files Browse the repository at this point in the history
After this patch, `DefinitionInfoMap` can process `dictionary` and
`partial dictionary`. Other definitions will be ignored for now.

ISSUE=#207
  • Loading branch information
romandev committed Dec 27, 2017
1 parent a74fce9 commit a7189d1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
23 changes: 22 additions & 1 deletion generator/new_parser/definition_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,26 @@ export interface OperationMemberInfo {
readonly arguments: ArgumentInfo[];
}

export interface DictionaryInfo {
readonly type: 'dictionary';
readonly name: string;
partial: boolean;
members: DictionaryMemberInfo[];
inheritance: string;
}

export interface DictionaryMemberInfo {
readonly type: 'field';
readonly name: string;
readonly required: boolean;
readonly idlType: TypeInfo;
readonly default: TypeValue;
}

export interface TypeValue {
readonly type: string;
readonly value: string;
}

export type InterfaceMemberInfo = OperationMemberInfo;
export type DefinitionInfo = InterfaceInfo;
export type DefinitionInfo = InterfaceInfo | DictionaryInfo;
30 changes: 28 additions & 2 deletions generator/new_parser/definition_info_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import {
DefinitionInfo, InterfaceInfo
DefinitionInfo, DictionaryInfo, InterfaceInfo
} from 'generator/new_parser/definition_info';

interface DefinitionInfoStore {
Expand All @@ -24,6 +24,8 @@ interface DefinitionInfoStore {

const store: DefinitionInfoStore = {};

// FIXME(zino): Can we merge updateInterfaceInfo() with updateDictionaryInfo()?
// They have the same logic but it's not resolved as a generic type.
function updateInterfaceInfo(info: InterfaceInfo): void {
const storedInfo: DefinitionInfo = store[info.name];

Expand All @@ -43,9 +45,33 @@ function updateInterfaceInfo(info: InterfaceInfo): void {
storedInfo.members = storedInfo.members.concat(info.members);
}

function updateDictionaryInfo(info: DictionaryInfo): void {
const storedInfo: DefinitionInfo = store[info.name];

if (storedInfo === undefined) {
store[info.name] = info;

return;
}

if (storedInfo.type !== info.type) {
throw new SyntaxError('IDL defintions are duplicated');
}

// In this case, one of thing is partial interface. So, we should merge them.
storedInfo.partial = false;
storedInfo.inheritance = storedInfo.inheritance || info.inheritance;
storedInfo.members = storedInfo.members.concat(info.members);
}

function updateDefinitionInfo(info: DefinitionInfo): void {
switch (info.type) {
case 'interface': updateInterfaceInfo(info as InterfaceInfo); break;
case 'interface':
updateInterfaceInfo(info as InterfaceInfo);
break;
case 'dictionary':
updateDictionaryInfo(info as DictionaryInfo);
break;
default:
}
}
Expand Down

0 comments on commit a7189d1

Please sign in to comment.