• 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 {
39 	@Override
genRecognizerHeaderFile(Tool tool, CodeGenerator generator, Grammar grammar, ST headerFileST, String extName)40 	protected void genRecognizerHeaderFile(Tool tool,
41 										   CodeGenerator generator,
42 										   Grammar grammar,
43 										   ST headerFileST,
44 										   String extName)
45 	throws IOException
46 	{
47 		generator.write(headerFileST, grammar.name + Grammar.grammarTypeToFileNameSuffix[grammar.type] + extName);
48 	}
49 
50 	@Override
getTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator, String literal)51 	public String getTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator,
52 														   String literal)
53 	{
54 		if  (literal.startsWith("'\\u") ) {
55 			literal = "0x" +literal.substring(3, 7);
56 		} else	{
57 			int c = literal.charAt(1); // TJP
58 			if  (c < 32 || c > 127) {
59 				literal  =  "0x" + Integer.toHexString(c);
60 			}
61 		}
62 
63 		return literal;
64 	}
65 
66 	/** Convert from an ANTLR string literal found in a grammar file to
67 	*  an equivalent string literal in the target language.  For Java, this
68 	*  is the translation 'a\n"' &rarr; "a\n\"".  Expect single quotes
69 	*  around the incoming literal.  Just flip the quotes and replace
70 	*  double quotes with \"
71 	*/
72 	@Override
getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator, String literal)73 	public String getTargetStringLiteralFromANTLRStringLiteral(CodeGenerator generator,
74 															   String literal)
75 	{
76 		literal = Utils.replace(literal,"\"","\\\"");
77 		StringBuilder buf = new StringBuilder(literal);
78 		buf.setCharAt(0,'"');
79 		buf.setCharAt(literal.length()-1,'"');
80 		buf.insert(0,'@');
81 		return buf.toString();
82 	}
83 
84 	/** If we have a label, prefix it with the recognizer's name */
85 	@Override
getTokenTypeAsTargetLabel(CodeGenerator generator, int ttype)86 	public String getTokenTypeAsTargetLabel(CodeGenerator generator, int ttype) {
87 		String name = generator.grammar.getTokenDisplayName(ttype);
88 		// If name is a literal, return the token type instead
89 		if ( name.charAt(0)=='\'' ) {
90 			return String.valueOf(ttype);
91 		}
92 		return name;
93 		//return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[generator.grammar.type] + "_" + name;
94 		//return super.getTokenTypeAsTargetLabel(generator, ttype);
95 		//return this.getTokenTextAndTypeAsTargetLabel(generator, null, ttype);
96 	}
97 
98 	/** 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)99 	public String getTokenTextAndTypeAsTargetLabel(CodeGenerator generator, String text, int tokenType) {
100 		String name = generator.grammar.getTokenDisplayName(tokenType);
101 		// If name is a literal, return the token type instead
102 		if ( name.charAt(0)=='\'' ) {
103 			return String.valueOf(tokenType);
104 		}
105 		String textEquivalent = text == null ? name : text;
106 		if (textEquivalent.charAt(0) >= '0' && textEquivalent.charAt(0) <= '9') {
107 			return textEquivalent;
108 		} else {
109 			return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[generator.grammar.type] + "_" + textEquivalent;
110 		}
111 	}
112 
113 }
114 
115