bool binspector_parser_t::is_atom_field(atom_base_type_t& atom_base_type, adobe::array_t& bit_count_expression, adobe::array_t& is_big_endian_expression) { static const adobe::array_t is_little_endian_expression_k(1, adobe::any_regular_t(false)); static const adobe::array_t is_big_endian_expression_k(1, adobe::any_regular_t(true)); if (is_keyword(key_signed)) atom_base_type = atom_signed_k; else if (is_keyword(key_unsigned)) atom_base_type = atom_unsigned_k; else if (is_keyword(key_float)) atom_base_type = atom_float_k; else return false; require_expression(bit_count_expression); if (is_keyword(key_little)) is_big_endian_expression = is_little_endian_expression_k; else if (is_keyword(key_big)) is_big_endian_expression = is_big_endian_expression_k; else require_expression(is_big_endian_expression); return true; }
//------------------------------------------------------------------ void code_colorer::color_code(const char_type* p) { while(*p) { unsigned len; if(is_block_comment(p, &len)) { m_result.add(keyword_rem, p, len, true); p += len; } else if(is_line_comment(p, &len)) { m_result.add(keyword_rem, p, len, true); p += len; } else if(is_string_literal(p, &len)) { m_result.add(keyword_str, p, len, true); p += len; } else if(is_number(p, &len)) { m_result.add(keyword_num, p, len, true); p += len; } else if(is_operator(p, &len)) { m_result.add(keyword_op, p, len, true); p += len; } else if(is_identifier(p, &len)) { const keyword* kw = 0; if (is_keyword(keyword_kw1_suffix, p, len)) kw = &keyword_kw1; else if(is_keyword(keyword_kw2_suffix, p, len)) kw = &keyword_kw2; else if(is_keyword(keyword_kw3_suffix, p, len)) kw = &keyword_kw3; else if(is_keyword(keyword_kw4_suffix, p, len)) kw = &keyword_kw4; if(kw) { m_result.add(backslash); m_result.add(kw->name, kw->len); m_result.add(open_brace); } m_result.add(p, len, true); if(kw) { m_result.add(close_brace); } p += len; } else { m_result.add(*p++); } } }
bool expression_parser::is_boolean(any_regular_t& result) { if (is_keyword(true_k)) { result = any_regular_t(true); return true; } else if (is_keyword(false_k)) { result = any_regular_t(false); return true; } return false; }
bool binspector_parser_t::is_simple_assign_field(adobe::name_t keyword, adobe::name_t field_type) { if (!is_keyword(keyword)) return false; adobe::name_t name; require_identifier(name); require_token(adobe::assign_k); adobe::array_t expression; require_expression(expression); adobe::dictionary_t parameters; parameters[key_field_type].assign(field_type); parameters[key_field_name].assign(name); parameters[key_field_assign_expression].assign(expression); parameters[key_field_size_type].assign(field_size_none_k); // intentionally fixed parameters[key_field_size_expression].assign(adobe::array_t()); // intentionally fixed parameters[key_field_offset_expression].assign(adobe::array_t()); // intentionally fixed insert_parser_metadata(parameters); add_field_proc_m(name, parameters); return true; }
// inverse of mangle_id() std::string demangle_id(const std::string &id, bool restore_slashes) { std::string result; size_t len = id.length(); int start_pos = 0; for (size_t n = 0;n < len;++n) { char ch = id[n]; if (ch == '.' || ch == '/') { start_pos = n + 1; if (id[n+1] == '_') start_pos++; //remove leading underscore } if (ch == '.' && restore_slashes) { ch = '/'; } else if (ch == '_' && (n + 1 == len || id[n + 1] == '.' || id[n + 1] == '/')) { std::string sub_id = id.substr(start_pos, n - start_pos); if (is_keyword(sub_id)) { continue; } // exclude the final underscore } result += ch; } return result; }
bool binspector_parser_t::is_enum_map_default() { if (!is_keyword(key_default)) return false; require_token(adobe::colon_k); static std::size_t uid_s(0); static const adobe::array_t empty_array_k; std::string lambda_identifier; // REVISIT (fbrereto) : String concatenation here. lambda_identifier += std::string("<") + current_struct_m.c_str() + ":enumerate_default_" + boost::lexical_cast<std::string>(++uid_s) + ">"; adobe::name_t identifier(lambda_identifier.c_str()); adobe::dictionary_t parameters; parameters[key_field_name].assign(identifier); parameters[key_field_size_expression].assign(empty_array_k); parameters[key_field_offset_expression].assign(empty_array_k); parameters[key_field_type].assign(value_field_type_enumerated_default); parameters[key_named_type_name].assign(identifier); insert_parser_metadata(parameters); add_field_proc_m(identifier, parameters); require_scope_content(identifier); return true; }
/** * Handles parsing the source code to create an identifier * * @return the token representing the identifier */ Token Scanner::handle_identifier() { Token t; std::string s = ""; // assign position t.line_position = line_number; t.col_position = col_number; while (position < source_length && (is_letter(source[position]) || is_digit(source[position]))) { s += source[position]; position++; col_number++; } // check if s is a keyword if (is_keyword(s)) { return handle_keyword(t, s); } // return identifier - try putting this above the is_keyword thing // and pass a reference into the keyword so it'll change it if needed, // then just return t out here t.kind = IDENTIFIER; t.identifier = s; return t; }
void Scanner::scan_id(char token) { std::stringstream id; // Make sure the char isn't whitespace if (!std::isspace(static_cast<unsigned char>(token))) { // Check if the first char is a digit or other invalid input if (std::isdigit(token) || !std::isalnum(static_cast<unsigned char>(token)) ) { std::cout << "Found an error\n"; error = line; } id << token; while (std::isalnum(static_cast<unsigned char>(file.peek()))) { char next = file.get(); id << next; } if (error != -1) { add_token("", Token::ERROR); } else { std::string id_str = id.str(); if(!is_keyword(id_str)) { add_token(id_str, Token::ID); } } } }
bool binspector_parser_t::is_skip() { if (!is_keyword(key_skip)) return false; adobe::name_t name; require_identifier(name); require_token(adobe::open_bracket_k); adobe::array_t expression; require_expression(expression); require_token(adobe::close_bracket_k); adobe::dictionary_t parameters; parameters[key_field_type].assign(value_field_type_skip); parameters[key_field_name].assign(name); parameters[key_skip_expression].assign(expression); insert_parser_metadata(parameters); add_field_proc_m(name, parameters); return true; }
// assert_decl = "assert" "(" identifier "," expression ")". bool adam_test_parser::is_assert_decl() { if (is_keyword(assert_k)) { require_token(open_parenthesis_k); name_t name; if (!is_identifier(name)) throw_exception("sheet name expected"); queryable_sheet_t& qs(sheet_from_name(name)); require_token(comma_k); array_t expression; if (!is_expression(expression)) throw_exception("expression expected"); require_token(close_parenthesis_k); any_regular_t result = qs.inspect(expression); bool success = result.cast<bool>(); out_m << "\n### assert " << (success ? std::string("succeeded: ") : std::string("failed: ")) #if defined(ADOBE_STD_SERIALIZATION) << begin_asl_cel << expression << end_asl_cel #endif << " in sheet " << name << std::endl; if (!success) all_checks_passed_m = false; return true; } return false; }
// sheet . bool adam_test_parser::is_sheet() { if (!is_keyword(name_t("sheet"))) return false; putback(); sheets_m.push_back(new queryable_sheet_t(*this)); return true; }
pointer ff_is_keyword(pointer p) { assert(cdr(p) == NIL); if(is_keyword(car(p))) { return BOOLEAN_TRUE; } return BOOLEAN_FALSE; }
static token_type scan_id_keyword() { char c; int k; token_type the_type; c = getchar(); if (isalpha(c)) { while (isalnum(c) && token_length < MAX_TOKEN_LENGTH) { current_token[token_length] = c; token_length++; c = getchar(); } current_token[token_length] = '\0'; ungetc(c, stdin); k = is_keyword(current_token); if (k == 0) { the_type = ID; } else { the_type = (token_type) k; } return the_type; } else { return ERROR; } }
/* * pgq_quote_ident - Quote an identifier only if needed */ static int pgq_quote_ident(char *dst, const uint8 *src, int srclen) { /* * Can avoid quoting if ident starts with a lowercase letter or * underscore and contains only lowercase letters, digits, and * underscores, *and* is not any SQL keyword. Otherwise, supply * quotes. */ int nquotes = 0; bool safe; const char *ptr; char *optr; char ident[NAMEDATALEN + 1]; /* expect idents be not bigger than NAMEDATALEN */ if (srclen > NAMEDATALEN) srclen = NAMEDATALEN; memcpy(ident, src, srclen); ident[srclen] = 0; /* * would like to use <ctype.h> macros here, but they might yield * unwanted locale-specific results... */ safe = ((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_'); for (ptr = ident; *ptr; ptr++) { char ch = *ptr; if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || (ch == '_')) continue; /* okay */ safe = false; if (ch == '"') nquotes++; } if (safe) { if (is_keyword(ident)) safe = false; } optr = dst; if (!safe) *optr++ = '"'; for (ptr = ident; *ptr; ptr++) { char ch = *ptr; if (ch == '"') *optr++ = '"'; *optr++ = ch; } if (!safe) *optr++ = '"'; return optr - dst; }
main(int argc, char **argv) { FILE *fp; int i; progname = argv[0]; for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++) if (!strcmp(argv[i], "-noquote")) showquotes = 0; else errormsg(Error, "%s: unknown option %s\n", progname, argv[i]); nwindex = new_recognizer(ALPHANUM, SYMBOLS); if (i == argc) { #line 75 "finduses.nw" { FILE *tmp = tmpfile(); char *line; if (tmp == NULL) #line 155 "finduses.nw" errormsg(Fatal, "%s: couldn't open temporary file\n", progname); #line 78 "finduses.nw" while ((line = getline_noweb(stdin)) != NULL) { if (fputs(line, tmp) == EOF) #line 157 "finduses.nw" errormsg(Fatal, "%s: error writing temporary file\n", progname); #line 80 "finduses.nw" if (is_index(line, "defn")) { if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0; add_ident(nwindex, line+1+5+1+4+1); } else if (is_index(line, "localdefn")) { if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0; add_ident(nwindex, line+1+5+1+9+1); } else if (is_keyword(line, "fatal")) { exit(1); } } rewind(tmp); stop_adding(nwindex); add_use_markers(tmp, stdout); } #line 46 "finduses.nw" } else { #line 56 "finduses.nw" for (; i < argc; i++) if ((fp=fopen(argv[i],"r"))==NULL) errormsg(Error, "%s: couldn't open file %s\n", progname, argv[i]); else { read_ids(fp); fclose(fp); } #line 48 "finduses.nw" stop_adding(nwindex); add_use_markers(stdin, stdout); } nowebexit(NULL); return errorlevel; /* slay warning */ }
void Lex_analyzer::addToken(int tokenType, QString tokenValue){ TokenType *t = new TokenType; Token *tok = new Token; switch(tokenType){ case 1: if(is_keyword(tokenValue,t)){ qDebug() <<"Token" <<tokenValue <<"found in keywords: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "keywords"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); }else if(is_type(tokenValue,t)){ qDebug() <<"Token" <<tokenValue <<"found in types: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "types"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); }else{ is_id(tokenValue, t); qDebug() <<"Token" <<tokenValue <<"found in/inserted to ids: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "ids"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); } break; case 2: is_const(tokenValue, t); qDebug() <<"Token" <<tokenValue <<"found in/inserted to constants: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "constants"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); break; case 3: case 4: if(is_operator(tokenValue, t)){ qDebug() <<"Token" <<tokenValue <<"found in operators/separators: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "separators"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); }else{ qDebug() <<"Error unknown token type: " <<tokenType <<endl; } break; default: qDebug() <<"Error unknown token type: " <<tokenType <<endl; } qDebug() <<"Adding token of type[" <<tokenType <<"] = " <<tokenValue <<endl; }
static void add_use_markers(FILE *in, FILE *out) { char *line; int incode = 0; LineOut info; info.line = (char *)0; info.out = out; while ((line = getline_noweb(in)) != NULL) { if (is_begin(line, "code") || showquotes && is_keyword(line, "quote")) incode = 1; else if (is_end(line, "code") || is_keyword(line, "endquote")) incode = 0; if (is_keyword(line, "text") && incode) { info.line = line + 6; /* skip "@text " */ search_for_ident(nwindex, line, write_index_use, &info); if (*info.line && *info.line != '\n') fprintf(out, "@text %s", info.line); /* has newline */ } else fprintf(out, "%s", line); } }
int main(){ /* Driver: Test All Cases Test Correctly */ int i = 0; for(i = 0; i < KEYWORD_TOTAL; i++){ /* These should all print '1' for Answer!*/ printf("Is %s a keyword? Answer: %d\n", keywords[i], is_keyword(keywords[i])); } return 0; }
bool binspector_parser_t::is_pp_include() { if (!is_keyword(key_include)) return false; adobe::any_regular_t value; require_token(adobe::string_k, value); std::string value_str(value.cast<std::string>()); // std::cerr << "Include file " << value_str << '\n'; // do the parse; so far we don't support include directories; at this point // it'll get complicated when it does. // // we also need to track which files we include so we do not include them twice. boost::filesystem::path parsepath(include_directory_set_m[0] / value_str.c_str()); // REVISIT (fbrereto) : A std::string to a c-string to a std::string to a... c'mon. if (!exists(parsepath)) throw_exception(adobe::make_string("Could not find requested include file: ", value_str.c_str()).c_str()); // check if file has already been parsed and added to the AST. if (adobe::find(included_file_set_m, parsepath) != included_file_set_m.end()) return true; included_file_set_m.push_back(parsepath); boost::filesystem::ifstream include_stream(parsepath); adobe::line_position_t::getline_proc_t getline(new adobe::line_position_t::getline_proc_impl_t(std::bind(&get_input_line, std::ref(include_stream), std::placeholders::_2))); adobe::line_position_t position(adobe::name_t(parsepath.string().c_str()), getline); try { binspector_parser_t(include_stream, position, include_directory_set_m, set_structure_proc_m, add_field_proc_m, add_unnamed_field_proc_m, add_typedef_proc_m, included_file_set_m).parse(); } catch (const adobe::stream_error_t& error) { throw std::runtime_error(adobe::format_stream_error(include_stream, error)); } return true; }
bool adam_parser::is_conditional(line_position_t& position, array_t& expression) { if (!is_keyword(when_k)) return false; require_token(open_parenthesis_k); position = next_position(); require_expression(expression); require_token(close_parenthesis_k); return true; }
bool step() { if(get_tval()=="datatype") { try_lex(); do_read_datatype(); lex_save(); try_lex(); } else if(get_tval()=="match") { try_lex(); do_read_match(); lex_save(); } else if(types.count(get_tval())) { do_read_type(); } else if(constructors.count(get_tval())) { do_read_ctor(); } else if(d<=d_limit && (get_tval()=="namespace" || get_tval()=="class" || get_tval()=="struct" || get_tval()=="=")) { maybe_acceptablescope=true; d_limit++; fprintf(OUTF,"%s",get_tval().c_str()); return lex(); } else if(maybe_function && is_identifier(get_tid()) && d<=d_limit && last_token==")") { /* assume matching function */ fprintf(OUTF,";\n"); lex_rewind(); // go back to last } or ; lex(); while(!is_identifier(get_tid()) && !is_keyword(get_tid())) lex(); maybe_function=false; //prevent nasty recursion do_read_matchfun(); last_token=""; maybe_function=true; } else { if(get_tval()=="{") { maybe_acceptablescope=false; ++d; } else if(get_tval()=="}") { lex_save(); --d; if(d<d_limit) --d_limit; // leave a "good" scope (namespace, class, struct) } else if(get_tval()==";") { lex_save(); if(maybe_acceptablescope) { maybe_acceptablescope=false; --d_limit; //class or struct was just a declaration } } fprintf(OUTF,"%s",get_tval().c_str()); if(!is_space(get_tid())) last_token=get_tval(); return lex(); } return true; }
bool binspector_parser_t::is_field_size(field_size_t& field_size_type, adobe::array_t& field_size_expression, bool& shuffleable) { if (!is_token(adobe::open_bracket_k)) return false; if (is_keyword(key_while)) { field_size_type = field_size_while_k; require_token(adobe::colon_k); } else if (is_keyword(key_terminator)) { field_size_type = field_size_terminator_k; require_token(adobe::colon_k); } else if (is_keyword(key_delimiter)) { field_size_type = field_size_delimiter_k; require_token(adobe::colon_k); } else { field_size_type = field_size_integer_k; } require_expression(field_size_expression); require_token(adobe::close_bracket_k); shuffleable = is_keyword(key_shuffle); return true; }
bool binspector_parser_t::is_constant() { bool is_const(is_keyword(key_const)); bool is_invis(!is_const && is_keyword(key_invis)); if (!is_const && !is_invis) return false; adobe::name_t name; require_identifier(name); require_token(adobe::assign_k); adobe::array_t expression; require_expression(expression); // REVISIT (fbrereto) : // I should generalize these into "decorators", add them to the grammar and // reduce is_constant into a forwarding of is_simple_assign_field bool noprint(false); if (is_keyword(key_noprint) || is_invis) noprint = true; adobe::dictionary_t parameters; parameters[key_field_type].assign(value_field_type_const); parameters[key_field_name].assign(name); parameters[key_const_expression].assign(expression); parameters[key_const_no_print].assign(noprint); insert_parser_metadata(parameters); add_field_proc_m(name, parameters); return true; }
/************************************************** * * FUNCTION: * is_in_table - "Is In Table" * * DESCRIPTION: * Checks whether a string is in the * symbol table * * RETURNS: * Returns TRUE if the string is in the * symbol table and FALSE if it isn't. * **************************************************/ token_class_t8 is_in_table ( char *str /* string to check */ ) { token_class_t8 tok = is_keyword( str ); if( TOK_NO_CLASS == tok ) { return( is_identifier( str ) ); } return( tok ); } /* is_in_table() */
// sheet_specifier = [lead_comment] "sheet" identifier "{" { qualified_cell_decl } "}" [trail_comment]. bool adam_parser::is_sheet_specifier(name_t& name) { /* REVISIT (sparent) : Top level block is ignored. */ is_token(lead_comment_k); if (!is_keyword(sheet_k)) return false; if(!is_identifier(name)) throw_exception("sheet name identifier expected"); require_token(open_brace_k); while (is_qualified_cell_decl()) { } require_token(close_brace_k); is_token(trail_comment_k); return true; }
bool adam_parser::is_set_decl(name_t token, set_decl_t set_decl) { if (!is_keyword(token)) return false; require_token(colon_k); while (true) { std::string detailed; (void)is_lead_comment(detailed); if (!(this->*set_decl)(detailed)) break; } return true; }
// transform an id so that it has no special characters ("/" "[" or "]") std::string mangle_id(const std::string &id) { std::string result; int last_start_pos = 0; for (size_t n = 0;n < id.length();++n) { char ch = id[n]; if (ch == '/') { ch = '.'; } if (ch == '.') { std::string sub_id = id.substr(last_start_pos, n - last_start_pos); if (is_keyword(sub_id)) { result += '_'; } last_start_pos = n + 1; } result += ch; if (ch == '.' && std::isdigit(id[n+1])) result += '_'; /* if (ch == '/') { result += "_S"; } else if (ch == '[') { result += "_L"; } else if (ch == ']') { result += "_R"; } else if (ch == '_') { result += "__"; } else { result += ch; } */ } std::string sub_id = id.substr(last_start_pos, std::string::npos); if (is_keyword(sub_id)) { result += '_'; } return result; }
// dump_decl = "dump" "(" identifier ")" bool adam_test_parser::is_dump_decl() { if (is_keyword(dump_k)) { require_token(open_parenthesis_k); name_t name; if (!is_identifier(name)) throw_exception("sheet name expected"); queryable_sheet_t& qs(sheet_from_name(name)); require_token(close_parenthesis_k); any_regular_t result; #if defined(ADOBE_STD_SERIALIZATION) out_m << "\n### dump\nsheet " << name << " {\n"; out_m << "input:\n"; { dictionary_t input_dict; populate_dict(input_dict, qs.input_index(), qs); out_m << begin_asl_cel << input_dict << end_asl_cel << std::endl; } out_m << "interface:\n"; { dictionary_t interface_dict; populate_dict(interface_dict, qs.interface_index(), qs, true, true); out_m << begin_asl_cel << interface_dict << end_asl_cel << std::endl; } out_m << "output:\n"; { dictionary_t output_dict; populate_dict(output_dict, qs.output_index(), qs, true); out_m << begin_asl_cel << output_dict << end_asl_cel << std::endl; } out_m << "invariant:\n"; { dictionary_t invariant_dict; populate_dict(invariant_dict, qs.invariant_index(), qs); out_m << begin_asl_cel << invariant_dict << end_asl_cel << std::endl; } out_m << "}" << std::endl; #endif return true; } return false; }
static void err_bad_excl (const CLIF_option *optn, char c, int n) { CLIF_option tmp = *optn; char *ss; char *excl = show_excl (curr.option_list, 0); /* Note: show_(short|long)() nested!!! */ tmp.arg_name = NULL; if (c) ss = show_short (&tmp); else ss = show_long (&tmp); err_report ("%s `%s' (argc %d): Only one of:\n %s\n" "may be specified.", (c || !is_keyword (optn)) ? "Option" : "Keyword", ss, n, excl); }
// contributing_decl = "contributing" "(" identifier ")". bool adam_test_parser::is_contributing_decl() { if (is_keyword(contributing_k)) { require_token(open_parenthesis_k); name_t name; if (!is_identifier(name)) throw_exception("sheet name expected"); queryable_sheet_t& qs(sheet_from_name(name)); require_token(close_parenthesis_k); any_regular_t result; out_m << "\n### contributing of sheet " << name << " ###\n"; #if defined(ADOBE_STD_SERIALIZATION) out_m << begin_asl_cel << qs.contributing() << end_asl_cel << std::endl; #endif return true; } return false; }