Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

TokenHandler uses static TokenSeparatorHandler which ignores custom ITokenSeparatorProvider #628

Closed
jjamid opened this issue Mar 4, 2020 · 1 comment

Comments

@jjamid
Copy link

jjamid commented Mar 4, 2020

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
Copy link
Collaborator

swmal commented Mar 8, 2020

Agree, this looks like a design flaw. Have copied this issue to our new repo https://github.com/EPPlusSoftware/EPPlus

@swmal swmal closed this as completed Mar 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants