00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #define _GNU_SOURCE
00027 #include <string.h>
00028 #include <sys/types.h>
00029 #include <yyast/node.h>
00030 #include <yyast/leaf.h>
00031 #include <yyast/count.h>
00032 #include <yyast/utils.h>
00033
00034 ya_position_t ya_previous_position = {0, 0, 0};
00035 ya_position_t ya_current_position = {0, 0, 0};
00036
00037 char *ya_filenames[YA_MAX_NR_FILENAMES];
00038 int ya_nr_filenames = 0;
00039
00040 ya_t ya_count(char *s, size_t s_length)
00041 {
00042 uint32_t i;
00043 char c;
00044 int ascii;
00045 int utf8_start;
00046 int printable_ascii;
00047 ya_t r;
00048
00049 ya_previous_position = ya_current_position;
00050
00051
00052 for (i = 0; i < s_length; i++) {
00053 switch (c = s[i]) {
00054 case '\n':
00055 ya_current_position.line++;
00056 ya_current_position.column = 0;
00057 break;
00058 case '\r':
00059 ya_current_position.column = 0;
00060 break;
00061 case '\t':
00062 ya_current_position.column+= 8 - (ya_current_position.column % 8);
00063 default:
00064 printable_ascii = (c >= ' ') && (c <= '~');
00065 utf8_start = (c & 0xc0) == 0x80;
00066
00067 if (printable_ascii || utf8_start) {
00068
00069 ya_current_position.column++;
00070 }
00071 }
00072 }
00073 r.size = 0;
00074 r.type = YA_NODE_TYPE_COUNT;
00075 r.position = ya_previous_position;
00076 r.node = NULL;
00077 return r;
00078 }
00079
00080 uint32_t ya_get_file_nr(char *filename)
00081 {
00082 int i;
00083
00084 for (i = 0; i < YA_MAX_NR_FILENAMES; i++) {
00085 if (i < ya_nr_filenames) {
00086 if (strcmp(ya_filenames[i], filename) == 0) {
00087
00088 return i;
00089 }
00090
00091
00092 } else {
00093
00094 ya_filenames[i] = strdup(filename);
00095 ya_nr_filenames++;
00096 return i;
00097 }
00098 }
00099
00100
00101 return UINT32_MAX;
00102 }
00103
00104 void ya_reposition(char *s, size_t s_length)
00105 {
00106 char *s_filename;
00107 char *filename;
00108 long line = strtol(s, &s_filename, 10);
00109
00110 ya_previous_position = ya_current_position;
00111
00112 if (s == s_filename) {
00113 fprintf(stderr, "ya_reposition unexpected characters.\n");
00114 abort();
00115 }
00116
00117 ya_current_position.column = 0;
00118 ya_current_position.line = line - 1;
00119
00120 if (*s_filename == ' ') {
00121
00122 filename = strndup(&s_filename[2], s_length - (s_filename - s) - 3);
00123
00124 ya_current_position.file = ya_get_file_nr(filename);
00125 }
00126 }
00127
00128 ya_t ya_get_filenames(void)
00129 {
00130 ya_t filename;
00131 ya_t filename_list = YA_EMPTYLIST;
00132 ya_t filenames;
00133 int i;
00134
00135 for (i = 0; i < ya_nr_filenames; i++) {
00136 filename = ya_text("#file", ya_filenames[i], strlen(ya_filenames[i]));
00137 ya_clear_position(&filename);
00138 filename_list = YA_LIST(&filename_list, &filename);
00139 }
00140
00141 filenames = YA_BRANCH("#files", &filename_list);
00142 ya_clear_position(&filenames);
00143 return filenames;
00144 }
00145
00146 void ya_clear_position(ya_t *node)
00147 {
00148 node->position.column = UINT32_MAX;
00149 node->position.line = UINT32_MAX;
00150 node->position.file = UINT32_MAX;
00151 node->node->position.column = UINT32_MAX;
00152 node->node->position.line = UINT32_MAX;
00153 node->node->position.file = UINT32_MAX;
00154 }
00155