Ejemplo n.º 1
0
 private void SetNewLine(Location loc)
 {
     startLoc = loc;
     endLoc = loc;
     endLoc.Column++;
 }
Ejemplo n.º 2
0
        private CodeGen CompileModuleInit(CompilerContext context, GlobalSuite gs, TypeGen tg, string moduleName)
        {
            CodeGen init;
            if (!AutoImportAll) {
                init = OutputGenerator.GenerateModuleInitialize(context, gs, tg);
            } else {
                // auto-import all compiled modules, useful for CodeDom scenarios.
                init = OutputGenerator.GenerateModuleInitialize(context, gs, tg, staticTypes, delegate(CodeGen cg) {

                    Location dummyLocation = new Location(1, 1);

                    for (int i = 0; i < sourceFiles.Count; i++) {
                        string otherModName = GetModuleFromFilename(sourceFiles[i]);
                        if (otherModName == moduleName) continue;

                        FromImportStatement stmt = new FromImportStatement(
                            new DottedName(new SymbolId[] { SymbolTable.StringToId(otherModName) }),
                            FromImportStatement.Star, null);
                        stmt.Start = dummyLocation;
                        stmt.End = dummyLocation;
                        stmt.Emit(cg);
                    }

                    // Import the first part of all namespaces in all referenced assemblies

                    // First, determine the set of unique such prefixes
                    Dictionary<string, object> nsPrefixes = new Dictionary<string, object>();
                    foreach (string name in ReferencedAssemblies) {
                        Assembly a = LoadAssembly(name);

                        foreach (Type t in a.GetTypes()) {
                            // We only care about public types
                            if (!t.IsPublic) continue;

                            // Ignore types that don't have a namespace
                            if (t.Namespace == null) continue;

                            string nsPrefix = t.Namespace.Split('.')[0];
                            nsPrefixes[nsPrefix] = null;
                        }
                    }

                    // Import all the uniquer prefixes we found
                    foreach (string nsPrefix in nsPrefixes.Keys) {
                        SymbolId symbolId = SymbolTable.StringToId(nsPrefix);
                        cg.Names.CreateGlobalSlot(symbolId);
                        DottedName dottedName = new DottedName(new SymbolId[] { symbolId });
                        ImportStatement importStmt = new ImportStatement(
                            new DottedName[] { dottedName },
                            new SymbolId[] { SymbolTable.Empty });
                        importStmt.Start = dummyLocation;
                        importStmt.End = dummyLocation;
                        importStmt.Emit(cg);
                    }
                });
            }
            return init;
        }
Ejemplo n.º 3
0
        public static int Compare(Location left, Location right)
        {
            int res = left.line - right.line;
            if (res < 0) return -1;
            if (res > 0) return 1;

            res = left.column - right.column;
            if (res < 0) return -1;
            if (res > 0) return 1;

            return 0;
        }
Ejemplo n.º 4
0
        private void SetIndent(int spaces, StringBuilder chars)
        {
            int current = indent[indentLevel];
            if (spaces == current) {
                return;
            } else if (spaces > current) {
                indent[++indentLevel] = spaces;
                if (indentFormat != null)
                    indentFormat[indentLevel] = chars;
                pendingDedents = -1;
                return;
            } else {
                while (spaces < current) {

                    indentLevel -= 1;
                    pendingDedents += 1;
                    current = indent[indentLevel];
                }
                if (spaces != current) {
                    ReportSyntaxError("unindent does not match any outer indentation level on line " + this.current.Line.ToString(), ErrorCodes.IndentationError);
                }
            }
        }
Ejemplo n.º 5
0
        private Expression FinishSlice(Expression e0, Location start)
        {
            Expression e1 = null;
            Expression e2 = null;
            Token t = PeekToken();

            switch (t.Kind) {
                case TokenKind.Comma:
                case TokenKind.RightBracket:
                    break;
                case TokenKind.Colon:
                    NextToken();
                    e2 = ParseSliceEnd();
                    break;
                default:
                    e1 = ParseTest();
                    if (MaybeEat(TokenKind.Colon)) {
                        e2 = ParseSliceEnd();
                    }
                    break;
            }
            SliceExpression ret = new SliceExpression(e0, e1, e2);
            ret.SetLoc(GetExternal(), start, GetEnd());
            return ret;
        }
Ejemplo n.º 6
0
 internal void ReportSyntaxError(Location start, Location end, string message, int errorCode)
 {
     string lineText = tokenizer.GetRawLineForError(start.Line);
     context.AddError(message, lineText, start.Line, start.Column, end.Line, end.Column, errorCode, Severity.Error);
 }
Ejemplo n.º 7
0
 internal void ReportSyntaxError(Location start, Location end, string message)
 {
     ReportSyntaxError(start, end, message, ErrorCodes.SyntaxError);
 }
Ejemplo n.º 8
0
        private Token PeekToken()
        {
            if (peekedToken != null) return peekedToken;

            savedStart = tokenizer.StartLocation;
            savedEnd = tokenizer.EndLocation;
            savedExternal = tokenizer.ExternalLineLocation;

            Token p = NextToken();
            peekedToken = p;

            return p;
        }
Ejemplo n.º 9
0
        //single_input: Newline | simple_stmt | compound_stmt Newline
        //eval_input: testlist Newline* ENDMARKER
        //file_input: (Newline | stmt)* ENDMARKER
        public Statement ParseFileInput()
        {
            List<Statement> l = new List<Statement>();

            //
            // A future statement must appear near the top of the module.
            // The only lines that can appear before a future statement are:
            // - the module docstring (if any),
            // - comments,
            // - blank lines, and
            // - other future statements.
            //

            while (MaybeEat(TokenKind.NewLine)) ;

            if (PeekToken(TokenKind.Constant)) {
                Statement s = ParseStmt();
                l.Add(s);
                fromFutureAllowed = false;
                if (s is ExpressionStatement) {
                    ConstantExpression ce = ((ExpressionStatement)s).Expression as ConstantExpression;
                    if (ce != null && ce.Value is string) {
                        // doc string
                        fromFutureAllowed = true;
                    }
                }
            }

            while (MaybeEat(TokenKind.NewLine)) ;

            // from __future__
            if (fromFutureAllowed) {
                while (PeekToken(Tokens.KeywordFromToken)) {
                    Statement s = ParseStmt();
                    l.Add(s);
                    if (s is FromImportStatement) {
                        FromImportStatement fis = (FromImportStatement)s;
                        if (!fis.IsFromFuture) {
                            // end of from __future__
                            break;
                        }
                    }
                }
            }

            // the end of from __future__ sequence
            fromFutureAllowed = false;

            while (true) {
                if (MaybeEat(TokenKind.EndOfFile)) break;
                if (MaybeEat(TokenKind.NewLine)) continue;

                Statement s = ParseStmt();
                l.Add(s);
            }

            Statement[] stmts = l.ToArray();

            SuiteStatement ret = new SuiteStatement(stmts);
            Location start = new Location();
            start.Column = start.Line = 1;
            ret.SetLoc(GetExternal(), start, GetEnd());
            return ret;
        }