Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
swolchok committed Aug 13, 2015
0 parents commit 18a9c67
Show file tree
Hide file tree
Showing 53 changed files with 8,537 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
bison.tab.cpp
bison.tab.hpp
*.o
parser.output
dump_json_ast
Ast.h
Ast.cpp
AstVisitor.h
*.dSYM
CmakeCache.txt
CMakeFiles
CMakeScripts
Makefile
cmake_install.cmake
*.a
*.dylib
*.so
GraphQLParser.py
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: cpp

compiler:
- clang
- gcc

before_install:
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
- sudo apt-get update -qq
# bison and flex are not installed in CI because
# 1) the versions in Travis are too old, and
# 2) up-to-date bison and flex output should be checked in.
# Versions of g++ prior to 4.8 don't have very good C++11 support.
- sudo apt-get install -y g++-4.8
&& sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 90
- wget http://www.cmake.org/files/v3.1/cmake-3.1.3-Linux-x86_64.tar.gz
&& tar zxf cmake-3.1.3-Linux-x86_64.tar.gz
&& sudo cp -fR cmake-3.1.3-Linux-x86_64/* /usr/
- wget https://googletest.googlecode.com/files/gtest-1.7.0.zip
&& cd test
&& unzip ../gtest-1.7.0.zip
&& cd ..
&& rm gtest-1.7.0.zip

script: cmake . -Dtest=ON && make && test/runTests
38 changes: 38 additions & 0 deletions AstNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#pragma once

#include "location.hh"

namespace facebook {
namespace graphql {
namespace ast {

namespace visitor {
class AstVisitor;
}

class Node {
yy::location location_;
public:
explicit Node(const yy::location &location)
: location_(location) {}

virtual ~Node() {}

const yy::location &getLocation() const
{ return location_; }

virtual void accept(visitor::AstVisitor *visitor) = 0;
};

}
}
}
85 changes: 85 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
PROJECT(libgraphqlparser C CXX)

FIND_PACKAGE(PythonInterp)
IF (NOT PYTHON_VERSION_MAJOR EQUAL 2)
MESSAGE(FATAL_ERROR "Python 2 is required.")
ENDIF()

FIND_PROGRAM(CTYPESGEN_FOUND ctypesgen.py)

FIND_PACKAGE(BISON 3)
FIND_PACKAGE(FLEX)
IF (BISON_FOUND)
BISON_TARGET(graphqlparser parser.ypp parser.tab.cpp)
ENDIF()

IF(FLEX_FOUND)
FLEX_TARGET(GraphQLScanner lexer.lpp lexer.cpp COMPILE_FLAGS "--header-file=lexer.h")
IF (BISON_FOUND)
ADD_FLEX_BISON_DEPENDENCY(GraphQLScanner graphqlparser)
ENDIF()
ENDIF()

ADD_LIBRARY(graphqlparser SHARED
Ast.cpp
JsonVisitor.cpp Ast.h AstVisitor.h
c/GraphQLAst.h
c/GraphQLAst.cpp
c/GraphQLAstNode.cpp
c/GraphQLAstForEachConcreteType.h
c/GraphQLAstVisitor.h
c/GraphQLAstVisitor.cpp
c/GraphQLParser.cpp
parser.tab.cpp parser.tab.hpp
lexer.cpp
GraphQLParser.cpp)

TARGET_COMPILE_FEATURES(graphqlparser PUBLIC cxx_auto_type cxx_override)

ADD_EXECUTABLE(dump_json_ast dump_json_ast.cpp)
TARGET_LINK_LIBRARIES(dump_json_ast graphqlparser)

ADD_CUSTOM_COMMAND(
OUTPUT Ast.h
COMMAND python ./ast/ast.py c++ ./ast/ast.ast > Ast.h
DEPENDS ./ast/ast.ast
)

ADD_CUSTOM_COMMAND(
OUTPUT AstVisitor.h
COMMAND python ./ast/ast.py c++visitor ./ast/ast.ast > AstVisitor.h
DEPENDS ./ast/ast.ast
)

ADD_CUSTOM_COMMAND(
OUTPUT Ast.cpp
COMMAND python ./ast/ast.py c++impl ./ast/ast.ast > Ast.cpp
DEPENDS ./ast/ast.ast
)

ADD_CUSTOM_COMMAND(
OUTPUT c/GraphQLAst.h
COMMAND python ./ast/ast.py c ./ast/ast.ast > c/GraphQLAst.h
DEPENDS ./ast/ast.ast
)

ADD_CUSTOM_COMMAND(
OUTPUT c/GraphQLAst.cpp
COMMAND python ./ast/ast.py cimpl ./ast/ast.ast > c/GraphQLAst.cpp
DEPENDS ./ast/ast.ast
)

ADD_CUSTOM_COMMAND(
OUTPUT c/GraphQLAstForEachConcreteType.h
COMMAND python ./ast/ast.py cvisitorimpl ./ast/ast.ast > c/GraphQLAstForEachConcreteType.h
DEPENDS ./ast/ast.ast
)

ADD_SUBDIRECTORY(python)

OPTION(test "Build tests." OFF)

IF (test)
ADD_SUBDIRECTORY(test)
ENDIF()
31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Contributing to libgraphqlparser
We want to make contributing to this project as easy and transparent as
possible.

## Pull Requests
We actively welcome your pull requests.

1. Fork the repo and create your branch from `master`.
2. If you've added code that should be tested, add tests
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
6. If you haven't already, complete the Contributor License Agreement ("CLA").

## Contributor License Agreement ("CLA")
In order to accept your pull request, we need you to submit a CLA. You only need
to do this once to work on any of Facebook's open source projects.

Complete your CLA here: <https://code.facebook.com/cla>

## Issues
We use GitHub issues to track public bugs. Please ensure your description is
clear and has sufficient instructions to be able to reproduce the issue.

Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe
disclosure of security bugs. In those cases, please go through the process
outlined on that page and do not file a public issue.

## License
By contributing to libgraphqlparser, you agree that your contributions
will be licensed under its BSD license.
47 changes: 47 additions & 0 deletions GraphQLParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#include "GraphQLParser.h"

#include "AstNode.h"

#include "parser.tab.hpp"
#include "lexer.h"

void lexer_reset();

namespace facebook {
namespace graphql {

// Given properly-configured yylex, run the parser and return the
// result.
static std::unique_ptr<ast::Node> doParse(const char **outError) {
Node *outAST;
lexer_reset();
yy::GraphQLParserImpl parser(&outAST, outError);
int failure = parser.parse();
yylex_destroy();
return !failure ? std::unique_ptr<ast::Node>(outAST) : nullptr;
}

std::unique_ptr<ast::Node> parseString(const char *text, const char **error) {
YY_BUFFER_STATE buffer = yy_scan_string(text);
yy_switch_to_buffer(buffer);

return doParse(error);
}

std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error) {
yyin = file;

return doParse(error);
}

}
}
43 changes: 43 additions & 0 deletions GraphQLParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

/**
* The purpose of this file is to provide a nice interface to parsing
* GraphQL, rather than the old-fashioned interface provided by bison
* and flex.
*/

#pragma once

#include <memory>
#include <stdio.h>

namespace facebook {
namespace graphql {

namespace ast {
class Node;
}

/**
* Parse the given GraphQL source string, returning an AST. Returns
* nullptr on error and, if error is not null, places a string
* describing what went wrong in error that must be freed with free(3).
*/
std::unique_ptr<ast::Node> parseString(const char *text, const char **error);

/**
* Read and parse GraphQL source from the given file, returning an
* AST. Returns nullptr on error and, if error is not null, places an
* error string in error that must be freed with free(3).
*/
std::unique_ptr<ast::Node> parseFile(FILE *file, const char **error);

}
}
Loading

0 comments on commit 18a9c67

Please sign in to comment.