/** Converts a String into a representation that can be use as a literal * when surrounded by double-quotes. * * Used for escaping semantic predicate strings for exceptions. * * @param s The String to be changed into a literal */ public String escapeString(String s) { StringBuffer retval = new StringBuffer(); for (int i = 0; i < s.length(); i++) { retval.append(escapeChar(s.charAt(i))); } return retval.toString(); }
/** Convert from an ANTLR string literal found in a grammar file to * an equivalent string literal in the target language. For Java, this * is the identify translation; i.e., "\"\n" -> "\"\n". Most languages * will be able to use this 1-to-1 mapping. Expect double quotes * around the incoming literal. * Depending on the charvocabulary the string should be prefixed with a 'L' */ public String getTargetStringLiteralFromANTLRStringLiteral( CodeGenerator codegen, String literal) { StringBuffer buf = Grammar.getUnescapedStringFromGrammarStringLiteral(literal); String prefix = "\""; if( codegen.grammar.getMaxCharValue() > 255 ) prefix = "L\""; return prefix+escapeString(buf.toString())+"\""; } /** Character constants get truncated to this value.
/** Convert from an ANTLR string literal found in a grammar file to * an equivalent string literal in the target language. For Java, this * is the identify translation; i.e., "\"\n" -> "\"\n". Most languages * will be able to use this 1-to-1 mapping. Expect double quotes * around the incoming literal. * Depending on the charvocabulary the string should be prefixed with a 'L' */ public String getTargetStringLiteralFromANTLRStringLiteral( CodeGenerator codegen, String literal) { StringBuffer buf = Grammar.getUnescapedStringFromGrammarStringLiteral(literal); String prefix = "\""; if( codegen.grammar.getMaxCharValue() > 255 ) prefix = "L\""; return prefix+escapeString(buf.toString())+"\""; } /** Character constants get truncated to this value.
/** Converts a String into a representation that can be use as a literal * when surrounded by double-quotes. * * Used for escaping semantic predicate strings for exceptions. * * @param s The String to be changed into a literal */ public String escapeString(String s) { StringBuffer retval = new StringBuffer(); for (int i = 0; i < s.length(); i++) { retval.append(escapeChar(s.charAt(i))); } return retval.toString(); }
/** Convert from an ANTLR char literal found in a grammar file to * an equivalent char literal in the target language. For Java, this * is the identify translation; i.e., '\n' -> '\n'. Most languages * will be able to use this 1-to-1 mapping. Expect single quotes * around the incoming literal. * Depending on the charvocabulary the charliteral should be prefixed with a 'L' */ public String getTargetCharLiteralFromANTLRCharLiteral( CodeGenerator codegen, String literal) { int c = Grammar.getCharValueFromGrammarCharLiteral(literal); String prefix = "'"; if( codegen.grammar.getMaxCharValue() > 255 ) prefix = "L'"; else if( (c & 0x80) != 0 ) // if in char mode prevent sign extensions return ""+c; return prefix+escapeChar(c)+"'"; }
/** Convert from an ANTLR char literal found in a grammar file to * an equivalent char literal in the target language. For Java, this * is the identify translation; i.e., '\n' -> '\n'. Most languages * will be able to use this 1-to-1 mapping. Expect single quotes * around the incoming literal. * Depending on the charvocabulary the charliteral should be prefixed with a 'L' */ public String getTargetCharLiteralFromANTLRCharLiteral( CodeGenerator codegen, String literal) { int c = Grammar.getCharValueFromGrammarCharLiteral(literal); String prefix = "'"; if( codegen.grammar.getMaxCharValue() > 255 ) prefix = "L'"; else if( (c & 0x80) != 0 ) // if in char mode prevent sign extensions return ""+c; return prefix+escapeChar(c)+"'"; }