Skip to content

order of tokens in choice matters? #26

@stef

Description

@stef

If a token is a prefix of another token in a choice, then depending on the order of their listing in the choice influences the results of the parser. an example:

#include <hammer/hammer.h>
#include <stdio.h>
#include <string.h>

// test with
// gcc -DBUG -o prefixbug prefixbug.c $(pkg-config --cflags --libs libhammer)
// echo "asdfv" | ./prefixbug # should return yay, does boo
// c.f.
// gcc -o prefixbug prefixbug.c $(pkg-config --cflags --libs libhammer)
// echo "asdfv" | ./prefixbug # correctly^Was expected returns yay

int main(int argc, char *argv[]) {
    uint8_t input[1024];
    size_t inputsize;

    HParser *eol = h_token("\n",1);
    HParser *words = h_choice(
#ifdef BUG
                              h_token("asdf",strlen("asdf")),
                              h_token("asdfv",strlen("asdfv")),
#else // no bug
                              h_token("asdfv",strlen("asdfv")),
                              h_token("asdf",strlen("asdf")),
#endif
                              NULL);
    HParser *rule = h_sequence(words,eol,NULL);

    inputsize = fread(input, 1, sizeof(input), stdin);

    HParseResult *result = h_parse(rule, input, inputsize);
    if(result) {
        printf("yay!\n");
    } else {
        printf("boo!\n");
    }
}

sorry if this is due to my poor understanding and in fact an intended feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions