Beispiel #1
0
        /// <summary>
        /// Retuns a sequence of all keywords usable as a statement in a
        /// particular version of Python.
        /// </summary>
        public static IEnumerable <string> Statement(PythonLanguageVersion version = PythonLanguageVersion.None)
        {
            yield return("assert");

            yield return("break");

            yield return("continue");

            yield return("class");

            yield return("def");

            yield return("del");

            if (version.Is2x())
            {
                yield return("exec");
            }
            yield return("if");

            yield return("elif");

            yield return("except");

            yield return("finally");

            yield return("for");

            yield return("from");

            yield return("global");

            yield return("import");

            if (version.Is3x())
            {
                yield return("nonlocal");
            }
            yield return("pass");

            if (version.Is2x())
            {
                yield return("print");
            }
            yield return("raise");

            yield return("return");

            yield return("try");

            yield return("while");

            yield return("with");

            yield return("yield");
        }
Beispiel #2
0
        public async Task InClassDefinition(Server server, PythonLanguageVersion version)
        {
            var uri = await server.OpenDefaultDocumentAndGetUriAsync("class C(object, parameter=MC): pass");

            (await server.SendCompletion(uri, 0, 7)).Should().HaveNoCompletion();
            if (version.Is2x())
            {
                (await server.SendCompletion(uri, 0, 8)).Should().HaveLabels("object").And.NotContainLabels("metaclass=");
            }
            else
            {
                (await server.SendCompletion(uri, 0, 8)).Should().HaveLabels("metaclass=", "object");
            }

            (await server.SendCompletion(uri, 0, 14)).Should().HaveLabels("any");
            (await server.SendCompletion(uri, 0, 16)).Should().HaveLabels("any");
            (await server.SendCompletion(uri, 0, 28)).Should().HaveLabels("object");
            (await server.SendCompletion(uri, 0, 29)).Should().HaveNoCompletion();
            (await server.SendCompletion(uri, 0, 30)).Should().HaveLabels("any");

            await server.ChangeDefaultDocumentAndGetAnalysisAsync("class D(o");

            (await server.SendCompletion(uri, 0, 7)).Should().HaveNoCompletion();
            (await server.SendCompletion(uri, 0, 8)).Should().HaveLabels("any");

            await server.ChangeDefaultDocumentAndGetAnalysisAsync("class E(metaclass=MC,o): pass");

            (await server.SendCompletion(uri, 0, 21)).Should().HaveLabels("object").And.NotContainLabels("metaclass=");
        }
        public string GetConstantRepr(PythonLanguageVersion version, bool escape8bitStrings = false)
        {
            if (_value == null)
            {
                return("None");
            }
            else if (_value is AsciiString)
            {
                StringBuilder res = new StringBuilder();
                if (!version.Is2x())
                {
                    res.Append("b");
                }
                AppendEscapedString(res, ((AsciiString)_value).String, escape8bitStrings);
                return(res.ToString());
            }
            else if (_value is string)
            {
                StringBuilder res = new StringBuilder();
                if (!version.Is3x())
                {
                    res.Append("u");
                }
                AppendEscapedString(res, (string)_value, escape8bitStrings);
                return(res.ToString());
            }
            else if (_value is Complex)
            {
                Complex n    = (Complex)_value;
                string  real = NegativeZeroAwareToString(n.Real);
                string  imag = NegativeZeroAwareToString(n.Imaginary);
                if (n.Real != 0)
                {
                    if (!imag.StartsWith("-"))
                    {
                        imag = "+" + imag;
                    }
                    return("(" + real + imag + "j)");
                }
                else
                {
                    return(imag + "j");
                }
            }
            else if (_value is BigInteger)
            {
                if (!version.Is3x())
                {
                    return(_value.ToString() + "L");
                }
            }
            else if (_value is double)
            {
                double n = (double)_value;
                string s = NegativeZeroAwareToString(n);
                // If there's no fractional part, and this is not NaN or +-Inf, G format will not include the decimal
                // point. This is okay if we're using scientific notation as this implies float, but if not, add the
                // decimal point to indicate the type, just like Python repr() does.
                if ((n - Math.Truncate(n)) == 0 && !s.Contains("e"))
                {
                    s += ".0";
                }
                return(s);
            }
            else if (_value is IFormattable)
            {
                return(((IFormattable)_value).ToString(null, CultureInfo.InvariantCulture));
            }

            // TODO: We probably need to handle more primitives here
            return(_value.ToString());
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new parser from a seekable stream including scanning the BOM or looking for a # coding: comment to detect the appropriate coding.
        /// </summary>
        public static Parser CreateParser(Stream stream, PythonLanguageVersion version, ParserOptions parserOptions = null) {
            var options = parserOptions ?? ParserOptions.Default;

            var defaultEncoding = version.Is2x() ? PythonAsciiEncoding.Instance : Encoding.UTF8;

            var reader = GetStreamReaderWithEncoding(stream, defaultEncoding, options.ErrorSink);

            return CreateParser(reader, version, options);
        }
Beispiel #5
0
 private Action<Expression> CheckStrOrBytes(PythonLanguageVersion version, string str) {
     return expr => {
         if (version.Is2x()) {
             CheckConstant(ToBytes(str));
         } else {
             CheckConstant(str);
         }
     };
 }
Beispiel #6
0
 /// <summary>
 /// Retuns a sequence of all keywords usable as a statement in a
 /// particular version of Python.
 /// </summary>
 public static IEnumerable<string> Statement(PythonLanguageVersion version = PythonLanguageVersion.None) {
     yield return "assert";
     if (version.IsNone() || version >= PythonLanguageVersion.V35) {
         yield return "async";
         yield return "await";
     }
     yield return "break";
     yield return "continue";
     yield return "class";
     yield return "def";
     yield return "del";
     if (version.IsNone() || version.Is2x()) {
         yield return "exec";
     }
     yield return "if";
     yield return "elif";
     yield return "except";
     yield return "finally";
     yield return "for";
     yield return "from";
     yield return "global";
     yield return "import";
     if (version.IsNone() || version.Is3x()) {
         yield return "nonlocal";
     }
     yield return "pass";
     if (version.IsNone() || version.Is2x()) {
         yield return "print";
     }
     yield return "raise";
     yield return "return";
     yield return "try";
     yield return "while";
     yield return "with";
     yield return "yield";
 }
Beispiel #7
0
        public string GetConstantRepr(PythonLanguageVersion version, bool escape8bitStrings = false) {
            if (_value == null) {
                return "None";
            } else if (_value is AsciiString) {
                StringBuilder res = new StringBuilder();
                if (!version.Is2x()) {
                    res.Append("b");
                }
                AppendEscapedString(res, ((AsciiString)_value).String, escape8bitStrings);
                return res.ToString();
            } else if (_value is string) {
                StringBuilder res = new StringBuilder();
                if (!version.Is3x()) {
                    res.Append("u");
                }
                AppendEscapedString(res, (string)_value, escape8bitStrings);
                return res.ToString();
            } else if (_value is Complex) {
                Complex n = (Complex)_value;
                string real = NegativeZeroAwareToString(n.Real);
                string imag =  NegativeZeroAwareToString(n.Imaginary);
                if (n.Real != 0) {
                    if (!imag.StartsWith("-")) {
                        imag = "+" + imag;
                    }
                    return "(" + real + imag + "j)";
                } else {
                    return imag + "j";
                }
            } else if (_value is BigInteger) {
                if (!version.Is3x()) {
                    return _value.ToString() + "L";
                }
            } else if (_value is double) {
                double n = (double)_value;
                string s = NegativeZeroAwareToString(n);
                // If there's no fractional part, and this is not NaN or +-Inf, G format will not include the decimal
                // point. This is okay if we're using scientific notation as this implies float, but if not, add the
                // decimal point to indicate the type, just like Python repr() does.
                if ((n - Math.Truncate(n)) == 0 && !s.Contains("e")) {
                    s += ".0";
                }
                return s;
            } else if (_value is IFormattable) {
                return ((IFormattable)_value).ToString(null, CultureInfo.InvariantCulture);
            }

            // TODO: We probably need to handle more primitives here
            return _value.ToString();
        }