// the caller should push and pop the typedef table struct compound_statement *parse_compound_statement(struct parser *parser) { expect(parser->lexer, TOK_LBRACE); // lexer_push_typedef_tab(parser->lexer); // look one token ahead to determing if this is a declaration or statement or empty block union token tok = lexer_next_token(parser->lexer); struct dynarr *decl_or_stmt_list = dynarr_init(); while (tok.tok_tag != TOK_RBRACE) { if (initiate_declaration(tok)) { #if 0 if (dynarr_size(stmtList) > 0) { panic("encounter declaration after statement"); } #endif lexer_put_back(parser->lexer, tok); dynarr_add(decl_or_stmt_list, parse_declaration(parser)); } else { // initiate a statement lexer_put_back(parser->lexer, tok); dynarr_add(decl_or_stmt_list, parse_statement(parser)); } tok = lexer_next_token(parser->lexer); } // lexer_pop_typedef_tab(parser->lexer); return compound_statement_init(decl_or_stmt_list); }
TreeNode<Token>* Parser::parse_declaration_block() { TreeNode<Token>* node = NULL; if( Lexer::getInstance()->getCurrentToken() == TOK_INT) { TreeNode<Token>* node = ast->AddNode(NODE_DECLARATION_BLOCK); bool loop = false; do { ast->AddChild( node, parse_declaration( ) ); loop = ( Lexer::getInstance()->getCurrentToken() == TOK_COMMA ); if(loop) Lexer::getInstance()->getToken(); } while (loop); if(Lexer::getInstance()->getCurrentToken() != TOK_SEMICOLON) std::cout << "Expecting ;" << std::endl; exit(0); } Lexer::getInstance()->getToken(); return node; }
static fz_css_property *parse_declaration_list(struct lexbuf *buf) { fz_css_property *head, *tail; if (buf->lookahead == '}' || buf->lookahead == EOF) return NULL; head = tail = parse_declaration(buf); while (accept(buf, ';')) { if (buf->lookahead != '}' && buf->lookahead != ';' && buf->lookahead != EOF) { tail = tail->next = parse_declaration(buf); } } return head; }
static boolean translate( struct translate_ctx *ctx ) { eat_opt_white( &ctx->cur ); if (!parse_header( ctx )) return FALSE; if (ctx->processor == TGSI_PROCESSOR_TESS_CTRL || ctx->processor == TGSI_PROCESSOR_TESS_EVAL) ctx->implied_array_size = 32; while (*ctx->cur != '\0') { uint label_val = 0; if (!eat_white( &ctx->cur )) { report_error( ctx, "Syntax error" ); return FALSE; } if (*ctx->cur == '\0') break; if (parse_label( ctx, &label_val )) { if (!parse_instruction( ctx, TRUE )) return FALSE; } else if (str_match_nocase_whole( &ctx->cur, "DCL" )) { if (!parse_declaration( ctx )) return FALSE; } else if (str_match_nocase_whole( &ctx->cur, "IMM" )) { if (!parse_immediate( ctx )) return FALSE; } else if (str_match_nocase_whole( &ctx->cur, "PROPERTY" )) { if (!parse_property( ctx )) return FALSE; } else if (!parse_instruction( ctx, FALSE )) { return FALSE; } } return TRUE; }
// Statement // Parses a single full statement. In general this means a declaration // or an expression, but also includes things like return and break // statements. StatementNode* Parser::parse_statement() { switch (token_iter->type) { // Return statement case K_RETURN: { return parse_return(); } // Declaration case K_CONST: case K_VAL: case K_VAR: case K_FN: case K_STRUCT: case K_TYPE: { return parse_declaration(); } // Expression case INTEGER_LIT: case FLOAT_LIT: case STRING_LIT: case RAW_STRING_LIT: case LPAREN: case IDENTIFIER: case OPERATOR: case DOLLAR: { return parse_expression(); } default: { // Error std::ostringstream msg; msg << "Unknown statement '" << token_iter->text << "'."; parsing_error(*token_iter, msg.str()); throw 0; // Silence warnings about not returning, parsing_error throws anyway } } }
static boolean translate( struct translate_ctx *ctx ) { eat_opt_white( &ctx->cur ); if (!parse_header( ctx )) return FALSE; while (*ctx->cur != '\0') { uint label_val = 0; if (!eat_white( &ctx->cur )) { report_error( ctx, "Syntax error" ); return FALSE; } if (*ctx->cur == '\0') break; if (parse_label( ctx, &label_val )) { if (!parse_instruction( ctx, TRUE )) return FALSE; } else if (str_match_no_case( &ctx->cur, "DCL" )) { if (!parse_declaration( ctx )) return FALSE; } else if (str_match_no_case( &ctx->cur, "IMM" )) { if (!parse_immediate( ctx )) return FALSE; } else if (str_match_no_case( &ctx->cur, "PROPERTY" )) { if (!parse_property( ctx )) return FALSE; } else if (!parse_instruction( ctx, FALSE )) { return FALSE; } } return TRUE; }
// statement = declaration | return_statement | if_statement // assignment | expression [ ";" ] static enum v7_err parse_statement(struct v7 *v7, int *is_return_statement) { if (*v7->cursor == '_' || is_alpha(*v7->cursor)) { TRY(parse_identifier(v7)); // Load identifier into v7->tok, v7->tok_len if (test_token(v7, "var", 3)) { TRY(parse_declaration(v7)); } else if (test_token(v7, "return", 6)) { TRY(parse_return_statement(v7)); if (is_return_statement != NULL) *is_return_statement = 1; } else if (test_token(v7, "if", 2)) { TRY(parse_if_statement(v7)); } else { v7->cursor = v7->tok; TRY(parse_expression(v7)); } } else { TRY(parse_expression(v7)); } // Skip optional semicolons while (*v7->cursor == ';') match(v7, *v7->cursor); //return is_return_statement; return V7_OK; }
/* * structures_init * * Module initialization. Sets up the expression-binding * routine, the name table management hooks, and the * predeclared structures. */ void structures_init (expr_ctx_t ctx, scopectx_t kwdscope) { parse_ctx_t pctx = expr_parse_ctx(ctx); namectx_t namectx = expr_namectx(ctx); struct pdclinfo_s pdinfo; machinedef_t *mach = expr_machinedef(ctx); int nounits = (machine_scalar_units(mach) == 1); int signext = machine_signext_supported(mach); nametype_vectors_t vec; int i; for (i = 0; i < sizeof(mynames)/sizeof(mynames[0]); i++) { name_declare(kwdscope, &mynames[i], 0, 0, 0, 0); } expr_dispatch_register(ctx, LEXTYPE_NAME_STRUCTURE, structure_bind); parser_lexfunc_register(pctx, ctx, LEXTYPE_LXF_FIELDEXPAND, parse_FIELDEXPAND); parser_lexfunc_register(pctx, ctx, LEXTYPE_LXF_SIZE, parse_SIZE); memset(&vec, 0, sizeof(vec)); vec.typesize = sizeof(strudef_t); vec.typeinit = structure_init; vec.typefree = structure_free; vec.typecopy = structure_copy; vec.typeser = structure_serialize; vec.typedes = structure_deserialize; nametype_dataop_register(namectx, LEXTYPE_NAME_STRUCTURE, &vec, ctx); vec.typesize = sizeof(lexseq_t); vec.typeinit = field_init; vec.typefree = field_free; vec.typecopy = field_copy; vec.typeser = field_serialize; vec.typedes = field_deserialize; nametype_dataop_register(namectx, LEXTYPE_NAME_FIELD, &vec, ctx); vec.typesize = sizeof(namereflist_t); vec.typeinit = fieldset_init; vec.typefree = fieldset_free; vec.typecopy = fieldset_copy; vec.typeser = fieldset_serialize; vec.typedes = fieldset_deserialize; nametype_dataop_register(namectx, LEXTYPE_NAME_FIELDSET, &vec, ctx); pdinfo.current = 0; pdinfo.lines[0] = (nounits ? (signext ? predeclared_vector_nu_s : predeclared_vector_nu_ns) : (signext ? predeclared_vector_u_s : predeclared_vector_u_ns)); pdinfo.lines[1] = predeclared_bitvector; pdinfo.lines[2] = (nounits ? predeclared_block_nu : predeclared_block_u); pdinfo.lines[3] = (nounits ? predeclared_blockvector_nu : predeclared_blockvector_u); expr_predeclaring_set(ctx, 1); parser_popen(pctx, predeclare_structures, &pdinfo); for (i = 0; i < 4; i++) { parse_declaration(ctx); } expr_predeclaring_set(ctx, 0); parser_atend(pctx); // Force the closure of the internal token stream } /* structures_init */
xmlResult parse_buffer(GNode *parent, gchar *text, guint length) { gchar *p; gchar start_tag[512]; gchar tag_name[512]; gchar body[65536]; gchar *content; gint content_length; gint body_length; GNode *node; gboolean no_end_tag; gchar *special_str; g_assert(text != NULL); #ifdef XML_TRACE LOG(LOG_DEBUG, "IN : parse_buffer()"); #endif body_length = 0; p = text; while((p - text) < length){ if(*p == '<'){ if(body_length != 0){ ((NODE_DATA *)(parent->data))->content = encoded_to_special(body); body[0] = '\0'; body_length = 0; } no_end_tag = FALSE; get_start_tag(p, start_tag); // <xxx/>の場合には対応するエンドタグがない if(start_tag[strlen(start_tag) - 1] == '/'){ start_tag[strlen(start_tag) - 1] = '\0'; no_end_tag = TRUE; } get_tag_name(start_tag, tag_name); // 宣言部分 if(start_tag[0] == '?'){ if(start_tag[strlen(start_tag) - 1] == '?'){ start_tag[strlen(start_tag) - 1] = '\0'; } parse_declaration(parent, &start_tag[1]); skip_start_tag(&p, tag_name); continue; } node = xml_add_child(parent, tag_name, NULL); parse_attribute(node, start_tag); if(no_end_tag == FALSE){ get_content(p, tag_name, &content, &content_length); parse_buffer(node, content, content_length); skip_end_tag(&p, tag_name); } else { skip_start_tag(&p, tag_name); } } else if (*p == '\n') { p++; } else { body[body_length] = *p; body_length ++; body[body_length] = '\0'; p++; } } if(body_length != 0){ special_str = encoded_to_special(body); ((NODE_DATA *)(parent->data))->content = iconv_convert( ((NODE_DATA *)(parent->data))->doc->encoding, "UTF-8", special_str); g_free(special_str); } #ifdef XML_TRACE LOG(LOG_DEBUG, "OUT : parse_buffer()"); #endif return XML_OK; }