Esempio n. 1
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            try
            {
                switch (sourceUnit.Kind)
                {
                    case SourceCodeKind.SingleStatement:
                    case SourceCodeKind.Expression:
                    case SourceCodeKind.AutoDetect:
                    case SourceCodeKind.InteractiveCode:
                        return new TotemScriptCode(
                            engine, engine.ParseExprToLambda(sourceUnit),
                            sourceUnit);

                    //case SourceCodeKind.Statements:
                    //case SourceCodeKind.File:
                    //    return new TotemScriptCode(
                    //        engine, engine.ParseFileToLambda(sourceUnit.Path, sourceUnit.GetCode()),
                    //        sourceUnit);

                    default:
                        throw Assert.Unreachable;
                }
            }
            catch (Exception e)
            {
                // Real language implementation would have a specific type
                // of exception.  Also, they would pass errorSink down into
                // the parser and add messages while doing tighter error
                // recovery and continuing to parse.
                errorSink.Add(sourceUnit, e.Message, SourceSpan.None, 0,
                              Severity.FatalError);
                return null;
            }
        }
Esempio n. 2
0
 public Tokenizer(ErrorSink errorSink) {
     _errors = errorSink;
     _verbatim = false;
     _state = new State(null);
     _dontImplyDedent = true;
     _python26 = true;
 }
Esempio n. 3
0
 public Tokenizer() {
     _errors = ErrorSink.Null;
     _verbatim = true;
     _state = new State(null);
     _dontImplyDedent = true;
     _python26 = true;
 }
Esempio n. 4
0
        public Tokenizer(ErrorSink errorSink, bool verbatim, bool dontImplyDedent) {
            ContractUtils.RequiresNotNull(errorSink, "errorSink");

            _errors = errorSink;
            _verbatim = verbatim;
            _state = new State(null);
            _dontImplyDedent = dontImplyDedent;
        }
Esempio n. 5
0
        public Tokenizer(ErrorSink errorSink, PythonCompilerOptions options) {
            ContractUtils.RequiresNotNull(errorSink, "errorSink");
            ContractUtils.RequiresNotNull(options, "options");

            _errors = errorSink;
            _verbatim = options.Verbatim;
            _state = new State(null);
            _dontImplyDedent = options.DontImplyDedent;
            _printFunction = options.PrintFunction;
            _python26 = options.Python26;
        }
Esempio n. 6
0
        private Parser(Tokenizer tokenizer, ErrorSink errorSink, ParserSink parserSink, ModuleOptions 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. 7
0
        public Parser(ILexer lexer, ErrorSink errorSink = null, LuaCompilerOptions options = null)
        {
            Contract.Requires(lexer != null);

            _lexer = lexer;
            _errors = errorSink ?? ErrorSink.Default;
            _options = options ?? LuaCompilerOptions.Default;

            // Debug: used to display the sequence of tokens that were read
            //TokenSink = (t, s) => Debug.Print("{0,-12} {1,-10} {2,-10} {3}", t.Symbol, s.Start, s.End, t.Lexeme);

            // initialise the token management variables
            Current = GetNextToken();
            Next = GetNextToken();
        }
Esempio n. 8
0
        private Parser(CompilerContext context, Tokenizer tokenizer, ErrorSink errorSink, ParserSink parserSink, ModuleOptions languageFeatures) {
            ContractUtils.RequiresNotNull(tokenizer, "tokenizer");
            ContractUtils.RequiresNotNull(errorSink, "errorSink");
            ContractUtils.RequiresNotNull(parserSink, "parserSink");

            tokenizer.ErrorSink = new TokenizerErrorSink(this);

            _tokenizer = tokenizer;
            _errors = errorSink;
            if (parserSink != ParserSink.Null) {
                _sink = parserSink;
            }
            _context = context;

            Reset(tokenizer.SourceUnit, languageFeatures);
        }
Esempio n. 9
0
        private TotemParser(CompilerContext context, TotemTokenizer tokenizer, ErrorSink errorSink, ParserSink parserSink)
        {
            ContractUtils.RequiresNotNull(tokenizer, "tokenizer");
            ContractUtils.RequiresNotNull(errorSink, "errorSink");
            ContractUtils.RequiresNotNull(parserSink, "parserSink");

            tokenizer.ErrorSink = new TokenizerErrorSink(this);

            _tokenizer = tokenizer;
            _errors = errorSink;
            if (parserSink != ParserSink.Null)
                _sink = parserSink;
            _context = context;

            Reset(tokenizer.SourceUnit);
        }
Esempio n. 10
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            SilverLexer lexer = new SilverLexer("source", sourceUnit.GetCode());
            CommonTokenStream stream = new CommonTokenStream(lexer.Queue);
            SilverParser parser = new SilverParser(stream);
            lexer.SetLines (parser);
            parser.SourceUnit = sourceUnit;

            var res = parser.program();

            if (res != null) {
                Expression mainBlock = AstExpression.Block (res);
                return new SilverScriptCode(mainBlock, sourceUnit);
            } else {
                throw new SyntaxErrorException("Syntax error", sourceUnit, SourceSpan.None, 0, Severity.Error);
            }
        }
Esempio n. 11
0
 public ErrorCounter(ErrorSink /*!*/ sink)
 {
     ContractUtils.RequiresNotNull(sink, "sink");
     _sink = sink;
 }
Esempio n. 12
0
 public CompilerContext(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
     : this(sourceUnit, options, errorSink, null)
 {
 }
Esempio n. 13
0
 public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
 {
     throw new NotImplementedException();
 }
Esempio n. 14
0
 public JavascriptGenerator(SourceUnit src, ErrorSink sink)
     : base()
 {
     this.src = src;
     this.sink = sink;
 }
Esempio n. 15
0
        /// <summary>
        /// Compiles a list of source units into a single module.
        /// <c>scope</c> can be <c>null</c>.
        /// <c>options</c> can be <c>null</c>.
        /// <c>errorSink</c> can be <c>null</c>.
        /// </summary>
        public ScriptModule CompileModule(string name, ScriptModuleKind kind, Scope scope, CompilerOptions options, ErrorSink errorSink,
                                          params SourceUnit[] sourceUnits)
        {
            Contract.RequiresNotNull(name, "name");
            Contract.RequiresNotNullItems(sourceUnits, "sourceUnits");

            // TODO: Two phases: parse/compile?

            // compiles all source units:
            ScriptCode[] scriptCodes = new ScriptCode[sourceUnits.Length];
            for (int i = 0; i < sourceUnits.Length; i++)
            {
                scriptCodes[i] = LanguageContext.FromEngine(sourceUnits[i].Engine).CompileSourceCode(sourceUnits[i], options, errorSink);
            }

            return(CreateModule(name, kind, scope, scriptCodes));
        }
Esempio n. 16
0
        protected override Microsoft.Scripting.ScriptCode CompileSourceCode(Microsoft.Scripting.SourceUnit sourceUnit, Microsoft.Scripting.CompilerOptions options, Microsoft.Scripting.ErrorSink errorSink)
        {
            return(null);

            //    ClojureParser cp = new ClojureParser(sourceUnit);
            //    LambdaExpression ast;

            //    switch (sourceUnit.Kind)
            //    {
            //        case SourceCodeKind.InteractiveCode:
            //            {
            //                ScriptCodeParseResult result;
            //                object code = cp.ParseInteractiveStatement(out result);
            //                sourceUnit.CodeProperties = result;
            //                if (result != ScriptCodeParseResult.Complete)
            //                    return null;
            //                //ast = Generator.Generate(code, true);
            //                ast = Compiler.GenerateLambda(code, true);
            //            }
            //            break;

            //        default:
            //            sourceUnit.CodeProperties = ScriptCodeParseResult.Complete;
            //            ast = Generator.Generate(cp.ParseFile(), sourceUnit);
            //            break;
            //    }

            //    //ast = new GlobalLookupRewriter().RewriteLambda(ast);

            //    //DEBUG!!!
            //    //Compiler.SaveContext();

            //    return new LegacyScriptCode(ast, sourceUnit);
        }
Esempio n. 17
0
 public Tokenizer(ErrorSink errorSink)
     : this(errorSink, false) {
 }
Esempio n. 18
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            ScriptCode res = CompileTotemCode(sourceUnit, options, errorSink);
            if (res != null)
            {
                Scope scope = res.CreateScope();

                // if this is an optimized module we need to initialize the optimized totemCode.
                // Optimized scopes come w/ extensions already attached so we use that to know
                // if we're optimized or not.
                TotemScopeExtension scopeExtension = (TotemScopeExtension)scope.GetExtension(ContextId);
                if (scopeExtension != null)
                {
                    InitializeModule(sourceUnit.Path, scopeExtension.ModuleContext, res, ModuleOptions.None);
                }
            }
            return res;
        }
