コード例 #1
0
        public static PhpSyntaxTree ParseCode(
            string content,
            PhpParseOptions parseOptions,
            PhpParseOptions scriptParseOptions,
            string fname)
        {
            if (fname == null)
            {
                throw ExceptionUtilities.ArgumentNull(nameof(fname));
            }

            // TODO: new parser implementation based on Roslyn

            // TODO: file.IsScript ? scriptParseOptions : parseOptions
            var unit = new PhpSourceUnit(
                fname,
                SourceText.From(content, Encoding.UTF8),
                encoding: Encoding.UTF8);

            var result = new PhpSyntaxTree(unit);

            var errorSink = new ErrorSink(result);
            var factory   = new NodesFactory(unit, parseOptions.Defines);

            //
            try
            {
                unit.Parse(factory, errorSink,
                           features: GetLanguageFeatures(parseOptions),
                           state: (parseOptions.Kind == SourceCodeKind.Regular) ? Lexer.LexicalStates.INITIAL : Lexer.LexicalStates.ST_IN_SCRIPTING);
            }
            finally
            {
                unit.Close();
            }

            //
            result.Diagnostics = errorSink.Diagnostics;

            result.Lambdas    = factory.Lambdas.AsImmutableSafe();
            result.Types      = factory.Types.AsImmutableSafe();
            result.Functions  = factory.Functions.AsImmutableSafe();
            result.YieldNodes = factory.YieldNodes.AsImmutableSafe();

            if (factory.Root != null)
            {
                result.Root = factory.Root;
            }
            else
            {
                // Parser leaves factory.Root to null in the case of syntax errors -> create a proxy syntax node
                var fullSpan = new Devsense.PHP.Text.Span(0, content.Length);
                result.Root = new GlobalCode(fullSpan, ImmutableArray <Statement> .Empty, unit);
            }

            //
            return(result);
        }
コード例 #2
0
ファイル: PhpSyntaxTree.cs プロジェクト: tyty999/peachpie
 private PhpSyntaxTree(PhpSourceUnit source)
 {
     _source = source ?? throw ExceptionUtilities.ArgumentNull(nameof(source));
 }