Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TokenHandler uses static TokenSeparatorHandler which ignores custom ITokenSeparatorProvider #51

Closed
swmal opened this issue Mar 8, 2020 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@swmal
Copy link
Contributor

swmal commented Mar 8, 2020

Original issue: JanKallman/EPPlus#628

The TokenHandler class calls the static TokenSeparatorHandler.Handle method inside its Handle method

        private void Handle()
        {
            var c = _context.FormulaChars[_tokenIndex];
            Token tokenSeparator;
            if (CharIsTokenSeparator(c, out tokenSeparator))
            {
                if (TokenSeparatorHandler.Handle(c, tokenSeparator, _context, this))
                {
                    return;
                }

The Handle method uses a static collection which contains a MultipleCharSeparatorHandler instance

        private static SeparatorHandler[] _handlers = new SeparatorHandler[]
        { 
            new StringHandler(),
            new BracketHandler(),
            new SheetnameHandler(),
            new MultipleCharSeparatorHandler()
        };

This causes the constructor of MultipleCharSeparatorHandler to use the default TokenSeparatorProvider anyway

        public MultipleCharSeparatorHandler()
            : this(new TokenSeparatorProvider())
        {

        }

This means that it does not use the ITokenSeparatorProvider that was provided initially to the TokenHandler. e.g.

   ITokenSeparatorProvider myTokenSeparatorProvider = new MyTokenSeparatorProvider();

   var tokenFactory = new TokenFactory(myTokenSeparatorProvider, NameValueProvider.Empty, 

   FunctionNameProvider.Empty, false);
   var tokenizer = new SourceCodeTokenizer(tokenFactory, myTokenSeparatorProvider);

Suggested solution:

TokenSeparatorHandler should be non-static and accept a ITokenSeparatorProvider in its constructor to pass on to MultipleCharSeparatorHandler.

TokenHandler should initialize it inside its constructor also passing the ITokenSeparatorProvider
e.g.

         public TokenSeparatorHandler(ITokenSeparatorProvider tokenSeparatorProvider)
	 {
		    _handlers = new SeparatorHandler[]
			{
				new StringHandler(),
				new BracketHandler(),
				new SheetnameHandler(),
				new MultipleCharSeparatorHandler(tokenSeparatorProvider)
			};
	 }

         public TokenHandler(TokenizerContext context, ITokenFactory tokenFactory, 
         ITokenSeparatorProvider tokenProvider)
        {
            _context = context;
            _tokenFactory = tokenFactory;
            _tokenProvider = tokenProvider;
            _tokenSeparatorHandler = new TokenSeparatorHandler(_tokenProvider);
@swmal swmal added the enhancement New feature or request label Mar 8, 2020
@swmal swmal self-assigned this Mar 8, 2020
swmal added a commit that referenced this issue Mar 28, 2020
@swmal swmal closed this as completed Mar 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant