]> git.rm.cloudns.org Git - xonotic/gmqcc.git/commitdiff
Itegration of corrector. Seems to be some leaks in the score keeping for the probabi...
authorDale Weiler <killfieldengine@gmail.com>
Fri, 4 Jan 2013 11:44:25 +0000 (11:44 +0000)
committerDale Weiler <killfieldengine@gmail.com>
Fri, 4 Jan 2013 11:44:25 +0000 (11:44 +0000)
correct.c
parser.c

index d144c31baab8afdcb1f22a2830ce46ec614ee0c1..b2e9c69a708bfba016e777a39b82e07fcaa05e61 100644 (file)
--- a/correct.c
+++ b/correct.c
@@ -356,11 +356,12 @@ char *correct_str(ht table, const char *ident) {
     if (correct_find(table, ident))
         return found;
 
-    mem_d(found);
+    /*mem_d(found);*/
     if ((e1rows = correct_size(ident))) {
         e1      = correct_edit(ident);
 
         if ((e1ident = correct_maximum(table, e1, e1rows))) {
+            mem_d(found);
             found = util_strdup(e1ident);
             correct_cleanup(e1, e1rows);
             return found;
@@ -368,8 +369,10 @@ char *correct_str(ht table, const char *ident) {
     }
 
     e2 = correct_known(table, e1, e1rows, &e2rows);
-    if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows))))
+    if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) {
+        mem_d(found);
         found = util_strdup(e2ident);
+    }
     
     correct_cleanup(e1, e1rows);
     correct_cleanup(e2, e2rows);
index cb02e742b5a117acc87bf7dc5d5e084fbb7cb477..7c6854b6c14dd8db480ca30c9e0ec0d80eba219b 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -74,6 +74,10 @@ typedef struct {
     ht htglobals;
     ht *typedefs;
 
+    /* same as above but for the spelling corrector */
+    ht       *correct_variables;
+    size_t ***correct_variables_score;  /* vector of vector of size_t* */
+
     /* not to be used directly, we use the hash table */
     ast_expression **_locals;
     size_t          *_blocklocals;
@@ -1614,13 +1618,15 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                 }
                 else
                 {
+                    size_t i;
+                    char  *correct = NULL;
+
                     /*
                      * sometimes people use preprocessing predefs without enabling them
                      * i've done this thousands of times already myself.  Lets check for
                      * it in the predef table.  And diagnose it better :)
                      */
                     if (!OPTS_FLAG(FTEPP_PREDEFS)) {
-                        size_t i;
                         for (i = 0; i < sizeof(ftepp_predefs)/sizeof(*ftepp_predefs); i++) {
                             if (!strcmp(ftepp_predefs[i].name, parser_tokval(parser))) {
                                 parseerror(parser, "unexpected ident: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
@@ -1629,7 +1635,29 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                         }
                     }
 
-                    parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
+                    /*
+                     * TODO: determine the best score for the identifier: be it
+                     * a variable, a field.
+                     *
+                     * We should also consider adding correction tables for
+                     * other things as well.
+                     */
+                    for (i = 0; i < vec_size(parser->correct_variables); i++) {
+                        correct = correct_str(parser->correct_variables[i], "ello");
+                        if (strcmp(correct, parser_tokval(parser))) {
+                            break;
+                        } else if (correct) {
+                            mem_d(correct);
+                        }
+                    }
+
+                    if (correct) {
+                        parseerror(parser, "unexpected ident: %s (did you mean %s?)", parser_tokval(parser), correct);
+                        /*mem_d(correct);*/
+                    } else {
+                        parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
+                    }
+
                     goto onerr;
                 }
             }
@@ -1968,6 +1996,10 @@ static void parser_enterblock(parser_t *parser)
     vec_push(parser->typedefs, util_htnew(TYPEDEF_HT_SIZE));
     vec_push(parser->_blocktypedefs, vec_size(parser->_typedefs));
     vec_push(parser->_block_ctx, parser_ctx(parser));
+
+    /* corrector */
+    vec_push(parser->correct_variables, util_htnew(PARSER_HT_SIZE));
+    vec_push(parser->correct_variables_score, NULL);
 }
 
 static bool parser_leaveblock(parser_t *parser)
@@ -1981,7 +2013,11 @@ static bool parser_leaveblock(parser_t *parser)
     }
 
     util_htdel(vec_last(parser->variables));
+    util_htdel(vec_last(parser->correct_variables)); /* corrector */
+    vec_free(vec_last(parser->correct_variables_score)); /* corrector */
+
     vec_pop(parser->variables);
+    vec_pop(parser->correct_variables); /* corrector */
     if (!vec_size(parser->_blocklocals)) {
         parseerror(parser, "internal error: parser_leaveblock with no block (2)");
         return false;
@@ -2008,6 +2044,7 @@ static bool parser_leaveblock(parser_t *parser)
     vec_pop(parser->typedefs);
 
     vec_pop(parser->_block_ctx);
+
     return rv;
 }
 
@@ -2015,6 +2052,13 @@ static void parser_addlocal(parser_t *parser, const char *name, ast_expression *
 {
     vec_push(parser->_locals, e);
     util_htset(vec_last(parser->variables), name, (void*)e);
+
+    /* corrector */
+    correct_add (
+         vec_last(parser->correct_variables),
+        &vec_last(parser->correct_variables_score),
+        name
+    );
 }
 
 static ast_expression* process_condition(parser_t *parser, ast_expression *cond, bool *_ifnot)
@@ -3542,6 +3586,7 @@ static bool parse_function_body(parser_t *parser, ast_value *var)
 
             vec_push(parser->globals, (ast_expression*)thinkfunc);
             util_htset(parser->htglobals, thinkfunc->name, thinkfunc);
+
             nextthink = (ast_expression*)thinkfunc;
 
         } else {
@@ -4783,6 +4828,14 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
 
                     /* Add it to the local scope */
                     util_htset(vec_last(parser->variables), var->name, (void*)var);
+
+                    /* corrector */
+                    correct_add (
+                         vec_last(parser->correct_variables),
+                        &vec_last(parser->correct_variables_score),
+                        var->name
+                    );
+
                     /* now rename the global */
                     ln = strlen(var->name);
                     vec_append(defname, ln, var->name);
@@ -4796,6 +4849,13 @@ static bool parse_variable(parser_t *parser, ast_block *localblock, bool nofield
                         for (i = 0; i < 3; ++i) {
                             util_htset(vec_last(parser->variables), me[i]->name, (void*)(me[i]));
 
+                            /* corrector */
+                            correct_add(
+                                 vec_last(parser->correct_variables),
+                                &vec_last(parser->correct_variables_score),
+                                me[i]->name
+                            );
+
                             vec_shrinkto(defname, prefix_len);
                             ln = strlen(me[i]->name);
                             vec_append(defname, ln, me[i]->name);
@@ -5342,6 +5402,17 @@ void parser_cleanup()
     vec_free(parser->_blocklocals);
     vec_free(parser->_locals);
 
+    /* corrector */
+    for (i = 0; i < vec_size(parser->correct_variables); ++i) {
+        correct_del(parser->correct_variables[i], parser->correct_variables_score[i]);
+    }
+    for (i = 0; i < vec_size(parser->correct_variables_score); ++i) {
+        vec_free(parser->correct_variables_score[i]);
+    }
+    vec_free(parser->correct_variables);
+    vec_free(parser->correct_variables_score);
+
+
     for (i = 0; i < vec_size(parser->_typedefs); ++i)
         ast_delete(parser->_typedefs[i]);
     vec_free(parser->_typedefs);