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 */
28package org.antlr.codegen;
29
30import org.antlr.Tool;
31import org.antlr.analysis.Label;
32import org.antlr.misc.Utils;
33import org.stringtemplate.v4.ST;
34import org.antlr.tool.Grammar;
35
36public class DelphiTarget extends Target
37{
38  public DelphiTarget() {
39    targetCharValueEscape['\n'] = "'#10'";
40    targetCharValueEscape['\r'] = "'#13'";
41    targetCharValueEscape['\t'] = "'#9'";
42    targetCharValueEscape['\b'] = "\\b";
43    targetCharValueEscape['\f'] = "\\f";
44    targetCharValueEscape['\\'] = "\\";
45    targetCharValueEscape['\''] = "''";
46    targetCharValueEscape['"'] = "'";
47  }
48
49  protected ST chooseWhereCyclicDFAsGo(Tool tool,
50                           CodeGenerator generator,
51                           Grammar grammar,
52                           ST recognizerST,
53                           ST cyclicDFAST)
54  {
55    return recognizerST;
56  }
57
58  public String encodeIntAsCharEscape(int v)
59  {
60    if (v <= 127)
61    {
62      String hex1 = Integer.toHexString(v | 0x10000).substring(3, 5);
63      return "'#$" + hex1 + "'";
64    }
65    String hex = Integer.toHexString(v | 0x10000).substring(1, 5);
66    return "'#$" + hex + "'";
67  }
68
69  public String getTargetCharLiteralFromANTLRCharLiteral(
70    CodeGenerator generator,
71    String literal)
72  {
73    StringBuffer buf = new StringBuffer();
74    int c = Grammar.getCharValueFromGrammarCharLiteral(literal);
75    if ( c<Label.MIN_CHAR_VALUE ) {
76      return "0";
77    }
78    // normal char
79    buf.append(c);
80
81    return buf.toString();
82  }
83
84  public String getTargetStringLiteralFromString(String s, boolean quoted) {
85    if ( s==null ) {
86      return null;
87    }
88    StringBuffer buf = new StringBuffer();
89    if ( quoted ) {
90      buf.append('\'');
91    }
92    for (int i=0; i<s.length(); i++) {
93      int c = s.charAt(i);
94      if ( c!='"' && // don't escape double quotes in strings for Delphi
95         c<targetCharValueEscape.length &&
96         targetCharValueEscape[c]!=null )
97      {
98        buf.append(targetCharValueEscape[c]);
99      }
100      else {
101        buf.append((char)c);
102      }
103      if ((i & 127) == 127)
104      {
105        // Concatenate string literals because Delphi doesn't support literals over 255 characters,
106        // and the code editor doesn't support lines over 1023 characters
107        buf.append("\' + \r\n  \'");
108      }
109    }
110    if ( quoted ) {
111      buf.append('\'');
112    }
113    return buf.toString();
114  }
115
116  public String getTargetStringLiteralFromANTLRStringLiteral(
117    CodeGenerator generator,
118    String literal)
119  {
120    literal = Utils.replace(literal,"\\\'","''"); // \' to ' to normalize
121    literal = Utils.replace(literal,"\\r\\n","'#13#10'");
122    literal = Utils.replace(literal,"\\r","'#13'");
123    literal = Utils.replace(literal,"\\n","'#10'");
124    StringBuffer buf = new StringBuffer(literal);
125    buf.setCharAt(0,'\'');
126    buf.setCharAt(literal.length()-1,'\'');
127    return buf.toString();
128  }
129
130  public String getTarget64BitStringFromValue(long word) {
131    int numHexDigits = 8*2;
132    StringBuffer buf = new StringBuffer(numHexDigits+2);
133    buf.append("$");
134    String digits = Long.toHexString(word);
135    digits = digits.toUpperCase();
136    int padding = numHexDigits - digits.length();
137    // pad left with zeros
138    for (int i=1; i<=padding; i++) {
139      buf.append('0');
140    }
141    buf.append(digits);
142    return buf.toString();
143  }
144
145}