void start_rule_callback(struct gzl_parse_state *parse_state) { struct gzl_buffer *buffer = (struct gzl_buffer*)parse_state->user_data; struct gzlparse_state *user_state = (struct gzlparse_state*)buffer->user_data; struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(parse_state->parse_stack); assert(frame->frame_type == GZL_FRAME_TYPE_RTN); struct gzl_rtn_frame *rtn_frame = &frame->f.rtn_frame; print_newline(user_state, false); print_indent(user_state); char *rule = get_json_escaped_string(rtn_frame->rtn->name, 0); printf("{\"rule\":%s, \"start\": %zu, \"line\": %zu, \"column\": %zu, ", rule, frame->start_offset.byte, frame->start_offset.line, frame->start_offset.column); free(rule); if(parse_state->parse_stack_len > 1) { frame--; struct gzl_rtn_frame *prev_rtn_frame = &frame->f.rtn_frame; char *slotname = get_json_escaped_string(prev_rtn_frame->rtn_transition->slotname, 0); printf("\"slotname\":%s, \"slotnum\":%d, ", slotname, prev_rtn_frame->rtn_transition->slotnum); free(slotname); } printf("\"children\": ["); RESIZE_DYNARRAY(user_state->first_child, user_state->first_child_len+1); *DYNARRAY_GET_TOP(user_state->first_child) = true; }
void print_newline(struct gzlparse_state *user_state, bool suppress_comma) { if(user_state->first_child_len > 0 || suppress_comma) { if(user_state->first_child_len > 0 && (*DYNARRAY_GET_TOP(user_state->first_child) || suppress_comma)) { *DYNARRAY_GET_TOP(user_state->first_child) = false; fputs("\n", stdout); } else { fputs(",\n", stdout); } } }
void terminal_callback(struct gzl_parse_state *parse_state, struct gzl_terminal *terminal) { struct gzl_buffer *buffer = (struct gzl_buffer*)parse_state->user_data; struct gzlparse_state *user_state = (struct gzlparse_state*)buffer->user_data; struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(parse_state->parse_stack); assert(frame->frame_type == GZL_FRAME_TYPE_RTN); struct gzl_rtn_frame *rtn_frame = &frame->f.rtn_frame; print_newline(user_state, false); print_indent(user_state); char *terminal_name = get_json_escaped_string(terminal->name, 0); int start = terminal->offset.byte - buffer->buf_offset; assert(start >= 0); assert(start+terminal->len <= buffer->buf_len); char *terminal_text = get_json_escaped_string(buffer->buf+ (terminal->offset.byte - buffer->buf_offset), terminal->len); char *slotname = get_json_escaped_string(rtn_frame->rtn_transition->slotname, 0); printf("{\"terminal\": %s, \"slotname\": %s, \"slotnum\": %d, \"byte_offset\": %zu, " "\"line\": %zu, \"column\": %zu, \"len\": %zu, \"text\": %s}", terminal_name, slotname, rtn_frame->rtn_transition->slotnum, terminal->offset.byte, terminal->offset.line, terminal->offset.column, terminal->len, terminal_text); free(terminal_name); free(terminal_text); free(slotname); }
void end_rule_callback(struct gzl_parse_state *parse_state) { struct gzl_buffer *buffer = (struct gzl_buffer*)parse_state->user_data; struct gzlparse_state *user_state = (struct gzlparse_state*)buffer->user_data; struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(parse_state->parse_stack); assert(frame->frame_type == GZL_FRAME_TYPE_RTN); RESIZE_DYNARRAY(user_state->first_child, user_state->first_child_len-1); print_newline(user_state, true); print_indent(user_state); printf("], \"len\": %zu}", parse_state->offset.byte - frame->start_offset.byte); }
static void end_rule_callback(ParseState *parse_state) { struct gzl_parse_stack_frame *frame = DYNARRAY_GET_TOP(parse_state->parse_stack); struct gzl_rtn_frame *rtn_frame = &frame->f.rtn_frame; VALUE self = user_data_obj(parse_state->user_data); char *rule_name = rtn_frame->rtn->name; VALUE ruby_rule_name = rb_str_new2(rule_name); VALUE ruby_input = rb_user_data_input(parse_state, frame); rb_funcall(self, rb_intern("run_rule"), 2, ruby_rule_name, ruby_input); }