onlineParser.d.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { Lexer } from '../lexer';
  2. import {
  3. GraphQLGrammarTokenConstraint,
  4. GraphQLGrammarOfTypeConstraint,
  5. GraphQLGrammarListOfTypeConstraint,
  6. GraphQLGrammarPeekConstraint,
  7. GraphQLGrammarConstraintsSet,
  8. } from './grammar';
  9. interface BaseOnlineParserRule {
  10. kind: string;
  11. name?: string;
  12. depth: number;
  13. step: number;
  14. expanded: boolean;
  15. state: string;
  16. optional?: boolean;
  17. eatNextOnFail?: boolean;
  18. }
  19. interface TokenOnlineParserRule
  20. extends BaseOnlineParserRule,
  21. GraphQLGrammarTokenConstraint {}
  22. interface OfTypeOnlineParserRule
  23. extends BaseOnlineParserRule,
  24. GraphQLGrammarOfTypeConstraint {}
  25. interface ListOfTypeOnlineParserRule
  26. extends BaseOnlineParserRule,
  27. GraphQLGrammarListOfTypeConstraint {}
  28. interface PeekOnlineParserRule
  29. extends BaseOnlineParserRule,
  30. GraphQLGrammarPeekConstraint {
  31. index: number;
  32. matched: boolean;
  33. }
  34. interface ConstraintsSetOnlineParserRule extends BaseOnlineParserRule {
  35. constraintsSet: boolean;
  36. constraints: GraphQLGrammarConstraintsSet;
  37. }
  38. type OnlineParserRule =
  39. | TokenOnlineParserRule
  40. | OfTypeOnlineParserRule
  41. | ListOfTypeOnlineParserRule
  42. | PeekOnlineParserRule
  43. | ConstraintsSetOnlineParserRule;
  44. export interface OnlineParserState {
  45. rules: Array<OnlineParserRule>;
  46. kind: () => string;
  47. step: () => number;
  48. levels: Array<number>;
  49. indentLevel: number | undefined;
  50. name: string | null;
  51. type: string | null;
  52. }
  53. interface Token {
  54. kind: string;
  55. value?: string;
  56. tokenName?: string | undefined;
  57. ruleName?: string | undefined;
  58. }
  59. type OnlineParserConfig = {
  60. tabSize: number;
  61. };
  62. type OnlineParserConfigOption = {
  63. tabSize?: number;
  64. };
  65. export class OnlineParser {
  66. state: OnlineParserState;
  67. _lexer: Lexer;
  68. _config: OnlineParserConfig;
  69. constructor(
  70. source: string,
  71. state?: OnlineParserState,
  72. config?: OnlineParserConfigOption,
  73. );
  74. static startState(): OnlineParserState;
  75. static copyState(state: OnlineParserState): OnlineParserState;
  76. sol(): boolean;
  77. parseToken(): Token;
  78. indentation(): number;
  79. private readonly _parseTokenConstraint;
  80. private readonly _parseListOfTypeConstraint;
  81. private readonly _parseOfTypeConstraint;
  82. private readonly _parsePeekConstraint;
  83. private readonly _parseConstraintsSetRule;
  84. private readonly _matchToken;
  85. private readonly _butNot;
  86. private readonly _transformLexerToken;
  87. private readonly _getNextRule;
  88. private readonly _popMatchedRule;
  89. private readonly _rollbackRule;
  90. private readonly _pushRule;
  91. private readonly _getRuleKind;
  92. private readonly _advanceToken;
  93. private readonly _lookAhead;
  94. }
  95. export const TokenKind: {
  96. NAME: string;
  97. INT: string;
  98. FLOAT: string;
  99. STRING: string;
  100. BLOCK_STRING: string;
  101. COMMENT: string;
  102. PUNCTUATION: string;
  103. EOF: string;
  104. INVALID: string;
  105. };
  106. export const RuleKind: {
  107. TOKEN_CONSTRAINT: string;
  108. OF_TYPE_CONSTRAINT: string;
  109. LIST_OF_TYPE_CONSTRAINT: string;
  110. PEEK_CONSTRAINT: string;
  111. CONSTRAINTS_SET: string;
  112. CONSTRAINTS_SET_ROOT: string;
  113. RULE_NAME: string;
  114. INVALID: string;
  115. };