• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2010 Terence Parr
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *  1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *  3. The name of the author may not be used to endorse or promote products
15  *      derived from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 package org.antlr.codegen;
29 
30 import org.antlr.Tool;
31 import org.stringtemplate.v4.ST;
32 import org.antlr.tool.Grammar;
33 
34 public class ActionScriptTarget extends Target {
35 
getTargetCharLiteralFromANTLRCharLiteral( CodeGenerator generator, String literal)36     public String getTargetCharLiteralFromANTLRCharLiteral(
37             CodeGenerator generator,
38             String literal) {
39 
40         int c = Grammar.getCharValueFromGrammarCharLiteral(literal);
41         return String.valueOf(c);
42     }
43 
getTokenTypeAsTargetLabel(CodeGenerator generator, int ttype)44     public String getTokenTypeAsTargetLabel(CodeGenerator generator,
45                                             int ttype) {
46         // use ints for predefined types;
47         // <invalid> <EOR> <DOWN> <UP>
48         if (ttype >= 0 && ttype <= 3) {
49             return String.valueOf(ttype);
50         }
51 
52         String name = generator.grammar.getTokenDisplayName(ttype);
53 
54         // If name is a literal, return the token type instead
55         if (name.charAt(0) == '\'') {
56             return String.valueOf(ttype);
57         }
58 
59         return name;
60     }
61 
62     /**
63      * ActionScript doesn't support Unicode String literals that are considered "illegal"
64      * or are in the surrogate pair ranges.  For example "/uffff" will not encode properly
65      * nor will "/ud800".  To keep things as compact as possible we use the following encoding
66      * if the int is below 255, we encode as hex literal
67      * If the int is between 255 and 0x7fff we use a single unicode literal with the value
68      * If the int is above 0x7fff, we use a unicode literal of 0x80hh, where hh is the high-order
69      * bits followed by \xll where ll is the lower order bits of a 16-bit number.
70      *
71      * Ideally this should be improved at a future date.  The most optimal way to encode this
72      * may be a compressed AMF encoding that is embedded using an Embed tag in ActionScript.
73      *
74      * @param v
75      * @return
76      */
encodeIntAsCharEscape(int v)77     public String encodeIntAsCharEscape(int v) {
78         // encode as hex
79         if ( v<=255 ) {
80 			return "\\x"+ Integer.toHexString(v|0x100).substring(1,3);
81 		}
82         if (v <= 0x7fff) {
83             String hex = Integer.toHexString(v|0x10000).substring(1,5);
84 		    return "\\u"+hex;
85         }
86         if (v > 0xffff) {
87             System.err.println("Warning: character literal out of range for ActionScript target " + v);
88             return "";
89         }
90         StringBuffer buf = new StringBuffer("\\u80");
91         buf.append(Integer.toHexString((v >> 8) | 0x100).substring(1, 3)); // high - order bits
92         buf.append("\\x");
93         buf.append(Integer.toHexString((v & 0xff) | 0x100).substring(1, 3)); // low -order bits
94         return buf.toString();
95     }
96 
97     /** Convert long to two 32-bit numbers separted by a comma.
98      *  ActionScript does not support 64-bit numbers, so we need to break
99      *  the number into two 32-bit literals to give to the Bit.  A number like
100      *  0xHHHHHHHHLLLLLLLL is broken into the following string:
101      *  "0xLLLLLLLL, 0xHHHHHHHH"
102 	 *  Note that the low order bits are first, followed by the high order bits.
103      *  This is to match how the BitSet constructor works, where the bits are
104      *  passed in in 32-bit chunks with low-order bits coming first.
105 	 */
getTarget64BitStringFromValue(long word)106 	public String getTarget64BitStringFromValue(long word) {
107 		StringBuffer buf = new StringBuffer(22); // enough for the two "0x", "," and " "
108 		buf.append("0x");
109         writeHexWithPadding(buf, Integer.toHexString((int)(word & 0x00000000ffffffffL)));
110         buf.append(", 0x");
111         writeHexWithPadding(buf, Integer.toHexString((int)(word >> 32)));
112 
113         return buf.toString();
114 	}
115 
writeHexWithPadding(StringBuffer buf, String digits)116     private void writeHexWithPadding(StringBuffer buf, String digits) {
117        digits = digits.toUpperCase();
118 		int padding = 8 - digits.length();
119 		// pad left with zeros
120 		for (int i=1; i<=padding; i++) {
121 			buf.append('0');
122 		}
123 		buf.append(digits);
124     }
125 
chooseWhereCyclicDFAsGo(Tool tool, CodeGenerator generator, Grammar grammar, ST recognizerST, ST cyclicDFAST)126     protected ST chooseWhereCyclicDFAsGo(Tool tool,
127                                                      CodeGenerator generator,
128                                                      Grammar grammar,
129                                                      ST recognizerST,
130                                                      ST cyclicDFAST) {
131         return recognizerST;
132     }
133 }
134 
135