using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Rokojori
{  
  public class CSharpObjectDefinition
  {
    public LexerEvent type;
    public LexerEvent name;


  }

  public class CSharpLexer:Lexer
  {
    public static List<LexerEvent> Lex( string source )
    {
      var lexer = new CSharpLexer();
      var events = lexer.LexToList( source );      

      if ( lexer.hasError )
      {
        return null;
      }

      events.ForEach( ev => { ev.GrabMatch( source ); } );
      return events;
    }

    public static List<CSharpObjectDefinition> GetAllObjectDefinitions( string source )
    {
      var tokens = Lex( source ).Filter( e => ! ( e.isDone || e.isError ) );

      return GetAllObjectDefinitions( tokens );
    }

    public static List<CSharpObjectDefinition> GetAllObjectDefinitions( List<LexerEvent> tokens )
    {
      
      
      var START    = Trillean.True;
      var END      = Trillean.False;
      var INSIDE   = Trillean.Any;

      var sequences = LexerEvent.FindSequences( tokens,         
        ( int index, bool inSequence ) =>
        {
          var le = tokens[ index ];

          if ( 

            le.Is( LexerMatcherLibrary.ClassMatcher ) ||
            le.Is( LexerMatcherLibrary.StructMatcher ) ||
            le.Is( LexerMatcherLibrary.InterfaceMatcher ) ||
            le.Is( LexerMatcherLibrary.RecordMatcher ) ||
            le.Is( LexerMatcherLibrary.EnumMatcher ) 
          )
          {
            return START;
          }
          
          if ( inSequence && le.Is( LexerMatcherLibrary.CwordMatcher ) )
          {
            return END;
          }

          return INSIDE;
        } 
      );

      return sequences.Map( s => new CSharpObjectDefinition{ type = s[ 0 ], name = s[ s.Count - 1 ] } );
    }

    public CSharpLexer()
    {
      AddAllMatchers(
        LexerMatcherLibrary.SingleLineCommentMatcher,
        LexerMatcherLibrary.MultiLineCommentMatcher,
        LexerMatcherLibrary.DoubleQuotedStringMatcher,
        LexerMatcherLibrary.SingleQuotedStringMatcher,
        LexerMatcherLibrary.CInstructionMatcher,
        LexerMatcherLibrary.NumberMatcher,
        LexerMatcherLibrary.NullMatcher,
        LexerMatcherLibrary.BoolMatcher,
        LexerMatcherLibrary.BreakMatcher,
        LexerMatcherLibrary.WhiteSpaceMatcher,
        LexerMatcherLibrary.LogicMatcher,
        LexerMatcherLibrary.BracketMatcher,
        LexerMatcherLibrary.AccessModifierMatcher,
        LexerMatcherLibrary.ClassMatcher,
        LexerMatcherLibrary.EnumMatcher,
        LexerMatcherLibrary.StructMatcher,
        LexerMatcherLibrary.InterfaceMatcher,
        LexerMatcherLibrary.RecordMatcher,
        LexerMatcherLibrary.OperatorMatcher,
        LexerMatcherLibrary.CFunctionMatcher,
        LexerMatcherLibrary.CwordMatcher,
        LexerMatcherLibrary.AnySymbolMatcher
      );
    }
  }
}