1 21 22 package net.percederberg.grammatica.test; 23 24 import java.io.StringReader ; 25 26 import net.percederberg.grammatica.parser.ParseException; 27 import net.percederberg.grammatica.parser.Parser; 28 import net.percederberg.grammatica.parser.ParserCreationException; 29 30 36 public class TestRegexpParser extends ParserTestCase { 37 38 41 private static final String VALID_INPUT = 42 "[a-z.]+(a|b).?"; 43 44 47 private static final String VALID_OUTPUT = 48 "Expr(2001)\n" + 49 " Term(2002)\n" + 50 " Fact(2003)\n" + 51 " Atom(2004)\n" + 52 " LEFT_BRACKET(1003): \"[\", line: 1, col: 1\n" + 53 " CharacterSet(2006)\n" + 54 " Character(2007)\n" + 55 " CHAR(1014): \"a\", line: 1, col: 2\n" + 56 " Character(2007)\n" + 57 " CHAR(1014): \"-\", line: 1, col: 3\n" + 58 " Character(2007)\n" + 59 " CHAR(1014): \"z\", line: 1, col: 4\n" + 60 " Character(2007)\n" + 61 " DOT(1011): \".\", line: 1, col: 5\n" + 62 " RIGHT_BRACKET(1004): \"]\", line: 1, col: 6\n" + 63 " AtomModifier(2005)\n" + 64 " PLUS(1009): \"+\", line: 1, col: 7\n" + 65 " Fact(2003)\n" + 66 " Atom(2004)\n" + 67 " LEFT_PAREN(1001): \"(\", line: 1, col: 8\n" + 68 " Expr(2001)\n" + 69 " Term(2002)\n" + 70 " Fact(2003)\n" + 71 " Atom(2004)\n" + 72 " CHAR(1014): \"a\", line: 1, col: 9\n" + 73 " VERTICAL_BAR(1010): \"|\", line: 1, col: 10\n" + 74 " Expr(2001)\n" + 75 " Term(2002)\n" + 76 " Fact(2003)\n" + 77 " Atom(2004)\n" + 78 " CHAR(1014): \"b\", line: 1, col: 11\n" + 79 " RIGHT_PAREN(1002): \")\", line: 1, col: 12\n" + 80 " Fact(2003)\n" + 81 " Atom(2004)\n" + 82 " DOT(1011): \".\", line: 1, col: 13\n" + 83 " AtomModifier(2005)\n" + 84 " QUESTION(1007): \"?\", line: 1, col: 14\n"; 85 86 89 private static final String UNEXPECTED_EOF_INPUT = "(abc"; 90 91 94 private static final String UNEXPECTED_CHAR_INPUT = "a\nb"; 95 96 99 private static final String UNEXPECTED_TOKEN_INPUT = "abc)"; 100 101 106 public TestRegexpParser(String name) { 107 super(name); 108 } 109 110 113 public void testValidInput() { 114 parse(createParser(VALID_INPUT), VALID_OUTPUT); 115 } 116 117 120 public void testUnexpectedEOF() { 121 failParse(createParser(UNEXPECTED_EOF_INPUT), 122 ParseException.UNEXPECTED_EOF_ERROR, 123 1, 124 5); 125 } 126 127 130 public void testUnexpectedChar() { 131 failParse(createParser(UNEXPECTED_CHAR_INPUT), 132 ParseException.UNEXPECTED_CHAR_ERROR, 133 1, 134 2); 135 } 136 137 140 public void testUnexpectedToken() { 141 failParse(createParser(UNEXPECTED_TOKEN_INPUT), 142 ParseException.UNEXPECTED_TOKEN_ERROR, 143 1, 144 4); 145 } 146 147 154 private Parser createParser(String input) { 155 Parser parser = null; 156 157 try { 158 parser = new RegexpParser(new StringReader (input)); 159 parser.prepare(); 160 } catch (ParserCreationException e) { 161 fail(e.getMessage()); 162 } 163 return parser; 164 } 165 } 166 | Popular Tags |