bool InteractiveParser::parse(FILE *file, const std::string *filename, parser::ProgramNode &programStmts, stmtFunction callback) const {
    yydebug = DEBUG_PARSER;

    yyscan_t scanner;
    yylex_init(&scanner);

    bool interactive = (stdin == file);
#ifndef OS_WINDOWS
    interactive = interactive && (isatty(fileno(file)) == 1);
#endif
    yyset_debug(DEBUG_LEXER, scanner);

    LexerContext lexerCtx(&programStmts, filename, interactive, callback);
    yylex_init_extra(&lexerCtx, &scanner);
    lexerCtx.scanner = scanner;
    lexerCtx.interactive = interactive;
#ifndef OS_WINDOWS
    if(stdin == file){
        rl_instream = stdin;
    }
#endif
    if(interactive){
        std::cout << "Welcome to the sugar interactive shell" << std::endl;
        std::cout << "Use 'quit' or 'exit' to quit this shell" << std::endl;
    }

    yy_push_state(lex_state_newline, scanner);
    yyset_in( file, scanner );
    bool succeed = (yyparse(&lexerCtx) == 0);

    yylex_destroy(scanner);
    fclose(file);
    return succeed && (interactive || !lexerCtx.hasError);
}
Exemple #2
0
unsigned char parse_and_report(const char *filename)
{
	yyscan_t scanner;
	FILE *f;
	unsigned char err = 0;

	if(filename == NULL || strcmp(filename, "-") == 0) {
		f = stdin;
		filename = "stdin";
	} else {
		f = fopen(filename, "r");
		if(!f) {
			perror(filename);
			return 1;
		}
	}

	yylex_init(&scanner);
	yyset_in(f, scanner);
	yyset_extra((void *) filename, scanner);
#ifdef DEBUG
	yyset_debug(1, scanner);
#endif

	if(xml_output) printf("\t<file name=\"%s\">\n", filename);

	switch(yyparse(scanner)) {
	case 0:
		if(!xml_output) printf("%s: valid JSON\n", filename);
		break;
	case 1:
		if(!xml_output) printf("%s: parsing failed\n", filename);
		err = 1;
		break;
	case 2:
		fprintf(stderr, "Out of memory\n");
		exit(1);
		break;
	}

	if(xml_output) printf("\t</file>\n");

	yylex_destroy(scanner);
	if(f != stdin) fclose(f);

	return err;
}
Exemple #3
0
ParserStatus FidlParser::parse(const std::string& file, bool debug)
{
  clear();

  //std::cout << "Opening file '" << file << "' for parsing\n";
  bool ok = open_and_buffer_file(&open_files_, file);
  if (!ok)
  {
    return first_error_;
  }

  assert(open_files_.size() == 1);

  //std::cout << "Initializing lexer.\n";

  yylex_init(&lexer_);

  if (debug)
  {
    yyset_debug(1, lexer_);
    yydebug = 1;
  }

  yyset_extra(this, lexer_);
  yyset_in(current_file_ptr(), lexer_);

  //std::cout << "Starting parsing of '" << current_file() << "'.\n";

  root_node_ = new ast::Root();

  yyparse(this, lexer_);

  //std::cout << "Done parsing '" << current_file() << "'.\n";

  yylex_destroy(lexer_);

  return first_error_;
}