Пример #1
0
 public virtual void UpdateFile(TokenStreamRewriter rewriter)
 {
     using (StreamWriter writer = new StreamWriter(this.fileName, false))
     {
         writer.Write(rewriter.GetText());
     }
 }
        static void Main(string[] args)
        {
            try
            {
                var excerpt           = @"<?php

$heredocString = <<<TXT
HEREDOC TEXT
TXT;

$heredocString = <<<TXT
HEREDOC TEXT
TXT
;

";
                var inputStream       = new AntlrInputStream(excerpt);
                var lexer             = new PhpLexer(inputStream);
                var commonTokenStream = new CommonTokenStream(lexer);

                var parser = new PhpParser(commonTokenStream);
                parser.htmlDocument();

                var writer = new TokenStreamRewriter(commonTokenStream);

                var text = writer.GetText();
                Console.WriteLine(text);
            }
            catch (Exception Ex)
            {
                Console.Error.WriteLine(Ex.Message);
            }
        }
 public BreadcrumbControllerInjector(
     IStringUtilService stringUtilService,
     ICSharpParserService cSharpParserService,
     IBreadcrumbCommandParserService breadcrumbCommandParserService,
     ICSharpCommonStgService cSharpCommonStgService,
     BufferedTokenStream tokenStream,
     ControllerDictionary controllerDictionary,
     string breadcrumbServiceNamespace,
     string controllerRootNamespace,
     string defaultAreaBreadcrumbServiceRootName,
     string tabString)
 {
     _stringUtilService              = stringUtilService;
     _cSharpParserService            = cSharpParserService;
     _breadcrumbCommandParserService = breadcrumbCommandParserService;
     _cSharpCommonStgService         = cSharpCommonStgService;
     Tokens         = tokenStream;
     Rewriter       = new TokenStreamRewriter(tokenStream);
     ControllerDict = controllerDictionary;
     _breadcrumbServiceNamespace           = breadcrumbServiceNamespace;
     _controllerRootNamespace              = controllerRootNamespace;
     _defaultAreaBreadcrumbServiceRootName = defaultAreaBreadcrumbServiceRootName;
     _tabString         = tabString;
     _currentNamespace  = new Stack <string>();
     _currentClass      = new Stack <string>();
     _isControllerClass = new Stack <bool>();
     _isControllerClass.Push(false);
     _isClassModified = new Stack <bool>();
     _isClassModified.Push(false);
     IsModified = false;
 }
Пример #4
0
 public CSVCleanerListener(ITokenStream tokens)
 {
     r         = new TokenStreamRewriter(tokens);
     index     = 0;
     LogRows   = new List <KeyValuePair <int, string> >();
     shortRows = false;
 }
Пример #5
0
        public ModuleState SetTokenStream(ICodeModule module, ITokenStream tokenStream)
        {
            TokenStream = tokenStream;
            var tokenStreamRewriter = new TokenStreamRewriter(tokenStream);

            ModuleRewriter = new ModuleRewriter(module, tokenStreamRewriter);
            return(this);
        }
Пример #6
0
 public VBComponentParseResult(VBComponent component, IParseTree parseTree, IEnumerable <CommentNode> comments, TokenStreamRewriter rewriter)
 {
     _component     = component;
     _qualifiedName = new QualifiedModuleName(component);
     _parseTree     = parseTree;
     _comments      = comments;
     _rewriter      = rewriter;
 }
Пример #7
0
 public ComponentParseTask(VBComponent vbComponent, VBAPreprocessor preprocessor, IAttributeParser attributeParser, TokenStreamRewriter rewriter = null)
 {
     _attributeParser = attributeParser;
     _preprocessor = preprocessor;
     _component = vbComponent;
     _rewriter = rewriter;
     _qualifiedName = new QualifiedModuleName(vbComponent); 
 }
 public ComponentParseTask(VBComponent vbComponent, VBAPreprocessor preprocessor, IAttributeParser attributeParser, TokenStreamRewriter rewriter = null)
 {
     _attributeParser = attributeParser;
     _preprocessor    = preprocessor;
     _component       = vbComponent;
     _rewriter        = rewriter;
     _qualifiedName   = new QualifiedModuleName(vbComponent);
 }
