001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.test;
029:
030: import junit.framework.TestCase;
031:
032: /** General code generation testing; compilation and/or execution.
033: * These tests are more about avoiding duplicate var definitions
034: * etc... than testing a particular ANTLR feature.
035: */
036: public class TestJavaCodeGeneration extends BaseTest {
037: public void testDupVarDefForPinchedState() {
038: // so->s2 and s0->s3->s1 pinches back to s1
039: // LA3_1, s1 state for DFA 3, was defined twice in similar scope
040: // just wrapped in curlies and it's cool.
041: String grammar = "grammar t;\n" + "a : (| A | B) X Y\n"
042: + " | (| A | B) X Z\n" + " ;\n";
043: boolean found = rawGenerateAndBuildRecognizer("t.g", grammar,
044: "tParser", null, false);
045: boolean expecting = true; // should be ok
046: assertEquals(expecting, found);
047: }
048:
049: public void testLabeledNotSetsInLexer() {
050: // d must be an int
051: String grammar = "lexer grammar t;\n"
052: + "A : d=~('x'|'y') e='0'..'9'\n" + " ; \n";
053: boolean found = rawGenerateAndBuildRecognizer("t.g", grammar,
054: null, "tLexer", false);
055: boolean expecting = true; // should be ok
056: assertEquals(expecting, found);
057: }
058:
059: public void testLabeledSetsInLexer() {
060: // d must be an int
061: String grammar = "grammar T;\n" + "a : A ;\n"
062: + "A : d=('x'|'y') {System.out.println((char)$d);}\n"
063: + " ; \n";
064: String found = execParser("T.g", grammar, "TParser", "TLexer",
065: "a", "x", false);
066: assertEquals("x\n", found);
067: }
068:
069: public void testLabeledRangeInLexer() {
070: // d must be an int
071: String grammar = "grammar T;\n" + "a : A;\n"
072: + "A : d='a'..'z' {System.out.println((char)$d);} \n"
073: + " ; \n";
074: String found = execParser("T.g", grammar, "TParser", "TLexer",
075: "a", "x", false);
076: assertEquals("x\n", found);
077: }
078:
079: public void testLabeledWildcardInLexer() {
080: // d must be an int
081: String grammar = "grammar T;\n" + "a : A;\n"
082: + "A : d=. {System.out.println((char)$d);}\n"
083: + " ; \n";
084: String found = execParser("T.g", grammar, "TParser", "TLexer",
085: "a", "x", false);
086: assertEquals("x\n", found);
087: }
088:
089: public void testSynpredWithPlusLoop() {
090: String grammar = "grammar T; \n" + "a : (('x'+)=> 'x'+)?;\n";
091: boolean found = rawGenerateAndBuildRecognizer("T.g", grammar,
092: "TParser", "TLexer", false);
093: boolean expecting = true; // should be ok
094: assertEquals(expecting, found);
095: }
096:
097: public void testDoubleQuoteEscape() {
098: String grammar = "lexer grammar T; \n" + "A : '\\\\\"';\n"; // this is A : '\\"';
099: boolean found = rawGenerateAndBuildRecognizer("T.g", grammar,
100: null, "TLexer", false);
101: boolean expecting = true; // should be ok
102: assertEquals(expecting, found);
103: }
104:
105: }
|