void nl_input_debug(NLInput *in_input) { lprintf(BRAIN_DEBUG, "nl_input_debug():\n"); /* enumerate all sentences */ for (int i = 0; i < array_count(in_input->sentences); i++) { NLSentence *sentence = array_item(in_input->sentences, i); lprintf(BRAIN_DEBUG, "Sentence %d:\n", (i+1)); /* enumerate tokens */ for (int t = 0; t < sentence->token_count; t++) { NLToken *token = sentence->tokens[t]; lprintf(BRAIN_DEBUG, "Token %d: flags=%04X, chars=\"%s\" (len=%d) \n", (t+1), token->flags, token->characters, token->length); PtrSet *concepts = token->concepts; for (int c = 0; c < ptrset_count(concepts); c++) { knconcept_t *concept = ptrset_item(concepts, c); lprintf(BRAIN_DEBUG, " concept: %s\n", kn_concept_name(concept)); } } } }
/** * Returns true (and returns a copy of the memoized node) if there is a memoized value for the node type at the current position. */ static int is_memoized(memo_map_t *map, int type, int start_offset, syntax_node_t **res, int *end_offset) { int i, len; array_t *da; assert(map); assert(type < PEG_NUM_NODE_TYPES); da = &map->records[type]; len = array_size(da); for (i = 0; i < len; ++i) { memo_rec_t *rec = (memo_rec_t *) array_item(da, i); if (rec->start_offset == start_offset) { *res = syntax_node_copy(rec->parse_tree); *end_offset = rec->end_offset; return 1; } } return 0; } /* is_memoized() */
static PyObj array_subscript(PyObj self, PyObj arg) { Py_ssize_t len = py_array_length(self); if (PyIndex_Check(arg)) { Py_ssize_t i = PyNumber_AsSsize_t(arg, PyExc_IndexError); if (i == -1 && PyErr_Occurred()) return(NULL); if (i < 0) i += py_array_length(self); return(array_item(self, i)); } else if (PySlice_Check(arg)) { Py_ssize_t start, stop, step, slicelength; int r; r = PySlice_GetIndicesEx(arg, len, &start, &stop, &step, &slicelength); if (r < 0) return(NULL); if (step != 1) { /* TODO: implement custom step values for array subscript */ PyErr_Format(PyExc_NotImplementedError, "unsupported step value in array subscript"); return(NULL); } if (slicelength == len && start == 0) { Py_INCREF(self); return(self); } return(array_slice(self, start, stop)); } else { PyErr_Format(PyExc_TypeError, "array indexes must be integers, not %.200s", Py_TYPE(arg)->tp_name); return(NULL); } }
/** Given a particular character offset, figures out which lines it's on. */ int input_buffer_find_line(int pos, array_t *end_offsets) { int n, i, len = array_size(end_offsets); for (i = 0; i < len; ++i) { n = * (int *) array_item(end_offsets, i); if (pos < n) return i+1; } if (pos == n) return len; else return -1; } /* find_line() */
static void delete_children(array_t *children, int start_index) { int i, len; assert(children); len = array_size(children); for (i = start_index; i < len; ++i) { syntax_node_t *child = *(syntax_node_t **) array_item(children, i); syntax_node_destroy(child); } children->num = start_index; } /* delete_children() */
/** * Destroys a memoization map. */ static void memo_map_destroy(memo_map_t *map) { int i; assert(map); for (i = 0; i < PEG_NUM_NODE_TYPES; ++i) { int j, count = array_size(&map->records[i]); for (j = 0; j < count; ++j) { memo_rec_t *mr = (memo_rec_t *) array_item(&map->records[i], j); if (mr->parse_tree) syntax_node_destroy(mr->parse_tree); } array_deinit(&map->records[i]); } free(map); } /* memo_map_destroy() */