Пример #1
0
        /// <summary>
        /// The actual matching method of the token kind against the input text.
        /// </summary>
        protected override sealed MatchResult Match(IInputText text)
        {
            var peek = text.Peek(_symbol.Length);

            if (peek == _symbol)
            {
                return(MatchResult.Succeed(peek));
            }

            return(MatchResult.Fail);
        }
Пример #2
0
        protected static Token GetToken(IInputText text, IEnumerable <MatchableTokenKind> tokenKinds)
        {
            foreach (var kind in tokenKinds)
            {
                if (kind.TryMatch(text, out Token token))
                {
                    return(token);
                }
            }

            return(null);
        }
Пример #3
0
        protected virtual Token GetToken(IInputText text)
        {
            foreach (var kind in TokenKinds)
            {
                if (kind.TryMatch(text, out Token token))
                {
                    return(token);
                }
            }

            return(null);
        }
Пример #4
0
        public TwoOperandsCalculator(IOutputText outputText, IInputText inputText)
        {
            this.OutputText = outputText;
            this.InputText  = inputText;

            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.PreOperatorNumber, 0, MemoryFieldNames.Categories.Standard));
            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.PostOperatorNumber, 0, MemoryFieldNames.Categories.Standard));
            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.YearsNumber, 0, MemoryFieldNames.Categories.Special));
            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.EffectiveInterestNumber, 0, MemoryFieldNames.Categories.Special));
            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.StartNumber, 0, MemoryFieldNames.Categories.Special));
            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.RateNumber, 0, MemoryFieldNames.Categories.Special));
            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.EndNumber, 0, MemoryFieldNames.Categories.Special));
            this.MemoryFields.Add(new SimpleMemoryField <int>(MemoryFieldNames.RatesPerAnnumNumber, 12, MemoryFieldNames.Categories.Special));
            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.NominalInterestRateNumber, 0, MemoryFieldNames.Categories.Special));
            this.MemoryFields.Add(new SimpleMemoryField <double>(MemoryFieldNames.RepaymentRateNumber, 0, MemoryFieldNames.Categories.Special));
            this.MemoryFields.Add(new SimpleMemoryField <bool>(MemoryFieldNames.IsAdvance, false, MemoryFieldNames.Categories.Special));
        }
Пример #5
0
        /// <summary>
        /// Tries to match the token kind against the input text.
        /// </summary>
        /// <param name="text">The input text. Not null.</param>
        /// <param name="token">The token of this kind if matching succeeds. Null otherwise.</param>
        /// <returns></returns>
        public bool TryMatch(IInputText text, out Token token)
        {
            var match = Match(text);

            if (match.Success)
            {
                var matchValue = match.Value;

                if (string.IsNullOrEmpty(matchValue))
                {
                    throw new InvalidOperationException("A successful match should always yield a non-empty lexeme.");
                }

                token = new Token(this, text.Position, matchValue);
                return(true);
            }

            token = null;
            return(false);
        }
Пример #6
0
 public TokenizationException(IInputText inputText, string message) : base(message)
 {
     InputText = inputText;
 }
Пример #7
0
 /// <summary>
 /// Creates a lexeme for the token.
 /// </summary>
 /// <param name="text">The input text. Not null.</param>
 /// <remarks>The text position should not be amended.</remarks>
 protected abstract string CreateLexeme(IInputText text);
Пример #8
0
 /// <summary>
 /// Creates a token of this kind at the current position of input text.
 /// </summary>
 /// <param name="text">The input text. Not null.</param>
 public Token CreateTokenAtCurrentPosition(IInputText text)
 {
     return(new Token(this, text.Position, CreateLexeme(text)));
 }
Пример #9
0
 /// <summary>
 /// The actual matching against the input text.
 /// </summary>
 /// <returns>The matching result. Not null. A successful match should always yield a non-empty lexeme.</returns>
 protected abstract MatchResult Match(IInputText text);
Пример #10
0
 /// <summary>
 /// Returns null.
 /// </summary>
 /// <param name="text">The input text. Not used.</param>
 protected sealed override string CreateLexeme(IInputText text) => null;
Пример #11
0
 /// <summary>
 /// Creates a lexeme for the token by peeking 50 characters starting at the current position.
 /// </summary>
 /// <param name="text">The input text. Not null.</param>
 /// <remarks>The text position is not be amended.</remarks>
 protected sealed override string CreateLexeme(IInputText text) => text.Peek(50);
Пример #12
0
 /// <summary>
 /// The actual matching method of the token kind against the input text.
 /// </summary>
 protected override sealed MatchResult Match(IInputText text) => text.Match(_regex);
Пример #13
0
 protected Token CreateUnknownToken(IInputText text) => _unknown.CreateTokenAtCurrentPosition(text);