onlineParser.mjs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  2. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  4. import { Lexer } from "../lexer.mjs";
  5. import { Source } from "../source.mjs";
  6. import GraphQLGrammar from "./grammar.mjs";
  7. export var TokenKind = {
  8. NAME: 'Name',
  9. INT: 'Int',
  10. FLOAT: 'Float',
  11. STRING: 'String',
  12. BLOCK_STRING: 'BlockString',
  13. COMMENT: 'Comment',
  14. PUNCTUATION: 'Punctuation',
  15. EOF: '<EOF>',
  16. INVALID: 'Invalid'
  17. };
  18. export var RuleKind = {
  19. TOKEN_CONSTRAINT: 'TokenConstraint',
  20. OF_TYPE_CONSTRAINT: 'OfTypeConstraint',
  21. LIST_OF_TYPE_CONSTRAINT: 'ListOfTypeConstraint',
  22. PEEK_CONSTRAINT: 'PeekConstraint',
  23. CONSTRAINTS_SET: 'ConstraintsSet',
  24. CONSTRAINTS_SET_ROOT: 'ConstraintsSetRoot',
  25. RULE_NAME: 'RuleName',
  26. INVALID: 'Invalid'
  27. };
  28. export var OnlineParser = /*#__PURE__*/function () {
  29. function OnlineParser(source, state, config) {
  30. var _config$tabSize;
  31. this.state = state || OnlineParser.startState();
  32. this._config = {
  33. tabSize: (_config$tabSize = config === null || config === void 0 ? void 0 : config.tabSize) !== null && _config$tabSize !== void 0 ? _config$tabSize : 2
  34. };
  35. this._lexer = new Lexer(new Source(source));
  36. }
  37. OnlineParser.startState = function startState() {
  38. return {
  39. rules: [// $FlowFixMe[cannot-spread-interface]
  40. _objectSpread(_objectSpread({
  41. name: 'Document',
  42. state: 'Document',
  43. kind: 'ListOfTypeConstraint'
  44. }, GraphQLGrammar.Document), {}, {
  45. expanded: false,
  46. depth: 1,
  47. step: 1
  48. })],
  49. name: null,
  50. type: null,
  51. levels: [],
  52. indentLevel: 0,
  53. kind: function kind() {
  54. var _this$rules;
  55. return ((_this$rules = this.rules[this.rules.length - 1]) === null || _this$rules === void 0 ? void 0 : _this$rules.state) || '';
  56. },
  57. step: function step() {
  58. var _this$rules2;
  59. return ((_this$rules2 = this.rules[this.rules.length - 1]) === null || _this$rules2 === void 0 ? void 0 : _this$rules2.step) || 0;
  60. }
  61. };
  62. };
  63. OnlineParser.copyState = function copyState(state) {
  64. return {
  65. name: state.name,
  66. type: state.type,
  67. rules: JSON.parse(JSON.stringify(state.rules)),
  68. levels: [].concat(state.levels),
  69. indentLevel: state.indentLevel,
  70. kind: function kind() {
  71. var _this$rules3;
  72. return ((_this$rules3 = this.rules[this.rules.length - 1]) === null || _this$rules3 === void 0 ? void 0 : _this$rules3.state) || '';
  73. },
  74. step: function step() {
  75. var _this$rules4;
  76. return ((_this$rules4 = this.rules[this.rules.length - 1]) === null || _this$rules4 === void 0 ? void 0 : _this$rules4.step) || 0;
  77. }
  78. };
  79. };
  80. var _proto = OnlineParser.prototype;
  81. _proto.sol = function sol() {
  82. return this._lexer.source.locationOffset.line === 1 && this._lexer.source.locationOffset.column === 1;
  83. };
  84. _proto.parseToken = function parseToken() {
  85. var rule = this._getNextRule();
  86. if (this.sol()) {
  87. this.state.indentLevel = Math.floor(this.indentation() / this._config.tabSize);
  88. }
  89. if (!rule) {
  90. return {
  91. kind: TokenKind.INVALID,
  92. value: ''
  93. };
  94. }
  95. var token;
  96. if (this._lookAhead().kind === '<EOF>') {
  97. return {
  98. kind: TokenKind.EOF,
  99. value: '',
  100. ruleName: rule.name
  101. };
  102. }
  103. switch (rule.kind) {
  104. case RuleKind.TOKEN_CONSTRAINT:
  105. token = this._parseTokenConstraint(rule);
  106. break;
  107. case RuleKind.LIST_OF_TYPE_CONSTRAINT:
  108. token = this._parseListOfTypeConstraint(rule);
  109. break;
  110. case RuleKind.OF_TYPE_CONSTRAINT:
  111. token = this._parseOfTypeConstraint(rule);
  112. break;
  113. case RuleKind.PEEK_CONSTRAINT:
  114. token = this._parsePeekConstraint(rule);
  115. break;
  116. case RuleKind.CONSTRAINTS_SET_ROOT:
  117. token = this._parseConstraintsSetRule(rule);
  118. break;
  119. default:
  120. return {
  121. kind: TokenKind.INVALID,
  122. value: '',
  123. ruleName: rule.name
  124. };
  125. }
  126. if (token && token.kind === TokenKind.INVALID) {
  127. if (rule.optional === true) {
  128. this.state.rules.pop();
  129. } else {
  130. this._rollbackRule();
  131. }
  132. return this.parseToken() || token;
  133. }
  134. return token;
  135. };
  136. _proto.indentation = function indentation() {
  137. var match = this._lexer.source.body.match(/\s*/);
  138. var indent = 0;
  139. if (match && match.length === 0) {
  140. var whiteSpaces = match[0];
  141. var pos = 0;
  142. while (whiteSpaces.length > pos) {
  143. if (whiteSpaces.charCodeAt(pos) === 9) {
  144. indent += 2;
  145. } else {
  146. indent++;
  147. }
  148. pos++;
  149. }
  150. }
  151. return indent;
  152. };
  153. _proto._parseTokenConstraint = function _parseTokenConstraint(rule) {
  154. rule.expanded = true;
  155. var token = this._lookAhead();
  156. if (!this._matchToken(token, rule)) {
  157. return {
  158. kind: TokenKind.INVALID,
  159. value: '',
  160. tokenName: rule.tokenName,
  161. ruleName: rule.name
  162. };
  163. }
  164. this._advanceToken();
  165. var parserToken = this._transformLexerToken(token, rule);
  166. this._popMatchedRule(parserToken);
  167. return parserToken;
  168. };
  169. _proto._parseListOfTypeConstraint = function _parseListOfTypeConstraint(rule) {
  170. this._pushRule(GraphQLGrammar[rule.listOfType], rule.depth + 1, rule.listOfType, 1, rule.state);
  171. rule.expanded = true;
  172. var token = this.parseToken();
  173. return token;
  174. };
  175. _proto._parseOfTypeConstraint = function _parseOfTypeConstraint(rule) {
  176. if (rule.expanded) {
  177. this._popMatchedRule();
  178. return this.parseToken();
  179. }
  180. this._pushRule(rule.ofType, rule.depth + 1, rule.tokenName, 1, rule.state);
  181. rule.expanded = true;
  182. var token = this.parseToken();
  183. return token;
  184. };
  185. _proto._parsePeekConstraint = function _parsePeekConstraint(rule) {
  186. if (rule.expanded) {
  187. this._popMatchedRule();
  188. return this.parseToken();
  189. }
  190. while (!rule.matched && rule.index < rule.peek.length - 1) {
  191. rule.index++;
  192. var constraint = rule.peek[rule.index];
  193. var ifCondition = constraint.ifCondition;
  194. if (typeof ifCondition === 'string') {
  195. ifCondition = GraphQLGrammar[ifCondition];
  196. }
  197. var token = this._lookAhead();
  198. if (ifCondition && this._matchToken(token, ifCondition)) {
  199. rule.matched = true;
  200. rule.expanded = true;
  201. this._pushRule(constraint.expect, rule.depth + 1, '', 1, rule.state);
  202. token = this.parseToken();
  203. return token;
  204. }
  205. }
  206. return {
  207. kind: TokenKind.INVALID,
  208. value: '',
  209. ruleName: rule.name
  210. };
  211. };
  212. _proto._parseConstraintsSetRule = function _parseConstraintsSetRule(rule) {
  213. if (rule.expanded) {
  214. this._popMatchedRule();
  215. return this.parseToken();
  216. }
  217. for (var index = rule.constraints.length - 1; index >= 0; index--) {
  218. this._pushRule(rule.constraints[index], rule.depth + 1, '', index, rule.state);
  219. }
  220. rule.expanded = true;
  221. return this.parseToken();
  222. };
  223. _proto._matchToken = function _matchToken(token, rule) {
  224. if (typeof token.value === 'string') {
  225. if (typeof rule.ofValue === 'string' && token.value !== rule.ofValue || Array.isArray(rule.oneOf) && !rule.oneOf.includes(token.value) || typeof rule.ofValue !== 'string' && !Array.isArray(rule.oneOf) && token.kind !== rule.token) {
  226. return false;
  227. }
  228. return this._butNot(token, rule);
  229. }
  230. if (token.kind !== rule.token) {
  231. return false;
  232. }
  233. return this._butNot(token, rule);
  234. };
  235. _proto._butNot = function _butNot(token, rule) {
  236. var _this = this;
  237. if (rule.butNot) {
  238. if (Array.isArray(rule.butNot)) {
  239. if (rule.butNot.reduce(function (matched, constraint) {
  240. return matched || _this._matchToken(token, constraint);
  241. }, false)) {
  242. return false;
  243. }
  244. return true;
  245. }
  246. return !this._matchToken(token, rule.butNot);
  247. }
  248. return true;
  249. };
  250. _proto._transformLexerToken = function _transformLexerToken(lexerToken, rule) {
  251. var token;
  252. var ruleName = rule.name || '';
  253. var tokenName = rule.tokenName || '';
  254. if (lexerToken.kind === '<EOF>' || lexerToken.value !== undefined) {
  255. token = {
  256. kind: lexerToken.kind,
  257. value: lexerToken.value || '',
  258. tokenName: tokenName,
  259. ruleName: ruleName
  260. };
  261. if (token.kind === TokenKind.STRING) {
  262. token.value = "\"".concat(token.value, "\"");
  263. } else if (token.kind === TokenKind.BLOCK_STRING) {
  264. token.value = "\"\"\"".concat(token.value, "\"\"\"");
  265. }
  266. } else {
  267. token = {
  268. kind: TokenKind.PUNCTUATION,
  269. value: lexerToken.kind,
  270. tokenName: tokenName,
  271. ruleName: ruleName
  272. };
  273. if (/^[{([]/.test(token.value)) {
  274. if (this.state.indentLevel !== undefined) {
  275. this.state.levels = this.state.levels.concat(this.state.indentLevel + 1);
  276. }
  277. } else if (/^[})\]]/.test(token.value)) {
  278. this.state.levels.pop();
  279. }
  280. }
  281. return token;
  282. };
  283. _proto._getNextRule = function _getNextRule() {
  284. return this.state.rules[this.state.rules.length - 1] || null;
  285. };
  286. _proto._popMatchedRule = function _popMatchedRule(token) {
  287. var rule = this.state.rules.pop();
  288. if (!rule) {
  289. return;
  290. }
  291. if (token && rule.kind === RuleKind.TOKEN_CONSTRAINT) {
  292. var constraint = rule;
  293. if (typeof constraint.definitionName === 'string') {
  294. this.state.name = token.value || null;
  295. } else if (typeof constraint.typeName === 'string') {
  296. this.state.type = token.value || null;
  297. }
  298. }
  299. var nextRule = this._getNextRule();
  300. if (!nextRule) {
  301. return;
  302. }
  303. if (nextRule.depth === rule.depth - 1 && nextRule.expanded && nextRule.kind === RuleKind.CONSTRAINTS_SET_ROOT) {
  304. this.state.rules.pop();
  305. }
  306. if (nextRule.depth === rule.depth - 1 && nextRule.expanded && nextRule.kind === RuleKind.LIST_OF_TYPE_CONSTRAINT) {
  307. nextRule.expanded = false;
  308. nextRule.optional = true;
  309. }
  310. };
  311. _proto._rollbackRule = function _rollbackRule() {
  312. var _this2 = this;
  313. if (!this.state.rules.length) {
  314. return;
  315. }
  316. var popRule = function popRule() {
  317. var lastPoppedRule = _this2.state.rules.pop();
  318. if (lastPoppedRule.eatNextOnFail === true) {
  319. _this2.state.rules.pop();
  320. }
  321. };
  322. var poppedRule = this.state.rules.pop();
  323. if (!poppedRule) {
  324. return;
  325. }
  326. var popped = 0;
  327. var nextRule = this._getNextRule();
  328. while (nextRule && (poppedRule.kind !== RuleKind.LIST_OF_TYPE_CONSTRAINT || nextRule.expanded) && nextRule.depth > poppedRule.depth - 1) {
  329. this.state.rules.pop();
  330. popped++;
  331. nextRule = this._getNextRule();
  332. }
  333. if (nextRule && nextRule.expanded) {
  334. if (nextRule.optional === true) {
  335. popRule();
  336. } else {
  337. if (nextRule.kind === RuleKind.LIST_OF_TYPE_CONSTRAINT && popped === 1) {
  338. this.state.rules.pop();
  339. return;
  340. }
  341. this._rollbackRule();
  342. }
  343. }
  344. };
  345. _proto._pushRule = function _pushRule(baseRule, depth, name, step, state) {
  346. var _this$_getNextRule, _this$_getNextRule2, _this$_getNextRule3, _this$_getNextRule4, _this$_getNextRule5, _this$_getNextRule6, _this$_getNextRule7, _this$_getNextRule8, _this$_getNextRule9, _this$_getNextRule10;
  347. this.state.name = null;
  348. this.state.type = null;
  349. var rule = baseRule;
  350. switch (this._getRuleKind(rule)) {
  351. case RuleKind.RULE_NAME:
  352. rule = rule;
  353. this._pushRule(GraphQLGrammar[rule], depth, (typeof name === 'string' ? name : undefined) || rule, step, state);
  354. break;
  355. case RuleKind.CONSTRAINTS_SET:
  356. rule = rule;
  357. this.state.rules.push({
  358. name: name || '',
  359. depth: depth,
  360. expanded: false,
  361. constraints: rule,
  362. constraintsSet: true,
  363. kind: RuleKind.CONSTRAINTS_SET_ROOT,
  364. state: (typeof name === 'string' ? name : undefined) || (typeof state === 'string' ? state : undefined) || ((_this$_getNextRule = this._getNextRule()) === null || _this$_getNextRule === void 0 ? void 0 : _this$_getNextRule.state) || '',
  365. step: typeof step === 'number' ? step : (((_this$_getNextRule2 = this._getNextRule()) === null || _this$_getNextRule2 === void 0 ? void 0 : _this$_getNextRule2.step) || 0) + 1
  366. });
  367. break;
  368. case RuleKind.OF_TYPE_CONSTRAINT:
  369. rule = rule;
  370. this.state.rules.push({
  371. name: name || '',
  372. ofType: rule.ofType,
  373. optional: Boolean(rule.optional),
  374. butNot: rule.butNot,
  375. eatNextOnFail: Boolean(rule.eatNextOnFail),
  376. depth: depth,
  377. expanded: false,
  378. kind: RuleKind.OF_TYPE_CONSTRAINT,
  379. state: (typeof rule.tokenName === 'string' ? rule.tokenName : undefined) || (typeof name === 'string' ? name : undefined) || (typeof state === 'string' ? state : undefined) || ((_this$_getNextRule3 = this._getNextRule()) === null || _this$_getNextRule3 === void 0 ? void 0 : _this$_getNextRule3.state) || '',
  380. step: typeof step === 'number' ? step : (((_this$_getNextRule4 = this._getNextRule()) === null || _this$_getNextRule4 === void 0 ? void 0 : _this$_getNextRule4.step) || 0) + 1
  381. });
  382. break;
  383. case RuleKind.LIST_OF_TYPE_CONSTRAINT:
  384. rule = rule;
  385. this.state.rules.push({
  386. listOfType: rule.listOfType,
  387. optional: Boolean(rule.optional),
  388. butNot: rule.butNot,
  389. eatNextOnFail: Boolean(rule.eatNextOnFail),
  390. name: name || '',
  391. depth: depth,
  392. expanded: false,
  393. kind: RuleKind.LIST_OF_TYPE_CONSTRAINT,
  394. state: (typeof name === 'string' ? name : undefined) || (typeof state === 'string' ? state : undefined) || ((_this$_getNextRule5 = this._getNextRule()) === null || _this$_getNextRule5 === void 0 ? void 0 : _this$_getNextRule5.state) || '',
  395. step: typeof step === 'number' ? step : (((_this$_getNextRule6 = this._getNextRule()) === null || _this$_getNextRule6 === void 0 ? void 0 : _this$_getNextRule6.step) || 0) + 1
  396. });
  397. break;
  398. case RuleKind.TOKEN_CONSTRAINT:
  399. rule = rule;
  400. this.state.rules.push({
  401. token: rule.token,
  402. ofValue: rule.ofValue,
  403. oneOf: rule.oneOf,
  404. definitionName: Boolean(rule.definitionName),
  405. typeName: Boolean(rule.typeName),
  406. optional: Boolean(rule.optional),
  407. butNot: rule.butNot,
  408. eatNextOnFail: Boolean(rule.eatNextOnFail),
  409. name: name || '',
  410. depth: depth,
  411. expanded: false,
  412. kind: RuleKind.TOKEN_CONSTRAINT,
  413. state: (typeof rule.tokenName === 'string' ? rule.tokenName : undefined) || (typeof state === 'string' ? state : undefined) || ((_this$_getNextRule7 = this._getNextRule()) === null || _this$_getNextRule7 === void 0 ? void 0 : _this$_getNextRule7.state) || '',
  414. step: typeof step === 'number' ? step : (((_this$_getNextRule8 = this._getNextRule()) === null || _this$_getNextRule8 === void 0 ? void 0 : _this$_getNextRule8.step) || 0) + 1
  415. });
  416. break;
  417. case RuleKind.PEEK_CONSTRAINT:
  418. rule = rule;
  419. this.state.rules.push({
  420. peek: rule.peek,
  421. optional: Boolean(rule.optional),
  422. butNot: rule.butNot,
  423. eatNextOnFail: Boolean(rule.eatNextOnFail),
  424. name: name || '',
  425. depth: depth,
  426. index: -1,
  427. matched: false,
  428. expanded: false,
  429. kind: RuleKind.PEEK_CONSTRAINT,
  430. state: (typeof state === 'string' ? state : undefined) || ((_this$_getNextRule9 = this._getNextRule()) === null || _this$_getNextRule9 === void 0 ? void 0 : _this$_getNextRule9.state) || '',
  431. step: typeof step === 'number' ? step : (((_this$_getNextRule10 = this._getNextRule()) === null || _this$_getNextRule10 === void 0 ? void 0 : _this$_getNextRule10.step) || 0) + 1
  432. });
  433. break;
  434. }
  435. };
  436. _proto._getRuleKind = function _getRuleKind(rule) {
  437. if (Array.isArray(rule)) {
  438. return RuleKind.CONSTRAINTS_SET;
  439. }
  440. if (rule.constraintsSet === true) {
  441. return RuleKind.CONSTRAINTS_SET_ROOT;
  442. }
  443. if (typeof rule === 'string') {
  444. return RuleKind.RULE_NAME;
  445. }
  446. if (Object.prototype.hasOwnProperty.call(rule, 'ofType')) {
  447. return RuleKind.OF_TYPE_CONSTRAINT;
  448. }
  449. if (Object.prototype.hasOwnProperty.call(rule, 'listOfType')) {
  450. return RuleKind.LIST_OF_TYPE_CONSTRAINT;
  451. }
  452. if (Object.prototype.hasOwnProperty.call(rule, 'peek')) {
  453. return RuleKind.PEEK_CONSTRAINT;
  454. }
  455. if (Object.prototype.hasOwnProperty.call(rule, 'token')) {
  456. return RuleKind.TOKEN_CONSTRAINT;
  457. }
  458. return RuleKind.INVALID;
  459. };
  460. _proto._advanceToken = function _advanceToken() {
  461. return this._lexer.advance();
  462. };
  463. _proto._lookAhead = function _lookAhead() {
  464. try {
  465. return this._lexer.lookahead();
  466. } catch (err) {
  467. return {
  468. kind: TokenKind.INVALID,
  469. value: ''
  470. };
  471. }
  472. };
  473. return OnlineParser;
  474. }();