ActionParser { public N parse(File file) { try { char[] chars = new String(Files.readAllBytes(Paths.get(file.getPath())), charset).toCharArray(); return parse(new Input(chars, file.toURI())); } catch (IOException e) { throw new RuntimeException(e); } } ActionParser(Charset charset, LexerlessGrammarBuilder b, Class grammarClass, Object treeFactory, NodeBuilder nodeBuilder, GrammarRuleKey rootRule); N parse(File file); N parse(String source); GrammarRuleKey rootRule();  }
@Test(expected = RecognitionException.class) public void not_matching() throws Exception { parse(MyGrammarKeys.NUMERIC, "x"); }
@Test public void basic() throws Exception { assertThat(parse(MyGrammarKeys.NUMERIC, "42", Numeric.class).toString()).isEqualTo("42"); }
@Test public void firstOf() throws Exception { assertThat(parse(MyGrammarKeys.OPERATOR, "+", Operator.class).toString()).isEqualTo("+"); assertThat(parse(MyGrammarKeys.OPERATOR, "-", Operator.class).toString()).isEqualTo("-"); assertNotParse(MyGrammarKeys.OPERATOR, "x"); }
@Test public void optional() throws Exception { assertThat(parse(MyGrammarKeys.UNARY_EXP, "42", UnaryExp.class).toString()).isEqualTo("42"); assertThat(parse(MyGrammarKeys.UNARY_EXP, "+42", UnaryExp.class).toString()).isEqualTo("+ 42"); }
@Test public void oneOrMore() throws Exception { assertThat(parse(MyGrammarKeys.NUMERIC_LIST, "42 7", NumericList.class).toString()).isEqualTo("[42, 7]"); assertNotParse(MyGrammarKeys.NUMERIC_LIST, ""); }
@Test public void zeroOrMore() throws Exception { assertThat(parse(MyGrammarKeys.POTENTIALLY_EMPTY_NUMERIC_LIST, "42 7", NumericList.class).toString()).isEqualTo("[42, 7]"); assertThat(parse(MyGrammarKeys.POTENTIALLY_EMPTY_NUMERIC_LIST, "", NumericList.class).toString()).isEqualTo("[]"); }
@Test public void skipped_astnode() throws Exception { assertThat(parse(MyGrammarKeys.NUMERIC_WITH_EOF, "42", Numeric.class).toString()).isEqualTo("42"); }
@Test public void undefined_token_type() throws Exception { Numeric numeric = parse(MyGrammarKeys.NUMERIC, "42", Numeric.class); TokenType type = numeric.getFirstChild().getToken().getType(); assertThat(type.hasToBeSkippedFromAst(null)).isFalse(); assertThat(type.getValue()).isEqualTo("TOKEN"); assertThat(type.toString()).isNotNull(); }
@Test public void comment() throws Exception { Numeric numeric = parse(MyGrammarKeys.NUMERIC, "42", Numeric.class); Trivia trivia = numeric.getFirstChild().getToken().getTrivia().get(0); assertThat(trivia.isComment()).isTrue(); assertThat(trivia.getToken().getOriginalValue()).isEqualTo(""); }
@Test public void skipped_text() throws Exception { assertThat(parse(MyGrammarKeys.NUMERIC, " 42", Numeric.class).toString()).isEqualTo("42"); }
@Test public void unknown_trivia() throws Exception { thrown.expect(IllegalStateException.class); thrown.expectMessage("Unexpected trivia kind"); parse(MyGrammarKeys.NUMERIC, "#preprocessor 42", Numeric.class); }
@Test public void parse_file() throws Exception { ActionParser<AstNode> parser = parser(MyGrammarKeys.UNARY_EXP); AstNode node = parser.parse(new File("src/test/resources/typed/42.txt")); assertThat(node).isInstanceOf(UnaryExp.class); }
@Test public void unknown_file() throws Exception { ActionParser<AstNode> parser = parser(MyGrammarKeys.NUMERIC); try { parser.parse(new File("unknown")); Assert.fail("expceted exception"); } catch (RuntimeException e) { assertThat(e.getCause()).isInstanceOf(NoSuchFileException.class); } }
@Test public void more_than_one_call_to_the_same_action_method() throws Exception { assertThat(parse(MyGrammarKeys.NUMERIC, "42", Numeric.class).toString()).isEqualTo("42"); assertThat(parse(MyGrammarKeys.NUMERIC2, "42", Numeric.class).toString()).isEqualTo("42"); }