mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-07 04:26:32 +08:00
This change makes it easier to run the script from directly `roll_py.deps` without having to fetch the dependencies first. This also makes it easier to track which dependencies our tooling have. Change-Id: I1ce55074ee6a1fffa6f2a2f05a56d0d74b4aca90 Bug: 1255619 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3431346 Reviewed-by: Mathias Bynens <mathias@chromium.org> Commit-Queue: Johan Bay <jobay@chromium.org>
122 lines
3.3 KiB
JavaScript
122 lines
3.3 KiB
JavaScript
import { Tokeniser } from "./tokeniser.js";
|
|
import { Enum } from "./productions/enum.js";
|
|
import { Includes } from "./productions/includes.js";
|
|
import { ExtendedAttributes } from "./productions/extended-attributes.js";
|
|
import { Typedef } from "./productions/typedef.js";
|
|
import { CallbackFunction } from "./productions/callback.js";
|
|
import { Interface } from "./productions/interface.js";
|
|
import { Mixin } from "./productions/mixin.js";
|
|
import { Dictionary } from "./productions/dictionary.js";
|
|
import { Namespace } from "./productions/namespace.js";
|
|
import { CallbackInterface } from "./productions/callback-interface.js";
|
|
import { autoParenter } from "./productions/helpers.js";
|
|
import { Eof } from "./productions/token.js";
|
|
|
|
/**
|
|
* @param {Tokeniser} tokeniser
|
|
* @param {object} options
|
|
* @param {boolean} [options.concrete]
|
|
*/
|
|
function parseByTokens(tokeniser, options) {
|
|
const source = tokeniser.source;
|
|
|
|
function error(str) {
|
|
tokeniser.error(str);
|
|
}
|
|
|
|
function consume(...candidates) {
|
|
return tokeniser.consume(...candidates);
|
|
}
|
|
|
|
function callback() {
|
|
const callback = consume("callback");
|
|
if (!callback) return;
|
|
if (tokeniser.probe("interface")) {
|
|
return CallbackInterface.parse(tokeniser, callback);
|
|
}
|
|
return CallbackFunction.parse(tokeniser, callback);
|
|
}
|
|
|
|
function interface_(opts) {
|
|
const base = consume("interface");
|
|
if (!base) return;
|
|
const ret =
|
|
Mixin.parse(tokeniser, base, opts) ||
|
|
Interface.parse(tokeniser, base, opts) ||
|
|
error("Interface has no proper body");
|
|
return ret;
|
|
}
|
|
|
|
function partial() {
|
|
const partial = consume("partial");
|
|
if (!partial) return;
|
|
return (
|
|
Dictionary.parse(tokeniser, { partial }) ||
|
|
interface_({ partial }) ||
|
|
Namespace.parse(tokeniser, { partial }) ||
|
|
error("Partial doesn't apply to anything")
|
|
);
|
|
}
|
|
|
|
function definition() {
|
|
if (options.productions) {
|
|
for (const production of options.productions) {
|
|
const result = production(tokeniser);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
return (
|
|
callback() ||
|
|
interface_() ||
|
|
partial() ||
|
|
Dictionary.parse(tokeniser) ||
|
|
Enum.parse(tokeniser) ||
|
|
Typedef.parse(tokeniser) ||
|
|
Includes.parse(tokeniser) ||
|
|
Namespace.parse(tokeniser)
|
|
);
|
|
}
|
|
|
|
function definitions() {
|
|
if (!source.length) return [];
|
|
const defs = [];
|
|
while (true) {
|
|
const ea = ExtendedAttributes.parse(tokeniser);
|
|
const def = definition();
|
|
if (!def) {
|
|
if (ea.length) error("Stray extended attributes");
|
|
break;
|
|
}
|
|
autoParenter(def).extAttrs = ea;
|
|
defs.push(def);
|
|
}
|
|
const eof = Eof.parse(tokeniser);
|
|
if (options.concrete) {
|
|
defs.push(eof);
|
|
}
|
|
return defs;
|
|
}
|
|
const res = definitions();
|
|
if (tokeniser.position < source.length) error("Unrecognised tokens");
|
|
return res;
|
|
}
|
|
|
|
/**
|
|
* @param {string} str
|
|
* @param {object} [options]
|
|
* @param {*} [options.sourceName]
|
|
* @param {boolean} [options.concrete]
|
|
* @param {Function[]} [options.productions]
|
|
* @return {import("./productions/base").Base[]}
|
|
*/
|
|
export function parse(str, options = {}) {
|
|
const tokeniser = new Tokeniser(str);
|
|
if (typeof options.sourceName !== "undefined") {
|
|
tokeniser.source.name = options.sourceName;
|
|
}
|
|
return parseByTokens(tokeniser, options);
|
|
}
|