onlineParser.js 18 KB

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