-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Integrate graphql-js experimental parser
- Loading branch information
1 parent
6114f2a
commit 7b42f3d
Showing
4 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/graphql-language-service-parser/src/GraphqlParser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |