• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2010 Terence Parr and Alan Condit
4  *  Copyright (c) 2006 Kay Roepke (Objective-C runtime)
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *  3. The name of the author may not be used to endorse or promote products
16  *      derived from this software without specific prior written permission.
17  *
18  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 package org.antlr.codegen;
30 
31 import org.antlr.Tool;
32 import org.antlr.misc.Utils;
33 import org.stringtemplate.v4.ST;
34 import org.antlr.tool.Grammar;
35 
36 import java.io.IOException;
37 
38 public class ObjCTarget extends Target {
genRecognizerHeaderFile(Tool tool, CodeGenerator generator, Grammar grammar, ST headerFileST, String extName)39 	protected void genRecognizerHeaderFile(Tool tool,
40 										   CodeGenerator generator,
41 										   Grammar grammar,
42 										   ST headerFileST,
43 										   String extName)
44 	throws IOException
45 	{
46 		generator.write(headerFileST, grammar.name + Grammar.grammarTypeToFileNameSuffix[grammar.type] + extName);
47 	}
48 
getTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator, String literal)49 	public String getTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator,
50 														   String literal)
51 	{
52 		if  (literal.startsWith("'\\u") ) {
53 			literal = "0x" +literal.substring(3, 7);
54 		} else	{
55 			int c = literal.charAt(1); // TJP
56 			if  (c < 32 || c > 127) {
57 				literal  =  "0x" + Integer.toHexString(c);
58 			}
59 		}
60 
61 		return literal;
62 	}
63 
64 	/** Convert from an ANTLR string literal found in a grammar file to
65 	*  an equivalent string literal in the target language.  For Java, this
66 	*  is the translation 'a\n"' -> "a\n\"".  Expect single quotes
67 	*  around the incoming literal.  Just flip the quotes and replace
68 	*  double quotes with \"
69 	*/
getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal)70 	public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator,
71 															   String literal)
72 	{
73 		literal = Utils.replace(literal,"\"","\\\"");
74 		StringBuffer buf = new StringBuffer(literal);
75 		buf.setCharAt(0,'"');
76 		buf.setCharAt(literal.length()-1,'"');
77 		buf.insert(0,'@');
78 		return buf.toString();
79 	}
80 
81 	/** If we have a label, prefix it with the recognizer's name */
getTokenTypeAsTargetLabel(CodeGenerator generator, int ttype)82 	public String getTokenTypeAsTargetLabel(CodeGenerator generator, int ttype) {
83 		String name = generator.grammar.getTokenDisplayName(ttype);
84 		// If name is a literal, return the token type instead
85 		if ( name.charAt(0)=='\'' ) {
86 			return String.valueOf(ttype);
87 		}
88 		return name;
89 		//return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[generator.grammar.type] + "_" + name;
90 		//return super.getTokenTypeAsTargetLabel(generator, ttype);
91 		//return this.getTokenTextAndTypeAsTargetLabel(generator, null, ttype);
92 	}
93 
94 	/** Target must be able to override the labels used for token types. Sometimes also depends on the token text.*/
getTokenTextAndTypeAsTargetLabel(CodeGenerator generator, String text, int tokenType)95 	public String getTokenTextAndTypeAsTargetLabel(CodeGenerator generator, String text, int tokenType) {
96 		String name = generator.grammar.getTokenDisplayName(tokenType);
97 		// If name is a literal, return the token type instead
98 		if ( name.charAt(0)=='\'' ) {
99 			return String.valueOf(tokenType);
100 		}
101 		String textEquivalent = text == null ? name : text;
102 		if (textEquivalent.charAt(0) >= '0' && textEquivalent.charAt(0) <= '9') {
103 			return textEquivalent;
104 		} else {
105 			return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[generator.grammar.type] + "_" + textEquivalent;
106 		}
107 	}
108 
109 }
110 
111