本文整理汇总了TypeScript中webidl2.parse函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parse函数的具体用法?TypeScript parse怎么用?TypeScript parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: convert
export function convert(text: string, commentMap: Record<string, string>) {
const rootTypes = webidl2.parse(text);
const partialInterfaces: Browser.Interface[] = [];
const partialMixins: Browser.Interface[] = [];
const partialDictionaries: Browser.Dictionary[] = [];
const includes: webidl2.IncludesType[] = [];
const browser = getEmptyWebIDL();
for (const rootType of rootTypes) {
if (rootType.type === "interface") {
const converted = convertInterface(rootType, commentMap);
if (rootType.partial) {
partialInterfaces.push(converted);
}
else {
browser.interfaces!.interface[rootType.name] = converted;
}
}
else if (rootType.type === "interface mixin") {
const converted = convertInterfaceMixin(rootType, commentMap);
if (rootType.partial) {
partialMixins.push(converted);
}
else {
browser["mixins"]!.mixin[rootType.name] = converted;
}
}
else if (rootType.type === "callback interface") {
browser["callback-interfaces"]!.interface[rootType.name] = convertInterface(rootType, commentMap);
}
else if (rootType.type === "callback") {
browser["callback-functions"]!["callback-function"][rootType.name]
= convertCallbackFunctions(rootType);
addComments(browser["callback-functions"]!["callback-function"][rootType.name], commentMap, rootType.name);
}
else if (rootType.type === "dictionary") {
const converted = convertDictionary(rootType, commentMap);
if (rootType.partial) {
partialDictionaries.push(converted);
}
else {
browser.dictionaries!.dictionary[rootType.name] = converted;
}
}
else if (rootType.type === "enum") {
browser.enums!.enum[rootType.name] = convertEnum(rootType);
}
else if (rootType.type === "typedef") {
browser.typedefs!.typedef.push(convertTypedef(rootType));
}
else if (rootType.type === "includes") {
includes.push(rootType);
}
}
return { browser, partialInterfaces, partialMixins, partialDictionaries, includes };
}
示例2: switch
import * as webidl2 from "webidl2";
const parsed = webidl2.parse("");
for (const rootType of parsed) {
if (rootType.type !== "implements" && rootType.type !== "includes") {
console.log(rootType.name);
}
switch (rootType.type) {
case "interface":
console.log(rootType.inheritance);
logMembers(rootType.members);
console.log(rootType.partial);
break;
case "interface mixin":
logMembers(rootType.members);
console.log(rootType.partial);
break;
case "namespace":
console.log(rootType.partial);
logNamespaceMembers(rootType.members);
break;
case "callback interface":
logMembers(rootType.members);
console.log(rootType.partial);
break;
case "callback":
logArguments(rootType.arguments);
break;
case "dictionary":
console.log(rootType.inheritance);
示例3: exportIDLSnippets
function exportIDLSnippets(idlTexts: string[], origin: FetchResult) {
const snippets: IDLSnippetContent[] = [];
for (const item of idlTexts) {
try {
const snippet = createIDLSnippetContentContainer();
const parsed = WebIDL2.parse(item);
const implementsMap = new Map<string, Element[]>();
for (const rootItem of parsed) {
/*
implements: if the IDL snippet has target interface or partial interface, then insert <implements> into it
if not, create a new partial interface that contains <implements>
*/
if (rootItem.type === "implements") {
const implementEl = document.createElement("implements");
implementEl.textContent = rootItem.implements;
if (!implementsMap.has(rootItem.target)) {
implementsMap.set(rootItem.target, [implementEl]);
}
else {
implementsMap.get(rootItem.target).push(implementEl);
}
}
else {
insert(rootItem, snippet);
}
}
for (const entry of implementsMap.entries()) {
let interfaceEl = snippet.interfaces.filter(item => item.getAttribute("name") === entry[0])[0];
if (!interfaceEl) {
interfaceEl = document.createElement("interface");
interfaceEl.setAttribute("name", entry[0]);
// temp: absence of 'extends' causes incorrect interface declaration on TSJS-lib-generator
interfaceEl.setAttribute("extends", "Object");
interfaceEl.setAttribute("no-interface-object", "1");
interfaceEl.setAttribute("sn:partial", "1");
snippet.interfaces.push(interfaceEl);
}
for (const implementsEl of entry[1]) {
interfaceEl.appendChild(implementsEl);
}
}
snippets.push(snippet);
}
catch (err) {
if (isWebIDLParseError(err)) {
console.warn(`A syntax error has found in a WebIDL code line ${err.line} from ${origin.description.url}:\n${err.message}\n${err.input}\n`);
}
else {
err.message = `An error occured while converting WebIDL from ${origin.description.url}: ${err.message}`;
throw err;
}
}
}
return snippets;
}