const.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. /*
  10. Package parser contains a GraphQL parser. Based on GraphQL spec June 2018.
  11. Lexer for Source Text - @spec 2.1
  12. Lex() is a lexer function to convert a given search query into a list of tokens.
  13. Based on a talk by Rob Pike: Lexical Scanning in Go
  14. https://www.youtube.com/watch?v=HxaD_trXwRE
  15. The lexer's output is pushed into a channel which is consumed by the parser.
  16. This design enables the concurrent processing of the input text by lexer and
  17. parser.
  18. Parser
  19. Parse() is a parser which produces a parse tree from a given set of lexer tokens.
  20. Based on an article by Douglas Crockford: Top Down Operator Precedence
  21. http://crockford.com/javascript/tdop/tdop.html
  22. which is based on the ideas of Vaughan Pratt and his paper: Top Down Operator Precedence
  23. http://portal.acm.org/citation.cfm?id=512931
  24. https://tdop.github.io/
  25. ParseWithRuntime() parses a given input and decorates the resulting parse tree
  26. with runtime components which can be used to interpret the parsed query.
  27. */
  28. package parser
  29. /*
  30. LexTokenID represents a unique lexer token ID
  31. */
  32. type LexTokenID int
  33. /*
  34. Available lexer token types
  35. */
  36. const (
  37. TokenError LexTokenID = iota // Lexing error token with a message as val
  38. TokenEOF // End-of-file token
  39. // Punctuators - @spec 2.1.8
  40. // GraphQL documents include punctuation in order to describe structure.
  41. // GraphQL is a data description language and not a programming language,
  42. // therefore GraphQL lacks the punctuation often used to describe mathematical expressions.
  43. TokenPunctuator
  44. // Names - @spec 2.1.9
  45. // GraphQL Documents are full of named things: operations, fields, arguments, types,
  46. // directives, fragments, and variables. All names must follow the same grammatical
  47. // form. Names in GraphQL are case‐sensitive. That is to say name, Name, and NAME
  48. // all refer to different names. Underscores are significant, which means
  49. // other_name and othername are two different names. Names in GraphQL are limited
  50. // to this ASCII subset of possible characters to support interoperation with as
  51. // many other systems as possible.
  52. TokenName
  53. // Integer value - @spec 2.9.1
  54. // An Integer number is specified without a decimal point or exponent (ex. 1).
  55. TokenIntValue
  56. // Float value - @spec 2.9.2
  57. // A Float number includes either a decimal point (ex. 1.0) or an exponent
  58. // (ex. 1e50) or both (ex. 6.0221413e23).
  59. TokenFloatValue
  60. // String Value - @spec 2.9.4
  61. // Strings are sequences of characters wrapped in double‐quotes (").
  62. // (ex. "Hello World"). White space and other otherwise‐ignored characters are
  63. // significant within a string value. Unicode characters are allowed within String
  64. // value literals, however SourceCharacter must not contain some ASCII control
  65. // characters so escape sequences must be used to represent these characters.
  66. TokenStringValue
  67. // General token used for plain ASTs
  68. TokenGeneral
  69. )
  70. /*
  71. Available parser AST node types
  72. */
  73. const (
  74. NodeAlias = "Alias"
  75. NodeArgument = "Argument"
  76. NodeArguments = "Arguments"
  77. NodeDefaultValue = "DefaultValue"
  78. NodeDirective = "Directive"
  79. NodeDirectives = "Directives"
  80. NodeDocument = "Document"
  81. NodeEnumValue = "EnumValue"
  82. NodeEOF = "EOF"
  83. NodeExecutableDefinition = "ExecutableDefinition"
  84. NodeField = "Field"
  85. NodeFragmentDefinition = "FragmentDefinition"
  86. NodeFragmentName = "FragmentName"
  87. NodeFragmentSpread = "FragmentSpread"
  88. NodeInlineFragment = "InlineFragment"
  89. NodeListValue = "ListValue"
  90. NodeName = "Name"
  91. NodeObjectField = "ObjectField"
  92. NodeObjectValue = "ObjectValue"
  93. NodeOperationDefinition = "OperationDefinition"
  94. NodeOperationType = "OperationType"
  95. NodeSelectionSet = "SelectionSet"
  96. NodeType = "Type"
  97. NodeTypeCondition = "TypeCondition"
  98. NodeValue = "Value"
  99. NodeVariable = "Variable"
  100. NodeVariableDefinition = "VariableDefinition"
  101. NodeVariableDefinitions = "VariableDefinitions"
  102. )
  103. /*
  104. ValueNodes are AST nodes which contain a significant value
  105. */
  106. var ValueNodes = []string{
  107. NodeAlias,
  108. NodeDefaultValue,
  109. NodeEnumValue,
  110. NodeFragmentName,
  111. NodeFragmentSpread,
  112. NodeName,
  113. NodeObjectField,
  114. NodeOperationType,
  115. NodeType,
  116. NodeTypeCondition,
  117. NodeValue,
  118. NodeVariable,
  119. }