123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723 |
- // @flow strict
- import { Lexer } from '../lexer';
- import { Source } from '../source';
- import GraphQLGrammar from './grammar';
- import type {
- GraphQLGrammarRule,
- GraphQLGrammarRuleName,
- GraphQLGrammarRuleConstraint,
- GraphQLGrammarTokenConstraint,
- GraphQLGrammarOfTypeConstraint,
- GraphQLGrammarListOfTypeConstraint,
- GraphQLGrammarPeekConstraint,
- GraphQLGrammarConstraintsSet,
- } from './grammar';
- export const TokenKind = {
- NAME: 'Name',
- INT: 'Int',
- FLOAT: 'Float',
- STRING: 'String',
- BLOCK_STRING: 'BlockString',
- COMMENT: 'Comment',
- PUNCTUATION: 'Punctuation',
- EOF: '<EOF>',
- INVALID: 'Invalid',
- };
- export const RuleKind = {
- TOKEN_CONSTRAINT: 'TokenConstraint',
- OF_TYPE_CONSTRAINT: 'OfTypeConstraint',
- LIST_OF_TYPE_CONSTRAINT: 'ListOfTypeConstraint',
- PEEK_CONSTRAINT: 'PeekConstraint',
- CONSTRAINTS_SET: 'ConstraintsSet',
- CONSTRAINTS_SET_ROOT: 'ConstraintsSetRoot',
- RULE_NAME: 'RuleName',
- INVALID: 'Invalid',
- };
- interface BaseOnlineParserRule {
- kind: string;
- name?: string;
- depth: number;
- step: number;
- expanded: boolean;
- state: string;
- optional?: boolean;
- eatNextOnFail?: boolean;
- }
- interface TokenOnlineParserRule
- extends BaseOnlineParserRule,
- GraphQLGrammarTokenConstraint {}
- interface OfTypeOnlineParserRule
- extends BaseOnlineParserRule,
- GraphQLGrammarOfTypeConstraint {}
- interface ListOfTypeOnlineParserRule
- extends BaseOnlineParserRule,
- GraphQLGrammarListOfTypeConstraint {}
- interface PeekOnlineParserRule
- extends BaseOnlineParserRule,
- GraphQLGrammarPeekConstraint {
- index: number;
- matched: boolean;
- }
- interface ConstraintsSetOnlineParserRule extends BaseOnlineParserRule {
- constraintsSet: boolean;
- constraints: GraphQLGrammarConstraintsSet;
- }
- type OnlineParserRule =
- | TokenOnlineParserRule
- | OfTypeOnlineParserRule
- | ListOfTypeOnlineParserRule
- | PeekOnlineParserRule
- | ConstraintsSetOnlineParserRule;
- export type OnlineParserState = {|
- rules: Array<OnlineParserRule>,
- kind: () => string,
- step: () => number,
- levels: Array<number>,
- indentLevel: number,
- name: string | null,
- type: string | null,
- |};
- type Token = {|
- kind: string,
- value: string,
- tokenName?: ?string,
- ruleName?: ?string,
- |};
- type LexerToken = {|
- kind: string,
- value: ?string,
- |};
- type OnlineParserConfig = {|
- tabSize: number,
- |};
- type OnlineParserConfigOption = {|
- tabSize: ?number,
- |};
- export class OnlineParser {
- state: OnlineParserState;
- _lexer: Lexer;
- _config: OnlineParserConfig;
- constructor(
- source: string,
- state?: OnlineParserState,
- config?: OnlineParserConfigOption,
- ) {
- this.state = state || OnlineParser.startState();
- this._config = {
- tabSize: config?.tabSize ?? 2,
- };
- this._lexer = new Lexer(new Source(source));
- }
- static startState(): OnlineParserState {
- return {
- rules: [
- // $FlowFixMe[cannot-spread-interface]
- {
- name: 'Document',
- state: 'Document',
- kind: 'ListOfTypeConstraint',
- ...GraphQLGrammar.Document,
- expanded: false,
- depth: 1,
- step: 1,
- },
- ],
- name: null,
- type: null,
- levels: [],
- indentLevel: 0,
- kind(): string {
- return this.rules[this.rules.length - 1]?.state || '';
- },
- step(): number {
- return this.rules[this.rules.length - 1]?.step || 0;
- },
- };
- }
- static copyState(state: OnlineParserState): OnlineParserState {
- return {
- name: state.name,
- type: state.type,
- rules: JSON.parse(JSON.stringify(state.rules)),
- levels: [...state.levels],
- indentLevel: state.indentLevel,
- kind(): string {
- return this.rules[this.rules.length - 1]?.state || '';
- },
- step(): number {
- return this.rules[this.rules.length - 1]?.step || 0;
- },
- };
- }
- sol(): boolean {
- return (
- this._lexer.source.locationOffset.line === 1 &&
- this._lexer.source.locationOffset.column === 1
- );
- }
- parseToken(): Token {
- const rule = (this._getNextRule(): any);
- if (this.sol()) {
- this.state.indentLevel = Math.floor(
- this.indentation() / this._config.tabSize,
- );
- }
- if (!rule) {
- return {
- kind: TokenKind.INVALID,
- value: '',
- };
- }
- let token;
- if (this._lookAhead().kind === '<EOF>') {
- return {
- kind: TokenKind.EOF,
- value: '',
- ruleName: rule.name,
- };
- }
- switch (rule.kind) {
- case RuleKind.TOKEN_CONSTRAINT:
- token = this._parseTokenConstraint(rule);
- break;
- case RuleKind.LIST_OF_TYPE_CONSTRAINT:
- token = this._parseListOfTypeConstraint(rule);
- break;
- case RuleKind.OF_TYPE_CONSTRAINT:
- token = this._parseOfTypeConstraint(rule);
- break;
- case RuleKind.PEEK_CONSTRAINT:
- token = this._parsePeekConstraint(rule);
- break;
- case RuleKind.CONSTRAINTS_SET_ROOT:
- token = this._parseConstraintsSetRule(rule);
- break;
- default:
- return {
- kind: TokenKind.INVALID,
- value: '',
- ruleName: rule.name,
- };
- }
- if (token && token.kind === TokenKind.INVALID) {
- if (rule.optional === true) {
- this.state.rules.pop();
- } else {
- this._rollbackRule();
- }
- return this.parseToken() || token;
- }
- return token;
- }
- indentation(): number {
- const match = this._lexer.source.body.match(/\s*/);
- let indent = 0;
- if (match && match.length === 0) {
- const whiteSpaces = match[0];
- let pos = 0;
- while (whiteSpaces.length > pos) {
- if (whiteSpaces.charCodeAt(pos) === 9) {
- indent += 2;
- } else {
- indent++;
- }
- pos++;
- }
- }
- return indent;
- }
- _parseTokenConstraint(rule: TokenOnlineParserRule): Token {
- rule.expanded = true;
- const token = this._lookAhead();
- if (!this._matchToken(token, rule)) {
- return {
- kind: TokenKind.INVALID,
- value: '',
- tokenName: rule.tokenName,
- ruleName: rule.name,
- };
- }
- this._advanceToken();
- const parserToken = this._transformLexerToken(token, rule);
- this._popMatchedRule(parserToken);
- return parserToken;
- }
- _parseListOfTypeConstraint(rule: ListOfTypeOnlineParserRule): Token {
- this._pushRule(
- GraphQLGrammar[rule.listOfType],
- rule.depth + 1,
- rule.listOfType,
- 1,
- rule.state,
- );
- rule.expanded = true;
- const token = this.parseToken();
- return token;
- }
- _parseOfTypeConstraint(rule: OfTypeOnlineParserRule): Token {
- if (rule.expanded) {
- this._popMatchedRule();
- return this.parseToken();
- }
- this._pushRule(rule.ofType, rule.depth + 1, rule.tokenName, 1, rule.state);
- rule.expanded = true;
- const token = this.parseToken();
- return token;
- }
- _parsePeekConstraint(rule: PeekOnlineParserRule): Token {
- if (rule.expanded) {
- this._popMatchedRule();
- return this.parseToken();
- }
- while (!rule.matched && rule.index < rule.peek.length - 1) {
- rule.index++;
- const constraint = rule.peek[rule.index];
- let { ifCondition } = constraint;
- if (typeof ifCondition === 'string') {
- ifCondition = GraphQLGrammar[ifCondition];
- }
- let token = this._lookAhead();
- if (ifCondition && this._matchToken(token, ifCondition)) {
- rule.matched = true;
- rule.expanded = true;
- this._pushRule(constraint.expect, rule.depth + 1, '', 1, rule.state);
- token = this.parseToken();
- return token;
- }
- }
- return {
- kind: TokenKind.INVALID,
- value: '',
- ruleName: rule.name,
- };
- }
- _parseConstraintsSetRule(rule: ConstraintsSetOnlineParserRule): Token {
- if (rule.expanded) {
- this._popMatchedRule();
- return this.parseToken();
- }
- for (let index = rule.constraints.length - 1; index >= 0; index--) {
- this._pushRule(
- rule.constraints[index],
- rule.depth + 1,
- '',
- index,
- rule.state,
- );
- }
- rule.expanded = true;
- return this.parseToken();
- }
- _matchToken(
- token: Token | LexerToken,
- rule: GraphQLGrammarTokenConstraint,
- ): boolean {
- if (typeof token.value === 'string') {
- 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)
- ) {
- return false;
- }
- return this._butNot(token, rule);
- }
- if (token.kind !== rule.token) {
- return false;
- }
- return this._butNot(token, rule);
- }
- _butNot(
- token: Token | LexerToken,
- rule: GraphQLGrammarRuleConstraint,
- ): boolean {
- if (rule.butNot) {
- if (Array.isArray(rule.butNot)) {
- if (
- rule.butNot.reduce(
- (matched, constraint) =>
- matched || this._matchToken(token, constraint),
- false,
- )
- ) {
- return false;
- }
- return true;
- }
- return !this._matchToken(token, rule.butNot);
- }
- return true;
- }
- _transformLexerToken(lexerToken: LexerToken, rule: any): Token {
- let token;
- const ruleName = rule.name || '';
- const tokenName = rule.tokenName || '';
- if (lexerToken.kind === '<EOF>' || lexerToken.value !== undefined) {
- token = {
- kind: lexerToken.kind,
- value: lexerToken.value || '',
- tokenName,
- ruleName,
- };
- if (token.kind === TokenKind.STRING) {
- token.value = `"${token.value}"`;
- } else if (token.kind === TokenKind.BLOCK_STRING) {
- token.value = `"""${token.value}"""`;
- }
- } else {
- token = {
- kind: TokenKind.PUNCTUATION,
- value: lexerToken.kind,
- tokenName,
- ruleName,
- };
- if (/^[{([]/.test(token.value)) {
- if (this.state.indentLevel !== undefined) {
- this.state.levels = this.state.levels.concat(
- this.state.indentLevel + 1,
- );
- }
- } else if (/^[})\]]/.test(token.value)) {
- this.state.levels.pop();
- }
- }
- return token;
- }
- _getNextRule(): OnlineParserRule | null {
- return this.state.rules[this.state.rules.length - 1] || null;
- }
- _popMatchedRule(token: ?Token) {
- const rule = this.state.rules.pop();
- if (!rule) {
- return;
- }
- if (token && rule.kind === RuleKind.TOKEN_CONSTRAINT) {
- const constraint = rule;
- if (typeof constraint.definitionName === 'string') {
- this.state.name = token.value || null;
- } else if (typeof constraint.typeName === 'string') {
- this.state.type = token.value || null;
- }
- }
- const nextRule = this._getNextRule();
- if (!nextRule) {
- return;
- }
- if (
- nextRule.depth === rule.depth - 1 &&
- nextRule.expanded &&
- nextRule.kind === RuleKind.CONSTRAINTS_SET_ROOT
- ) {
- this.state.rules.pop();
- }
- if (
- nextRule.depth === rule.depth - 1 &&
- nextRule.expanded &&
- nextRule.kind === RuleKind.LIST_OF_TYPE_CONSTRAINT
- ) {
- nextRule.expanded = false;
- nextRule.optional = true;
- }
- }
- _rollbackRule() {
- if (!this.state.rules.length) {
- return;
- }
- const popRule = () => {
- const lastPoppedRule = this.state.rules.pop();
- if (lastPoppedRule.eatNextOnFail === true) {
- this.state.rules.pop();
- }
- };
- const poppedRule = this.state.rules.pop();
- if (!poppedRule) {
- return;
- }
- let popped = 0;
- let nextRule = this._getNextRule();
- while (
- nextRule &&
- (poppedRule.kind !== RuleKind.LIST_OF_TYPE_CONSTRAINT ||
- nextRule.expanded) &&
- nextRule.depth > poppedRule.depth - 1
- ) {
- this.state.rules.pop();
- popped++;
- nextRule = this._getNextRule();
- }
- if (nextRule && nextRule.expanded) {
- if (nextRule.optional === true) {
- popRule();
- } else {
- if (
- nextRule.kind === RuleKind.LIST_OF_TYPE_CONSTRAINT &&
- popped === 1
- ) {
- this.state.rules.pop();
- return;
- }
- this._rollbackRule();
- }
- }
- }
- _pushRule(
- baseRule: any,
- depth: number,
- name?: string,
- step?: number,
- state?: string,
- ) {
- this.state.name = null;
- this.state.type = null;
- let rule = baseRule;
- switch (this._getRuleKind(rule)) {
- case RuleKind.RULE_NAME:
- rule = (rule: GraphQLGrammarRuleName);
- this._pushRule(
- GraphQLGrammar[rule],
- depth,
- (typeof name === 'string' ? name : undefined) || rule,
- step,
- state,
- );
- break;
- case RuleKind.CONSTRAINTS_SET:
- rule = (rule: GraphQLGrammarConstraintsSet);
- this.state.rules.push({
- name: name || '',
- depth,
- expanded: false,
- constraints: rule,
- constraintsSet: true,
- kind: RuleKind.CONSTRAINTS_SET_ROOT,
- state:
- (typeof name === 'string' ? name : undefined) ||
- (typeof state === 'string' ? state : undefined) ||
- this._getNextRule()?.state ||
- '',
- step:
- typeof step === 'number'
- ? step
- : (this._getNextRule()?.step || 0) + 1,
- });
- break;
- case RuleKind.OF_TYPE_CONSTRAINT:
- rule = (rule: GraphQLGrammarOfTypeConstraint);
- this.state.rules.push({
- name: name || '',
- ofType: rule.ofType,
- optional: Boolean(rule.optional),
- butNot: rule.butNot,
- eatNextOnFail: Boolean(rule.eatNextOnFail),
- depth,
- expanded: false,
- kind: RuleKind.OF_TYPE_CONSTRAINT,
- state:
- (typeof rule.tokenName === 'string' ? rule.tokenName : undefined) ||
- (typeof name === 'string' ? name : undefined) ||
- (typeof state === 'string' ? state : undefined) ||
- this._getNextRule()?.state ||
- '',
- step:
- typeof step === 'number'
- ? step
- : (this._getNextRule()?.step || 0) + 1,
- });
- break;
- case RuleKind.LIST_OF_TYPE_CONSTRAINT:
- rule = (rule: GraphQLGrammarListOfTypeConstraint);
- this.state.rules.push({
- listOfType: rule.listOfType,
- optional: Boolean(rule.optional),
- butNot: rule.butNot,
- eatNextOnFail: Boolean(rule.eatNextOnFail),
- name: name || '',
- depth,
- expanded: false,
- kind: RuleKind.LIST_OF_TYPE_CONSTRAINT,
- state:
- (typeof name === 'string' ? name : undefined) ||
- (typeof state === 'string' ? state : undefined) ||
- this._getNextRule()?.state ||
- '',
- step:
- typeof step === 'number'
- ? step
- : (this._getNextRule()?.step || 0) + 1,
- });
- break;
- case RuleKind.TOKEN_CONSTRAINT:
- rule = (rule: GraphQLGrammarTokenConstraint);
- this.state.rules.push({
- token: rule.token,
- ofValue: rule.ofValue,
- oneOf: rule.oneOf,
- definitionName: Boolean(rule.definitionName),
- typeName: Boolean(rule.typeName),
- optional: Boolean(rule.optional),
- butNot: rule.butNot,
- eatNextOnFail: Boolean(rule.eatNextOnFail),
- name: name || '',
- depth,
- expanded: false,
- kind: RuleKind.TOKEN_CONSTRAINT,
- state:
- (typeof rule.tokenName === 'string' ? rule.tokenName : undefined) ||
- (typeof state === 'string' ? state : undefined) ||
- this._getNextRule()?.state ||
- '',
- step:
- typeof step === 'number'
- ? step
- : (this._getNextRule()?.step || 0) + 1,
- });
- break;
- case RuleKind.PEEK_CONSTRAINT:
- rule = (rule: GraphQLGrammarPeekConstraint);
- this.state.rules.push({
- peek: rule.peek,
- optional: Boolean(rule.optional),
- butNot: rule.butNot,
- eatNextOnFail: Boolean(rule.eatNextOnFail),
- name: name || '',
- depth,
- index: -1,
- matched: false,
- expanded: false,
- kind: RuleKind.PEEK_CONSTRAINT,
- state:
- (typeof state === 'string' ? state : undefined) ||
- this._getNextRule()?.state ||
- '',
- step:
- typeof step === 'number'
- ? step
- : (this._getNextRule()?.step || 0) + 1,
- });
- break;
- }
- }
- _getRuleKind(rule: GraphQLGrammarRule | OnlineParserRule): string {
- if (Array.isArray(rule)) {
- return RuleKind.CONSTRAINTS_SET;
- }
- if (rule.constraintsSet === true) {
- return RuleKind.CONSTRAINTS_SET_ROOT;
- }
- if (typeof rule === 'string') {
- return RuleKind.RULE_NAME;
- }
- if (Object.prototype.hasOwnProperty.call(rule, 'ofType')) {
- return RuleKind.OF_TYPE_CONSTRAINT;
- }
- if (Object.prototype.hasOwnProperty.call(rule, 'listOfType')) {
- return RuleKind.LIST_OF_TYPE_CONSTRAINT;
- }
- if (Object.prototype.hasOwnProperty.call(rule, 'peek')) {
- return RuleKind.PEEK_CONSTRAINT;
- }
- if (Object.prototype.hasOwnProperty.call(rule, 'token')) {
- return RuleKind.TOKEN_CONSTRAINT;
- }
- return RuleKind.INVALID;
- }
- _advanceToken(): LexerToken {
- return (this._lexer.advance(): any);
- }
- _lookAhead(): LexerToken {
- try {
- return (this._lexer.lookahead(): any);
- } catch (err) {
- return { kind: TokenKind.INVALID, value: '' };
- }
- }
- }
|