const.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. )
  68. /*
  69. Available parser AST node types
  70. */
  71. const (
  72. NodeAlias = "Alias"
  73. NodeArgument = "Argument"
  74. NodeArguments = "Arguments"
  75. NodeDefaultValue = "DefaultValue"
  76. NodeDirective = "Directive"
  77. NodeDirectives = "Directives"
  78. NodeDocument = "Document"
  79. NodeEnumValue = "EnumValue"
  80. NodeEOF = "EOF"
  81. NodeExecutableDefinition = "ExecutableDefinition"
  82. NodeField = "Field"
  83. NodeFragmentDefinition = "FragmentDefinition"
  84. NodeFragmentName = "FragmentName"
  85. NodeFragmentSpread = "FragmentSpread"
  86. NodeInlineFragment = "InlineFragment"
  87. NodeListValue = "ListValue"
  88. NodeName = "Name"
  89. NodeObjectField = "ObjectField"
  90. NodeObjectValue = "ObjectValue"
  91. NodeOperationDefinition = "OperationDefinition"
  92. NodeOperationType = "OperationType"
  93. NodeSelectionSet = "SelectionSet"
  94. NodeType = "Type"
  95. NodeTypeCondition = "TypeCondition"
  96. NodeValue = "Value"
  97. NodeVariable = "Variable"
  98. NodeVariableDefinition = "VariableDefinition"
  99. NodeVariableDefinitions = "VariableDefinitions"
  100. )
  101. /*
  102. ValueNodes are AST nodes which contain a significant value
  103. */
  104. var ValueNodes = []string{
  105. NodeAlias,
  106. NodeDefaultValue,
  107. NodeEnumValue,
  108. NodeFragmentName,
  109. NodeFragmentSpread,
  110. NodeName,
  111. NodeObjectField,
  112. NodeOperationType,
  113. NodeType,
  114. NodeTypeCondition,
  115. NodeValue,
  116. NodeVariable,
  117. }