Beispiel #1
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral(
            CodeGenerator generator,
            string literal)
        {
            int c = Grammar.GetCharValueFromGrammarCharLiteral(literal);

            return(((char)c).ToString());
        }
Beispiel #2
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator, string literal)
        {
            StringBuilder buf = new StringBuilder();
            int           c   = Grammar.GetCharValueFromGrammarCharLiteral(literal);

            if (c < Label.MIN_CHAR_VALUE)
            {
                return("0");
            }
            // normal char
            buf.Append(c);

            return(buf.ToString());
        }
Beispiel #3
0
        /** 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 override string GetTargetCharLiteralFromANTLRCharLiteral(CodeGenerator codegen, string literal)
        {
            int    c      = Grammar.GetCharValueFromGrammarCharLiteral(literal);
            string prefix = "'";

            if (codegen.grammar.MaxCharValue > 255)
            {
                prefix = "L'";
            }
            else if ((c & 0x80) != 0)           // if in char mode prevent sign extensions
            {
                return("" + c);
            }
            return(prefix + EscapeChar(c) + "'");
        }
Beispiel #4
0
        public override string GetTargetCharLiteralFromANTLRCharLiteral(CodeGenerator generator,
                                                                        string literal)
        {
            StringBuilder buf = new StringBuilder(10);

            int c = Grammar.GetCharValueFromGrammarCharLiteral(literal);

            if (c < Label.MIN_CHAR_VALUE)
            {
                buf.Append("\\x{0000}");
            }
            else if (c < targetCharValueEscape.Length &&
                     targetCharValueEscape[c] != null)
            {
                buf.Append(targetCharValueEscape[c]);
            }
            else if ((c < 0x7F) && !char.IsControl((char)c))
            {
                // normal char
                buf.Append((char)c);
            }
            else
            {
                // must be something unprintable...use \\uXXXX
                // turn on the bit above max "\\uFFFF" value so that we pad with zeros
                // then only take last 4 digits
                string hex = c.ToString("X4");
                buf.Append("\\x{");
                buf.Append(hex);
                buf.Append("}");
            }

            if (buf.ToString().IndexOf('\\') == -1)
            {
                // no need for interpolation, use single quotes
                buf.Insert(0, '\'');
                buf.Append('\'');
            }
            else
            {
                // need string interpolation
                buf.Insert(0, '\"');
                buf.Append('\"');
            }

            return(buf.ToString());
        }
Beispiel #5
0
        /** Convert from an ANTLR char literal found in a grammar file to
         *  an equivalent char literal in the target language.  For most
         *  languages, this means leaving 'x' as 'x'.  Actually, we need
         *  to escape '\u000A' so that it doesn't get converted to \n by
         *  the compiler.  Convert the literal to the char value and then
         *  to an appropriate target char literal.
         *
         *  Expect single quotes around the incoming literal.
         */
        public virtual string GetTargetCharLiteralFromANTLRCharLiteral(
            CodeGenerator generator,
            string literal)
        {
            StringBuilder buf = new StringBuilder();

            buf.Append('\'');
            int c = Grammar.GetCharValueFromGrammarCharLiteral(literal);

            if (c < Label.MIN_CHAR_VALUE)
            {
                return("'\u0000'");
            }

            if (c < targetCharValueEscape.Length &&
                targetCharValueEscape[c] != null)
            {
                buf.Append(targetCharValueEscape[c]);
            }
            else if (c <= 0x7f && !char.IsControl((char)c))
            {
                // normal char
                buf.Append((char)c);
            }
            else
            {
                // must be something unprintable...use \\uXXXX
                // turn on the bit above max "\\uFFFF" value so that we pad with zeros
                // then only take last 4 digits
                string hex = c.ToString("X4");
                buf.Append("\\u");
                buf.Append(hex);
            }

            buf.Append('\'');
            return(buf.ToString());
        }