Skip to content

Commit 18a9c67

Browse files
committed
Initial import
0 parents  commit 18a9c67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+8537
-0
lines changed

.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
bison.tab.cpp
2+
bison.tab.hpp
3+
*.o
4+
parser.output
5+
dump_json_ast
6+
Ast.h
7+
Ast.cpp
8+
AstVisitor.h
9+
*.dSYM
10+
CmakeCache.txt
11+
CMakeFiles
12+
CMakeScripts
13+
Makefile
14+
cmake_install.cmake
15+
*.a
16+
*.dylib
17+
*.so
18+
GraphQLParser.py

.travis.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: cpp
2+
3+
compiler:
4+
- clang
5+
- gcc
6+
7+
before_install:
8+
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
9+
- sudo apt-get update -qq
10+
# bison and flex are not installed in CI because
11+
# 1) the versions in Travis are too old, and
12+
# 2) up-to-date bison and flex output should be checked in.
13+
# Versions of g++ prior to 4.8 don't have very good C++11 support.
14+
- sudo apt-get install -y g++-4.8
15+
&& sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 90
16+
- wget http://www.cmake.org/files/v3.1/cmake-3.1.3-Linux-x86_64.tar.gz
17+
&& tar zxf cmake-3.1.3-Linux-x86_64.tar.gz
18+
&& sudo cp -fR cmake-3.1.3-Linux-x86_64/* /usr/
19+
- wget https://googletest.googlecode.com/files/gtest-1.7.0.zip
20+
&& cd test
21+
&& unzip ../gtest-1.7.0.zip
22+
&& cd ..
23+
&& rm gtest-1.7.0.zip
24+
25+
script: cmake . -Dtest=ON && make && test/runTests

AstNode.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#pragma once
11+
12+
#include "location.hh"
13+
14+
namespace facebook {
15+
namespace graphql {
16+
namespace ast {
17+
18+
namespace visitor {
19+
class AstVisitor;
20+
}
21+
22+
class Node {
23+
yy::location location_;
24+
public:
25+
explicit Node(const yy::location &location)
26+
: location_(location) {}
27+
28+
virtual ~Node() {}
29+
30+
const yy::location &getLocation() const
31+
{ return location_; }
32+
33+
virtual void accept(visitor::AstVisitor *visitor) = 0;
34+
};
35+
36+
}
37+
}
38+
}

CMakeLists.txt

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
2+
PROJECT(libgraphqlparser C CXX)
3+
4+
FIND_PACKAGE(PythonInterp)
5+
IF (NOT PYTHON_VERSION_MAJOR EQUAL 2)
6+
MESSAGE(FATAL_ERROR "Python 2 is required.")
7+
ENDIF()
8+
9+
FIND_PROGRAM(CTYPESGEN_FOUND ctypesgen.py)
10+
11+
FIND_PACKAGE(BISON 3)
12+
FIND_PACKAGE(FLEX)
13+
IF (BISON_FOUND)
14+
BISON_TARGET(graphqlparser parser.ypp parser.tab.cpp)
15+
ENDIF()
16+
17+
IF(FLEX_FOUND)
18+
FLEX_TARGET(GraphQLScanner lexer.lpp lexer.cpp COMPILE_FLAGS "--header-file=lexer.h")
19+
IF (BISON_FOUND)
20+
ADD_FLEX_BISON_DEPENDENCY(GraphQLScanner graphqlparser)
21+
ENDIF()
22+
ENDIF()
23+
24+
ADD_LIBRARY(graphqlparser SHARED
25+
Ast.cpp
26+
JsonVisitor.cpp Ast.h AstVisitor.h
27+
c/GraphQLAst.h
28+
c/GraphQLAst.cpp
29+
c/GraphQLAstNode.cpp
30+
c/GraphQLAstForEachConcreteType.h
31+
c/GraphQLAstVisitor.h
32+
c/GraphQLAstVisitor.cpp
33+
c/GraphQLParser.cpp
34+
parser.tab.cpp parser.tab.hpp
35+
lexer.cpp
36+
GraphQLParser.cpp)
37+
38+
TARGET_COMPILE_FEATURES(graphqlparser PUBLIC cxx_auto_type cxx_override)
39+
40+
ADD_EXECUTABLE(dump_json_ast dump_json_ast.cpp)
41+
TARGET_LINK_LIBRARIES(dump_json_ast graphqlparser)
42+
43+
ADD_CUSTOM_COMMAND(
44+
OUTPUT Ast.h
45+
COMMAND python ./ast/ast.py c++ ./ast/ast.ast > Ast.h
46+
DEPENDS ./ast/ast.ast
47+
)
48+
49+
ADD_CUSTOM_COMMAND(
50+
OUTPUT AstVisitor.h
51+
COMMAND python ./ast/ast.py c++visitor ./ast/ast.ast > AstVisitor.h
52+
DEPENDS ./ast/ast.ast
53+
)
54+
55+
ADD_CUSTOM_COMMAND(
56+
OUTPUT Ast.cpp
57+
COMMAND python ./ast/ast.py c++impl ./ast/ast.ast > Ast.cpp
58+
DEPENDS ./ast/ast.ast
59+
)
60+
61+
ADD_CUSTOM_COMMAND(
62+
OUTPUT c/GraphQLAst.h
63+
COMMAND python ./ast/ast.py c ./ast/ast.ast > c/GraphQLAst.h
64+
DEPENDS ./ast/ast.ast
65+
)
66+
67+
ADD_CUSTOM_COMMAND(
68+
OUTPUT c/GraphQLAst.cpp
69+
COMMAND python ./ast/ast.py cimpl ./ast/ast.ast > c/GraphQLAst.cpp
70+
DEPENDS ./ast/ast.ast
71+
)
72+
73+
ADD_CUSTOM_COMMAND(
74+
OUTPUT c/GraphQLAstForEachConcreteType.h
75+
COMMAND python ./ast/ast.py cvisitorimpl ./ast/ast.ast > c/GraphQLAstForEachConcreteType.h
76+
DEPENDS ./ast/ast.ast
77+
)
78+
79+
ADD_SUBDIRECTORY(python)
80+
81+
OPTION(test "Build tests." OFF)
82+
83+
IF (test)
84+
ADD_SUBDIRECTORY(test)
85+
ENDIF()

CONTRIBUTING.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Contributing to libgraphqlparser
2+
We want to make contributing to this project as easy and transparent as
3+
possible.
4+
5+
## Pull Requests
6+
We actively welcome your pull requests.
7+
8+
1. Fork the repo and create your branch from `master`.
9+
2. If you've added code that should be tested, add tests
10+
3. If you've changed APIs, update the documentation.
11+
4. Ensure the test suite passes.
12+
5. Make sure your code lints.
13+
6. If you haven't already, complete the Contributor License Agreement ("CLA").
14+
15+
## Contributor License Agreement ("CLA")
16+
In order to accept your pull request, we need you to submit a CLA. You only need
17+
to do this once to work on any of Facebook's open source projects.
18+
19+
Complete your CLA here: <https://code.facebook.com/cla>
20+
21+
## Issues
22+
We use GitHub issues to track public bugs. Please ensure your description is
23+
clear and has sufficient instructions to be able to reproduce the issue.
24+
25+
Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe
26+
disclosure of security bugs. In those cases, please go through the process
27+
outlined on that page and do not file a public issue.
28+
29+
## License
30+
By contributing to libgraphqlparser, you agree that your contributions
31+
will be licensed under its BSD license.

GraphQLParser.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#include "GraphQLParser.h"
11+
12+
#include "AstNode.h"
13+
14+
#include "parser.tab.hpp"
15+
#include "lexer.h"
16+
17+
void lexer_reset();
18+
19+
namespace facebook {
20+
namespace graphql {
21+
22+
// Given properly-configured yylex, run the parser and return the
23+
// result.
24+
static std::unique_ptr<ast::Node> doParse(const char **outError) {
25+
Node *outAST;
26+
lexer_reset();
27+
yy::GraphQLParserImpl parser(&outAST, outError);
28+
int failure = parser.parse();
29+
yylex_destroy();
30+
return !failure ? std::unique_ptr<ast::Node>(outAST) : nullptr;
31+
}
32+
33+
std::unique_ptr<ast::Node> parseString(const char *text, const char **error) {
34+
YY_BUFFER_STATE buffer = yy_scan_string(text);
35+
yy_switch_to_buffer(buffer);
36+
37+
return doParse(error);
38+
}
39+
40+
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error) {
41+
yyin = file;
42+
43+
return doParse(error);
44+
}
45+
46+
}
47+
}

GraphQLParser.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
/**
11+
* The purpose of this file is to provide a nice interface to parsing
12+
* GraphQL, rather than the old-fashioned interface provided by bison
13+
* and flex.
14+
*/
15+
16+
#pragma once
17+
18+
#include <memory>
19+
#include <stdio.h>
20+
21+
namespace facebook {
22+
namespace graphql {
23+
24+
namespace ast {
25+
class Node;
26+
}
27+
28+
/**
29+
* Parse the given GraphQL source string, returning an AST. Returns
30+
* nullptr on error and, if error is not null, places a string
31+
* describing what went wrong in error that must be freed with free(3).
32+
*/
33+
std::unique_ptr<ast::Node> parseString(const char *text, const char **error);
34+
35+
/**
36+
* Read and parse GraphQL source from the given file, returning an
37+
* AST. Returns nullptr on error and, if error is not null, places an
38+
* error string in error that must be freed with free(3).
39+
*/
40+
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error);
41+
42+
}
43+
}

0 commit comments

Comments
 (0)