Пример #9
0
        public ServiceClassInjector(
            IStringUtilService stringUtilService,
            ICSharpParserService cSharpParserService,
            IServiceCommandParserService serviceCommandParserService,
            BufferedTokenStream tokenStream,
            string serviceClassInterfaceName,
            ServiceFile serviceFile,
            string tabString = null)
        {
            _stringUtilService           = stringUtilService;
            _cSharpParserService         = cSharpParserService;
            _serviceCommandParserService = serviceCommandParserService;
            Tokens   = tokenStream;
            Rewriter = new TokenStreamRewriter(tokenStream);
            _serviceClassInterfaceName = serviceClassInterfaceName;
            _serviceFile      = serviceFile;
            _tabString        = tabString;
            _currentNamespace = new Stack <string>();
            _currentClass     = new Stack <string>();
            _isCorrectClass   = new Stack <bool>();
            _isCorrectClass.Push(false);
            _hasServiceNamespace   = false;
            _hasServiceClass       = false;
            _hasServiceConstructor = false;
            IsModified             = false;

            _usingSet = _serviceFile.UsingDirectives.ToHashSet();

            _ctorParamDict = new Dictionary <string, FixedParameter>();
            foreach (var fixedParam in
                     _serviceFile.ServiceDeclaration.Body.ConstructorDeclaration.FormalParameterList.FixedParameters)
            {
                _ctorParamDict.Add($"{fixedParam.Type} {fixedParam.Identifier}", fixedParam);
            }

            _fieldDict = new Dictionary <string, FieldDeclaration>();
            foreach (var fieldDec in _serviceFile.ServiceDeclaration.Body.FieldDeclarations)
            {
                _fieldDict.Add($"{fieldDec.Type} {fieldDec?.VariableDeclarator?.Identifier}", fieldDec);
            }

            _ctorAssignmentDict = new Dictionary <string, SimpleAssignment>();
            var statements = _serviceFile.ServiceDeclaration?.Body?.ConstructorDeclaration?.Body?.Statements;

            if (statements != null)
            {
                foreach (var statement in statements)
                {
                    if (statement.SimpleAssignment != null)
                    {
                        var sa = statement.SimpleAssignment;
                        _ctorAssignmentDict.Add($"{sa.LeftHandSide}={sa.RightHandSide};", sa);
                    }
                }
            }
        }
Пример #10
0
        public ComponentParseTask(QualifiedModuleName module, IVBAPreprocessor preprocessor, IAttributeParser attributeParser, IModuleExporter exporter, TokenStreamRewriter rewriter = null)
        {
            _taskId = Guid.NewGuid();

            _attributeParser = attributeParser;
            _exporter        = exporter;
            _preprocessor    = preprocessor;
            _module          = module;
            _rewriter        = rewriter;
            _parser          = new VBAModuleParser();
        }
Пример #11
0
        public ComponentParseTask(IVBComponent vbComponent, IVBAPreprocessor preprocessor, IAttributeParser attributeParser, TokenStreamRewriter rewriter = null)
        {
            _taskId = Guid.NewGuid();

            _attributeParser = attributeParser;
            _preprocessor    = preprocessor;
            _component       = vbComponent;
            _rewriter        = rewriter;
            _qualifiedName   = new QualifiedModuleName(vbComponent);
            _parser          = new VBAModuleParser();
        }
Пример #12
0
        public void RewriteDoesNotRewriteIfNotDirty()
        {
            var module = new Mock <ICodeModule>();

            module.Setup(m => m.Content()).Returns(string.Empty);
            module.Setup(m => m.Clear());

            var rewriter = new TokenStreamRewriter(new CommonTokenStream(new ListTokenSource(new List <IToken>())));
            var sut      = new ModuleRewriter(module.Object, rewriter);

            sut.Rewrite();
            module.Verify(m => m.Clear(), Times.Never);
        }
Пример #13
0
 public FixerListener(CommonTokenStream tokenStreams, string fileName)
 {
     TokenStream = tokenStreams;
     if (fileName.LastIndexOf('.') != -1)
     {
         FileName = fileName.Substring(0, fileName.LastIndexOf('.'));
     }
     else
     {
         FileName = fileName;
     }
     StreamRewriter = new TokenStreamRewriter(tokenStreams);
 }
Пример #14
0
        private IParseTree Parse(string code, out TokenStreamRewriter outRewriter)
        {
            var input  = new AntlrInputStream(code);
            var lexer  = new VBALexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new VBAParser(tokens);

            parser.AddErrorListener(new ExceptionErrorListener());
            outRewriter = new TokenStreamRewriter(tokens);

            var result = parser.startRule();

            return(result);
        }
Пример #15
0
 public ServiceClassScraper(
     ICSharpParserService cSharpParserService,
     BufferedTokenStream tokenStream,
     string serviceClassName,
     string serviceNamespace,
     List <TypeParameter> typeParameters)
 {
     _cSharpParserService = cSharpParserService;
     Tokens            = tokenStream;
     ServiceClassName  = serviceClassName;
     TypeParameters    = typeParameters;
     _serviceNamespace = serviceNamespace;
     Rewriter          = new TokenStreamRewriter(tokenStream);
     Results           = new ServiceFile();
     _currentNamespace = new Stack <string>();
     HasServiceClass   = false;
 }
Пример #16
0
        public void RewriteClearsEntireModule()
        {
            var module = new Mock <ICodeModule>();

            module.Setup(m => m.Clear());

            var rewriter = new TokenStreamRewriter(new CommonTokenStream(new ListTokenSource(new List <IToken>())));
            var sut      = new ModuleRewriter(module.Object, rewriter);

            sut.InsertAfter(0, "test");

            if (!sut.IsDirty)
            {
                sut.InsertBefore(0, "foo");
            }
            sut.Rewrite();

            module.Verify(m => m.Clear());
        }
Пример #17
0
        public Task ParseAsync(VBComponent component, CancellationToken token, TokenStreamRewriter rewriter = null)
        {
            _state.ClearStateCache(component);
            _state.SetModuleState(component, ParserState.Pending); // also clears module-exceptions

            var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_central.Token, token);

            //var taskFactory = new TaskFactory(new StaTaskScheduler());
            var task = new Task(() => ParseAsyncInternal(component, linkedTokenSource.Token, rewriter));

            _currentTasks.TryAdd(component, Tuple.Create(task, linkedTokenSource));

            Tuple <Task, CancellationTokenSource> removedTask;

            task.ContinueWith(t => _currentTasks.TryRemove(component, out removedTask)); // default also executes on cancel

            task.Start(/*taskFactory.Scheduler*/);
            return(task);
        }
Пример #18
0
 public ServiceStartupRegistration(
     IStringUtilService stringUtilService,
     ICSharpParserService cSharpParserService,
     IServiceCommandStgService serviceCommandStgService,
     BufferedTokenStream tokenStream,
     string rootNamespace,
     List <StartupRegistrationInfo> startupRegInfoList,
     string tabString = null)
 {
     _stringUtilService        = stringUtilService;
     _cSharpParserService      = cSharpParserService;
     _serviceCommandStgService = serviceCommandStgService;
     Tokens              = tokenStream;
     Rewriter            = new TokenStreamRewriter(tokenStream);
     _rootNamespace      = rootNamespace;
     _startupRegInfoList = startupRegInfoList;
     _tabString          = tabString;
     _currentNamespace   = new Stack <string>();
     _currentClass       = new Stack <string>();
     IsModified          = false;
 }
Пример #19
0
        public Task ParseAsync(VBComponent component, CancellationToken token, TokenStreamRewriter rewriter = null)
        {
            lock (_state)
                lock (component)
                {
                    _state.ClearStateCache(component);
                    _state.SetModuleState(component, ParserState.Pending); // also clears module-exceptions
                }

            var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_central.Token, token);

            var task = new Task(() => ParseAsyncInternal(component, linkedTokenSource.Token, rewriter));

            _currentTasks.TryAdd(component, Tuple.Create(task, linkedTokenSource));

            Tuple <Task, CancellationTokenSource> removedTask;

            task.ContinueWith(t => _currentTasks.TryRemove(component, out removedTask)); // default also executes on cancel
            // See http://stackoverflow.com/questions/6800705/why-is-taskscheduler-current-the-default-taskscheduler
            task.Start(TaskScheduler.Default);
            return(task);
        }
 public ServiceConstructorInjector(
     IStringUtilService stringUtilService,
     ICSharpParserService cSharpParserService,
     ICSharpCommonStgService cSharpCommonStgService,
     ILogger logger,
     BufferedTokenStream tokenStream,
     string constructorClassName,
     string constructorClassNamespace,
     string serviceIdentifier,
     string serviceNamespace,
     string serviceInterfaceType,
     FieldDeclaration fieldDeclaration,
     FixedParameter constructorParameter,
     SimpleAssignment constructorAssignment,
     ConstructorDeclaration constructorDeclaration,
     string tabString = null)
 {
     _stringUtilService      = stringUtilService;
     _cSharpParserService    = cSharpParserService;
     _cSharpCommonStgService = cSharpCommonStgService;
     _logger  = logger;
     Tokens   = tokenStream;
     Rewriter = new TokenStreamRewriter(tokenStream);
     _constructorClassName      = constructorClassName;
     _constructorClassNamespace = constructorClassNamespace;
     _serviceIdentifier         = serviceIdentifier;
     _serviceNamespace          = serviceNamespace;
     _serviceInterfaceType      = serviceInterfaceType;
     _fieldDeclaration          = fieldDeclaration;
     _constructorParameter      = constructorParameter;
     _constructorAssignment     = constructorAssignment;
     _constructorDeclaration    = constructorDeclaration;
     _tabString               = tabString;
     _currentNamespace        = new Stack <string>();
     IsModified               = false;
     _isConstructorClassFound = false;
     _isRewritten             = false;
 }
        public ServiceInterfaceInjector(
            IStringUtilService stringUtilService,
            ICSharpParserService cSharpParserService,
            IServiceCommandParserService serviceCommandParserService,
            BufferedTokenStream tokenStream,
            string serviceClassInterfaceName,
            ServiceFile serviceFile,
            string tabString = null)
        {
            _stringUtilService           = stringUtilService;
            _cSharpParserService         = cSharpParserService;
            _serviceCommandParserService = serviceCommandParserService;
            Tokens   = tokenStream;
            Rewriter = new TokenStreamRewriter(tokenStream);
            _serviceClassInterfaceName = serviceClassInterfaceName;
            _serviceFile         = serviceFile;
            _tabString           = tabString;
            _currentNamespace    = new Stack <string>();
            _hasServiceNamespace = false;
            _hasServiceInterface = false;
            IsModified           = false;

            _usingSet = _serviceFile.UsingDirectives.ToHashSet();
        }
Пример #22
0
 public UpperCaseClassName(ITokenStream tokenStream)
 {
     _tokenStream = tokenStream;
     ValidCode    = new TokenStreamRewriter(tokenStream);
 }
Пример #23
0
 public MacroExpansionListener(CommonTokenStream tokenStream)
 {
     _tokenStream          = tokenStream;
     _rewrittenTokenStream = new TokenStreamRewriter(tokenStream);
     _messages             = new List <Message>();
 }
