Skip to content

Commit

Permalink
Merge pull request #8 from schloerke/comment
Browse files Browse the repository at this point in the history
fix location positions and string / comment issues with libgraphql parser.
  • Loading branch information
jeroen authored May 25, 2017
2 parents de0f533 + 3d8fcb2 commit 543eb77
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 49 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ Imports:
jsonlite
LazyData: NA
SystemRequirements: NA
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
Suggests:
testthat
4 changes: 2 additions & 2 deletions R/graphql2json.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#' @param parse_schema boolean to enable schema defintion parsing
#' @examples
#' graphql2json("{ field(complex: { a: { b: [ $var ] } }) }")
#' graphql2json("schema { query: QueryType }")
graphql2json <- function(input, parse_schema = TRUE){
#' graphql2json("schema { query: QueryType }", TRUE)
graphql2json <- function(input, parse_schema = FALSE){
stopifnot(is.character(input))
input <- paste(input, collapse = "\n")
json <- dump_json_ast(input, parse_schema)
Expand Down
7 changes: 3 additions & 4 deletions man/graphql.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"graphql_dump_json_ast", (DL_FUNC) &graphql_dump_json_ast, 2},
{NULL, NULL, 0}
};

RcppExport void R_init_graphql(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
7 changes: 5 additions & 2 deletions src/libgraphqlparser/JsonVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ void JsonVisitor::NodeFieldPrinter::printLocation(
std::ostringstream &out,
const yy::location &location)
{
out << "{\"start\":" << location.begin.column
<< ",\"end\":" << location.end.column << '}';
out << "{\"start\": {\"line\": " << location.begin.line
<< ",\"column\":" << location.begin.column
<< "}, \"end\": {\"line\":" << location.end.line
<< ",\"column\":" << location.end.column
<< "}}";
}

void JsonVisitor::NodeFieldPrinter::printChildList(
Expand Down
69 changes: 34 additions & 35 deletions src/libgraphqlparser/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h,
* if you want the limit (max/min) macros for int types.
* if you want the limit (max/min) macros for int types.
*/
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS 1
Expand All @@ -51,7 +51,7 @@ typedef uint32_t flex_uint32_t;
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
typedef int flex_int32_t;
typedef unsigned char flex_uint8_t;
typedef unsigned char flex_uint8_t;
typedef unsigned short int flex_uint16_t;
typedef unsigned int flex_uint32_t;

Expand Down Expand Up @@ -169,10 +169,10 @@ typedef size_t yy_size_t;
#define EOB_ACT_CONTINUE_SCAN 0
#define EOB_ACT_END_OF_FILE 1
#define EOB_ACT_LAST_MATCH 2

#define YY_LESS_LINENO(n)
#define YY_LINENO_REWIND_TO(ptr)

/* Return all but the first "n" matched characters back to the input stream. */
#define yyless(n) \
do \
Expand Down Expand Up @@ -228,7 +228,7 @@ struct yy_buffer_state

int yy_bs_lineno; /**< The line count. */
int yy_bs_column; /**< The column count. */

/* Whether to try to fill the input buffer when we reach the
* end of it.
*/
Expand Down Expand Up @@ -634,9 +634,9 @@ static int yy_init_globals ( yyscan_t yyscanner );
/* This must go here because YYSTYPE and YYLTYPE are included
* from bison output in section 1.*/
# define yylval yyg->yylval_r

# define yylloc yyg->yylloc_r

int yylex_init (yyscan_t* scanner);

int yylex_init_extra ( YY_EXTRA_TYPE user_defined, yyscan_t* scanner);
Expand Down Expand Up @@ -679,9 +679,9 @@ YYSTYPE * yyget_lval ( yyscan_t yyscanner );
void yyset_lval ( YYSTYPE * yylval_param , yyscan_t yyscanner );

YYLTYPE *yyget_lloc ( yyscan_t yyscanner );

void yyset_lloc ( YYLTYPE * yylloc_param , yyscan_t yyscanner );

/* Macros after this point can all be overridden by user definitions in
* section 1.
*/
Expand All @@ -695,7 +695,7 @@ extern int yywrap ( yyscan_t yyscanner );
#endif

#ifndef YY_NO_UNPUT

#endif

#ifndef yytext_ptr
Expand Down Expand Up @@ -924,6 +924,7 @@ YY_RULE_SETUP
{
BEGIN(INITIAL);
yylval->str = yyextra->str.c_str();
*yylloc = yyextra->loc;
return yy::GraphQLParserImpl::token::TOK_STRING;
}
YY_BREAK
Expand All @@ -947,7 +948,6 @@ YY_RULE_SETUP
{
char *p = yytext;
while (*p) {
yyextra->loc.columns();
yyextra->str.push_back(*p++);
}
}
Expand Down Expand Up @@ -1017,13 +1017,13 @@ case 15:
/* rule 15 can match eol */
YY_RULE_SETUP
#line 100 "lexer.lpp"
{ yyextra->loc.step(); BEGIN(INITIAL); }
{ yyextra->loc.lines(yyleng / 2); yyextra->loc.step(); BEGIN(INITIAL); }
YY_BREAK
case 16:
/* rule 16 can match eol */
YY_RULE_SETUP
#line 101 "lexer.lpp"
{ yyextra->loc.step(); BEGIN(INITIAL); }
{ yyextra->loc.lines(yyleng); yyextra->loc.step(); BEGIN(INITIAL); }
YY_BREAK
case 17:
YY_RULE_SETUP
Expand Down Expand Up @@ -1746,7 +1746,7 @@ static void yy_load_buffer_state (yyscan_t yyscanner)
YY_BUFFER_STATE yy_create_buffer (FILE * file, int size , yyscan_t yyscanner)
{
YY_BUFFER_STATE b;

b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ,yyscanner );
if ( ! b )
YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
Expand Down Expand Up @@ -1812,7 +1812,7 @@ static void yy_load_buffer_state (yyscan_t yyscanner)
}

b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0;

errno = oerrno;
}

