Last active
August 29, 2015 14:14
-
-
Save fge/418a26cd2b1bd6a77a5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.parboiled1.grappa.samples; | |
import org.parboiled.BaseParser; | |
import org.parboiled.Rule; | |
import java.util.ArrayList; | |
import java.util.List; | |
@SuppressWarnings("AutoBoxing") | |
public class TestGrammar | |
extends BaseParser<List<Integer>> | |
{ | |
Rule whitespace() | |
{ | |
return zeroOrMore(wsp()); | |
} | |
boolean pushInt() | |
{ | |
try { | |
peek().add(Integer.parseInt(match())); | |
return true; | |
} catch (NumberFormatException ignored) { | |
return false; | |
} | |
} | |
Rule integer() | |
{ | |
return sequence(optional('-'), oneOrMore(digit())); | |
} | |
Rule addInteger() | |
{ | |
return sequence(integer(), pushInt()); | |
} | |
Rule separator() | |
{ | |
return sequence(whitespace(), ',', whitespace()); | |
} | |
Rule intList() | |
{ | |
return join(addInteger()).using(separator()).min(0); | |
} | |
Rule integerArray() | |
{ | |
return sequence('[', whitespace(), intList(), whitespace(), ']'); | |
} | |
Rule a() | |
{ | |
return sequence('A', whitespace(), '=', whitespace()); | |
} | |
Rule mainRule() | |
{ | |
return sequence(a(), push(new ArrayList<Integer>()), integerArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment