Ejemplo n.º 1
0
        public XSharpParser.SourceContext Parse()
        {
            System.Diagnostics.Trace.WriteLine("-->> SourceWalker.Parse()");
            ITokenStream tokenStream;

            XSharpParser.SourceContext tree;
            var options = _file.Project.ProjectNode.ParseOptions;

            if (options == null)
            {
                options = XSharpParseOptions.Default;
            }
            _errors = new List <XError>();
            bool ok = XSharp.Parser.VsParser.Parse(_source, _file.SourcePath, _file.Project.ProjectNode.ParseOptions,
                                                   this, out tokenStream, out tree);

            lock (this)
            {
                _hasParseErrors = !ok;
                if (ok)
                {
                    _tokenStream = tokenStream;
                    _tree        = tree;
                }
                else
                {
                    _tokenStream = null;
                    _tree        = null;
                }
                _file.HasParseErrors = _hasParseErrors;
            }
            System.Diagnostics.Trace.WriteLine("<<-- SourceWalker.Parse()");
            return(_tree);
        }
Ejemplo n.º 2
0
        public override void EnterSource([NotNull] XSharpParser.SourceContext context)
        {
            var source = (XSharpLexer)context.Start.TokenSource;

            source.Reset();
            _tokens = source.GetAllTokens();
        }
Ejemplo n.º 3
0
        public ITokenStream Lex()
        {
            System.Diagnostics.Trace.WriteLine("-->> SourceWalker.Lex()");
            ITokenStream tokenStream;

            _errors = new List <XError>();
            bool ok = XSharp.Parser.VsParser.Lex(_source, _file.SourcePath, _file.Project.ProjectNode.ParseOptions,
                                                 this, out tokenStream);

            lock (this)
            {
                _hasParseErrors = !ok;
                if (ok)
                {
                    _tree        = null;
                    _tokenStream = tokenStream;
                }
                else
                {
                    _tree           = null;
                    _tokenStream    = null;
                    _hasParseErrors = false;
                }
                _file.HasParseErrors = _hasParseErrors;
            }
            System.Diagnostics.Trace.WriteLine("<<-- SourceWalker.Lex()");
            return(this._tokenStream);
        }
Ejemplo n.º 4
0
 public XSharpModelDiscover(XFile file, XSharpParser.SourceContext ctx, IEnumerable <XError> errors)
 {
     // To store intermediate declarations
     this._file    = file;
     this._xSource = ctx;
     this._errors  = errors;
     //
     this._currentTypes   = new Stack <XType>();
     this._currentNSpaces = new Stack <XType>();
     //
     this._options      = file.Project.ProjectNode.ParseOptions;
     this._types        = new Dictionary <string, XType>();
     this._usings       = new List <string>();
     this._staticusings = new List <String>();
     this._globalType   = XType.CreateGlobalType(_file);
     this._classVars    = new List <XTypeMember>();
     _types.Add(_globalType.Name, _globalType);
     if (this._file != null && this._file.Project != null)
     {
         if (this._file.Project.Loaded)       // this will fail if the project file is already unloaded
         {
             if (this._file.Project.ProjectNode.PrefixClassesWithDefaultNamespace)
             {
                 this._defaultNS = this._file.Project.ProjectNode.RootNameSpace;
             }
         }
     }
 }
Ejemplo n.º 5
0
        public override void ExitSource([NotNull] XSharpParser.SourceContext context)
        {
            var lastEnt = context._Entities.LastOrDefault();

            if (lastEnt != null)
            {
                writeTrivia(CodeCompileUnit, lastEnt, true);
            }
        }
Ejemplo n.º 6
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         _errors      = null;
         _file        = null;
         _prjNode     = null;
         _tree        = null;
         _tokenStream = null;
         _snapshot    = null;
     }
 }
Ejemplo n.º 7
0
 public override void EnterSource([NotNull] XSharpParser.SourceContext context)
 {
     // Default namespaces
     addUsing("System", false);
     if (_file?.Project?.ProjectNode?.ParseOptions != null)
     {
         if (_file.Project.ProjectNode.ParseOptions.IsDialectVO)
         {
             addUsing("Vulcan", false);
         }
     }
 }
Ejemplo n.º 8
0
        public void BuildModel(XSharpParser.SourceContext xTree, bool buildLocals)
        {
            // abort when the project is unloaded or when no project
            // because in these cases there is no need to build a model
            if (_prjNode == null || !_file.Project.Loaded)
            {
                return;
            }

            //
            if (xTree != null)
            {
                try
                {
                    XSharpModelDiscover mdiscover;
                    if (buildLocals)
                    {
                        mdiscover = new XSharpModelDiscoverWithLocals(_file, xTree, _errors);
                    }
                    else
                    {
                        mdiscover = new XSharpModelDiscover(_file, xTree, _errors);
                    }

                    var walker = new LanguageService.SyntaxTree.Tree.ParseTreeWalker();
                    //
                    // Walk the tree. The XSharpModelDiscover class will build the model.
                    walker.Walk(mdiscover, xTree);
                    // Callback for LibraryManager
                    // Disabled for Now RvdH
                    //if ( _file.Project != null)
                    //    _file.Project.FileWalkComplete?.Invoke(_file);
                    //
                }
                catch (Exception e)
                {
                    Support.Debug("SourceWalker.BuildModel failed: " + e.Message);
                }
            }
        }
 public override void ExitSource([NotNull] XSharpParser.SourceContext context)
 {
     // Reset TypeList for this file
     this.File.SetTypes(_types, _usings, _staticusings, true);
 }
 public XSharpModelDiscoverWithLocals(XFile file, XSharpParser.SourceContext ctx, IEnumerable <XError> errors) : base(file, ctx, errors)
 {
     this._localDecls = new Stack <XSharpParser.LocalvarContext>();
 }
Ejemplo n.º 11
0
        public static bool Parse(string sourceText, string fileName, CSharpParseOptions options, IErrorListener listener,
                                 out ITokenStream tokens, out XSharpParser.SourceContext tree)
        {
            tree   = null;
            tokens = null;
            var parseErrors = ParseErrorData.NewBag();

            try
            {
                var lexer = XSharpLexer.Create(sourceText, fileName, options);
                lexer.Options = options;
                BufferedTokenStream tokenStream = lexer.GetTokenStream();
                tokenStream.Fill();
                tokens = (ITokenStream)tokenStream;

                GetLexerErrors(lexer, tokenStream, parseErrors);

                // do we need to preprocess
                #region Determine if we really need the preprocessor
                bool mustPreprocess = true;
                if (lexer.HasPreprocessorTokens || !options.NoStdDef)
                {
                    // no need to pre process in partial compilation
                    // if lexer does not contain UDCs, Messages or Includes
                    mustPreprocess = lexer.MustBeProcessed;
                }
                else
                {
                    mustPreprocess = false;
                }
                #endregion
                XSharpPreprocessor  pp       = null;
                BufferedTokenStream ppStream = null;
                pp = new XSharpPreprocessor(lexer, tokenStream, options, fileName, Encoding.Unicode, SourceHashAlgorithm.None, parseErrors);

                if (mustPreprocess)
                {
                    var ppTokens = pp.PreProcess();
                    ppStream = new CommonTokenStream(new XSharpListTokenSource(lexer, ppTokens));
                }
                else
                {
                    // No Standard Defs and no preprocessor tokens in the lexer
                    // so we bypass the preprocessor and use the lexer token stream
                    ppStream = new CommonTokenStream(new XSharpListTokenSource(lexer, tokenStream.GetTokens()));
                }
                ppStream.Fill();
                var parser = new XSharpParser(ppStream);
                parser.Interpreter.tail_call_preserves_sll = false;     // default = true   Setting to FALSE will reduce memory used by parser
                parser.Options = options;
                tree           = null;
                parser.RemoveErrorListeners();
                parser.Interpreter.PredictionMode = PredictionMode.Sll;
                parser.ErrorHandler = new BailErrorStrategy();
                try
                {
                    tree = parser.source();
                }
                catch (Exception)
                {
                    var errorListener = new XSharpErrorListener(fileName, parseErrors);
                    parser.AddErrorListener(errorListener);
                    parser.ErrorHandler = new XSharpErrorStrategy();
                    parser.Interpreter.PredictionMode = PredictionMode.Ll;
                    ppStream.Reset();
                    parser.Reset();
                    try
                    {
                        tree = parser.source();
                    }
                    catch (Exception)
                    {
                        tree = null;
                    }
                }
            }
            catch (Exception)
            {
                tree = null;
            }
            ReportErrors(parseErrors, listener);
            return(tree != null);
        }