frenzy::dom::NodeListp
frenzy::dom::Element::getElementsByTagName(ustring name)
{
  // Special case
  if (name == "*")
    return NodeList::create(shared_from_this(), TRAVERSE_TREE, type_filter(Node::ELEMENT_NODE));
  
  return NodeList::create(shared_from_this(), TRAVERSE_TREE, localnamematch(name));
}
Exemple #2
0
int print_stack(TDynamic_structure_buffer *b, TStack *stack) {
    const char* symbols[47];   
    symbols[0] = "=="; 
    symbols[1] = ">"; 
    symbols[2] = "<"; 
    symbols[3] = ">="; 
    symbols[4] = "<="; 
    symbols[5] = "!="; 
    symbols[6] = "+"; 
    symbols[7] = "-"; 
    symbols[8] = "*"; 
    symbols[9] = "/"; 
    symbols[10] = ")"; 
    symbols[11] = "("; 
    symbols[12] = "$"; 
    symbols[13] = "id"; 
    symbols[14] = "li"; 
    symbols[45] = "SHIFT"; 
    symbols[46] = "RVALUE"; 

    args_assert(b != NULL && stack != NULL, INTERNAL_ERROR);
    
    TToken *tmp = NULL;
    index_t next = ZERO_INDEX;
    catch_internal_error(
        dereference_structure(b, stack->top, (void**)&tmp),
        INTERNAL_ERROR,
        "Failed to dereference structure buffer."
    );
    fprintf(stderr, "\nStack:\n    %s %d <-- stack top\n", 
            symbols[type_filter(tmp->token_type)], tmp->original_type);

    while (tmp->token_type != END_OF_EXPR) {
        if ((next = tmp->next) == ZERO_INDEX)
            return SYNTAX_ERROR;

        catch_internal_error(
            dereference_structure(b, next, (void**)&tmp),
            INTERNAL_ERROR,
            "Failed to dereference structure buffer"
        );

        fprintf(stderr, "    %s %d\n", symbols[tmp->token_type], tmp->original_type);
    }
    return RETURN_OK;
}
Exemple #3
0
int get_types(TDynamic_structure_buffer *b, TStack *stack, int *values) {
    args_assert(b != NULL && stack != NULL && values != NULL, INTERNAL_ERROR);
    debug_print("%s\n", "GET TYPES");
    
    TToken *tmp = NULL;
    index_t next = ZERO_INDEX;
    int i;
    int t;
    int *s;
    int *f;

    catch_internal_error(
        dereference_structure(b, stack->top, (void**)&tmp),
        INTERNAL_ERROR,
        "Failed to dereference structure buffer."
    );
    
    for (i = 1; tmp->token_type != SHIFT; i++) {
        if (i > MAX_RULE_LENGTH)
            return SYNTAX_ERROR;
        values[i] = type_filter(tmp->token_type);
        if ((next = tmp->next) == ZERO_INDEX)
            return SYNTAX_ERROR;
        
        catch_internal_error(
            dereference_structure(b, next, (void**)&tmp),
            INTERNAL_ERROR,
            "Failed to dereference structure buffer."
        );
    }
    values[0] = i - 1;           // store length of rule to index 0
    for (s = (values + 1), f = (values + i - 1); s < f; s++, f--) {
        t = *s;
        *s = *f;
        *f = t;
    }
    return RETURN_OK;
}
Exemple #4
0
int check_expression(Resources *res, TToken **last_token, index_t *last_index) {
    args_assert(res != NULL, INTERNAL_ERROR);

    TToken *input_token = NULL;
    TToken *top_token = NULL;
    TToken *tmp = NULL;
    index_t top_index = ZERO_INDEX;
    index_t input_index = ZERO_INDEX;
    TStack stack;
    int iRet = RETURN_OK;
    int return_type;

    init_stack(&stack);

    new_item(&res->struct_buff, top_index, top_token);
    top_token->token_type = END_OF_EXPR;
    push(&res->struct_buff, &stack, top_index); // $ on top of the stack

    if ((*last_token) != NULL) 
        input_index = *last_index;
    else 
        input_index = get_token(res->source, &res->string_buff, &res->struct_buff);
    
    catch_internal_error(
        dereference_structure(&res->struct_buff, input_index, (void **)&input_token),
        INTERNAL_ERROR,
        "Failed to dereference structure buffer."
    );

    if (input_token->token_type == ERRORT) {
        iRet = LEXICAL_ERROR;
        goto EXIT;
    }
    
        
    catch_internal_error(
        dereference_structure(&res->struct_buff, top_index, (void **)&top_token),
        INTERNAL_ERROR,
        "Failed to dereference structure buffer."
    );

    do {
#if DEBUG
         print_stack(&res->struct_buff, &stack);
#endif
         debug_print("%s %d\n", "TOP", top_token->token_type);
         debug_print("%s %d\n", "INPUT", input_token->token_type);
        
        if (top_token->token_type == IDENTIFIER 
            && input_token->token_type == OPENING_BRACKET) {
            debug_print("%s\n", "FUNCTION CALL IN EXPR");
            
            index_t last_id = top_token->token_index;
            catch_undefined_error(is_func_declared(res, last_id),
                                 SEMANTIC_ERROR, "Function declaration check failed.", 1
            );
            
            dereference_structure(&res->struct_buff, input_index, (void **)last_token);

            if ((iRet = generate_function_call(res, last_id)) != 0) goto EXIT;
            return_type = get_return_type(res, top_token->token_index);
            catch_internal_error(return_type, SYNTAX_ERROR, "Failed to get function return type.");

            // Reduction of function call
            if((iRet = reduce(&res->struct_buff, &stack, return_type)) != RETURN_OK)
                goto EXIT;

            top_index = stack.top;
            catch_syntax_error(
                get_first_token(&res->struct_buff, &stack, &top_index),
                INTERNAL_ERROR,
                "Failed to get first token", 1
            );
            input_index = get_token(res->source, &res->string_buff, &res->struct_buff);
            catch_internal_error(
                dereference_structure(&res->struct_buff, input_index, (void **)&input_token),
                INTERNAL_ERROR,
                "Failed to dereference structure buffer."
            );

            if (input_token->token_type == ERRORT) {
                iRet = LEXICAL_ERROR;
                goto EXIT;
            }

            catch_internal_error(
                dereference_structure(&res->struct_buff, top_index, (void **)&top_token),
                INTERNAL_ERROR,
                "Failed to dereference structure buffer."
            );
            if (type_filter(top_token->token_type) == END_OF_EXPR &&
                type_filter(input_token->token_type) == END_OF_EXPR)
                break;

        }

        switch(precedence_table[type_filter(top_token->token_type)]
                               [type_filter(input_token->token_type)]) {
            case H:
                debug_print("%s\n", "CASE H");
                top_index = input_index;
                push(&res->struct_buff, &stack, top_index);
                input_index = get_token(res->source, &res->string_buff, &res->struct_buff);
                catch_internal_error(
                    dereference_structure(&res->struct_buff, input_index, (void **)&input_token),
                    INTERNAL_ERROR,
                    "Failed to dereference structure buffer."
                );
                
                if (input_token->token_type == ERRORT) {
                    iRet = LEXICAL_ERROR;
                    goto EXIT;
                }

                catch_internal_error(
                    dereference_structure(&res->struct_buff, top_index, (void **)&top_token),
                    INTERNAL_ERROR,
                    "Failed to dereference structure buffer."
                );

                break;

            case S:
                debug_print("%s\n", "CASE S");
                new_item(&res->struct_buff, top_index, top_token);
                catch_internal_error(
                    dereference_structure(&res->struct_buff, stack.top, (void **)&tmp),
                    INTERNAL_ERROR,
                    "Failed to dereference structure buffer."
                );

                top_token->token_type = SHIFT;

                if (tmp->token_type == RVALUE) {
                    index_t rvalue_index = stack.top;
                    pop(&res->struct_buff, &stack);
                    push(&res->struct_buff, &stack, top_index);
                    push(&res->struct_buff, &stack, rvalue_index);

                } else
                    push(&res->struct_buff, &stack, top_index);
                
                catch_internal_error(
                    dereference_structure(&res->struct_buff, input_index, (void **)&input_token),
                    INTERNAL_ERROR,
                    "Failed to dereference structure buffer."
                );

                top_index = input_index;
                push(&res->struct_buff, &stack, top_index);
                input_index = get_token(res->source, &res->string_buff, &res->struct_buff);
                catch_internal_error(
                    dereference_structure(&res->struct_buff, input_index, (void **)&input_token),
                    INTERNAL_ERROR,
                    "Failed to dereference structure buffer."
                );

                if (input_token->token_type == ERRORT) {
                    iRet = LEXICAL_ERROR;
                    goto EXIT;
                }

                catch_internal_error(
                    dereference_structure(&res->struct_buff, top_index, (void **)&top_token),
                    INTERNAL_ERROR,
                    "Failed to dereference structure buffer."
                );
                break;
            
            case R:
                debug_print("%s\n", "CASE R");
                if ((iRet = get_rule(res, &stack)) != RETURN_OK)
                    goto EXIT;
                
                top_index = stack.top;
                
                catch_syntax_error(
                    get_first_token(&res->struct_buff, &stack, &top_index),
                    INTERNAL_ERROR,
                    "Failed to get first token", 1
                );
                catch_internal_error(
                    dereference_structure(&res->struct_buff, top_index, (void **)&top_token),
                    INTERNAL_ERROR,
                    "Failed to dereference structure buffer."
                );
                break;
 
            case E:
                debug_print("%s\n", "CASE E");
                if (type_filter(top_token->token_type) == END_OF_EXPR && 
                    type_filter(input_token->token_type) == CLOSING_BRACKET) {
                    catch_internal_error(
                         dereference_structure(&res->struct_buff, input_index, (void **)last_token),
                         INTERNAL_ERROR,
                         "Failed to dereference structure buffer."
                    );
                    
                    catch_internal_error(
                         dereference_structure(&res->struct_buff, stack.top, (void **)&top_token),
                         INTERNAL_ERROR,
                         "Failed to dereference structure buffer."
                    );
                    if (top_token->original_type == 0) {      // Empty expression, there was nothing reduced on top
                        debug_print("%s: %d\n", "EMPTY EXPRESSION RETURN", SYNTAX_ERROR);
                        iRet = SYNTAX_ERROR;
                        goto EXIT;
                    }

                    goto FINISH;

                }

                iRet = SYNTAX_ERROR;
                goto EXIT;
            
            default:
                debug_print("%s", "DEFAULT\n");
                iRet = INTERNAL_ERROR;
                goto EXIT;
        }
                  
    } while (type_filter(top_token->token_type) != END_OF_EXPR || type_filter(input_token->token_type) != END_OF_EXPR);
    
    catch_internal_error(
         dereference_structure(&res->struct_buff, input_index, (void **)last_token),
         INTERNAL_ERROR,
         "Failed to dereference structure buffer."
    );
    
    catch_internal_error(
         dereference_structure(&res->struct_buff, stack.top, (void **)&top_token),
         INTERNAL_ERROR,
         "Failed to dereference structure buffer."
    );

FINISH:
    debug_print("%s: %d\n", "TYPE OF EXPRESSION", top_token->original_type);
    // send type of expression back to syntax_analysis
    (*last_token)->original_type = top_token->original_type;
    
    // set type of stack top on runtime stack
    catch_internal_error(new_instruction_int_int(&res->instruction_buffer, 0lu, top_token->original_type, 0, SET_TYPE),
                         INTERNAL_ERROR, "Failed to generate new instruction");
    

EXIT:
    debug_print("%s: %d\n", "RETURN", iRet);
    return iRet;
}