コード例 #1
0
ファイル: Parser.cs プロジェクト: Alxandr/Totem-2.0
        private SourceUnitTree Parse(SourceUnit sourceUnit, bool allowSingle, out bool isExpression)
        {
            isExpression = false;
            IronyParser parser = new IronyParser(allowSingle ? singleStatement : fullGrammar);
            parser.Context.Mode = ParseMode.CommandLine;

            scopes = new Stack<LexicalScopeBuilder>();
            EnterTopLevelScope();
            try
            {
                var parsedScript = parser.Parse(sourceUnit.GetCode());
                if (parsedScript.HasErrors())
                {
                    sourceUnit.CodeProperties = ScriptCodeParseResult.Invalid;
                    return null;
                }

                if (sourceUnit.Kind == SourceCodeKind.InteractiveCode && parser.Context.Status == ParserStatus.AcceptedPartial)
                {
                    sourceUnit.CodeProperties = ScriptCodeParseResult.IncompleteStatement;
                    return null;
                }

                sourceUnit.CodeProperties = ScriptCodeParseResult.Complete;
                return BuildSourceTree(parsedScript.Root, sourceUnit, allowSingle, out isExpression);
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                LeaveScope();
            }
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: Project-AZUSA/IronTJS
        public static int GetNextAutoIndentSize(SourceUnit sourceUnit, int autoIndentTabWidth)
        {
            new Parser().Parse(new CompilerContext(sourceUnit, new CompilerOptions(), ErrorSink.Null));
            var autoIndentSize = sourceUnit.GetCode().TakeWhile(x => x == ' ' || x == '\t').Aggregate(0, (x, y) => y == ' ' ? x + 1 : x + autoIndentTabWidth);

            if (sourceUnit.CodeProperties != ScriptCodeParseResult.Complete)
            {
                autoIndentSize += autoIndentTabWidth;
            }
            return(autoIndentSize);
        }
コード例 #3
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            var res = NovaParser.Parse(sourceUnit.GetCode(), sourceUnit);

            if (res != null)
            {
                Expression mainBlock = NovaExpression.NovaBlock(res);
                return(new NovaScriptCode(mainBlock, sourceUnit));
            }
            else
            {
                throw new SyntaxErrorException("Syntax error", sourceUnit, SourceSpan.None, 0, Severity.Error);
            }
        }
コード例 #4
0
        public SyntaxErrorException(string message, SourceUnit sourceUnit, SourceSpan span, int errorCode, Severity severity)
            : base(message) {
            ContractUtils.RequiresNotNull(message, "message");

            _span = span;
            _severity = severity;
            _errorCode = errorCode;
            if (sourceUnit != null) {
                _sourcePath = sourceUnit.Path;
                try {
                    _sourceCode = sourceUnit.GetCode();
                    _sourceLine = sourceUnit.GetCodeLine(Line);
                } catch (System.IO.IOException) {
                    // could not get source code.
                }
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
0
 public ClojureParser(SourceUnit src)
 {
     _source = src;
     _text = src.GetCode();
 }
コード例 #7
0
 public override void Add(SourceUnit source, String message, SourceSpan span, Int32 errorCode, Severity severity)
 {
     Add(message, source.Path, source.GetCode(), source.GetCodeLine(span.Start.Line), span, errorCode, severity);
 }
コード例 #8
0
        public static object Evaluate(MutableString /*!*/ code, RubyScope /*!*/ targetScope, object self, RubyModule module, MutableString file, int line)
        {
            Assert.NotNull(code, targetScope);

            RubyContext     context     = targetScope.RubyContext;
            RubyMethodScope methodScope = targetScope.GetInnerMostMethodScope();

            Utils.Log(Interlocked.Increment(ref _stringEvalCounter).ToString(), "EVAL");

            // we want to create a new top-level local scope:
            var options = CreateCompilerOptionsForEval(targetScope, methodScope, module != null, line);

            SourceUnit source = context.CreateSnippet(code.ConvertToString(), file != null ? file.ConvertToString() : "(eval)", SourceCodeKind.Statements);
            Expression <EvalEntryPointDelegate> lambda;

            try {
                lambda = context.ParseSourceCode <EvalEntryPointDelegate>(source, options, context.RuntimeErrorSink);
            } catch (SyntaxError e) {
                Utils.Log(e.Message, "EVAL_ERROR");
                Utils.Log(new String('-', 50), "EVAL_ERROR");
                Utils.Log(source.GetCode(), "EVAL_ERROR");
                Utils.Log(new String('-', 50), "EVAL_ERROR");
                throw;
            }
            Debug.Assert(lambda != null);

            Proc           blockParameter;
            RubyMethodInfo methodDefinition;

            if (methodScope != null)
            {
                blockParameter   = methodScope.BlockParameter;
                methodDefinition = methodScope.Method;
            }
            else
            {
                blockParameter   = null;
                methodDefinition = null;
            }

            if (context.Options.InterpretedMode)
            {
                return(Interpreter.TopLevelExecute(new InterpretedScriptCode(lambda, source),
                                                   targetScope,
                                                   self,
                                                   module,
                                                   blockParameter,
                                                   methodDefinition,
                                                   targetScope.RuntimeFlowControl
                                                   ));
            }
            else
            {
                return(lambda.Compile(source.EmitDebugSymbols)(
                           targetScope,
                           self,
                           module,
                           blockParameter,
                           methodDefinition,
                           targetScope.RuntimeFlowControl
                           ));
            }
        }
コード例 #9
0
 public ClojureParser(SourceUnit src)
 {
     _source = src;
     _text   = src.GetCode();
 }
コード例 #10
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);
        }