Пример #1
0
        private static ConstDefinition CreateConstDefinition(ref Lexem lex)
        {
            DataType constType = DataType.Undefined;
            switch (lex.Type)
            {
                case LexemType.BooleanLiteral:
                    constType = DataType.Boolean;
                    break;
                case LexemType.DateLiteral:
                    constType = DataType.Date;
                    break;
                case LexemType.NumberLiteral:
                    constType = DataType.Number;
                    break;
                case LexemType.StringLiteral:
                    constType = DataType.String;
                    break;
                case LexemType.NullLiteral:
                    constType = DataType.GenericValue;
                    break;
            }

            ConstDefinition cDef = new ConstDefinition()
            {
                Type = constType,
                Presentation = lex.Content
            };
            return cDef;
        }
Пример #2
0
 public static bool IsIdentifier(ref Lexem lex)
 {
     return lex.Type == LexemType.Identifier;
 }
Пример #3
0
 public static bool IsUserSymbol(ref Lexem lex)
 {
     return lex.Type == LexemType.Identifier && lex.Token == Token.NotAToken;
 }
Пример #4
0
 public static bool IsLiteral(ref Lexem lex)
 {
     return lex.Type == LexemType.StringLiteral
         || lex.Type == LexemType.NumberLiteral
         || lex.Type == LexemType.BooleanLiteral
         || lex.Type == LexemType.DateLiteral
         || lex.Type == LexemType.UndefinedLiteral
         || lex.Type == LexemType.NullLiteral;
 }
Пример #5
0
        public override Lexem ReadNextLexem(ParseIterator iterator)
        {
            StringBuilder ContentBuilder = new StringBuilder();

            while (iterator.MoveNext())
            {
                var cs = iterator.CurrentSymbol;

                if (cs == SpecialChars.StringQuote)
                {
                    if (iterator.MoveNext())
                    {
                        if (iterator.CurrentSymbol == SpecialChars.StringQuote)
                        {
                            /* Двойная кавычка */
                            ContentBuilder.Append("\"");
                            continue;
                        }

                        /* Завершение строки */
                        SkipSpacesAndComments(iterator);

                        if (iterator.CurrentSymbol == SpecialChars.StringQuote)
                        {
                            /* Сразу же началась новая строка */
                            ContentBuilder.Append('\n');
                            continue;
                        }
                    }

                    var lex = new Lexem
                    {
                        Type    = LexemType.StringLiteral,
                        Content = ContentBuilder.ToString()
                    };
                    return(lex);
                }
                else if (cs == '\r')
                {
                    continue;
                }
                else if (cs == '\n')
                {
                    iterator.MoveNext();
                    SkipSpacesAndComments(iterator);

                    if (iterator.CurrentSymbol != '|')
                    {
                        throw CreateExceptionOnCurrentLine("Некорректный строковый литерал!", iterator);
                    }

                    ContentBuilder.Append('\n');

                    continue;
                }
                else
                {
                    ContentBuilder.Append(cs);
                }
            }

            throw CreateExceptionOnCurrentLine("Незавершённый строковой интервал!", iterator);
        }
Пример #6
0
        public override Lexem ReadNextLexem(ParseIterator iterator)
        {
            StringBuilder ContentBuilder = new StringBuilder();

            while (iterator.MoveNext())
            {
                var cs = iterator.CurrentSymbol;

                if (cs == SpecialChars.StringQuote)
                {
                    iterator.MoveNext();
                    if (iterator.CurrentSymbol == SpecialChars.StringQuote)
                    { /* Двойная кавычка */
                        ContentBuilder.Append("\"");
                        continue;
                    }

                    /* Завершение строки */
                    SkipSpacesAndComments(iterator);

                    if (iterator.CurrentSymbol == SpecialChars.StringQuote)
                    {   /* Сразу же началась новая строка */
                        ContentBuilder.Append('\n');
                        continue;
                    }

                    var lex = new Lexem
                    {
                        Type = LexemType.StringLiteral,
                        Content = ContentBuilder.ToString()
                    };
                    return lex;
                }
                else if(cs == '\r')
                {
                    continue;
                }
                else if (cs == '\n')
                {
                    iterator.MoveNext();
                    SkipSpacesAndComments(iterator);

                    if (iterator.CurrentSymbol != '|')
                        throw CreateExceptionOnCurrentLine("Некорректный строковый литерал!", iterator);

                    ContentBuilder.Append('\n');

                    continue;
                }
                else
                    ContentBuilder.Append(cs);

            }

            throw CreateExceptionOnCurrentLine("Незавершённый строковой интервал!", iterator);

        }
Пример #7
0
 private static Lexem ExtractOperatorContent(ParseIterator iterator)
 {
     Lexem lex;
     var content = iterator.GetContents().content;
     lex = new Lexem()
     {
         Type = LexemType.Operator,
         Content = content,
         Token = LanguageDef.GetToken(content)
     };
     return lex;
 }
Пример #8
0
        public Lexem NextLexem()
        {
            _state = _emptyState;

            while (true)
            {
                if (_iterator.MoveToContent())
                {
                    char cs = _iterator.CurrentSymbol;
                    if (Char.IsLetter(cs) || cs == '_')
                    {
                        _state = _wordState;
                    }
                    else if (Char.IsDigit(cs))
                    {
                        _state = _numberState;
                    }
                    else if (cs == SpecialChars.DateQuote)
                    {
                        _state = _dateState;
                    }
                    else if (cs == SpecialChars.StringQuote)
                    {
                        _state = _stringState;
                    }
                    else if (SpecialChars.IsOperatorChar(cs))
                    {
                        _state = _operatorState;
                    }
                    else if (cs == SpecialChars.EndOperator)
                    {
                        _iterator.MoveNext();
                        return(new Lexem()
                        {
                            Type = LexemType.EndOperator, Token = Token.Semicolon
                        });
                    }
                    else if (cs == '?')
                    {
                        _iterator.MoveNext();
                        return(new Lexem()
                        {
                            Type = LexemType.Identifier, Token = Token.Question
                        });
                    }
                    else
                    {
                        var cp = _iterator.GetPositionInfo(_iterator.CurrentLine);
                        throw new ParserException(cp, string.Format("Неизвестный символ {0}", cs));
                    }

                    var lex = _state.ReadNextLexem(_iterator);
                    if (lex.Type == LexemType.NotALexem)
                    {
                        _state = _emptyState;
                        continue;
                    }

                    return(lex);
                }
                else
                {
                    return(Lexem.EndOfText());
                }
            }
        }
Пример #9
0
 private bool IsLiteral(ref Lexem lex)
 {
     return LanguageDef.IsLiteral(ref lex);
 }
Пример #10
0
        public override Lexem ReadNextLexem(ParseIterator iterator)
        {
            bool isEndOfText = false;
            Char cs          = '\0';

            while (true)
            {
                if (!isEndOfText)
                {
                    cs = iterator.CurrentSymbol;
                }
                if (SpecialChars.IsDelimiter(cs) || isEndOfText)
                {
                    var content = iterator.GetContents().content;

                    Lexem lex;

                    if (String.Compare(content, "и", true) == 0 ||
                        String.Compare(content, "или", true) == 0 ||
                        String.Compare(content, "не", true) == 0)
                    {
                        lex = new Lexem()
                        {
                            Type    = LexemType.Operator,
                            Token   = LanguageDef.GetToken(content),
                            Content = content
                        };
                    }
                    else if (String.Compare(content, "истина", true) == 0 ||
                             String.Compare(content, "ложь", true) == 0)
                    {
                        lex = new Lexem()
                        {
                            Type    = LexemType.BooleanLiteral,
                            Content = content
                        };
                    }
                    else if (String.Compare(content, "неопределено", true) == 0)
                    {
                        lex = new Lexem()
                        {
                            Type    = LexemType.UndefinedLiteral,
                            Content = content
                        };
                    }
                    else if (String.Compare(content, "null", true) == 0)
                    {
                        lex = new Lexem()
                        {
                            Type    = LexemType.NullLiteral,
                            Content = content
                        };
                    }
                    else
                    {
                        lex = new Lexem()
                        {
                            Type    = LexemType.Identifier,
                            Content = content,
                            Token   = LanguageDef.GetToken(content)
                        };

                        if (LanguageDef.IsBuiltInFunction(lex.Token))
                        {
                            iterator.SkipSpaces();
                            if (iterator.CurrentSymbol != '(')
                            {
                                lex.Token = Token.NotAToken;
                            }
                        }
                    }

                    return(lex);
                }

                if (!iterator.MoveNext())
                {
                    if (isEndOfText)
                    {
                        break;
                    }
                    else
                    {
                        isEndOfText = true;
                    }
                }
            }

            return(Lexem.Empty());
        }
