Beispiel #1
0
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);
}
/* Compile a string */
void compile_string(compiler_type *comp_void, char *str, bool include_baselib) {
  compiler_core_type *compiler = (compiler_core_type *)comp_void;
  ins_stream_type *baselib = 0; /* TODO: should be gc root */
  char path[PATH_MAX];

  /* Actually parse the input stream. */
  yylex_init_extra(compiler, &(compiler->scanner));
  yy_scan_string(str, compiler->scanner);

  /* TODO: Need a better way to handle GC than leaking */
  gc_protect(compiler->gc);

  /* Inject include for base library */
  if (include_baselib) {
    strcpy(path, compiler->home);
    strcat(path, "/lib/baselib.scm");

    STREAM_NEW(baselib, string, path);
    setup_include(compiler, baselib); 
  }
  
  parse_internal(compiler, compiler->scanner);
  
  gc_unprotect(compiler->gc);

  yylex_destroy(compiler->scanner);
}
int
main()
{
  struct pcdata p = { NULL, 0, NULL };

  /* set up scanner */
  if(yylex_init_extra(&p, &p.scaninfo)) {
    perror("init alloc failed");
    return 1;
  }

  /* allocate and zero out the symbol table */
  if(!(p.symtab = calloc(NHASH, sizeof(struct symbol)))) {
    perror("sym alloc failed");
    return 1;
  }

  for(;;) {
    printf("> "); 
    yyparse(&p);
    if(p.ast) {
      printf("= %4.4g\n> ", eval(&p, p.ast));
      treefree(&p, p.ast);
      p.ast = 0;
    }
  }
}
Beispiel #4
0
void ne_init(void)
{
	memset(&ne, 0, sizeof(ne));

	yylex_init_extra(&ne, &ne.scanner);

	init_built_in(&ne);
	init_plugin(&ne);
}
Beispiel #5
0
void push_parse_state(interp_core_type *interp, FILE *fin) {
    
    scanner_stack_type *scanner=alloc_scanner();

    yylex_init_extra(interp, &(scanner->scanner));
    yyset_in(fin, scanner->scanner);

    scanner->previous=interp->scanner;
    interp->scanner=scanner;
}
Beispiel #6
0
// tokenizes and adds everything from a file under a single field
wp_error* wp_entry_add_file(wp_entry* entry, const char* field, FILE* f) {
  yyscan_t scanner;
  lexinfo charpos = {0, 0};

  yylex_init_extra(&charpos, &scanner);
  yyset_in(f, scanner);
  RELAY_ERROR(add_from_lexer(entry, &scanner, field));
  yylex_destroy(scanner);

  return NO_ERROR;
}
Beispiel #7
0
// tokenizes and adds everything under a single field
wp_error* wp_entry_add_string(wp_entry* entry, const char* field, const char* string) {
  yyscan_t scanner;
  lexinfo charpos = {0, 0};

  yylex_init_extra(&charpos, &scanner);
  YY_BUFFER_STATE state = yy_scan_string(string, scanner);
  RELAY_ERROR(add_from_lexer(entry, &scanner, field));
  yy_delete_buffer(state, scanner);
  yylex_destroy(scanner);

  return NO_ERROR;
}
Beispiel #8
0
EagleDbParser* EagleDbParser_ParseWithString(const char *sql)
{
    EagleDbParser *parser = EagleDbParser_New();

    yylex_init_extra(parser, &parser->yyparse);
    if (NULL != parser) {
        yy_scan_string(sql, parser->yyparse);
        yyparse(parser, parser->yyparse);
    }

    return parser;
}
static CtPgnReader
ct_pgn_reader_new(CtGraph graph, CtGameTags game_tags, char *error_message)
{
  CtPgnReader pgn_reader;

  pgn_reader = ct_malloc(sizeof(CtPgnReaderStruct));
  pgn_reader->graph = graph;
  pgn_reader->game_tags = game_tags;
  pgn_reader->error_message = error_message;
  yylex_init_extra(pgn_reader, &pgn_reader->pgn_scanner);
  ct_pgn_reader_reset(pgn_reader);
  return pgn_reader;
}
Beispiel #10
0
static int svtParserParseInit(svt_parser_t *parser)
{
    if (yylex_init_extra(&parser->yylval, &parser->scanner) != 0){
        perror("Can't initialize scanner");
        return -1;
    }
    yyset_in(parser->input, parser->scanner);

    parser->cur_tok = -1;
    parser->cur_obj = NULL;

    return 0;
}
Beispiel #11
0
ast *lat_parse_expr(char *expr) {
  ast *ret = NULL;
  yyscan_t scanner;
  YY_BUFFER_STATE state;

  lex_state scan_state = {.insert = 0};
  yylex_init_extra(&scan_state, &scanner);
  state = yy_scan_string(expr, scanner);
  yyparse(&ret, scanner);
  yy_delete_buffer(state, scanner);
  yylex_destroy(scanner);
  return ret;
}

