Interactive Parsing

version 3 by Jon Ingold

  • Home page
  • Beginning
  • Previous
  • Next



  • Book 3 - Analyse the input line


    Chapter 1 - Core of the Analysis system - Tokenise, suggest and colourise

    Include (-

    Global suggested_wordnum = 0;
    Global previous_checks_wordnum = 0;
    Global cursor_word = 0;
    Constant MAX_PREVIOUS_CHECKS = 120;
    Array previous_checks_failed --> MAX_PREVIOUS_CHECKS+1;
    Array previous_checks_passed --> MAX_PREVIOUS_CHECKS+1;
    Global previous_suggestion_best_score = 0;

    [ ErasePreviousSuggestions i;
        previous_checks_wordnum = 0;
        current_analysis_stage = PENDING;
        prompted_word = 0;
        for (i = 0: i < 20: i++) word_colours->i = NORMAL;
    ];

    [ ResetRunningChecks minor_only;
                ! set up storage of advance parsing by the suggestion system
        if (~~minor_only)
        {
                previous_checks_passed--> 0 = 0;
                previous_checks_failed--> 0 = 0;
                previous_checks_wordnum = 0;
        }
        current_analysis_stage = PERFORMED;
        previous_suggestion_best_score = BASE_LINE_MATCH_SCORE;
        PrintPressReturn(false);
    ];


    [ BasicAnalysis
    print_buffer cur_pos i;

        ! clear last turn's suggestion
        prompted_word = 0;

    if (print_buffer-->0 > 0)
        {

            ! don't tokenise if there's no input as results are unpredictable
            
            VM_Tokenise(print_buffer, parse); wn = 1;

            cursor_word = WordUnderCursor(cur_pos);

            
            if (WordCount() == 0) rfalse;

            for (i = 1: i <= WordCount() : i++)
            {
                word_colours -> i = GetBaseColourOfNextWord(i, print_buffer);
            }
            
            ColourByAdvanceParse();
            
            !glk_set_window((+main-window+).ref_number);
            !print "Word Count = ", WordCount(), "^";

            ! this sets "previous checks wordnum" to the cursor word
            if (cursor_word ~= previous_checks_wordnum)
            {
                ResetRunningChecks(false);
                BlankSuggestion();
            }


            
            if (word_colours -> WordCount() == LONGWORD_ERROR)
                PrintPressReturn(false);
            else
                PrintPressReturn(command_in_full && word_understood_to == WordCount());
            
            
                ! unless the buffer ends in a space, go easy on the last word
                ColourLastWord(print_buffer, cursor_word);
        }

    ];

    [ SuggestionAnalysis print_buffer run_to_halt;

    ! glk_set_window((+main-window+).ref_number);
    ! print "Word_colour of word 2 = ", word_colours->cursor_word, "...^";

            if (word_colours->cursor_word ~= VALID || SuggestFromAbbreviation(DictionaryValue(cursor_word), WordLength(cursor_word)))
            {

                if (CreateSuggestionForWord(print_buffer, WordAddressInBuffer(cursor_word, print_buffer), WordLength(cursor_word) , cursor_word, run_to_halt))
                {
                    suggested_wordnum = cursor_word;
                    PrintSuggestion(prompted_word, CharacterNumber(cursor_word));
                }
                else BlankSuggestion();
            }
            else BlankSuggestion();
    ];

    [ SuggestFromAbbreviation wd len;
        return (len == 1 && wd ~= ',//') ;
    ];

    -)