ast.lua 939 B

1234567891011121314151617181920212223242526
  1. local lpeg = require 'lpeg'
  2. local P, R, S, V, C, Ct, Cmt, Cg = lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg
  3. -- Utility
  4. local space = S(' \t\r\n') ^ 0
  5. local comma = P(',') ^ -1
  6. -- "Terminals"
  7. local name = space * C(R('az', 'AZ') * (P('_') + R('09') + R('az', 'AZ')) ^ 0)
  8. local alias = space * name * P(':')
  9. local value = space * C(R('09') ^ 1) -- todo values are hard
  10. local argument = space * Ct(Cg(name, 'name') * P(':') * Cg(value, 'value')) * comma
  11. local arguments = P('(') * Ct(argument ^ 1) * P(')')
  12. -- Nonterminals
  13. local graphQL = P {
  14. 'input',
  15. input = space * V('selectionSet') * -1,
  16. selectionSet = space * P('{') * space * Ct(V('selection') ^ 0) * space * P('}'),
  17. selection = space * V('field'),
  18. field = Ct(space * Cg(alias ^ -1, 'alias') * Cg(name, 'name') * Cg(arguments ^ -1, 'arguments') * Cg(V('selectionSet'), 'children') ^ 0) * comma,
  19. }
  20. return function(str)
  21. return graphQL:match(str)
  22. end