Expand Down Expand Up @@ -1918,9 +1918,9 @@ static void yyensure_buffer_stack (yyscan_t yyscanner)
, yyscanner);
if ( ! yyg->yy_buffer_stack )
YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );

memset(yyg->yy_buffer_stack, 0, num_to_alloc * sizeof(struct yy_buffer_state*));

yyg->yy_buffer_stack_max = num_to_alloc;
yyg->yy_buffer_stack_top = 0;
return;
Expand Down Expand Up @@ -1949,12 +1949,12 @@ static void yyensure_buffer_stack (yyscan_t yyscanner)
* @param base the character buffer
* @param size the size in bytes of the character buffer
* @param yyscanner The scanner object.
* @return the newly allocated buffer state object.
* @return the newly allocated buffer state object.
*/
YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size , yyscan_t yyscanner)
{
YY_BUFFER_STATE b;

if ( size < 2 ||
base[size-2] != YY_END_OF_BUFFER_CHAR ||
base[size-1] != YY_END_OF_BUFFER_CHAR )
Expand Down Expand Up @@ -1990,7 +1990,7 @@ YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size , yyscan_t yyscann
*/
YY_BUFFER_STATE yy_scan_string (const char * yystr , yyscan_t yyscanner)
{

return yy_scan_bytes(yystr,(int) strlen(yystr) ,yyscanner);
}

Expand All @@ -2007,7 +2007,7 @@ YY_BUFFER_STATE yy_scan_bytes (const char * yybytes, int _yybytes_len , yyscan
char *buf;
yy_size_t n;
int i;

/* Get memory for full buffer, including space for trailing EOB's. */
n = (yy_size_t) (_yybytes_len + 2);
buf = (char *) yyalloc(n ,yyscanner );
Expand Down Expand Up @@ -2077,10 +2077,10 @@ YY_EXTRA_TYPE yyget_extra (yyscan_t yyscanner)
int yyget_lineno (yyscan_t yyscanner)
{
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;

if (! YY_CURRENT_BUFFER)
return 0;

return yylineno;
}

Expand All @@ -2090,10 +2090,10 @@ int yyget_lineno (yyscan_t yyscanner)
int yyget_column (yyscan_t yyscanner)
{
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;

if (! YY_CURRENT_BUFFER)
return 0;

return yycolumn;
}

Expand Down Expand Up @@ -2155,7 +2155,7 @@ void yyset_lineno (int _line_number , yyscan_t yyscanner)
/* lineno is only valid if an input buffer exists. */
if (! YY_CURRENT_BUFFER )
YY_FATAL_ERROR( "yyset_lineno called with no buffer" );

yylineno = _line_number;
}

Expand All @@ -2170,7 +2170,7 @@ void yyset_column (int _column_no , yyscan_t yyscanner)
/* column is only valid if an input buffer exists. */
if (! YY_CURRENT_BUFFER )
YY_FATAL_ERROR( "yyset_column called with no buffer" );

yycolumn = _column_no;
}