Esempio n. 19
0
 /// <summary>
 /// Executes in a new scope created by the language.
 /// </summary>
 public object Execute(ErrorSink errorSink)
 {
     return(Compile(errorSink).Run());
 }
Esempio n. 20
0
 public Tokenizer(ErrorSink errorSink, bool verbatim)
     : this(errorSink, verbatim, true) {
 }
Esempio n. 21
0
 public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
 {
     Module module = Parser.ParseModule(sourceUnit, this);
     #if WRITE_AST
     module.WriteString(new AstWriter(Console.Out));
     #endif
     AstAnalyzer ctsg = new AstAnalyzer(this);
     return ctsg.Analyze(module, sourceUnit);
 }
Esempio n. 22
0
 public ErrorCounter(ErrorSink/*!*/ sink) {
     ContractUtils.RequiresNotNull(sink, "sink");
     _sink = sink;
 }
Esempio n. 23
0
        internal static ScriptCode CompileTotemCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            var totemOptions = (TotemCompilerOptions)options;

            if (sourceUnit.Kind == SourceCodeKind.File)
            {
                totemOptions.Module |= ModuleOptions.Initialize;
            }

            CompilerContext context = new CompilerContext(sourceUnit, options, errorSink);
            TotemAst ast = ParseAndBindAst(context);
            if (ast == null)
            {
                return null;
            }

            return ast.ToScriptCode();
        }
Esempio n. 24
0
 public Tokenizer(bool verbatim, ILexicalVariableResolver/*!*/ localVariableResolver) {
     ContractUtils.RequiresNotNull(localVariableResolver, "localVariableResolver");
     
     _errorSink = ErrorSink.Null;
     _localVariableResolver = localVariableResolver;
     _verbatim = verbatim;
     _encoding = RubyEncoding.Binary;
 }
Esempio n. 25
0
        /// <summary>
        /// Used to support legacy CreateParser API.
        /// </summary>
        internal Tokenizer(ErrorSink errorSink, PythonCompilerOptions options, bool verbatim)
            : this(errorSink, options) {

            _verbatim = verbatim || options.Verbatim;
        }
Esempio n. 26
0
        public ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            Contract.RequiresNotNull(sourceUnit, "sourceUnit");

            if (options == null)
            {
                options = GetCompilerOptions();
            }
            if (errorSink == null)
            {
                errorSink = Engine.GetCompilerErrorSink();
            }

            CompilerContext context = new CompilerContext(sourceUnit, options, errorSink);

            CodeBlock block = ParseSourceCode(context);

            if (block == null)
            {
                throw new SyntaxErrorException("invalid syntax|" + sourceUnit.GetCode().Trim());
            }

            //DumpBlock(block, sourceUnit.Id);

            AnalyzeBlock(block);

            DumpBlock(block, sourceUnit.Id);

            // TODO: ParseSourceCode can update CompilerContext.Options
            return(new ScriptCode(block, Engine.GetLanguageContext(context.Options), context));
        }
Esempio n. 27
0
 public ScriptCode Compile(ErrorSink errorSink)
 {
     return(Compile(_language.GetCompilerOptions(), errorSink));
 }
Esempio n. 28
0
        public Tokenizer(bool verbatim, ErrorSink/*!*/ errorSink) {
            ContractUtils.RequiresNotNull(errorSink, "errorSink");

            _bigIntParser = new BignumParser();
            _errorSink = errorSink;
            _sourceUnit = null;
            _parser = null;
            _verbatim = verbatim;
            _compatibility = RubyCompatibility.Default;
//            _buffer = null;
            _initialLocation = SourceLocation.Invalid;
            _tokenSpan = SourceSpan.Invalid;
            _tokenValue = new TokenValue();
            _bufferPos = 0;
            
            // TODO:
            _input = null;
        }
Esempio n. 29
0
 /// <summary>
 /// Executes in a new scope created by the language.
 /// </summary>
 public object Execute(CompilerOptions options, ErrorSink errorSink)
 {
     return(Compile(options, errorSink).Run());
 }
Esempio n. 30
0
 public ErrorCollector(ErrorSink sourceErrors)
 {
     _sourceErrors = sourceErrors;
 }