Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][reference] Integrate graphql-js experimental parser #1653

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions packages/codemirror-graphql/src/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
*/

import CodeMirror from 'codemirror';
import {
LexRules,
ParseRules,
isIgnored,
onlineParser,
} from 'graphql-language-service-parser';
import { GraphqlParser } from 'graphql-language-service-parser';

/**
* The GraphQL mode is defined as a tokenizer along with a list of rules, each
Expand All @@ -36,16 +31,11 @@ import {
* which contains the relevant information to produce valuable typeaheads.
*/
CodeMirror.defineMode('graphql', config => {
const parser = onlineParser({
eatWhitespace: stream => stream.eatWhile(isIgnored),
lexRules: LexRules,
parseRules: ParseRules,
editorConfig: { tabSize: config.tabSize },
});
const parser = new GraphqlParser({ tabSize: config.tabSize });

return {
config,
startState: parser.startState,
startState: GraphqlParser.startState,
token: parser.token,
indent,
electricInput: /^\s*[})\]]/,
Expand All @@ -55,6 +45,7 @@ CodeMirror.defineMode('graphql', config => {
pairs: '()[]{}""',
explode: '()[]{}',
},
copyState: GraphqlParser.copyState,
};
});

Expand All @@ -65,7 +56,6 @@ function indent(state, textAfter) {
const level =
!levels || levels.length === 0
? state.indentLevel
: levels[levels.length - 1] -
(this.electricInput.test(textAfter) ? 1 : 0);
: levels[levels.length - 1] - (this.electricInput.test(textAfter) ? 1 : 0);
return level * this.config.indentUnit;
}
83 changes: 83 additions & 0 deletions packages/graphql-language-service-parser/src/GraphqlParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { OnlineParser, OnlineParserState, TokenKind } from 'graphql/experimentalOnlineParser';
import CharacterStream from './CharacterStream';
import { isIgnored, LexRules } from './Rules';
const styles = require('./styles.json');

export interface State extends ParserState {
name: string | null;
type: string | null;
prevState: State | null;
}

export default class GraphqlParser {
config: any;

constructor(config = {}) {
this.config = config;
}

static startState = (): State => {
const state: any = OnlineParser.startState();
state.prevState = null;

return state;
};

static copyState = (state: State): State => {
const newState: any = OnlineParser.copyState(state);
newState.prevState = state.prevState;

return newState as State;
};

token(stream: CharacterStream, state: State): string {
const prevState = GraphqlParser.copyState(state);

if (stream.eatWhile(isIgnored)) {
return 'ws';
}

if (stream.eol()) {
return 'invalidchar';
}

const source = ((stream.match(/.*/, false) as Array<string>) || [])[0] || '';
const parserState = OnlineParser.copyState(state);
const parser = new OnlineParser(source, parserState, this.config);

const token = parser.parseToken();

if (token.kind !== 'Invalid' && token.value) {
Object.assign(state, parserState);
if (token.kind === TokenKind.PUNCTUATION) {
stream.match(LexRules.Punctuation);
} else {
stream.match(token.value);
}
} else {
stream.skipToEnd();
}

state.prevState = prevState;

return styles[token.tokenName] || styles[token.ruleName] || styles[token.kind] || '';
}
}

export type ContextToken = {
start: number;
end: number;
string: string;
state: State;
style: string;
};

export type ContextTokenForCodeMirror = {
start: number;
end: number;
string: string;
type: string | null;
state: State;
};

export type ContextTokenUnion = ContextToken | ContextTokenForCodeMirror;
2 changes: 2 additions & 0 deletions packages/graphql-language-service-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export { butNot, list, opt, p, t } from './RuleHelpers';

export { default as onlineParser } from './onlineParser';

export { default as GraphqlParser } from './GraphqlParser';

export * from './types';
31 changes: 31 additions & 0 deletions packages/graphql-language-service-parser/src/styles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"Invalid": "invalidchar",
"<EOF>": "invalidchar",
"String": "string",
"BlockString": "string",
"EnumValue": "string-2",
"BooleanValue": "builtin",
"NullValue": "keyword",
"Int": "number",
"Float": "number",
"Punctuation": "punctuation",
"OperationType": "keyword",
"OperationName": "def",
"AliasName": "property",
"FieldName": "qualifier",
"ArgumentName": "attribute",
"FragmentDefinitionKeyword": "keyword",
"FragmentName": "def",
"TypeName": "atom",
"DirectiveName": "meta",
"SchemaDefinitionKeyword": "keyword",
"OperationTypeDefinationName": "atom",
"ExtendDefinitionKeyword": "keyword",
"ScalarDefinitionKeyword": "keyword",
"UnionDefinitionKeyword": "keyword",
"EnumDefinitionKeyword": "keyword",
"InputDefinitionKeyword": "keyword",
"OnKeyword": "keyword",
"TypeDefinitionKeyword": "keyword",
"ImplementsKeyword": "keyword"
}