Expand Down Expand Up @@ -2223,13 +2223,13 @@ YYLTYPE *yyget_lloc (yyscan_t yyscanner)
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
return yylloc;
}

void yyset_lloc (YYLTYPE * yylloc_param , yyscan_t yyscanner)
{
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
yylloc = yylloc_param;
}

/* User-visible API */

/* yylex_init is special because it creates the scanner itself, so it is
Expand Down Expand Up @@ -2273,20 +2273,20 @@ int yylex_init_extra(YY_EXTRA_TYPE yy_user_defined,yyscan_t* ptr_yy_globals )
errno = EINVAL;
return 1;
}

*ptr_yy_globals = (yyscan_t) yyalloc ( sizeof( struct yyguts_t ), &dummy_yyguts );

if (*ptr_yy_globals == NULL){
errno = ENOMEM;
return 1;
}

/* By setting to 0xAA, we expose bugs in
yy_init_globals. Leave at 0x00 for releases. */
memset(*ptr_yy_globals,0x00,sizeof(struct yyguts_t));

yyset_extra (yy_user_defined, *ptr_yy_globals);

return yy_init_globals ( *ptr_yy_globals );
}

Expand Down Expand Up @@ -2450,4 +2450,3 @@ static void escape(char c, char *buf) {
}
}
}

6 changes: 3 additions & 3 deletions src/libgraphqlparser/lexer.lpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ notnewline [^\n\r]
\" {
BEGIN(INITIAL);
yylval->str = yyextra->str.c_str();
*yylloc = yyextra->loc;
return yy::GraphQLParserImpl::token::TOK_STRING;
}

Expand All @@ -70,7 +71,6 @@ notnewline [^\n\r]
{STRINGCHAR}+ {
char *p = yytext;
while (*p) {
yyextra->loc.columns();
yyextra->str.push_back(*p++);
}
}
Expand All @@ -96,8 +96,8 @@ notnewline [^\n\r]
}

<LINE_COMMENT_STATE>{
{CRLF} { yyextra->loc.step(); BEGIN(INITIAL); }
{newline} { yyextra->loc.step(); BEGIN(INITIAL); }
{CRLF} { yyextra->loc.lines(yyleng / 2); yyextra->loc.step(); BEGIN(INITIAL); }
{newline} { yyextra->loc.lines(yyleng); yyextra->loc.step(); BEGIN(INITIAL); }
{notnewline}+ /* eat comment character */
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-kitchen_sink.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that("kitchen sink parses with schema functionality", {

kitchen_txt <- paste(readLines("kitchen-sink.graphql"), collapse = "\n")

json_txt <- graphql2json(kitchen_txt)
json_txt <- graphql2json(kitchen_txt, TRUE)

result <- jsonlite::fromJSON(json_txt)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-schema_kitchen_sink.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("kitchen sink schema parses", {

kitchen_schema_txt <- paste(readLines("schema-kitchen-sink.graphql"), collapse = "\n")

json_txt <- graphql2json(kitchen_schema_txt)
json_txt <- graphql2json(kitchen_schema_txt, TRUE)

result <- jsonlite::fromJSON(json_txt)

Expand Down

0 comments on commit 543eb77

Please sign in to comment.