Пример #24
0
        public BreadcrumbClassInjector(
            IStringUtilService stringUtilService,
            ICSharpParserService cSharpParserService,
            IBreadcrumbCommandParserService breadcrumbCommandParserService,
            BufferedTokenStream tokenStream,
            List <string> usingDirectives,
            string breadcrumbNamespace,
            BreadcrumbServiceDeclaration breadcrumbDeclaration,
            string tabString)
        {
            _stringUtilService              = stringUtilService;
            _cSharpParserService            = cSharpParserService;
            _breadcrumbCommandParserService = breadcrumbCommandParserService;
            Tokens                    = tokenStream;
            Rewriter                  = new TokenStreamRewriter(tokenStream);
            _breadcrumbNamespace      = breadcrumbNamespace;
            _breadcrumbDeclaration    = breadcrumbDeclaration;
            _tabString                = tabString;
            _currentNamespace         = new Stack <string>();
            _currentClass             = new Stack <string>();
            _hasBreadcrumbNamespace   = false;
            _hasBreadcrumbClass       = false;
            _hasBreadcrumbConstructor = false;
            IsModified                = false;

            _methodDictionary = new Dictionary <string, BreadcrumbMethodDeclaration>();
            foreach (var method in breadcrumbDeclaration.Body.MethodDeclarations)
            {
                _methodDictionary.Add(GetActionMethodName(method.ControllerRoot, method.Action), method);
            }

            _usingSet = usingDirectives.ToHashSet();

            _ctorParamDict = new Dictionary <string, FixedParameter>();
            foreach (var fixedParam in
                     _breadcrumbDeclaration.Body.ConstructorDeclaration.FormalParameterList.FixedParameters)
            {
                _ctorParamDict.Add($"{fixedParam.Type} {fixedParam.Identifier}", fixedParam);
            }

            _fieldDict = new Dictionary <string, FieldDeclaration>();
            foreach (var fieldDec in _breadcrumbDeclaration.Body.FieldDeclarations)
            {
                _fieldDict.Add($"{fieldDec.Type} {fieldDec?.VariableDeclarator?.Identifier}", fieldDec);
            }

            _ctorAssignmentDict = new Dictionary <string, SimpleAssignment>();
            var statements = _breadcrumbDeclaration?.Body?.ConstructorDeclaration?.Body?.Statements;

            if (statements != null)
            {
                foreach (var statement in statements)
                {
                    if (statement.SimpleAssignment != null)
                    {
                        var sa = statement.SimpleAssignment;
                        _ctorAssignmentDict.Add($"{sa.LeftHandSide}={sa.RightHandSide};", sa);
                    }
                }
            }
        }
Пример #25
0
 public ModuleRewriter(ICodeModule module, TokenStreamRewriter rewriter)
 {
     Module   = module;
     Rewriter = rewriter;
 }
Пример #26
0
        private void ParseAsyncInternal(VBComponent component, CancellationToken token, TokenStreamRewriter rewriter = null)
        {
            var preprocessor = new VBAPreprocessor(double.Parse(_vbe.Version, CultureInfo.InvariantCulture));
            var parser       = new ComponentParseTask(component, preprocessor, _attributeParser, rewriter);

            parser.ParseFailure   += (sender, e) => _state.SetModuleState(component, ParserState.Error, e.Cause as SyntaxErrorException);
            parser.ParseCompleted += (sender, e) =>
            {
                // possibly lock _state
                _state.SetModuleAttributes(component, e.Attributes);
                _state.AddParseTree(component, e.ParseTree);
                _state.AddTokenStream(component, e.Tokens);
                _state.SetModuleComments(component, e.Comments);
                _state.SetModuleAnnotations(component, e.Annotations);

                // This really needs to go last
                _state.SetModuleState(component, ParserState.Parsed);
            };
            _state.SetModuleState(component, ParserState.Parsing);
            parser.Start(token);
        }
Пример #27
0
 private void ParseComponent(VBComponent component, TokenStreamRewriter rewriter = null)
 {
     ParseAsync(component, CancellationToken.None, rewriter).Wait();
 }
Пример #28
0
        private void ParseAsyncInternal(VBComponent component, CancellationToken token, TokenStreamRewriter rewriter = null)
        {
            var preprocessor = new VBAPreprocessor(double.Parse(_vbe.Version, CultureInfo.InvariantCulture));
            var parser = new ComponentParseTask(component, preprocessor, _attributeParser, rewriter);
            parser.ParseFailure += (sender, e) => _state.SetModuleState(component, ParserState.Error, e.Cause as SyntaxErrorException);
            parser.ParseCompleted += (sender, e) =>
            {
                // possibly lock _state
                _state.SetModuleAttributes(component, e.Attributes);
                _state.AddParseTree(component, e.ParseTree);
                _state.AddTokenStream(component, e.Tokens);
                _state.SetModuleComments(component, e.Comments);
                _state.SetModuleAnnotations(component, e.Annotations);

                // This really needs to go last
                _state.SetModuleState(component, ParserState.Parsed);
            };
            _state.SetModuleState(component, ParserState.Parsing);
            parser.Start(token);
        }
