示例#1
0
        private IToken ScanNumber(int start)
        {
            bool ParsingFraction = false;

            while (Advancable())
            {
                if (CurrentCharacter.IsNumeric())
                {
                    continue;
                }
                else if (CurrentCharacter.IsWhiteSpace())
                {
                    return(Add(EToken.Number, start, this));
                }
                else if (CurrentCharacter != '.')
                {
                    return(Add(EToken.Number, start, Formula.Substring(start - 1, CharPosition-- - start)));
                }
                else if (ParsingFraction)
                {
                    return(Add(EToken.ScanError, start, this));
                }
                else
                {
                    ParsingFraction = true;
                }
            }
            return(Add(EToken.Number, start, this));
        }
示例#2
0
 private IToken ScanIdent(int start, Func <char, bool> extraChars)
 {
     while (Advancable())
     {
         if (CurrentCharacter.IsAlphanumeric() || extraChars(CurrentCharacter))
         {
             continue;
         }
         else if (CurrentCharacter.IsWhiteSpace())
         {
             return(Add(EToken.Identifier, start, this));
         }
         else
         {
             return(Add(EToken.Identifier, start, Formula.Substring(start - 1, CharPosition-- - start)));
         }
     }
     return(Add(EToken.Identifier, start, this));
 }
示例#3
0
 private IToken ScanStringLiteral(int start)
 {
     while (Advancable())
     {
         if (CurrentCharacter == '"')
         {
             if (!Advancable() || CurrentCharacter.IsWhiteSpace())
             {
                 return(Add(EToken.StringLiteral, start, this));
             }
             else if (CurrentCharacter == '"')
             {
                 continue;
             }
             else
             {
                 return(Add(EToken.StringLiteral, start, Formula.Substring(start - 1, CharPosition-- - start)));
             }
         }
     }
     return(Add(EToken.ScanError, start, this));
 }
示例#4
0
        public IToken Scan()
        {
            if (CharPosition == 1 && CurrentCharacter == '\'')
            {
                return(Add(EToken.StringLiteral, 1, Formula));
            }
            while (Advancable())
            {
                if (!CurrentCharacter.IsWhiteSpace())
                {
                    var start = CharPosition;
                    switch (CurrentCharacter)
                    {
                    case '!': return(Add(EToken.Bang, start, GetText(start)));

                    case '=': return(Add(EToken.EqualsOperator, start, GetText(start)));

                    case ',': return(Add(EToken.Comma, start, GetText(start)));

                    case ';': return(Add(EToken.Semicolon, start, GetText(start)));

                    case '+':
                    case '-':
                    case '%': return(Add(EToken.UnaryOperator, start, GetText(start)));

                    case '*':
                    case '/':
                    case '&':
                    case '^': return(Add(EToken.BinaryOperator, start, GetText(start)));

                    case '<':
                    case '>': if (IsEOT)
                        {
                            break;
                        }
                        if (NextCharacterIs('='))
                        {
                            CharPosition++;
                        }
                        return(Add(EToken.BinaryOperator, start, GetText(start)));

                    case '(': ParenDepth++; return(Add(EToken.OpenParen, start, GetText(start)));

                    case ')': ParenDepth--; return(Add(EToken.CloseParen, start, GetText(start)));

                    case '{': BraceDepth++; return(Add(EToken.OpenBrace, start, GetText(start)));

                    case '}': BraceDepth--; return(Add(EToken.CloseBrace, start, GetText(start)));

                    case '#': return(ScanErrorIdent(start));

                    case '"': return(ScanStringLiteral(start));

                    case '\'': return(ScanClosedExternalRef(start));

                    case '[': return(ScanOpenExternalRef(start));

                    default:
                        if (CurrentCharacter.IsNumeric())
                        {
                            return(ScanNumber(start));
                        }
                        if (CurrentCharacter.IsAlpha())
                        {
                            return(ScanIdentifier(start));
                        }
                        return(Add(EToken.ScanError, start, this));
                    }
                }
            }
            return(IsEOT ? Add(EToken.EOT, CharPosition, this)
                         : Add(EToken.ScanError, CharPosition, this));
        }