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 36 @Override getTargetCharLiteralFromANTLRCharLiteral( CodeGenerator generator, String literal)37 public String getTargetCharLiteralFromANTLRCharLiteral( 38 CodeGenerator generator, 39 String literal) { 40 41 int c = Grammar.getCharValueFromGrammarCharLiteral(literal); 42 return String.valueOf(c); 43 } 44 45 @Override getTokenTypeAsTargetLabel(CodeGenerator generator, int ttype)46 public String getTokenTypeAsTargetLabel(CodeGenerator generator, 47 int ttype) { 48 // use ints for predefined types; 49 // <invalid> <EOR> <DOWN> <UP> 50 if (ttype >= 0 && ttype <= 3) { 51 return String.valueOf(ttype); 52 } 53 54 String name = generator.grammar.getTokenDisplayName(ttype); 55 56 // If name is a literal, return the token type instead 57 if (name.charAt(0) == '\'') { 58 return String.valueOf(ttype); 59 } 60 61 return name; 62 } 63 64 /** 65 * ActionScript doesn't support Unicode String literals that are considered "illegal" 66 * or are in the surrogate pair ranges. For example "/uffff" will not encode properly 67 * nor will "/ud800". To keep things as compact as possible we use the following encoding 68 * if the int is below 255, we encode as hex literal 69 * If the int is between 255 and 0x7fff we use a single unicode literal with the value 70 * If the int is above 0x7fff, we use a unicode literal of 0x80hh, where hh is the high-order 71 * bits followed by \xll where ll is the lower order bits of a 16-bit number. 72 * 73 * Ideally this should be improved at a future date. The most optimal way to encode this 74 * may be a compressed AMF encoding that is embedded using an Embed tag in ActionScript. 75 * 76 * @param v 77 */ 78 @Override encodeIntAsCharEscape(int v)79 public String encodeIntAsCharEscape(int v) { 80 // encode as hex 81 if ( v<=255 ) { 82 return "\\x"+ Integer.toHexString(v|0x100).substring(1,3); 83 } 84 if (v <= 0x7fff) { 85 String hex = Integer.toHexString(v|0x10000).substring(1,5); 86 return "\\u"+hex; 87 } 88 if (v > 0xffff) { 89 System.err.println("Warning: character literal out of range for ActionScript target " + v); 90 return ""; 91 } 92 StringBuilder buf = new StringBuilder("\\u80"); 93 buf.append(Integer.toHexString((v >> 8) | 0x100).substring(1, 3)); // high - order bits 94 buf.append("\\x"); 95 buf.append(Integer.toHexString((v & 0xff) | 0x100).substring(1, 3)); // low -order bits 96 return buf.toString(); 97 } 98 99 /** Convert long to two 32-bit numbers separted by a comma. 100 * ActionScript does not support 64-bit numbers, so we need to break 101 * the number into two 32-bit literals to give to the Bit. A number like 102 * 0xHHHHHHHHLLLLLLLL is broken into the following string: 103 * "0xLLLLLLLL, 0xHHHHHHHH" 104 * Note that the low order bits are first, followed by the high order bits. 105 * This is to match how the BitSet constructor works, where the bits are 106 * passed in in 32-bit chunks with low-order bits coming first. 107 */ 108 @Override getTarget64BitStringFromValue(long word)109 public String getTarget64BitStringFromValue(long word) { 110 StringBuffer buf = new StringBuffer(22); // enough for the two "0x", "," and " " 111 buf.append("0x"); 112 writeHexWithPadding(buf, Integer.toHexString((int)(word & 0x00000000ffffffffL))); 113 buf.append(", 0x"); 114 writeHexWithPadding(buf, Integer.toHexString((int)(word >> 32))); 115 116 return buf.toString(); 117 } 118 writeHexWithPadding(StringBuffer buf, String digits)119 private void writeHexWithPadding(StringBuffer buf, String digits) { 120 digits = digits.toUpperCase(); 121 int padding = 8 - digits.length(); 122 // pad left with zeros 123 for (int i=1; i<=padding; i++) { 124 buf.append('0'); 125 } 126 buf.append(digits); 127 } 128 chooseWhereCyclicDFAsGo(Tool tool, CodeGenerator generator, Grammar grammar, ST recognizerST, ST cyclicDFAST)129 protected ST chooseWhereCyclicDFAsGo(Tool tool, 130 CodeGenerator generator, 131 Grammar grammar, 132 ST recognizerST, 133 ST cyclicDFAST) { 134 return recognizerST; 135 } 136 } 137 138