Пример #29
0
        public Task ParseAsync(VBComponent component, CancellationToken token, TokenStreamRewriter rewriter = null)
        {
            _state.ClearDeclarations(component);            
            _state.SetModuleState(component, ParserState.Pending); // also clears module-exceptions

            var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_central.Token, token);

            //var taskFactory = new TaskFactory(new StaTaskScheduler());
            var task = new Task(() => ParseAsyncInternal(component, linkedTokenSource.Token, rewriter));
            _currentTasks.TryAdd(component, Tuple.Create(task, linkedTokenSource));

            Tuple<Task, CancellationTokenSource> removedTask;
            task.ContinueWith(t => _currentTasks.TryRemove(component, out removedTask)); // default also executes on cancel

            task.Start(/*taskFactory.Scheduler*/);
            return task;
        }
Пример #30
0
        private Task ParseAsync(VBComponent component, CancellationTokenSource token, TokenStreamRewriter rewriter = null)
        {
            State.ClearStateCache(component);

            var task = new Task(() => ParseAsyncInternal(component, token.Token, rewriter));

            _currentTasks.TryAdd(component, Tuple.Create(task, token));

            Tuple <Task, CancellationTokenSource> removedTask;

            task.ContinueWith(t => _currentTasks.TryRemove(component, out removedTask), token.Token); // default also executes on cancel
            // See http://stackoverflow.com/questions/6800705/why-is-taskscheduler-current-the-default-taskscheduler
            task.Start(TaskScheduler.Default);
            return(task);
        }
Пример #31
0
 private void ParseComponent(VBComponent component, TokenStreamRewriter rewriter = null)
 {
     ParseAsync(component, CancellationToken.None, rewriter).Wait();
 }
Пример #32
0
 public ModuleRewriter(QualifiedModuleName module, ITokenStream tokenStream, ISourceCodeHandler sourceCodeHandler)
 {
     _module            = module;
     _rewriter          = new TokenStreamRewriter(tokenStream);
     _sourceCodeHandler = sourceCodeHandler;
 }
Пример #33
0
        private void ParseAsyncInternal(VBComponent component, CancellationToken token, TokenStreamRewriter rewriter = null)
        {
            var preprocessor = _preprocessorFactory();
            var parser       = new ComponentParseTask(component, preprocessor, _attributeParser, rewriter);

            parser.ParseFailure += (sender, e) =>
            {
                State.SetModuleState(component, ParserState.Error, e.Cause as SyntaxErrorException);
            };
            parser.ParseCompleted += (sender, e) =>
            {
                lock (State)
                    lock (component)
                    {
                        State.SetModuleAttributes(component, e.Attributes);
                        State.AddParseTree(component, e.ParseTree);
                        State.AddTokenStream(component, e.Tokens);
                        State.SetModuleComments(component, e.Comments);
                        State.SetModuleAnnotations(component, e.Annotations);

                        // This really needs to go last
                        State.SetModuleState(component, ParserState.Parsed);
                    }
            };
            State.SetModuleState(component, ParserState.Parsing);

            parser.Start(token);
        }
Пример #34
0
        private Task <ComponentParseTask.ParseCompletionArgs> FinishedParseComponentTask(QualifiedModuleName module, CancellationToken token, TokenStreamRewriter rewriter = null)
        {
            var tcs = new TaskCompletionSource <ComponentParseTask.ParseCompletionArgs>();

            var preprocessor = _preprocessorFactory();
            var parser       = new ComponentParseTask(module, preprocessor, _attributeParser, _exporter, _state.ProjectsProvider, rewriter);

            parser.ParseFailure += (sender, e) =>
            {
                if (e.Cause is OperationCanceledException)
                {
                    tcs.SetCanceled();
                }
                else
                {
                    tcs.SetException(e.Cause);
                }
            };
            parser.ParseCompleted += (sender, e) =>
            {
                tcs.SetResult(e);
            };

            parser.Start(token);

            return(tcs.Task);
        }