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 org.antlr.tool.*;
031: import org.antlr.Tool;
032: import org.antlr.stringtemplate.StringTemplateGroup;
033: import org.antlr.stringtemplate.StringTemplate;
034: import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
035: import org.antlr.codegen.CodeGenerator;
036: import org.antlr.codegen.ActionTranslatorLexer;
037:
038: /** Test templates in actions; %... shorthands */
039: public class TestTemplates extends BaseTest {
040: private static final String LINE_SEP = System
041: .getProperty("line.separator");
042:
043: public void testTemplateConstructor() throws Exception {
044: String action = "x = %foo(name={$ID.text});";
045: String expecting = "x = templateLib.getInstanceOf(\"foo\","
046: + LINE_SEP
047: + " new STAttrMap().put(\"name\", ID1.getText()));";
048:
049: ErrorQueue equeue = new ErrorQueue();
050: ErrorManager.setErrorListener(equeue);
051: Grammar g = new Grammar("grammar t;\n" + "options {\n"
052: + " output=template;\n" + "}\n" + "\n" + "a : ID {"
053: + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n");
054: Tool antlr = newTool();
055: CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
056: g.setCodeGenerator(generator);
057: generator.genRecognizer(); // forces load of templates
058: ActionTranslatorLexer translator = new ActionTranslatorLexer(
059: generator, "a", new antlr.CommonToken(
060: ANTLRParser.ACTION, action), 1);
061: String rawTranslation = translator.translate();
062: StringTemplateGroup templates = new StringTemplateGroup(".",
063: AngleBracketTemplateLexer.class);
064: StringTemplate actionST = new StringTemplate(templates,
065: rawTranslation);
066: String found = actionST.toString();
067:
068: assertNoErrors(equeue);
069:
070: assertEquals(expecting, found);
071: }
072:
073: public void testTemplateConstructorNoArgs() throws Exception {
074: String action = "x = %foo();";
075: String expecting = "x = templateLib.getInstanceOf(\"foo\");";
076:
077: ErrorQueue equeue = new ErrorQueue();
078: ErrorManager.setErrorListener(equeue);
079: Grammar g = new Grammar("grammar t;\n" + "options {\n"
080: + " output=template;\n" + "}\n" + "\n" + "a : ID {"
081: + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n");
082: Tool antlr = newTool();
083: CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
084: g.setCodeGenerator(generator);
085: generator.genRecognizer(); // forces load of templates
086: ActionTranslatorLexer translator = new ActionTranslatorLexer(
087: generator, "a", new antlr.CommonToken(
088: ANTLRParser.ACTION, action), 1);
089: String rawTranslation = translator.translate();
090: StringTemplateGroup templates = new StringTemplateGroup(".",
091: AngleBracketTemplateLexer.class);
092: StringTemplate actionST = new StringTemplate(templates,
093: rawTranslation);
094: String found = actionST.toString();
095:
096: assertNoErrors(equeue);
097:
098: assertEquals(expecting, found);
099: }
100:
101: public void testIndirectTemplateConstructor() throws Exception {
102: String action = "x = %({\"foo\"})(name={$ID.text});";
103: String expecting = "x = templateLib.getInstanceOf(\"foo\","
104: + LINE_SEP
105: + " new STAttrMap().put(\"name\", ID1.getText()));";
106:
107: ErrorQueue equeue = new ErrorQueue();
108: ErrorManager.setErrorListener(equeue);
109: Grammar g = new Grammar("grammar t;\n" + "options {\n"
110: + " output=template;\n" + "}\n" + "\n" + "a : ID {"
111: + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n");
112: Tool antlr = newTool();
113: CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
114: g.setCodeGenerator(generator);
115: generator.genRecognizer(); // forces load of templates
116: ActionTranslatorLexer translator = new ActionTranslatorLexer(
117: generator, "a", new antlr.CommonToken(
118: ANTLRParser.ACTION, action), 1);
119: String rawTranslation = translator.translate();
120: StringTemplateGroup templates = new StringTemplateGroup(".",
121: AngleBracketTemplateLexer.class);
122: StringTemplate actionST = new StringTemplate(templates,
123: rawTranslation);
124: String found = actionST.toString();
125:
126: assertNoErrors(equeue);
127:
128: assertEquals(expecting, found);
129: }
130:
131: public void testStringConstructor() throws Exception {
132: String action = "x = %{$ID.text};";
133: String expecting = "x = new StringTemplate(templateLib,ID1.getText());";
134:
135: ErrorQueue equeue = new ErrorQueue();
136: ErrorManager.setErrorListener(equeue);
137: Grammar g = new Grammar("grammar t;\n" + "options {\n"
138: + " output=template;\n" + "}\n" + "\n" + "a : ID {"
139: + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n");
140: Tool antlr = newTool();
141: CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
142: g.setCodeGenerator(generator);
143: generator.genRecognizer(); // forces load of templates
144: ActionTranslatorLexer translator = new ActionTranslatorLexer(
145: generator, "a", new antlr.CommonToken(
146: ANTLRParser.ACTION, action), 1);
147: String rawTranslation = translator.translate();
148: StringTemplateGroup templates = new StringTemplateGroup(".",
149: AngleBracketTemplateLexer.class);
150: StringTemplate actionST = new StringTemplate(templates,
151: rawTranslation);
152: String found = actionST.toString();
153:
154: assertNoErrors(equeue);
155:
156: assertEquals(expecting, found);
157: }
158:
159: public void testSetAttr() throws Exception {
160: String action = "%x.y = z;";
161: String expecting = "(x).setAttribute(\"y\", z);";
162:
163: ErrorQueue equeue = new ErrorQueue();
164: ErrorManager.setErrorListener(equeue);
165: Grammar g = new Grammar("grammar t;\n" + "options {\n"
166: + " output=template;\n" + "}\n" + "\n" + "a : ID {"
167: + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n");
168: Tool antlr = newTool();
169: CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
170: g.setCodeGenerator(generator);
171: generator.genRecognizer(); // forces load of templates
172: ActionTranslatorLexer translator = new ActionTranslatorLexer(
173: generator, "a", new antlr.CommonToken(
174: ANTLRParser.ACTION, action), 1);
175: String rawTranslation = translator.translate();
176: StringTemplateGroup templates = new StringTemplateGroup(".",
177: AngleBracketTemplateLexer.class);
178: StringTemplate actionST = new StringTemplate(templates,
179: rawTranslation);
180: String found = actionST.toString();
181:
182: assertNoErrors(equeue);
183:
184: assertEquals(expecting, found);
185: }
186:
187: public void testSetAttrOfExpr() throws Exception {
188: String action = "%{foo($ID.text).getST()}.y = z;";
189: String expecting = "(foo(ID1.getText()).getST()).setAttribute(\"y\", z);";
190:
191: ErrorQueue equeue = new ErrorQueue();
192: ErrorManager.setErrorListener(equeue);
193: Grammar g = new Grammar("grammar t;\n" + "options {\n"
194: + " output=template;\n" + "}\n" + "\n" + "a : ID {"
195: + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n");
196: Tool antlr = newTool();
197: CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
198: g.setCodeGenerator(generator);
199: generator.genRecognizer(); // forces load of templates
200: ActionTranslatorLexer translator = new ActionTranslatorLexer(
201: generator, "a", new antlr.CommonToken(
202: ANTLRParser.ACTION, action), 1);
203: String rawTranslation = translator.translate();
204: StringTemplateGroup templates = new StringTemplateGroup(".",
205: AngleBracketTemplateLexer.class);
206: StringTemplate actionST = new StringTemplate(templates,
207: rawTranslation);
208: String found = actionST.toString();
209:
210: assertNoErrors(equeue);
211:
212: assertEquals(expecting, found);
213: }
214:
215: public void testCannotHaveSpaceBeforeDot() throws Exception {
216: String action = "%x .y = z;";
217: String expecting = null;
218:
219: ErrorQueue equeue = new ErrorQueue();
220: ErrorManager.setErrorListener(equeue);
221: Grammar g = new Grammar("grammar t;\n" + "options {\n"
222: + " output=template;\n" + "}\n" + "\n" + "a : ID {"
223: + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n");
224: Tool antlr = newTool();
225: CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
226: g.setCodeGenerator(generator);
227: generator.genRecognizer(); // forces load of templates
228:
229: int expectedMsgID = ErrorManager.MSG_INVALID_TEMPLATE_ACTION;
230: Object expectedArg = "%x";
231: GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
232: expectedMsgID, g, null, expectedArg);
233: checkError(equeue, expectedMessage);
234: }
235:
236: public void testCannotHaveSpaceAfterDot() throws Exception {
237: String action = "%x. y = z;";
238: String expecting = null;
239:
240: ErrorQueue equeue = new ErrorQueue();
241: ErrorManager.setErrorListener(equeue);
242: Grammar g = new Grammar("grammar t;\n" + "options {\n"
243: + " output=template;\n" + "}\n" + "\n" + "a : ID {"
244: + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n");
245: Tool antlr = newTool();
246: CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
247: g.setCodeGenerator(generator);
248: generator.genRecognizer(); // forces load of templates
249:
250: int expectedMsgID = ErrorManager.MSG_INVALID_TEMPLATE_ACTION;
251: Object expectedArg = "%x.";
252: GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(
253: expectedMsgID, g, null, expectedArg);
254: checkError(equeue, expectedMessage);
255: }
256:
257: protected void checkError(ErrorQueue equeue,
258: GrammarSemanticsMessage expectedMessage) throws Exception {
259: /*
260: System.out.println(equeue.infos);
261: System.out.println(equeue.warnings);
262: System.out.println(equeue.errors);
263: */
264: Message foundMsg = null;
265: for (int i = 0; i < equeue.errors.size(); i++) {
266: Message m = (Message) equeue.errors.get(i);
267: if (m.msgID == expectedMessage.msgID) {
268: foundMsg = m;
269: }
270: }
271: assertTrue("no error; " + expectedMessage.msgID + " expected",
272: equeue.errors.size() > 0);
273: assertTrue("too many errors; " + equeue.errors, equeue.errors
274: .size() <= 1);
275: assertTrue("couldn't find expected error: "
276: + expectedMessage.msgID, foundMsg != null);
277: assertTrue("error is not a GrammarSemanticsMessage",
278: foundMsg instanceof GrammarSemanticsMessage);
279: assertEquals(expectedMessage.arg, foundMsg.arg);
280: assertEquals(expectedMessage.arg2, foundMsg.arg2);
281: }
282:
283: // S U P P O R T
284: private void assertNoErrors(ErrorQueue equeue) {
285: assertTrue("unexpected errors: " + equeue,
286: equeue.errors.size() == 0);
287: }
288: }
|