Пример #11
0
        public override Lexem ReadNextLexem(ParseIterator iterator)
        {
            if (iterator.CurrentSymbol == '<')
            {
                if (iterator.MoveNext())
                {
                    var next = iterator.CurrentSymbol;
                    if (next != '>' && next != '=')
                    {
                        iterator.MoveBack();
                    }
                    else
                    {
                        iterator.MoveNext();
                        return(ExtractOperatorContent(iterator));
                    }
                }
            }
            else if (iterator.CurrentSymbol == '>')
            {
                if (iterator.MoveNext())
                {
                    var next = iterator.CurrentSymbol;
                    if (next != '=')
                    {
                        iterator.MoveBack();
                    }
                    else
                    {
                        iterator.MoveNext();
                        return(ExtractOperatorContent(iterator));
                    }
                }
            }
            else if (iterator.CurrentSymbol == '/')
            {
                if (iterator.MoveNext())
                {
                    if (iterator.CurrentSymbol == '/')
                    {
                        // это комментарий
                        while (iterator.MoveNext())
                        {
                            if (iterator.CurrentSymbol == '\n')
                            {
                                iterator.GetContents();
                                return(Lexem.Empty());
                            }
                        }
                        iterator.GetContents();
                        return(Lexem.EndOfText());
                    }
                    else
                    {
                        iterator.MoveBack();
                    }
                }
            }

            var lex = ExtractOperatorContent(iterator);

            iterator.MoveNext();

            return(lex);
        }
Пример #12
0
 public override Lexem ReadNextLexem(ParseIterator iterator)
 {
     return(Lexem.Empty());
 }
Пример #13
0
        public override Lexem ReadNextLexem(ParseIterator iterator)
        {
            bool isEndOfText = false;
            char cs          = '\0';
            int  currentLine = iterator.CurrentLine;

            while (true)
            {
                if (!isEndOfText)
                {
                    cs = iterator.CurrentSymbol;
                }
                if (SpecialChars.IsDelimiter(cs) || isEndOfText)
                {
                    var content = iterator.GetContents().content;

                    Lexem lex;

                    if (_booleanOperators.Contains(content))
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.Operator,
                            Token      = LanguageDef.GetToken(content),
                            Content    = content,
                            LineNumber = currentLine
                        };
                    }
                    else if (_booleanLiterals.Contains(content))
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.BooleanLiteral,
                            Content    = content,
                            LineNumber = currentLine
                        };
                    }
                    else if (_undefined.Contains(content))
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.UndefinedLiteral,
                            Content    = content,
                            LineNumber = currentLine
                        };
                    }
                    else if (String.Compare(content, "null", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.NullLiteral,
                            Content    = content,
                            LineNumber = currentLine
                        };
                    }
                    else
                    {
                        lex = new Lexem()
                        {
                            Type       = LexemType.Identifier,
                            Content    = content,
                            Token      = LanguageDef.GetToken(content),
                            LineNumber = currentLine
                        };

                        if (LanguageDef.IsBuiltInFunction(lex.Token))
                        {
                            iterator.SkipSpaces();
                            if (iterator.CurrentSymbol != '(')
                            {
                                lex.Token = Token.NotAToken;
                            }
                        }
                    }

                    return(lex);
                }

                if (!iterator.MoveNext())
                {
                    if (isEndOfText)
                    {
                        break;
                    }
                    else
                    {
                        isEndOfText = true;
                    }
                }
            }

            return(Lexem.Empty());
        }
Пример #14
0
 private bool IsContinuationToken(ref Lexem lex)
 {
     return lex.Token == Token.Dot || lex.Token == Token.OpenBracket;
 }
Пример #15
0
 public static bool IsUserSymbol(ref Lexem lex)
 {
     return(lex.Type == LexemType.Identifier && lex.Token == Token.NotAToken);
 }
Пример #16
0
 private bool IsUserSymbol(ref Lexem lex)
 {
     return LanguageDef.IsUserSymbol(ref lex);
 }
Пример #17
0
        public override Lexem ReadNextLexem(ParseIterator iterator)
        {
            bool isEndOfText = false;
            Char cs = '\0';
            while (true)
            {
                if (!isEndOfText)
                {
                    cs = iterator.CurrentSymbol;
                }
                if (SpecialChars.IsDelimiter(cs) || isEndOfText)
                {
                    var content = iterator.GetContents().content;
                    
                    Lexem lex;

                    if(_booleanOperators.Contains(content))
                    {
                        lex = new Lexem()
                        {
                            Type = LexemType.Operator,
                            Token = LanguageDef.GetToken(content),
                            Content = content
                        };
                    }
                    else if (_booleanLiterals.Contains(content))
                    {
                        lex = new Lexem()
                        {
                            Type = LexemType.BooleanLiteral,
                            Content = content
                        };
                    }
                    else if (_undefined.Contains(content))
                    {
                        lex = new Lexem()
                        {
                            Type = LexemType.UndefinedLiteral,
                            Content = content
                        };

                    }
                    else if (String.Compare(content, "null", true) == 0)
                    {
                        lex = new Lexem()
                        {
                            Type = LexemType.NullLiteral,
                            Content = content
                        };

                    }
                    else
                    {
                        lex = new Lexem()
                        {
                            Type = LexemType.Identifier,
                            Content = content,
                            Token = LanguageDef.GetToken(content)
                        };

                        if (LanguageDef.IsBuiltInFunction(lex.Token))
                        {
                            iterator.SkipSpaces();
                            if (iterator.CurrentSymbol != '(')
                            {
                                lex.Token = Token.NotAToken;
                            }
                        }
                    }

                    return lex;
                }

                if (!iterator.MoveNext())
                {
                    if (isEndOfText)
                    {
                        break;
                    }
                    else
                    {
                        isEndOfText = true;
                    }
                }
            }

            return Lexem.Empty();
        }
Пример #18
0
 private bool IsValidIdentifier(ref Lexem lex)
 {
     return lex.Type == LexemType.Identifier;
 }
Пример #19
0
        public override Lexem ReadNextLexem(ParseIterator iterator)
        {
            var numbers = new StringBuilder();

            while (iterator.MoveNext())
            {
                var cs = iterator.CurrentSymbol;
                if (cs == SpecialChars.DateQuote)
                {
                    iterator.GetContents(1, 1);
                    iterator.MoveNext();

                    var lex = new Lexem()
                    {
                        Type = LexemType.DateLiteral,
                        Content = numbers.ToString()
                    };

                    return lex;
                }
                else if(Char.IsDigit(cs))
                {
                    numbers.Append(cs);
                }

                if (numbers.Length > 14)
                    throw CreateExceptionOnCurrentLine("Некорректный литерал даты", iterator);
            }

            throw CreateExceptionOnCurrentLine("Незавершенный литерал даты", iterator);
        }
Пример #20
0
 private void NextToken()
 {
     if (_lastExtractedLexem.Token != Token.EndOfText)
     {
         _lastExtractedLexem = _parser.NextLexem();
     }
     else
     {
         throw CompilerException.UnexpectedEndOfText();
     }
 }
Пример #21
0
        public override Lexem ReadNextLexem(ParseIterator iterator)
        {
            bool hasDecimalPoint = false;
            while (true)
            {
                if (Char.IsDigit(iterator.CurrentSymbol))
                {
                    if (!iterator.MoveNext())
                    {
                        var lex = new Lexem()
                        {
                            Type = LexemType.NumberLiteral,
                            Content = iterator.GetContents().content
                        };

                        return lex;
                    }
                }
                else if (SpecialChars.IsDelimiter(iterator.CurrentSymbol))
                {
                    if (iterator.CurrentSymbol == '.')
                    {
                        if (!hasDecimalPoint)
                        {
                            hasDecimalPoint = true;
                            iterator.MoveNext();
                            continue;
                        }
                        else
                        {
                            throw CreateExceptionOnCurrentLine("Некорректно указана десятичная точка в числе", iterator);
                        }
                    }

                    var lex = new Lexem()
                    {
                        Type = LexemType.NumberLiteral,
                        Content = iterator.GetContents().content
                    };

                    return lex;
                }
                else
                {
                    throw CreateExceptionOnCurrentLine("Некорректный символ", iterator);
                }
            }
        }
Пример #22
0
 public static bool IsIdentifier(ref Lexem lex)
 {
     return(lex.Type == LexemType.Identifier);
 }