t_s2l *ft_strsplitlst(const char *s, char c) { t_s2l *out; int i; int start; i = 0; out = NULL; start = i = skip_char(i, s, c); while (s[i]) { if (s[i] == c) { add_word(&out, s, start, i - start); start = i = skip_char(i, s, c); } else i++; } if (out == NULL) ft_s2lpushback(&out, ft_s2lcreate(s)); else if (i != start) add_word(&out, s, start, i - start); return (out); }
static LTSV *parse_ltsv(const char **string) { LTSV *ltsv = ltsv_init(); LTSV_Record *record; if (!ltsv) { fprintf(stderr, "couldn't allocate LTSV!\n"); return NULL; } while (**string != '\0') { record = parse_record(string); if (!record) { if (!is_newline(**string)) { goto bail; } fprintf(stderr, "string: %c\n", **string); return ltsv; } ltsv_add(ltsv, record); if (**string == 0x0d) { skip_char(string); } if (**string == 0x0a) { skip_char(string); } } return ltsv; bail: ltsv_free(ltsv); return NULL; }
Variant JSONReader::parse_array() { if (!skip_char('[')) throw Exception(position(), "expected start of array"); Variant::Vector vector; skip_space(); while (!skip_char(']')) { vector.emplace_back(parse_value()); skip_space(); if (skip_char(',')) { skip_space(); if (at(']')) throw Exception(position(), "expected value instead of ] after ,"); } else if (!at(']')) { throw Exception(position(), "expected , or ]"); } } return Variant(std::move(vector)); }
static void ft_tab(char **tab, char c, char const *s) { int i; int j; int count; i = 0; count = 0; while (s[count] != '\0' && (skip_char(s, c, count) != -1)) { j = 0; count = skip_char(s, c, count); tab[i] = (char *)malloc((letters(s, c, count) + 1) * sizeof(char)); if (tab[i] != NULL) { while (s[count] != c && s[count] != '\0') { tab[i][j] = s[count]; j++; count++; } tab[i][j] = '\0'; i++; } } tab[i] = NULL; }
/* Parser */ static void skip_quotes(const char **string) { skip_char(string); while (**string != '\"') { if (**string == '\0') { return; } if (**string == '\\') { skip_char(string); if (**string == '\0') { return; }} skip_char(string); } skip_char(string); }
static LTSV_Record *parse_record(const char **string) { LTSV_Record *output_record = ltsv_record_init(); const char *label = NULL, *value; if (!output_record) { fprintf(stderr, "couldn't allocate LTSV_Record!\n"); return NULL; } while (**string != '\0') { label = parse_string(string, &label_char_test_func); if (!label) { fprintf(stderr, "could not parse label!\n"); goto bail; } if (strlen(label) == 0) { if (is_newline(**string)) { free((void*)label); return output_record; } fprintf(stderr, "invalid character for label: %c\n", **string); goto bail; } if (**string != ':') { fprintf(stderr, "':' not found: '%c' found instead!\n", **string); goto bail; } skip_char(string); value = parse_string(string, &field_char_test_func); if (ltsv_record_get_value(output_record, label)) { fprintf(stderr, "dupliate entry for record: %s\n", label); goto bail; } ltsv_record_add_entry(output_record, label, value); if (**string == '\t') { skip_char(string); } free((void*)label); } return output_record; bail: if (label) { free((void*)label); } ltsv_record_free(output_record); return NULL; }
int init_ptf_format(const char *format, int *i, t_ptf_format *ptf_format) { int flag_cp; int var_set; *i = *i + 1; var_set = 0; while (format && format[*i]) { init_flag(format[*i], &flag_cp, ptf_format, &var_set); skip_char(format, i, ' '); if (!init_field_width(format, i, &var_set, ptf_format)) return (0); init_precision(format, i, ptf_format); if (getchar_pos("hl", format[*i]) != -1) ptf_format->length_modifier = format[*i]; else if (is_alpha(format[*i], 0) || format[*i] == '%') { if (format[*i] == 'n' && (ptf_format->field_width = printf_my_itoa(g_prog.i)) == NULL) return (0); ptf_format->conv_char = format[*i]; return (1); } *i = *i + 1; } return (1); }
static mrb_bool parse_lineno(mrb_state *mrb, char **sp, listcmd_parser_state *st) { if (*sp == NULL || **sp == '\0') { return FALSE; } st->has_line_min = FALSE; st->has_line_max = FALSE; if (parse_uint(sp, &st->line_min)) { st->has_line_min = TRUE; } else { return FALSE; } if (skip_char(sp, ',')) { if (parse_uint(sp, &st->line_max)) { st->has_line_max = TRUE; } else { st->parse_error = TRUE; return FALSE; } } return TRUE; }
/* Parses sort description line of the view and initialized its sort field. */ static void get_sort_info(FileView *view, const char line[]) { char *const sort = curr_stats.restart_in_progress ? ui_view_sort_list_get(view) : view->sort; int j = 0; while(*line != '\0' && j < SK_COUNT) { char *endptr; const int sort_opt = strtol(line, &endptr, 10); if(endptr != line) { line = endptr; view->sort_g[j++] = MIN(SK_LAST, MAX(-SK_LAST, sort_opt)); } else { line++; } line = skip_char(line, ','); } memset(&view->sort_g[j], SK_NONE, sizeof(view->sort_g) - j); if(j == 0) { view->sort_g[0] = SK_DEFAULT; } memcpy(sort, view->sort_g, sizeof(view->sort)); fview_sorting_updated(view); }
static int json_parse_object_tail(struct json_parser* parser) { for (;;) { skip_white_space(parser); switch (peek(parser)) { // Finished when we reach the final closing brace case '}': skip(parser); return JSON_PARSE_SUCCESS; case '"': // Key skip(parser); json_parse_string_tail(parser); // Sep skip_white_space(parser); skip_char(parser, ':'); // Value json_parse_value(parser); // Comma? break; default: return JSON_PARSE_FAIL; } } }
static node_t * find_node(node_t *root, const char *name, int create, node_t **last) { const char *end; size_t name_len; node_t *prev = NULL, *curr; node_t *new_node; name = skip_char(name, '/'); if(*name == '\0') return root; end = until_first(name, '/'); name_len = end - name; curr = root->child; while(curr != NULL) { int comp = strnoscmp(name, curr->name, name_len); if(comp == 0 && curr->name_len == name_len) { if(curr->valid && last != NULL) *last = curr; return find_node(curr, end, create, last); } else if(comp < 0) { break; } prev = curr; curr = curr->next; } if(!create) return NULL; new_node = malloc(sizeof(*new_node)); if(new_node == NULL) return NULL; if((new_node->name = malloc(name_len + 1)) == NULL) { free(new_node); return NULL; } strncpy(new_node->name, name, name_len); new_node->name[name_len] = '\0'; new_node->name_len = name_len; new_node->valid = 0; new_node->child = NULL; new_node->next = curr; if(root->child == curr) root->child = new_node; else prev->next = new_node; return find_node(new_node, end, create, last); }
static void skip_string(struct json_parser* parser, const json_char* s) { while (*s) { skip_char(parser, *s); s++; } }
static mrb_bool parse_listcmd_args(mrb_state *mrb, mrdb_state *mrdb, listcmd_parser_state *st) { char *p; switch (mrdb->wcnt) { case 2: p = mrdb->words[1]; /* mrdb->words[1] ::= <lineno> | <filename> ':' <lineno> | <filename> */ if (!parse_lineno(mrb, &p, st)) { if (parse_filename(mrb, &p, st)) { if (skip_char(&p, ':')) { if (!parse_lineno(mrb, &p, st)) { st->parse_error = TRUE; } } } else { st->parse_error = TRUE; } } if (*p != '\0') { st->parse_error = TRUE; } break; case 1: case 0: /* do nothing */ break; default: st->parse_error = TRUE; printf("too many arguments\n"); break; } if (!st->parse_error) { if (!st->has_line_min) { st->line_min = (!st->filename && mrdb->dbg->prvline > 0) ? mrdb->dbg->prvline : 1; } if (!st->has_line_max) { st->line_max = st->line_min + 9; } if (st->filename == NULL) { if (mrdb->dbg->prvfile && strcmp(mrdb->dbg->prvfile, "-")) { st->filename = replace_ext(mrb, mrdb->dbg->prvfile, ".rb"); } } } if (st->parse_error || st->filename == NULL) { return FALSE; } return TRUE; }
bool FASTQ_format::get_seq(vector<char>& id, vector<Letter>& seq, Compressed_istream & s) const { if (!have_char(s, '@')) return false; id.clear(); seq.clear(); copy_line(s, id, Raw_text()); copy_line(s, seq, Sequence_data()); skip_char(s, '+'); skip_line(s); skip_line(s); return true; }
Variant JSONReader::parse_object() { if (!skip_char('{')) throw Exception(position(), "expected start of object"); Variant::Map map; skip_space(); while (!skip_char('}')) { Variant key = parse_string(); if (map.find(key.as_string()) != map.cend()) throw Exception(position(), "duplicate key"); skip_space(); if (!skip_char(':')) throw Exception(position(), "expected :"); map.emplace(key.as_string(), parse_value()); skip_space(); if (skip_char(',')) { skip_space(); if (at('}')) throw Exception(position(), "expected key instead of } after ,"); } else if (!at('}')) { throw Exception(position(), "expected , or }"); } } return Variant(std::move(map)); }
static const char *parse_string(const char **string, LTSV_Char_Test_Func_Ptr ptr_test_func) { const char *string_start = *string; char *output; while (ptr_test_func((unsigned char)**string)) { if (**string == '\0') { break; } skip_char(string); } output = ltsv_strndup(string_start, *string - string_start); if (!output) { return NULL; } return output; }
Variant JSONReader::parse_number() { if (!at_digit() && !at('-')) throw Exception(position(), "expected digit or -"); std::string number; if (at('-')) number += next_get(); if (skip_char('0')) { number += '0'; if (at_digit()) throw Exception(position(), "number may not start with 0"); } else if (!at_digit()) throw Exception(position(), "expected digit between 1-9"); while (at_digit()) number += next_get(); if (at('.')) { number += next_get(); if (!at_digit()) throw Exception(position(), "expected digit"); while (at_digit()) number += next_get(); } if (at('e')) { number += next_get(); if (at('-') || at('+')) number += next_get(); if (!at_digit()) throw Exception(position(), "expected digit"); while (at_digit()) number += next_get(); } auto d = string_to_double(number); if (!d) throw Exception(position(), "number conversion failed"); return Variant(d.value()); }
/* Draws possibly centered formatted message with specified title and control * message on error_win. */ static void draw_msg(const char title[], const char msg[], const char ctrl_msg[], int centered, int recommended_width) { enum { margin = 1 }; int sw, sh; int w, h; int max_h; int len; size_t ctrl_msg_n; size_t wctrl_msg; size_t i; int first_line_x = 1; const int first_line_y = 2; curs_set(0); getmaxyx(stdscr, sh, sw); ctrl_msg_n = MAX(measure_sub_lines(ctrl_msg, &wctrl_msg), 1U); max_h = sh - 2 - ctrl_msg_n + !cfg.display_statusline; h = max_h; /* The outermost condition is for VLA below (to calm static analyzers). */ w = MAX(2 + 2*margin, MIN(sw - 2, MAX(MAX(recommended_width, sw/3), (int)MAX(wctrl_msg, determine_width(msg)) + 4))); wresize(error_win, h, w); werase(error_win); len = strlen(msg); if(len <= w - 2 && strchr(msg, '\n') == NULL) { first_line_x = (w - len)/2; h = 5 + ctrl_msg_n; wresize(error_win, h, w); mvwin(error_win, (sh - h)/2, (sw - w)/2); checked_wmove(error_win, first_line_y, first_line_x); wprint(error_win, msg); } else { int i = 0; int cy = first_line_y; while(i < len) { int j; char buf[w - 2 - 2*margin + 1]; int cx; copy_str(buf, sizeof(buf), msg + i); for(j = 0; buf[j] != '\0'; j++) if(buf[j] == '\n') break; if(buf[j] != '\0') i++; buf[j] = '\0'; i += j; if(buf[0] == '\0') continue; if(cy >= max_h - (int)ctrl_msg_n - 3) { /* Skip trailing part of the message if it's too long, just print how * many lines we're omitting. */ size_t max_len; const int more_lines = 1U + measure_sub_lines(msg + i, &max_len); snprintf(buf, sizeof(buf), "<<%d more line%s not shown>>", more_lines, (more_lines == 1) ? "" : "s"); /* Make sure this is the last iteration of the loop. */ i = len; } h = 1 + cy + 1 + ctrl_msg_n + 1; wresize(error_win, h, w); mvwin(error_win, (sh - h)/2, (sw - w)/2); cx = centered ? (w - utf8_strsw(buf))/2 : (1 + margin); if(cy == first_line_y) { first_line_x = cx; } checked_wmove(error_win, cy++, cx); wprint(error_win, buf); } } box(error_win, 0, 0); if(title[0] != '\0') { mvwprintw(error_win, 0, (w - strlen(title) - 2)/2, " %s ", title); } /* Print control message line by line. */ for(i = ctrl_msg_n; i > 0U; --i) { const size_t len = strcspn(ctrl_msg, "\n"); mvwaddnstr(error_win, h - i - 1, MAX(0, (w - (int)len)/2), ctrl_msg, len); ctrl_msg = skip_char(ctrl_msg + len + 1U, '/'); } checked_wmove(error_win, first_line_y, first_line_x); }
int main(int argc, char* argv[]) { FILE* pfi; FILE* pfo; FILE* pfw; char buf[BUF_SIZE]; struct VEC *v; int i, j, k; bool cnv, dec; char* s; char* p; char* q; char c; strcpy(input_file, default_input_file); strcpy(output_file, default_output_file); strcpy(unused_vec[0], default_unused_int); strcpy(unused_vec[1], default_unused_int); strcpy(fvector,default_fvector); for(i = 1 ; i < argc ; i++){ s = argv[i]; if(*s++ == '-'){ c = *s++; skip_space(&s); switch(c){ case 'I': // インクルードファイルの設定 case 'i': if(num_include < MAX_INCLUDE){ strcpy(include_file[num_include], s); num_include++; } break; case 'F': // フィックスベクタの定義 case 'f': strcpy(fvector, s); break; case 'O': // アウトプットファイルの設定 case 'o': strcpy(output_file, s); break; case 'R': // インプットファイルの設定 case 'r': strcpy(input_file, s); break; case 'M': // ボードモード case 'm': board_mode = atoi(s); break; default: printf("m16cvec -R<input_file> -O<output_file> -I<include_file> -M<mode> -F<fixvector>\n"); break; } } } printf("version = %s\n", version); printf("input file = %s\n", input_file); printf("output file = %s\n", output_file); printf("Fix Vector = %s\n", fvector); printf("board mode = %d : ", board_mode); if(board_mode == 1) printf("M30262F8FG(OAKS16 MINI)\n"); else printf("M30620FCAFP(OAKS16)\n"); if((pfi = fopen(input_file, "r")) == NULL){ fprintf(stderr, "can't open input file !"); exit(1); } if((pfo = fopen(output_file, "w")) == NULL){ fclose(pfi); fprintf(stderr, "can't open output file !"); exit(1); } for(i = 0 ; i < num_include ; i++){ printf("include file = %s\n", include_file[i]); } for(;;){ if((fgets(buf, BUF_SIZE, pfi)) == NULL) break; s = buf; switch(vec_state){ case INT_STATE: case EXC_STATE: if(test_string(&s, ";")) vec_state = NORMAL_STATE; else if(test_string(&s, "{")){ v = &vec_table[vec_state][num_vec[vec_state]]; p = &v->no_name[0]; if(skip_space(&s)) continue; dec = true; while(*s != ','){ if(*s == 0) continue; if(*s == '(' || *s == ')' || *s <= ' '){ s++; continue; } if(*s < '0' || *s > '9') dec = false; *p++ = *s++; } s++; *p++ = 0; if(dec) v->no = atoi(v->no_name); else{ v->no = -1; num_nodec++; } p = &v->no_label[0]; if(skip_char(&s, ',')) continue; test_string(&s, "INT_ENTRY("); test_string(&s, "EXC_ENTRY("); test_string(&s, "CFG_INT_ENTRY("); test_string(&s, "CFG_EXC_ENTRY("); test_string(&s, "(FP)"); if(skip_space(&s)) continue; while(*s != ')' && *s != ',' && *s > ' '){ *p++ = *s++; } *p++ = 0; num_vec[vec_state]++; } else continue; break; default: if(!test_string(&s, "const")) continue; if(test_string(&s, "INHINIB")) vec_state = INT_STATE; else if(test_string(&s, "EXCINIB")) vec_state = EXC_STATE; break; } } do{ cnv = false; for(i = 0 ; i < num_include ; i++){ if((pfw = fopen(include_file[i], "r")) != NULL){ for(;;){ if((fgets(buf, BUF_SIZE, pfw)) == NULL) break; s = buf; if(!test_string(&s, "#define")) continue; if(skip_space(&s)) continue; for(k = 0 ; k < 2 ; k++){ for(j = 0 ; j < num_vec[k] ; j++){ v = &vec_table[k][j]; if(v->no < 0){ p = s; dec = true; if(test_string(&p, v->no_name)){ q = &v->no_name[0]; if(!skip_space(&p)){ while(*p > ' '){ if(*p == '(' || *p == ')'){ p++; continue; } if(*p < '0' || *p > '9') dec = false; *q++ = *p++; } } *q++ = 0; } else dec = false; if(dec){ v->no = atoi(v->no_name); num_nodec--; } } } } } fclose(pfw); } else printf("open error %s !!\n", include_file[i]); } }while(cnv); for(i = 0 ; i < num_vec[EXC_STATE] ; i++){ v = &vec_table[EXC_STATE][i]; if(v->no >= 32 && v->no < MAX_INT){ vec_table[INT_STATE][num_vec[INT_STATE]] = vec_table[EXC_STATE][i]; num_vec[INT_STATE]++; for(j = i ; j < (num_vec[EXC_STATE]-1) ; j++) vec_table[EXC_STATE][j] = vec_table[EXC_STATE][j+1]; num_vec[EXC_STATE]--; } } for(j = 0 ; j < num_vec[INT_STATE] ; j++){ v = &vec_table[INT_STATE][j]; if(v->no >= MAX_INT) strcpy(unused_vec[INT_STATE], v->no_label); else if(max_int < v->no) max_int = v->no; printf("int %d:%d,%s,%s\n", j, v->no, v->no_name, v->no_label); } for(j = 0 ; j < num_vec[EXC_STATE] ; j++){ v = &vec_table[EXC_STATE][j]; if((v->no >= MAX_EXC && v->no < 32) || v->no >= MAX_INT) strcpy(unused_vec[EXC_STATE], v->no_label); printf("exc %d:%d,%s,%s\n", j, v->no, v->no_name, v->no_label); } if(num_nodec > 0) printf("%dのエクセプション番号を特定できません!\n", num_nodec); else{ fputs(lf, pfo); for(i = 0 ; i < 2 ; i++){ for(j = 0 ; j < num_vec[i] ; j++) set_global(pfo, vec_table[i][j].no_label); } if(!strcmp(default_unused_int, unused_vec[0])) set_global(pfo, unused_vec[0]); else if(!strcmp(default_unused_int, unused_vec[1])) set_global(pfo, unused_vec[1]); switch(board_mode){ case 1: // M30262F8FG(OAKS16 MINI) set_M30262F8FG(pfo, board_mode, max_int); break; default: // M30620FCAFP(OAKS16) set_M30620FCAFP(pfo, board_mode, max_int); break; } set_comment(pfo, exc_vec); fputs("\t.section\tfvector", pfo); fputs(lf, pfo); set_org(pfo, fvector); for(i = 0 ; i < (MAX_EXC-1) ; i++) set_vector(pfo, EXC_STATE, i); fputs("\t.lword\t_hardware_start\t\t\t; RESET", pfo); fputs(lf, pfo); } fclose(pfi); fclose(pfo); return 0; }
i8_t skip_space(i8_p * p_pdata, i32_t len) { return skip_char(p_pdata, len, ' '); }
//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- bool clause_set_parse(struct clause_set* set, FILE* fp) { int res; long long value, num_vars, num_clauses; static long long size_max = (size_t) -1; // Skip all comment lines. while (skip_char(fp, 'c')) { int c; while ((c = getc(fp)) != '\n') ; } // Read the pre conditions. if (!skip_char(fp, 'p')) { dpll_puterr("clause_set_parse(): expected 'p' instruction"); return false; } skip_whitespace(fp); if (!match_token(fp, "cnf")) { dpll_puterr("clause_set_parse(): expected 'cnf' token"); return false; } skip_whitespace(fp); if (!read_long_long(fp, &num_vars)) { dpll_puterr("clause_set_parse(): expected num vars"); return false; } skip_whitespace(fp); if (!read_long_long(fp, &num_clauses)) num_clauses = 0; if (num_vars < 0 || num_vars > size_max) { errno = EINVAL; dpll_puterr("clause_set_parse(): literal count too big or not positive"); return false; } set->num_vars = (size_t) num_vars; skip_whitespace(fp); if (!skip_char(fp, '\n')) { // expected newline after number of literals. errno = EINVAL; dpll_puterr("clause_set_parse(): expected newline after 'p' instruction, got %c", getc(fp)); return false; } // Read a clause from each line. Since empty clauses don't make // much sense in a CNF (as they evaluate to false), we simply // skip empty lines. while (!feof(fp)) { size_t index = 0; struct clause* clause = NULL; skip_whitespace(fp); if (skip_char(fp, '\n') || skip_char(fp, EOF)) continue; if (!clause_set_add(set, &index)) { errno = ENOMEM; dpll_puterr("clause_set_parse(): could not add new clause"); return false; } clause = &set->array[index]; while (true) { skip_whitespace(fp); if (skip_char(fp, '0')) { skip_whitespace(fp); if (!skip_char(fp, '\n')) { dpll_puterr("clause_set_parse(): clause #"PRsize_t": expected newline after clause terminator", index); return false; } break; } if (!read_long_long(fp, &value)) { errno = EINVAL; dpll_puterr("clause_set_parse(): clause #"PRsize_t": expected literal ", index); return false; } if (value < -num_vars || value > num_vars || value == 0) { errno = EINVAL; dpll_puterr( "clause_set_parse(): clause #"PRsize_t": literal " "'%llu' not in range", index, value); return false; } res = clause_add(clause, value); if (res == -1) { errno = ENOMEM; dpll_puterr("clause_set_parse(): could not add literal to clause"); return false; } else if (res == 1) { dpll_puterr( "clause_set_parse(): warning: clause #"PRsize_t": " "contains duplicate literal '%llu'", index, value); } else assert(res == 0); clause_shrink_to_fit(clause); } } return true; }
i8_t skip_tab(i8_p * p_pdata, i32_t len) { return skip_char(p_pdata, len, '\t'); }
Variant JSONReader::parse_string() { if (!skip_char('"')) throw Exception(position(), "expected start of string"); std::string string; while (!skip_char('\"')) { if (at_newline()) throw Exception(position(), "missing string termination"); if (skip_char('\\')) { if (skip_char('b')) string += '\b'; else if (skip_char('t')) string += '\t'; else if (skip_char('n')) string += '\n'; else if (skip_char('f')) string += '\f'; else if (skip_char('r')) string += '\r'; else if (skip_char('\"')) string += '\"'; else if (skip_char('/')) string += '/'; else if (skip_char('\\')) string += '\\'; else if (skip_char('u')) throw Exception(position(), "escaped code points currently not supported"); else throw Exception(position(), "invalid escape char"); } else string += next_get(); } return Variant(std::move(string)); }
int main(int argc, char* argv[]){ FILE* pfo; FILE* pfi; int nCnt, address; int shift_bit = 1; int shift_cnt = 0; char* s; char str[100]; char buf[BUF_SIZE]; char c; strcpy(input_file, default_input_file); strcpy(output_file, default_output_file); //コマンドライン入力を解析 for(nCnt = 1 ; nCnt < argc ; nCnt++){ s = argv[nCnt]; if(*s++ == '-'){ c = *s; while(*s != 0){ if(*s == ':') break; s++; } if(*s++ == ':'){ switch(c){ //output file name case 'O': case 'o': strcpy(output_file, s); break; //input file name case 'R': case 'r': strcpy(input_file, s); break; default: printf("tlcs900offset -R:input_file -O:output_file\n"); break; } } } } //input, outputファイル名の標準出力 printf("input file = %s\n", input_file); printf("output file = %s\n", output_file); if((pfi = fopen(input_file, "r")) == NULL){ fprintf(stderr, "can't open input file!"); exit(1); } if((pfo = fopen(output_file, "w")) == NULL){ fclose(pfi); fprintf(stderr, "can't open output file!"); exit(1); } //inputファイルの取り込み for(;;){ if((fgets(buf, BUF_SIZE, pfi)) == NULL) break; s = buf; switch(offset_state){ //; BEGIN TCB_texptnの範囲の解析 case TEXPTN_STATE: if(test_string(&s, "; END")){ offset_state = NORMAL_STATE; continue; } if(test_string(&s, "line") || test_string(&s, "call")) continue; if(skip_char(&s, 'x')){ continue; } get_num(s); address = atoh(s); printf("TCB_texptn\tequ\t%d\n",address); fputs("TCB_texptn\t\tequ\t", pfo); sprintf(str,"%d",address); fputs(str, pfo); fputs(lf, pfo); break; case SP_STATE: //; BEGIN TCB_spの範囲の解析 if(test_string(&s, "; END")){ offset_state = NORMAL_STATE; continue; } if(test_string(&s, "line") || test_string(&s, "call") ) continue; if(skip_space(&s)) continue; if(skip_char(&s, '(')){ continue; } if(skip_char(&s, 'x')){ continue; } get_num(s); address = atoh(s); printf("TCB_sp\t\tequ\t%d\n",address); fputs("TCB_sp\t\t\tequ\t", pfo); sprintf(str,"%d",address); fputs(str, pfo); fputs(lf, pfo); break; case PC_STATE: //; BEGIN TCB_pcの範囲の解析 if(test_string(&s, "; END")){ offset_state = NORMAL_STATE; continue; } if(test_string(&s, "line") || test_string(&s, "call")) continue; if(skip_char(&s, 'x')){ continue; } get_num(s); address = atoh(s); printf("TCB_pc\tequ\t%d\n",address); fputs("TCB_pc\t\t\tequ\t", pfo); sprintf(str,"%d",address); fputs(str, pfo); fputs(lf, pfo); break; case ENATEX_STATE: //; BEGIN TCB_pcの範囲の解析 if(test_string(&s, "; END")){ offset_state = NORMAL_STATE; continue; } if(test_string(&s, "line") || test_string(&s, "call")) continue; if(skip_char(&s, 'x')){ continue; } get_num(s); address = atoh(s); printf("TCB_enatex\tequ\t%d\n",address); fputs("TCB_enatex\t\tequ\t", pfo); sprintf(str,"%d",address); fputs(str, pfo); fputs(lf, pfo); get_num_undo(s); if(skip_char(&s, ',')){ continue; } *(s + BIT_SIZE) = '\0'; printf("TCB_enatex_mask\tequ\t%s\n",s); fputs("TCB_enatex_mask\tequ\t", pfo); fputs(s, pfo); fputs(lf, pfo); if(skip_char(&s, 'x')){ continue; } address = atoh(s); for(shift_cnt=0;shift_cnt<=16;shift_cnt++){ if(address == shift_bit){ printf("TCB_enatex_bit\tequ\t%d\n",shift_cnt); fputs("TCB_enatex_bit\tequ\t", pfo); sprintf(str,"%d",shift_cnt); fputs(str, pfo); fputs(lf, pfo); } shift_bit<<=1; } break; default: if(test_string(&s, "; BEGIN TCB_texptn")){ offset_state = TEXPTN_STATE; continue; }else if(test_string(&s, "; BEGIN TCB_sp")){ offset_state = SP_STATE; continue; }else if(test_string(&s, "; BEGIN TCB_pc")){ offset_state = PC_STATE; continue; }else if(test_string(&s, "; BEGIN TCB_enatex")){ offset_state = ENATEX_STATE; continue; } break; } } return 0; }
/* * Starts the build process for generating our token queue. Will return a boolean, true signifying a successful * queue generation, false should we have some syntactical issues in our input stream */ bool Lexer::build_tokens() { char current_char; this->m_tokenQueue.push(Token::make_token(token_identifier::FIRST_TOKEN, this->get_cur_loc())); while (this->f_fileStream.good()) { current_char = get(); update_location(current_char); /* * Punctuation */ switch (current_char) { case ' ': continue; case '\t': continue; case '\n': emit(token_identifier::NEW_LINE); break; case '+': { emit(token_identifier::ADDITION_OP); break; } case '-': { emit(token_identifier::SUBTRACTION_OP); break; } case ';': { emit(token_identifier::SEMICOLON); break; } case '(': { emit(token_identifier::LEFT_PAREN); break; } case ')': { emit(token_identifier::RIGHT_PARAN); break; } case '=': { if (peek() == '=') { skip_char(); emit(token_identifier::EQUALITY_OP); } else { emit(token_identifier::ASSIGNMENT_OP); } break; } case '&': { emit(token_identifier::AND_OP);break; } case '|': { if (peek() == '|') { emit(token_identifier::OR_OP); skip_char(); } else { emit(token_identifier::INVALID_TOKEN); skip_to('\n'); // missing matching bar } break; } case '<': { if (peek() == '=') { emit(token_identifier::LESS_THAN_EQ); skip_char(); } else { emit(token_identifier::LESS_THAN); } break; } case '>': { if (peek() == '=') { emit(token_identifier::GREATER_THAN_EQ); skip_char(); } else { emit(token_identifier::GREATER_THAN); } break; } case '!': { if (peek() == '!') { emit(token_identifier::NOT_EQ); skip_char(); } else { emit(token_identifier::INVALID_TOKEN); skip_to('\n'); } break; } } /* * Keywords and identifiers */ if (isalpha(current_char) || current_char == '_') { std::string string_val; string_val.reserve(16); string_val += current_char; current_char = peek(); while ((isalpha(current_char) || current_char == '_') && this->f_fileStream.good()) { string_val += current_char; skip_char(); current_char = peek(); } auto type = Keywords::get_token_identifier(string_val); if (type == IDENTIFIER) { this->m_tokenQueue.push(Token::create_identifier(this->get_cur_loc(), string_val)); } else { emit(type); } } /* * Integers */ if (isdigit(current_char)) { std::string num_val; num_val.reserve(12); num_val += current_char; current_char = peek(); while(isdigit(current_char) && this->f_fileStream.good()) { num_val += current_char; skip_char(); current_char = peek(); } this->m_tokenQueue.push(Token::create_integer(this->get_cur_loc(), num_val)); } /* * String literals */ if (current_char == '\"') { std::string string_val; string_val.reserve(16); current_char = peek(); while (current_char != '\n' && current_char != '"' && this->f_fileStream.good()) { string_val += current_char; skip_char(); current_char = peek(); } if (current_char == '\n') { throw ERROR_NON_TERMINATED_STRING; } if (current_char == '"') { skip_char(); } this->m_tokenQueue.push(Token::create_string(this->get_cur_loc(), string_val)); } } this->m_tokenQueue.push(Token::make_token(token_identifier::END_OF_FILE, this->get_cur_loc())); this->m_tokenQueue.push(Token::make_token(token_identifier::LAST_TOKEN, this->get_cur_loc())); return true; }
/* Draws possibly centered formatted message with specified title and control * message on error_win. */ static void draw_msg(const char title[], const char msg[], const char ctrl_msg[], int centered, int recommended_width) { enum { margin = 1 }; int sw, sh; int w, h; int len; size_t ctrl_msg_n; size_t wctrl_msg; size_t i; curs_set(FALSE); getmaxyx(stdscr, sh, sw); ctrl_msg_n = MAX(measure_sub_lines(ctrl_msg, &wctrl_msg), 1U); h = sh - 2 - ctrl_msg_n + !cfg.display_statusline; /* The outermost condition is for VLA below (to calm static analyzers). */ w = MAX(2 + 2*margin, MIN(sw - 2, MAX(MAX(recommended_width, sw/3), (int)MAX(wctrl_msg, determine_width(msg)) + 4))); wresize(error_win, h, w); werase(error_win); len = strlen(msg); if(len <= w - 2 && strchr(msg, '\n') == NULL) { h = 5 + ctrl_msg_n; wresize(error_win, h, w); mvwin(error_win, (sh - h)/2, (sw - w)/2); checked_wmove(error_win, 2, (w - len)/2); wprint(error_win, msg); } else { int i; int cy = 2; i = 0; while(i < len) { int j; char buf[w - 2 - 2*margin + 1]; int cx; copy_str(buf, sizeof(buf), msg + i); for(j = 0; buf[j] != '\0'; j++) if(buf[j] == '\n') break; if(buf[j] != '\0') i++; buf[j] = '\0'; i += j; if(buf[0] == '\0') continue; h = cy + 4; wresize(error_win, h, w); mvwin(error_win, (sh - h)/2, (sw - w)/2); cx = centered ? (w - utf8_strsw(buf))/2 : (1 + margin); checked_wmove(error_win, cy++, cx); wprint(error_win, buf); } } box(error_win, 0, 0); if(title[0] != '\0') { mvwprintw(error_win, 0, (w - strlen(title) - 2)/2, " %s ", title); } /* Print control message line by line. */ for(i = ctrl_msg_n; i > 0U; --i) { const size_t len = strcspn(ctrl_msg, "\n"); mvwaddnstr(error_win, h - i - 1, MAX(0, (w - (int)len)/2), ctrl_msg, len); ctrl_msg = skip_char(ctrl_msg + len + 1U, '/'); } }