示例#1
0
        public Lexer(string source, bool isFileSource)
        {
            if (isFileSource)
            {
                if(!File.Exists(source))
                    throw new FileNotFoundException("The specified source code file was not found.");

                var code = string.Empty;

                try
                {
                    code = File.ReadAllText(source);
                }
                catch (Exception ex)
                {
                    // To do: log here
                    throw;
                }
                Scanner = new Scanner.Scanner(code);
            }
            Scanner = new Scanner.Scanner(source);

            while (Scanner.IsContentInPipeline)
            {
                var character = Scanner.GetCharacter();

                if (character.Cargo == CharacterSymbols.WHITESPACE) // or statement terminator (add whatever is needed)
                {
                    isTokenUnderConstruction = false;
                    if (_currentToken != null)
                    {
                        _currentToken.FinalizeToken();
                        _currentToken.DetermineType();
                        _tokens.Add(_currentToken);
                    }
                    _currentToken = null;
                    continue;
                }

                // Token has be open
                if (_currentToken == null)
                {
                    _currentToken = new Token();
                    isTokenUnderConstruction = false;
                }

                _currentToken.Cargo += character.Cargo;

                if (!isTokenUnderConstruction)
                {
                    isTokenUnderConstruction = true;
                    _currentToken.TokenStartLineNumber = character.SourceLineNumber;
                    _currentToken.TokenStartColumnNumber = character.SourceColumnNumber;
                }

            }
        }