1 23 24 package net.percederberg.grammatica.test; 25 26 import java.io.Reader ; 27 28 import net.percederberg.grammatica.parser.Analyzer; 29 import net.percederberg.grammatica.parser.ParserCreationException; 30 import net.percederberg.grammatica.parser.ProductionPattern; 31 import net.percederberg.grammatica.parser.ProductionPatternAlternative; 32 import net.percederberg.grammatica.parser.RecursiveDescentParser; 33 34 40 class RegexpParser extends RecursiveDescentParser { 41 42 45 private static final int SUBPRODUCTION_1 = 3001; 46 47 50 private static final int SUBPRODUCTION_2 = 3002; 51 52 60 public RegexpParser(Reader in) throws ParserCreationException { 61 super(new RegexpTokenizer(in)); 62 createPatterns(); 63 } 64 65 74 public RegexpParser(Reader in, Analyzer analyzer) 75 throws ParserCreationException { 76 77 super(new RegexpTokenizer(in), analyzer); 78 createPatterns(); 79 } 80 81 87 private void createPatterns() throws ParserCreationException { 88 ProductionPattern pattern; 89 ProductionPatternAlternative alt; 90 91 pattern = new ProductionPattern(RegexpConstants.EXPR, 92 "Expr"); 93 alt = new ProductionPatternAlternative(); 94 alt.addProduction(RegexpConstants.TERM, 1, 1); 95 alt.addProduction(SUBPRODUCTION_1, 0, 1); 96 pattern.addAlternative(alt); 97 addPattern(pattern); 98 99 pattern = new ProductionPattern(RegexpConstants.TERM, 100 "Term"); 101 alt = new ProductionPatternAlternative(); 102 alt.addProduction(RegexpConstants.FACT, 1, -1); 103 pattern.addAlternative(alt); 104 addPattern(pattern); 105 106 pattern = new ProductionPattern(RegexpConstants.FACT, 107 "Fact"); 108 alt = new ProductionPatternAlternative(); 109 alt.addProduction(RegexpConstants.ATOM, 1, 1); 110 alt.addProduction(RegexpConstants.ATOM_MODIFIER, 0, 1); 111 pattern.addAlternative(alt); 112 addPattern(pattern); 113 114 pattern = new ProductionPattern(RegexpConstants.ATOM, 115 "Atom"); 116 alt = new ProductionPatternAlternative(); 117 alt.addToken(RegexpConstants.CHAR, 1, 1); 118 pattern.addAlternative(alt); 119 alt = new ProductionPatternAlternative(); 120 alt.addToken(RegexpConstants.NUMBER, 1, 1); 121 pattern.addAlternative(alt); 122 alt = new ProductionPatternAlternative(); 123 alt.addToken(RegexpConstants.COMMA, 1, 1); 124 pattern.addAlternative(alt); 125 alt = new ProductionPatternAlternative(); 126 alt.addToken(RegexpConstants.DOT, 1, 1); 127 pattern.addAlternative(alt); 128 alt = new ProductionPatternAlternative(); 129 alt.addToken(RegexpConstants.LEFT_PAREN, 1, 1); 130 alt.addProduction(RegexpConstants.EXPR, 1, 1); 131 alt.addToken(RegexpConstants.RIGHT_PAREN, 1, 1); 132 pattern.addAlternative(alt); 133 alt = new ProductionPatternAlternative(); 134 alt.addToken(RegexpConstants.LEFT_BRACKET, 1, 1); 135 alt.addProduction(RegexpConstants.CHARACTER_SET, 1, 1); 136 alt.addToken(RegexpConstants.RIGHT_BRACKET, 1, 1); 137 pattern.addAlternative(alt); 138 addPattern(pattern); 139 140 pattern = new ProductionPattern(RegexpConstants.ATOM_MODIFIER, 141 "AtomModifier"); 142 alt = new ProductionPatternAlternative(); 143 alt.addToken(RegexpConstants.QUESTION, 1, 1); 144 alt.addToken(RegexpConstants.QUESTION, 0, 1); 145 pattern.addAlternative(alt); 146 alt = new ProductionPatternAlternative(); 147 alt.addToken(RegexpConstants.ASTERISK, 1, 1); 148 alt.addToken(RegexpConstants.QUESTION, 0, 1); 149 pattern.addAlternative(alt); 150 alt = new ProductionPatternAlternative(); 151 alt.addToken(RegexpConstants.PLUS, 1, 1); 152 alt.addToken(RegexpConstants.QUESTION, 0, 1); 153 pattern.addAlternative(alt); 154 alt = new ProductionPatternAlternative(); 155 alt.addToken(RegexpConstants.LEFT_BRACE, 1, 1); 156 alt.addToken(RegexpConstants.NUMBER, 1, 1); 157 alt.addProduction(SUBPRODUCTION_2, 0, 1); 158 alt.addToken(RegexpConstants.RIGHT_BRACE, 1, 1); 159 alt.addToken(RegexpConstants.QUESTION, 0, 1); 160 pattern.addAlternative(alt); 161 addPattern(pattern); 162 163 pattern = new ProductionPattern(RegexpConstants.CHARACTER_SET, 164 "CharacterSet"); 165 alt = new ProductionPatternAlternative(); 166 alt.addProduction(RegexpConstants.CHARACTER, 1, -1); 167 pattern.addAlternative(alt); 168 addPattern(pattern); 169 170 pattern = new ProductionPattern(RegexpConstants.CHARACTER, 171 "Character"); 172 alt = new ProductionPatternAlternative(); 173 alt.addToken(RegexpConstants.CHAR, 1, 1); 174 pattern.addAlternative(alt); 175 alt = new ProductionPatternAlternative(); 176 alt.addToken(RegexpConstants.NUMBER, 1, 1); 177 pattern.addAlternative(alt); 178 alt = new ProductionPatternAlternative(); 179 alt.addToken(RegexpConstants.COMMA, 1, 1); 180 pattern.addAlternative(alt); 181 alt = new ProductionPatternAlternative(); 182 alt.addToken(RegexpConstants.DOT, 1, 1); 183 pattern.addAlternative(alt); 184 alt = new ProductionPatternAlternative(); 185 alt.addToken(RegexpConstants.VERTICAL_BAR, 1, 1); 186 pattern.addAlternative(alt); 187 alt = new ProductionPatternAlternative(); 188 alt.addToken(RegexpConstants.PLUS, 1, 1); 189 pattern.addAlternative(alt); 190 alt = new ProductionPatternAlternative(); 191 alt.addToken(RegexpConstants.ASTERISK, 1, 1); 192 pattern.addAlternative(alt); 193 alt = new ProductionPatternAlternative(); 194 alt.addToken(RegexpConstants.QUESTION, 1, 1); 195 pattern.addAlternative(alt); 196 alt = new ProductionPatternAlternative(); 197 alt.addToken(RegexpConstants.LEFT_BRACE, 1, 1); 198 pattern.addAlternative(alt); 199 alt = new ProductionPatternAlternative(); 200 alt.addToken(RegexpConstants.RIGHT_BRACE, 1, 1); 201 pattern.addAlternative(alt); 202 alt = new ProductionPatternAlternative(); 203 alt.addToken(RegexpConstants.LEFT_PAREN, 1, 1); 204 pattern.addAlternative(alt); 205 alt = new ProductionPatternAlternative(); 206 alt.addToken(RegexpConstants.RIGHT_PAREN, 1, 1); 207 pattern.addAlternative(alt); 208 alt = new ProductionPatternAlternative(); 209 alt.addToken(RegexpConstants.LEFT_BRACKET, 1, 1); 210 pattern.addAlternative(alt); 211 addPattern(pattern); 212 213 pattern = new ProductionPattern(SUBPRODUCTION_1, 214 "Subproduction1"); 215 pattern.setSynthetic(true); 216 alt = new ProductionPatternAlternative(); 217 alt.addToken(RegexpConstants.VERTICAL_BAR, 1, 1); 218 alt.addProduction(RegexpConstants.EXPR, 1, 1); 219 pattern.addAlternative(alt); 220 addPattern(pattern); 221 222 pattern = new ProductionPattern(SUBPRODUCTION_2, 223 "Subproduction2"); 224 pattern.setSynthetic(true); 225 alt = new ProductionPatternAlternative(); 226 alt.addToken(RegexpConstants.COMMA, 1, 1); 227 alt.addToken(RegexpConstants.NUMBER, 0, 1); 228 pattern.addAlternative(alt); 229 addPattern(pattern); 230 } 231 } 232 | Popular Tags |