void test_token_parse__no_comment(void) { token tk = token_parse("\"not # a comment\" \"foo\" bar"); ass(str_eq(token_str_value(tk), "not # a comment")); token tk2 = token_parse(token_rest(tk)); ass(str_eq(token_str_value(tk2), "foo")); }
void test_token_parse__comment(void) { token tk = token_parse("# abc def\nfoo #ghi jkl \n bar"); ass(str_eq(token_symbol_value(tk), "foo")); token tk2 = token_parse(token_rest(tk)); ass(str_eq(token_symbol_value(tk2), "bar")); ass(str_eq(token_rest(tk2), "")); }
void test_token_parse__empty(void) { token tk = token_parse("\n \t \r \n\r"); ass(token_is_empty(tk)); token tk2 = token_parse("\n \t foo \n \r\t"); ass(str_eq(token_symbol_value(tk2), "foo")); token tk3 = token_parse(token_rest(tk2)); ass(token_is_empty(tk3)); }
void test_token_parse__index(void) { token tk = token_parse(" \n [0] [12] bar"); ass(token_is_index(tk)); ass(token_index_value(tk) == 0); ass(str_eq(token_rest(tk), " [12] bar")); token tk2 = token_parse(token_rest(tk)); ass(token_is_index(tk2)); ass(token_index_value(tk2) == 12); ass(str_eq(token_rest(tk2), " bar")); }
void test_token_parse__str(void) { token tk = token_parse("\n \"hello world!\" \n \" foobar \" 3 4"); ass(token_is_str(tk)); ass(str_eq(token_str_value(tk), "hello world!")); ass(str_eq(token_rest(tk), " \n \" foobar \" 3 4")); token tk2 = token_parse(token_rest(tk)); ass(token_is_str(tk2)); ass(str_eq(token_str_value(tk2), " foobar ")); ass(str_eq(token_rest(tk2), " 3 4")); }
void test_token_parse__float(void) { token tk = token_parse("3.14 -2.72 foo bar"); ass(token_is_float(tk)); ass(token_float_value(tk) == 3.14); ass(str_eq(token_rest(tk), " -2.72 foo bar")); token tk2 = token_parse(token_rest(tk)); ass(token_is_float(tk2)); ass(token_float_value(tk2) == -2.72); ass(str_eq(token_rest(tk2), " foo bar")); }
void test_token_parse__symbol(void) { //TODO foo#comment token tk = token_parse("\nfoo bar\nspam\teggs"); ass(token_is_symbol(tk)); ass(str_eq(token_symbol_value(tk), "foo")); ass(str_eq(token_rest(tk), " bar\nspam\teggs")); token tk2 = token_parse(token_rest(tk)); ass(token_is_symbol(tk2)); ass(str_eq(token_symbol_value(tk2), "bar")); ass(str_eq(token_rest(tk2), "\nspam\teggs")); }
void test_token_parse__def(void) { token tk = token_parse(" foo: bar # abc \nspam: eggs"); ass(token_is_def(tk)); ass(str_eq(token_def_value(tk), "foo")); token tk2 = token_parse(token_rest(tk)); ass(token_is_symbol(tk2)); ass(str_eq(token_symbol_value(tk2), "bar")); token tk3 = token_parse(token_rest(tk2)); ass(token_is_def(tk3)); ass(str_eq(token_def_value(tk3), "spam")); }
/* * Return a given node from the database. */ DLLEXP struct pdb_node_t* pdb_query_node(struct pdb* dbptr, char* path) { char** tok_arr; struct pdb_node_t* nptr; struct pdb_node_types_t* tiptr; int i = 0; if (!dbptr) return 0; PDB_MUTEX_LOCK(dbptr); if (!strcmp(path, "") || !strcmp(path, "/")) { PDB_MUTEX_UNLOCK(dbptr); return dbptr->data; } tok_arr = token_parse(path, PDB_PATH_DELIM, NULL); nptr = dbptr->data; tiptr = NULL; while (tok_arr[i]) { tiptr = pdb_get_type_info(nptr->type); nptr = tiptr->query_cb(nptr, tok_arr[i]); if (!nptr) break; ++i; } token_free(tok_arr); PDB_MUTEX_UNLOCK(dbptr); return nptr; }
static pdfout_data * parser_parse (fz_context *ctx, pdfout_parser *parse) { parser *p = (parser *) parse; if (p->finished) pdfout_throw (ctx, "call to finished outline wysiwyg parser"); p->finished = true; pdfout_data *lines = get_lines (ctx, p); if (pdfout_data_array_len (ctx, lines) == 0) return lines; pdfout_data *tokens = get_tokens (ctx, lines); pdfout_data *outline = token_parse(ctx, tokens); pdfout_data_drop (ctx, tokens); pdfout_data_drop (ctx, lines); return outline; }
/* * in - Source file to be interpreted * file_name - Name of the source file, for debugging purposes * sym_table - Global (master) symbol table */ void liten(FILE * in, char * file_name, slist * sym_table, token ** ret) { // *ret = NULL; slist * block_stack = slist_init(); slist * local_table = slist_init(); slist_push(sym_table, local_table); file_desc * desc = malloc(sizeof(file_desc)); desc->file = in; desc->name = file_name; desc->line = 1; int tok; char * temp = malloc(sizeof(char)*MAX_STR); strcpy(temp, "\0"); int type = -1; slist * tok_queue = slist_init(); while (1) { if (in == stdin) { printf(":> "); } else if (tok == EOF) { break; } tok = getc(in); while (1) { if (tok == '\n' || tok == EOF) { desc->line++; tokenize(desc, &temp, &type, tok_queue); type = -1; // slist_print(tok_queue, slist_print_token); // TODO: Check line for syntax errors if (tok_queue->len == 0) break; if (block_stack->len == 0) { rpn(desc, tok_queue, sym_table, block_stack, ret); } else { char * temp_str = ((token *) slist_get(tok_queue, 0)->obj)->obj; block * temp = (block *) slist_get(block_stack, -1)->obj; if (strcmp("end", temp_str) == 0) { rpn(desc, tok_queue, sym_table, block_stack, ret); } else if (strcmp("if", temp_str) == 0 || strcmp("while", temp_str) == 0) { if (temp->value == 1) rpn(desc, tok_queue, sym_table, block_stack, ret); else { block * temp1 = malloc(sizeof(block)); temp1->value = 0; if (strcmp("if", temp_str) == 0) { temp1->type = IF_B; } else if (strcmp("while", temp_str) == 0) { temp1->type = WHILE_B; } slist_push(block_stack, temp1); } } else if (strcmp("elif", temp_str) == 0) { if (temp->type != IF_B) lite_error(desc, "'elif' exists in 'if' block"); if (temp->value == 1) temp->value = 0; else rpn(desc, tok_queue, sym_table, block_stack, ret); } else if (strcmp("else", temp_str) == 0) { if (temp->type != IF_B) lite_error(desc, "'else' exists in 'if' block"); if (temp->value == 1) temp->value = 0; else temp->value = 1; } else { if (temp->value == 1) rpn(desc, tok_queue, sym_table, block_stack, ret); } } tok_queue = slist_init(); } else { token_parse(desc, tok, &temp, &type, tok_queue); } if (in == stdin && tok == '\n') break; tok = getc(in); } } return; }