ast *lat_parse_file(char *infile) {
  if (infile == NULL) {
    printf("Especifique un archivo\n");
    return NULL;
  }
  file = fopen(infile, "r");
  if (file == NULL) {
    printf("No se pudo abrir el archivo\n");
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  int fsize = ftell(file);
  fseek(file, 0, SEEK_SET);
  buffer = calloc(fsize, 1);
  size_t newSize = fread(buffer, sizeof(char), fsize, file);
  if (buffer == NULL) {
    printf("No se pudo asignar %d bytes de memoria\n", fsize);
    fclose(file);
    return NULL;
  }
  buffer[newSize] = '\0';
  fclose(file);
  return lat_parse_expr(buffer);
}

void lat_compile(lat_vm *vm) {
  vm->regs[255] =
      ast_parse_tree(vm, lat_parse_expr(lat_get_str_value(lat_pop_stack(vm))));
}
Beispiel #12
0
std::unique_ptr<AstTranslationUnit> ParserDriver::parse(const std::string& code, SymbolTable& symbolTable,
        ErrorReport& errorReport, DebugReport& debugReport) {
    translationUnit = std::make_unique<AstTranslationUnit>(
            std::unique_ptr<AstProgram>(new AstProgram()), symbolTable, errorReport, debugReport);

    scanner_data data;
    data.yyfilename = "<in-memory>";
    yyscan_t scanner;
    yylex_init_extra(&data, &scanner);
    yy_scan_string(code.c_str(), scanner);
    yy::parser parser(*this, scanner);
    parser.set_debug_level(trace_parsing);
    parser.parse();

    yylex_destroy(scanner);

    translationUnit->getProgram()->finishParsing();

    return std::move(translationUnit);
}
Beispiel #13
0
/* Compile a file */
void compile_file(compiler_type *comp_void, char *file_name, bool include_baselib) {
  compiler_core_type *compiler = (compiler_core_type *)comp_void;
  ins_stream_type *baselib = 0; /* TODO: should be gc root */
  FILE *in = 0;
  char path[PATH_MAX];

  /* Actually parse the input stream. */
  yylex_init_extra(compiler, &(compiler->scanner));

  in = fopen(file_name, "r");
  if (!in) {
    (void)fprintf(stderr, "Error %i while attempting to open '%s'\n",
      errno, file_name);
      assert(0);
  }

  //yyset_in(in, compiler->scanner);
  yy_switch_to_buffer(
    yy_create_buffer(in, YY_BUF_SIZE, compiler->scanner), compiler->scanner);

  push_include_path(compiler, file_name);

  /* TODO: Need a better way to handle GC than leaking */
  gc_protect(compiler->gc);

  /* Inject include for base library */
  if (include_baselib) {
    strcpy(path, compiler->home);
    strcat(path, "/lib/baselib.scm");

    STREAM_NEW(baselib, string, path);
    setup_include(compiler, baselib); 
  }
  
  parse_internal(compiler, compiler->scanner);
  
  gc_unprotect(compiler->gc);

  yylex_destroy(compiler->scanner);
}
Beispiel #14
0
__attribute__ ((constructor)) void colorit_load(void) {
  int new_out;

  if (fork_count++)
    return;

  if (pipe(color_pipe) != 0) {
    perror("pipe creation failed");
    return;
  }

  child_pid = fork();
  if (!child_pid) {
    FILE *fd_in = NULL;
    colorit_data data = { NULL, NULL};

    close(color_pipe[1]);

    if (yylex_init_extra(&data, &data.scaninfo)) {
        perror("init alloc failed");
        exit(1);
    }
    yyset_in(fd_in = fdopen(color_pipe[0], "r"), data.scaninfo);
    data.out = stdout;

    if (colorit_init(&data, LIBCOLORIT_TERM) != 0) {
      fprintf(stderr, "Can't init %s\n", LIBCOLORIT_TERM);
      exit(1);
    }

    yyparse(&data);
    fclose(fd_in);
    exit(0);
  }

  unsetenv("LD_PRELOAD");
  close(color_pipe[0]);
  dup2(color_pipe[1], 1);
  dup2(color_pipe[1], 2);
}
Beispiel #15
0
HQLNode* ASTUtil::parser_hql(const string &hql)
{
    yyscan_t scanner;
    ShellState *shell;
    HQLNode *ret = NULL;
    string s = string("SAVE <<");
    s = s + hql;
    s = s + ">>;";
    shell = new ShellState();
    yylex_init_extra(shell, &scanner);
    yy_scan_string(s.c_str(), scanner);
    yyparse(scanner);
    yylex_destroy(scanner);
    HQLNode *n = shell->top_hql();
    if(n && !n->error()){
        ret = n;
        shell->pop_hql(1, false);
    }
    delete shell;
    return ret;
    //return new ErrorNode("parser error: " + hql);
}
Beispiel #16
0
void parse_file(FILE* f) {
  yyscan_t scanner;
  lexinfo charpos = {0, 0};
  int token_type;
  const char* token;

  yylex_init_extra(&charpos, &scanner);
  yyset_in(f, scanner);
  do {
    token_type = yylex(scanner);
    token = yyget_text(scanner);
    if(random() < 50000) {
      if(!new_query) printf(" ");
      new_query = 0;
      printf("%s", token);
    }
    if(!new_query && (random() < 50000)) {
      printf("\n");
      new_query = 1;
    }
  } while(token_type != TOK_DONE);
  yylex_destroy(scanner);
}
Beispiel #17
0
solid_ast_node *solid_parse_expr(char *expr)
{
	solid_ast_node *ret = NULL;
	yyscan_t scanner;
	YY_BUFFER_STATE state;

	scanner_state scan_state = {.insert = 0};
	yylex_init_extra(&scan_state, &scanner);
	state = yy_scan_string(expr, scanner);
	yyparse(&ret, scanner);
	yy_delete_buffer(state, scanner);
	yylex_destroy(scanner);

	return ret;
}

solid_ast_node *solid_parse_file(char *path)
{
	FILE *f = fopen(path, "r");
	char buffer[1024 * 1024] = {0};
	fread(buffer, sizeof(char), 1024 * 1024, f);
	fclose(f);
	return solid_parse_expr(buffer);
}
Beispiel #18
0
/*Thread task function for reentrant scanner*/
void *lex_thread_task(void *arg)
{  
  lex_thread_arg *ar = (lex_thread_arg*) arg;
  
  FILE * f = fopen(ar->file_name, "r");
  if (f == NULL) {
    DEBUG_STDOUT_PRINT("ERROR> Lexing thread %d could not open input file. Aborting.\n", ar->id);
    exit(1);
  }

  lex_token *flex_token;
  int8_t end_of_chunk = 0;
  yyscan_t scanner;   //reentrant flex instance data
  int32_t flex_return_code;
  token_node *token_builder = NULL;
  token_node_stack stack;
  sem_value_stack stack_char;
  sem_value_stack stack_int;

  uint32_t alloc_size = 0, realloc_size = 0;

  uint32_t chunk_length = ar->cut_point_dx - ar->cut_point_sx;

  par_compute_alloc_realloc_size((ar->cut_point_dx - ar->cut_point_sx), &alloc_size, &realloc_size);
  DEBUG_STDOUT_PRINT("LEXER %d > alloc_size %d, realloc_size %d\n", ar->id, alloc_size, realloc_size)

  /*Initialization flex_token*/
  flex_token = (lex_token*) malloc(sizeof(lex_token));
  if (flex_token == NULL) {
    DEBUG_STDOUT_PRINT("ERROR> could not complete malloc flex_token. Aborting.\n");
    exit(1);
  }
  flex_token->chunk_length = chunk_length;
  flex_token->num_chars = 0;
  flex_token->chunk_ended = 0;
  //Initialize stack for semantic values.
  //Since the possible semantic values are only char or int, we can allocate two different stacks, one for each type, avoiding to waste memory.
  init_sem_value_stack(&stack_char, alloc_size, 0);
  init_sem_value_stack(&stack_int, alloc_size, 1);
  flex_token->stack_char = &stack_char;
  flex_token->stack_int = &stack_int;
  flex_token->realloc_size = realloc_size;

  fseek(f, ar->cut_point_sx, SEEK_SET);

  if(yylex_init_extra(flex_token, &scanner))
  {
    DEBUG_STDOUT_PRINT("ERROR> yylex_init_extra failed.\n")
    exit(1);
  }

  yyset_in(f, scanner);
  
  ar->list_begin = NULL;
  init_token_node_stack(&(stack), alloc_size);

  flex_return_code = yylex(scanner); 

  /*The procedure to find cut points cannot cut a single token in two parts.*/
  while(!end_of_chunk && flex_return_code != __END_OF_CHUNK && flex_return_code != __END_OF_FILE)
  {
    if(flex_return_code == __LEX_CORRECT) {
      //append a token
      par_append_token_node(flex_token->token, flex_token->semantic_value, &token_builder, &(ar->list_begin), &stack, realloc_size);
      ar->lex_token_list_length++;
    }
    else {
      //flex_return_code is __ERROR)
      DEBUG_STDOUT_PRINT("Lexing thread %d scanned erroneous input. Abort.\n", ar->id)
      ar->result = 1; /*Signal error by returning result!=0.*/
      fclose(yyget_in(scanner));
      yylex_destroy(scanner);
      fclose(f);
      pthread_exit(NULL);
    }

    if (flex_token->chunk_ended)
      end_of_chunk = 1;
    else {
      //Continue to scan the chunk
      flex_return_code = yylex(scanner); 
    }
  }

  ar->list_end = token_builder;

  fclose(yyget_in(scanner));
  yylex_destroy(scanner);

  fclose(f);

  pthread_exit(NULL);

}
Beispiel #19
0
void create_parser(interp_core_type *interp) {
    
    interp->scanner=alloc_scanner();
    
    yylex_init_extra(interp, &(peek_scanner));
}
Beispiel #20
0
EHI::EHI(interactivity_enum _inter, EHInterpreter *_parent, ehcontext_t _context, const std::string &dir, const std::string &name) : scanner(), interactivity(_inter), yy_buffer(), buffer(), str_str(), parent(_parent), interpreter_context(_context), program_code(nullptr), stack(), stack_entry(name, nullptr, stack), inloop(0), breaking(0), continuing(0), returning(false), working_dir(dir), context_name(name) {
	yylex_init_extra(this, &scanner);
}
Beispiel #21
0
int shell_execute(shell_context_t *ctx, const char *cmd) {
	yyscan_t scanner;
	if(yylex_init_extra(ctx, &scanner) == -1)
		return -1;

	ctx->line = strdup(cmd);
	char *ptr = ctx->line;

	int token;
	int state = STATE_WANTSTR;
	int argc = 0;
	char **argv = NULL;
	int fret = 0;

	do {
		int noway = 0;

		token = yylex(scanner);

		if((token == TOK_SEPARATOR || token == TOK_COMMENT || token == 0)) {
			if(argc > 0) {
				int ret = shell_run(ctx, argc, argv);

				for(int i = 0; i < argc; i++) {
					free(argv[i]);
				}

				free(argv);

				argv = NULL;
				argc = 0;

				if(ret == -1) {
					fret = -1;

					break;
				}
			}

			state = STATE_WANTSTR;
		} else {
			switch(state) {
			case STATE_WANTSTR:
				if(token == TOK_SPACE) {
					state = STATE_WANTSTR;
				} else if(token == TOK_STRING) {
					int oargc = argc++;

					argv = realloc(argv, sizeof(char *) * argc);

					argv[oargc] = ctx->strval;

					state = STATE_WANTSPACE;
				} else {
					noway = 1;
				}

				break;

			case STATE_WANTSPACE:
				if(token == TOK_STRING) {
					free(ctx->strval);

					noway = 1;
				} else if(token == TOK_SPACE) {
					state = STATE_WANTSTR;
				} else
					noway = 1;
			}

			if(noway) {
				debug(LEVEL_ERROR, "No way from state %d by token %d\n", state, token);

				for(int i = 0; i < argc; i++)
					free(argv[i]);

				free(argv);

				fret = -1;

				break;
			}
		}

	} while(token && token != TOK_COMMENT);

	free(ptr);

	yylex_destroy(scanner);

	return fret;
}
Beispiel #22
0
EHI::EHI() : scanner(), interactivity(cli_prompt_e), yy_buffer(), buffer(), str_str(), parent(new EHInterpreter()), interpreter_context(parent->global_object), program_code(nullptr), stack(), stack_entry("(none)", nullptr, stack), inloop(0), breaking(0), continuing(0), returning(false), working_dir(eh_getcwd()), context_name("(none)") {
	yylex_init_extra(this, &scanner);
}