📄 testrecursivedescentparser.java
字号:
/* * TestRecursiveDescentParser.java * * This work is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2 of the License, * or (at your option) any later version. * * This work is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA * * As a special exception, the copyright holders of this library give * you permission to link this library with independent modules to * produce an executable, regardless of the license terms of these * independent modules, and to copy and distribute the resulting * executable under terms of your choice, provided that you also meet, * for each linked independent module, the terms and conditions of the * license of that module. An independent module is a module which is * not derived from or based on this library. If you modify this * library, you may extend this exception to your version of the * library, but you are not obligated to do so. If you do not wish to * do so, delete this exception statement from your version. * * Copyright (c) 2003 Per Cederberg. All rights reserved. */package net.percederberg.grammatica.parser;import junit.framework.TestCase;/** * A test case for the RecursiveDescentParser class. * * @author Per Cederberg, <per at percederberg dot net> * @version 1.0 */public class TestRecursiveDescentParser extends TestCase { /** * A token constant. */ private static final int T1 = 1001; /** * A token constant. */ private static final int T2 = 1002; /** * A token constant. */ private static final int T3 = 1003; /** * A production constant. */ private static final int P1 = 2001; /** * A production constant. */ private static final int P2 = 2002; /** * A production constant. */ private static final int P3 = 2003; /** * The production pattern variable used in all tests. */ private ProductionPattern pattern; /** * The production pattern alternative variable used in all tests. */ private ProductionPatternAlternative alt; /** * Tests adding an empty pattern to the parser. */ public void testEmptyPattern() { pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); addAlternative(pattern, alt); failAddPattern(createParser(), pattern); } /** * Tests adding a possible empty pattern to the parser. */ public void testPossiblyEmptyPattern() { pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 1, 1); addAlternative(pattern, alt); alt = new ProductionPatternAlternative(); alt.addToken(T2, 0, 1); addAlternative(pattern, alt); failAddPattern(createParser(), pattern); } /** * Tests adding a left-recursive pattern to the parser. */ public void testLeftRecursivePattern() { pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addProduction(P1, 0, 1); alt.addToken(T1, 1, 1); addAlternative(pattern, alt); failAddPattern(createParser(), pattern); } /** * Tests adding a right-recursive pattern to the parser. */ public void testRightRecursivePattern() { pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 1, 1); alt.addProduction(P1, 0, 1); addAlternative(pattern, alt); addPattern(createParser(), pattern); } /** * Tests adding the same pattern twice. */ public void testDuplicatePattern() { Parser parser = createParser(); pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); failAddPattern(parser, pattern); } /** * Tests adding two patterns with the same id. */ public void testPatternCollision() { Parser parser = createParser(); pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); pattern = new ProductionPattern(P1, "P2"); alt = new ProductionPatternAlternative(); alt.addToken(T2, 1, 1); addAlternative(pattern, alt); failAddPattern(parser, pattern); } /** * Tests adding two patterns with the same alternatives. */ public void testIdenticalPatterns() { Parser parser = createParser(); pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); pattern = new ProductionPattern(P2, "P2"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); } /** * Tests a simple grammar loop. */ public void testSimpleGrammarLoop() { Parser parser = createParser(); pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addProduction(P2, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); pattern = new ProductionPattern(P2, "P2"); alt = new ProductionPatternAlternative(); alt.addProduction(P1, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); failPrepareParser(parser); } /** * Tests a complex grammar loop with optional parts and * alternatives. */ public void testComplexGrammarLoop() { Parser parser = createParser(); pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addProduction(P2, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); pattern = new ProductionPattern(P2, "P2"); alt = new ProductionPatternAlternative(); alt.addProduction(P3, 0, 1); alt.addToken(T1, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); pattern = new ProductionPattern(P3, "P3"); alt = new ProductionPatternAlternative(); alt.addToken(T3, 1, 1); addAlternative(pattern, alt); alt = new ProductionPatternAlternative(); alt.addProduction(P1, 0, 1); alt.addToken(T2, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); failPrepareParser(parser); } /** * Tests an unresolvable conflict between two productions. */ public void testProductionConflict() { Parser parser = createParser(); pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addProduction(P2, 1, 1); addAlternative(pattern, alt); alt = new ProductionPatternAlternative(); alt.addProduction(P3, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); pattern = new ProductionPattern(P2, "P2"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 0, -1); alt.addToken(T2, 1, 1); addAlternative(pattern, alt); addPattern(parser, pattern); pattern = new ProductionPattern(P3, "P3"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 1, 1); alt.addProduction(P3, 0, 1); addAlternative(pattern, alt); addPattern(parser, pattern); failPrepareParser(parser); } /** * Tests an unresolvable conflict between two production * alternatives. */ public void testAlternativeConflict() { Parser parser = createParser(); pattern = new ProductionPattern(P1, "P1"); alt = new ProductionPatternAlternative(); alt.addToken(T1, 0, -1); alt.addToken(T2, 1, 1); addAlternative(pattern, alt); alt = new ProductionPatternAlternative(); alt.addToken(T1, 1, -1); alt.addToken(T3, 1, 1); addAlternative(pattern, alt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -