Example #1
0
        public static PhpSyntaxTree ParseCode(
            string content,
            PhpParseOptions parseOptions,
            PhpParseOptions scriptParseOptions,
            string fname)
        {
            // TODO: new parser implementation based on Roslyn

            // TODO: file.IsScript ? scriptParseOptions : parseOptions
            var unit = new CodeSourceUnit(
                content, fname, Encoding.UTF8,
                (parseOptions.Kind == SourceCodeKind.Regular) ? Lexer.LexicalStates.INITIAL : Lexer.LexicalStates.ST_IN_SCRIPTING);

            var result = new PhpSyntaxTree(unit);

            var errorSink = new ErrorSink(result);
            var factory   = new NodesFactory(unit);

            //
            unit.Parse(factory, errorSink);

            //
            result.Diagnostics = errorSink.Diagnostics;

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

            //
            return(result);
        }
Example #2
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);
        }
Example #3
0
        /// <summary>
        /// Gets enumeration of user declared routines (global code, functions, methods and lambdas) in the specified file
        /// identified by its syntax tree.
        /// </summary>
        public IEnumerable <IPhpRoutineSymbol> GetUserDeclaredRoutinesInFile(PhpSyntaxTree syntaxTree)
        {
            string relativePath = PhpFileUtilities.GetRelativePath(
                PhpFileUtilities.NormalizeSlashes(syntaxTree.Source.FilePath),
                PhpFileUtilities.NormalizeSlashes(_options.BaseDirectory));
            var fileSymbol = _tables.GetFile(relativePath);

            return(fileSymbol?.GetAllRoutines() ?? ImmutableArray <SourceRoutineSymbol> .Empty);
        }
Example #4
0
        /// <summary>
        /// Returns the offset of the location specified by (zero-based) line and character from the start of the file.
        /// In the case of invalid line, -1 is returned.
        /// </summary>
        public static int GetOffset(this PhpSyntaxTree tree, LinePosition linePosition)
        {
            if (linePosition.Line < 0 || linePosition.Line > tree.Source.LineBreaks.Count)
            {
                return(-1);
            }

            int lineStart = (linePosition.Line == 0) ? 0 : tree.Source.LineBreaks.EndOfLineBreak(linePosition.Line - 1);

            return(lineStart + linePosition.Character);
        }
Example #5
0
        public PhpParseOptions(
            DocumentationMode documentationMode = DocumentationMode.Parse,
            SourceCodeKind kind     = SourceCodeKind.Regular,
            Version languageVersion = null,
            bool shortOpenTags      = false)
            : base(kind, documentationMode)
        {
            if (!kind.IsValid())
            {
                throw new ArgumentOutOfRangeException(nameof(kind));
            }

            PhpSyntaxTree.ParseLanguageVersion(ref languageVersion);    // throws if value not supported

            _languageVersion    = languageVersion;
            _allowShortOpenTags = shortOpenTags;
        }
Example #6
0
        public static PhpSyntaxTree ParseCode(
            string content,
            PhpParseOptions parseOptions,
            PhpParseOptions scriptParseOptions,
            string fname)
        {
            // TODO: new parser implementation based on Roslyn

            // TODO: file.IsScript ? scriptParseOptions : parseOptions
            var unit   = new CodeSourceUnit(content.ToString(), fname, Encoding.UTF8);
            var result = new PhpSyntaxTree(unit);

            var errorSink = new ErrorSink(result);

            unit.Parse(new BasicNodesFactory(unit), errorSink);
            result.Diagnostics = errorSink.Diagnostics;

            return(result);
        }
Example #7
0
        public static PhpSyntaxTree ParseCode(
            string content,
            PhpParseOptions parseOptions,
            PhpParseOptions scriptParseOptions,
            string fname)
        {
            // TODO: new parser implementation based on Roslyn

            // TODO: file.IsScript ? scriptParseOptions : parseOptions
            var unit = new CodeSourceUnit(
                content, fname, Encoding.UTF8,
                (parseOptions.Kind == SourceCodeKind.Regular) ? Lexer.LexicalStates.INITIAL : Lexer.LexicalStates.ST_IN_SCRIPTING);

            var result = new PhpSyntaxTree(unit);

            var errorSink = new ErrorSink(result);
            var factory   = new NodesFactory(unit);

            //
            unit.Parse(factory, errorSink);

            //
            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, unit.Code.Length);
                result.Root = new GlobalCode(fullSpan, ImmutableArray <Statement> .Empty, unit);
            }

            //
            return(result);
        }