Esempio n. 1
0
        public PythonAst(Statement body, bool isModule, PythonLanguageFeatures languageFeatures, bool printExpressions) {
            ContractUtils.RequiresNotNull(body, "body");

            _body = body;
            _isModule = isModule;
            _printExpressions = printExpressions;
            _languageFeatures = languageFeatures;
        }
Esempio n. 2
0
        private Parser(Tokenizer tokenizer, ErrorSink errorSink, ParserSink parserSink, PythonLanguageFeatures languageFeatures) {
            ContractUtils.RequiresNotNull(tokenizer, "tokenizer");
            ContractUtils.RequiresNotNull(errorSink, "errorSink");
            ContractUtils.RequiresNotNull(parserSink, "parserSink");

            tokenizer.ErrorSink = new TokenizerErrorSink(this);

            _tokenizer = tokenizer;
            _errors = errorSink;
            _sink = parserSink;

            Reset(tokenizer.SourceUnit, languageFeatures);
        }
Esempio n. 3
0
        // 'from' relative_module 'import' identifier ['as' name] (',' identifier ['as' name]) *
        // 'from' relative_module 'import' '(' identifier ['as' name] (',' identifier ['as' name])* [','] ')'        
        // 'from' module 'import' "*"                                        
        private FromImportStatement ParseFromImportStmt() {
            Eat(TokenKind.KeywordFrom);
            SourceLocation start = GetStart();
            ModuleName dname = ParseRelativeModuleName();

            Eat(TokenKind.KeywordImport);

            bool ateParen = MaybeEat(TokenKind.LeftParenthesis);

            SymbolId[] names;
            SymbolId[] asNames;
            bool fromFuture = false;

            if (MaybeEat(TokenKind.Multiply)) {
                names = FromImportStatement.Star;
                asNames = null;

                if (dname is RelativeModuleName) {
                    ReportSyntaxError("'import *' not allowed with 'from .'");                         
                }
            } else {
                List<SymbolId> l = new List<SymbolId>();
                List<SymbolId> las = new List<SymbolId>();

                if (MaybeEat(TokenKind.LeftParenthesis)) {
                    ParseAsNameList(l, las);
                    Eat(TokenKind.RightParenthesis);
                } else {
                    ParseAsNameList(l, las);
                }
                names = l.ToArray();
                asNames = las.ToArray();
            }

            // Process from __future__ statement

            if (dname.Names.Count == 1 && dname.Names[0] == Symbols.Future) {
                if (!_fromFutureAllowed) {
                    ReportSyntaxError(IronPython.Resources.MisplacedFuture);
                }
                if (names == FromImportStatement.Star) {
                    ReportSyntaxError(IronPython.Resources.NoFutureStar);
                }
                fromFuture = true;
                foreach (SymbolId name in names) {
                    if (name == Symbols.Division) {
                        _languageFeatures |= PythonLanguageFeatures.TrueDivision;
                    } else if (name == Symbols.WithStmt) {
                        _languageFeatures |= PythonLanguageFeatures.AllowWithStatement;
                    } else if (name == Symbols.AbsoluteImport) {
                        _languageFeatures |= PythonLanguageFeatures.AbsoluteImports;
                    } else if (name == Symbols.PrintFunction && Python26) {
                        _languageFeatures |= PythonLanguageFeatures.PrintFunction;
                        _tokenizer.PrintFunction = true;
                    } else if (name == Symbols.UnicodeLiterals && Python26) {
                        // nop for us, just ignore it...
                    } else if (name == Symbols.NestedScopes) {
                    } else if (name == Symbols.Generators) {
                    } else {
                        string strName = SymbolTable.IdToString(name);
                        fromFuture = false;

                        if (strName != "braces") {
                            ReportSyntaxError(IronPython.Resources.UnknownFutureFeature + strName);
                        } else {
                            // match CPython error message
                            ReportSyntaxError(IronPython.Resources.NotAChance);
                        }
                    }
                }
            }

            if (ateParen) {
                Eat(TokenKind.RightParenthesis);
            }

            FromImportStatement ret = new FromImportStatement(dname, names, asNames, fromFuture, AbsoluteImports);
            ret.SetLoc(start, GetEnd());
            return ret;
        }
Esempio n. 4
0
        public void Reset(SourceUnit sourceUnit, PythonLanguageFeatures languageFeatures) {
            ContractUtils.RequiresNotNull(sourceUnit, "sourceUnit");

            _sourceUnit = sourceUnit;
            _languageFeatures = languageFeatures;
            _token = new TokenWithSpan();
            _lookahead = new TokenWithSpan();
            _fromFutureAllowed = true;
            _functions = null;
            _privatePrefix = null;

            _parsingStarted = false;
            _errorCode = 0;
        }
Esempio n. 5
0
 /// <summary>
 /// Creates a new PythonCompilerOptions with the specified language features enabled.
 /// </summary>
 public PythonCompilerOptions(PythonLanguageFeatures features) {
     _languageFeatures = features;
 }
Esempio n. 6
0
        /// <summary>
        /// Copy constructor.
        /// </summary>
        internal PythonModule(Scope/*!*/ scope, PythonModule/*!*/ module)
            : base(scope) {
            Assert.NotNull(module);

            _features = module.LanguageFeatures;
        }
Esempio n. 7
0
 protected override void ModuleReloading() {
     base.ModuleReloading();
     _features = PythonLanguageFeatures.Default;
     _showCls = false;
 }