Skip to content

firstOf() fails for one-character String argument #28

@akirschbaum

Description

@akirschbaum

When using firstOf() with only String arguments and if one String has length 1, then parser generation fails with a reference to trie(). This is incorrect for two reasons:

I didn't use trie() but firstOf(). The error message should use the function name that I actually did use, even if Grappa internally maps it.

I cannot think of any reason why one-character strings should be forbidden. Therefore this restriction seems so arbitrary to me that consider it a bug.

/tmp/bug-firstof$ cat build.gradle
defaultTasks 'run'
apply plugin: 'application'
sourceCompatibility = 1.8
mainClassName = 'app.Main'
dependencies { compile 'com.github.fge:grappa:2.0.0-beta.4' }
repositories { mavenCentral() }
/tmp/bug-firstof$ cat src/main/java/app/Main.java
package app;

import com.github.fge.grappa.Grappa;
import com.github.fge.grappa.parsers.BaseParser;
import com.github.fge.grappa.rules.Rule;

public class Main extends BaseParser<Object> {

    public static void main(final String[] args) {
        Grappa.createParser(Main.class).rule();
    }

    public Rule rule() {
        return firstOf("//", "#");
    }

}
/tmp/bug-firstof$ gradle
:compileJava
:processResources UP-TO-DATE
:classes
:run
Exception in thread "main" java.lang.IllegalArgumentException: strings in a trie must be two characters long or greater
        at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122)
        at com.github.fge.grappa.matchers.trie.TrieBuilder.addWord(TrieBuilder.java:52)
        at app.Main$$grappa.trie(Unknown Source)
        at app.Main$$grappa.firstOf(Unknown Source)
        at app.Main$$grappa.firstOf(Unknown Source)
        at app.Main.rule(Main.java:14)
        at app.Main$$grappa.rule(Unknown Source)
        at app.Main.main(Main.java:10)
:run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/local/java/64/jdk1.8.0_31/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 5.13 secs

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions