Exemplo n.º 1
0
        public static bool TryFindNumber(CompilationPool pool)
        {
            if (!char.IsDigit(pool.Code[pool.CodePosition]))
            {
                return(false);
            }

            var startIndex = pool.CodePosition;

            var isDouble = false;

            for (; pool.CodePosition < pool.Code.Length; pool.CodePosition++)
            {
                if (char.IsDigit(pool.Code[pool.CodePosition]))
                {
                    continue;
                }
                if (pool.Code[pool.CodePosition] == '.' && isDouble == false)
                {
                    isDouble = true;
                    continue;
                }
                break;
            }
            pool.Tokens.Add(new Token(TokenClass.Literal, 0,
                                      pool.Code.Substring(startIndex, pool.CodePosition - startIndex)));

            return(true);
        }
        public static bool IsIndentifier(CompilationPool pool)
        {
            var identifierName = GetIdentifierName(pool);

            if (identifierName.Length == 0)
            {
                return(false);
            }

            pool.CodePosition += identifierName.Length;

            var existingIdenfitier =
                pool.Identifiers.FirstOrDefault(
                    identifier => identifier.Identity.Equals(identifierName) && identifier.ScopeLevel.Equals(Scope));

            if (existingIdenfitier == null)
            {
                pool.Identifiers.Add(new Identifier(identifierName));
            }

            pool.Tokens.Add(new Token(TokenClass.Identifier,
                                      existingIdenfitier == null
                    ? pool.Identifiers.Count - 1
                    : pool.Identifiers.IndexOf(existingIdenfitier), identifierName));

            return(true);
        }
Exemplo n.º 3
0
        private bool TryFindToken(CompilationPool pool)
        {
            for (var stateNumber = 0;; pool.CodePosition++)
            {
                var state  = _states.ElementAt(stateNumber);
                var symbol = pool.Code[pool.CodePosition];

                if (!HasNextSymbol(pool, _tokenClass))
                {
                    if (state.Transitions.ContainsKey(symbol))
                    {
                        state = _states.ElementAt(state.Transitions[symbol]);
                    }
                    else
                    {
                        return(false);
                    }

                    // last chanсe to determine token
                    if (!state.IsFinal)
                    {
                        return(false);
                    }

                    pool.Tokens.Add(new Token(_tokenClass, state.TokenNumber));

                    pool.CodePosition++;
                    return(true);
                }

                var subStateNumber = state.Transitions.Where(pair => pair.Key.Equals(symbol)).Select(pair => pair.Value)
                                     .FirstOrDefault();

                if (subStateNumber != 0)
                {
                    var subState = _states.ElementAt(subStateNumber);

                    if (!subState.Transitions.Any() && subState.IsFinal)
                    {
                        pool.Tokens.Add(new Token(_tokenClass, subState.TokenNumber));
                        pool.CodePosition++;
                        return(true);
                    }
                }

                if (!state.Transitions.ContainsKey(symbol))
                {
                    return(false);
                }
                // go to next state
                stateNumber = state.Transitions[symbol];
            }
        }
Exemplo n.º 4
0
        public bool FindToken(CompilationPool pool)
        {
            var currentPosition = pool.CodePosition;

            if (TryFindToken(pool))
            {
                return(true);
            }

            // if token not found, restore code-cursor position
            pool.CodePosition = currentPosition;
            return(false);
        }
Exemplo n.º 5
0
        public static bool IsLiteral(CompilationPool pool)
        {
            var currentPosition = pool.CodePosition;

            if (TryFindString(pool))
            {
                return(true);
            }
            if (TryFindNumber(pool))
            {
                return(true);
            }
            pool.CodePosition = currentPosition;
            return(false);
        }
        private static string GetIdentifierName(CompilationPool pool)
        {
            var position = pool.CodePosition;

            if (!char.IsLetterOrDigit(pool.Code[position]))
            {
                return("");
            }

            while (char.IsLetterOrDigit(pool.Code[position]) ||
                   pool.Code[position].Equals('_') && position < pool.Code.Length)
            {
                position++;
            }

            return(pool.Code.Substring(pool.CodePosition, position - pool.CodePosition));
        }
Exemplo n.º 7
0
        public static bool TryFindString(CompilationPool pool)
        {
            if (pool.Code[pool.CodePosition] != '"')
            {
                return(false);
            }

            var index = pool.Code.IndexOf('"', pool.CodePosition + 1);

            if (index == -1)
            {
                return(false);
            }

            pool.Tokens.Add(new Token(TokenClass.Literal, 0,
                                      pool.Code.Substring(pool.CodePosition + 1, index - pool.CodePosition - 1)));
            pool.CodePosition = index + 1;
            return(true);
        }
Exemplo n.º 8
0
        public static bool HasNextSymbol(CompilationPool pool, TokenClass tokenClass)
        {
            if (pool.CodePosition + 1 == pool.Code.Length)
            {
                return(false);
            }

            var symbol = pool.Code[pool.CodePosition + 1].ToString();

            if (Constraints.Instance.Tokens.SkippedSymbols.Contains(symbol[0]))
            {
                return(false);
            }

            if (Constraints.Instance.Borders.ForSpecialChars.Contains(symbol))
            {
                return(false);
            }

            var result = Constraints.Instance.Borders.ForTokenClasses[tokenClass].Contains(symbol);

            return(result);
        }