예제 #1
0
        private void ProcessGroup()
        {
            var node = new LogicalQueryNode();

            _lastLogicalQueryNode = node;
            node.Kind             = KindOfLogicalQueryNode.Group;

            var parser = new LogicalExpressionParser(_context, true);

            parser.Run();

#if DEBUG
            //Log($"parser.Result = {parser.Result}");
#endif

            node.Left = parser.Result;

            var intermediateNode = new IntermediateAstNode(node);

            AstNodesLinker.SetNode(intermediateNode, _nodePoint);

            _state = State.GotPredicate;
        }
예제 #2
0
        /// <inheritdoc/>
        protected override void OnRun()
        {
#if DEBUG
            //Log($"_currToken = {_currToken}");
            //Log($"Result = {Result}");
            //Log($"_state = {_state}");
#endif

            if (_currToken.TokenKind != TokenKind.OpenFigureBracket)
            {
                _context.Recovery(_currToken);
            }

            var paser = new LogicalExpressionParser(_context, _terminatingTokenKind);
            paser.Run();

#if DEBUG
            //Log($"paser.Result = {paser.Result}");
#endif

            _baseRulePart.Expression = paser.Result;

            var nextToken = _context.GetToken();

#if DEBUG
            //Log($"nextToken = {nextToken}");
            //Log($"_terminatingTokenKind = {_terminatingTokenKind}");
#endif

            if ((nextToken.TokenKind != _terminatingTokenKind) || nextToken.TokenKind == TokenKind.CloseFactBracket)
            {
                _context.Recovery(nextToken);
            }

            Exit();
        }
예제 #3
0
        /// <inheritdoc/>
        protected override void OnRun()
        {
#if DEBUG
            //Log($"_state = {_state}");
            //Log($"_isGroup = {_isGroup}");
            //Log($"_currToken = {_currToken}");
            //Log($"Result = {Result}");
            //Log($"_nodePoint = {_nodePoint}");
#endif

            if (_terminatingTokenKindList.Contains(_currToken.TokenKind))
            {
                _context.Recovery(_currToken);
                Exit();
                return;
            }

            switch (_state)
            {
            case State.Init:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Word:
                case TokenKind.LogicalVar:
                case TokenKind.Identifier:
                    ProcessWord();
                    break;

                case TokenKind.Entity:
                {
                    var name = NameHelper.CreateName(_currToken.Content);

#if DEBUG
                    //Log($"name = {name}");
#endif

                    var node = new LogicalQueryNode();
                    node.Kind = KindOfLogicalQueryNode.Entity;
                    node.Name = name;

                    var intermediateNode = new IntermediateAstNode(node);

                    AstNodesLinker.SetNode(intermediateNode, _nodePoint);

                    _state = State.GotEntity;
                }
                break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.WaitForPredicateParameter:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Entity:
                case TokenKind.LogicalVar:
                {
                    _context.Recovery(_currToken);

                    var parser = new LogicalExpressionParser(_context);
                    parser.Run();

#if DEBUG
                    //Log($"parser.Result = {parser.Result}");
#endif

                    _lastLogicalQueryNode.ParamsList.Add(parser.Result);

                    _state = State.GotPredicateParameter;
                }
                break;

                case TokenKind.Word:
                case TokenKind.Identifier:
                {
                    switch (_currToken.KeyWordTokenKind)
                    {
                    case KeyWordTokenKind.Null:
                    {
                        _context.Recovery(_currToken);

                        var parser = new NullParser(_context);
                        parser.Run();

#if DEBUG
                        //Log($"parser.Result = {parser.Result}");
#endif

                        var node = new LogicalQueryNode();
                        node.Kind  = KindOfLogicalQueryNode.Value;
                        node.Value = parser.Result;

                        _lastLogicalQueryNode.ParamsList.Add(node);

                        _state = State.GotPredicateParameter;
                    }
                    break;

                    default:
                    {
                        var nextToken = _context.GetToken();

                        var terminatingTokenKindList = new List <TokenKind>()
                        {
                            TokenKind.CloseRoundBracket
                        };

                        if (nextToken.TokenKind != TokenKind.OpenRoundBracket)
                        {
                            terminatingTokenKindList.Add(TokenKind.Comma);
                        }

#if DEBUG
                        //if (_currToken.Content == "distance")
                        //{
                        //throw new NotImplementedException();
                        //}
#endif

#if DEBUG
                        //Log($"nextToken = {nextToken}");
#endif

                        _context.Recovery(_currToken);
                        _context.Recovery(nextToken);


                        var parser = new LogicalExpressionParser(_context, terminatingTokenKindList);
                        parser.Run();

#if DEBUG
                        //Log($"parser.Result = {parser.Result}");
                        //if (_currToken.Content == "distance")
                        //{
                        //    //throw new NotImplementedException();
                        //}
#endif

                        _lastLogicalQueryNode.ParamsList.Add(parser.Result);

                        _state = State.GotPredicateParameter;
                    }
                    break;
                    }
                }
                break;


                case TokenKind.Number:
                {
                    _context.Recovery(_currToken);

                    var parser = new NumberParser(_context);
                    parser.Run();

#if DEBUG
                    //Log($"parser.Result = {parser.Result}");
#endif
                    var node = new LogicalQueryNode();
                    node.Kind  = KindOfLogicalQueryNode.Value;
                    node.Value = parser.Result;

                    _lastLogicalQueryNode.ParamsList.Add(node);

                    _state = State.GotPredicateParameter;
                }
                break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotPredicateParameter:
                switch (_currToken.TokenKind)
                {
                case TokenKind.CloseRoundBracket:
                    _state = State.GotPredicate;
                    break;

                case TokenKind.Comma:
                    _state = State.WaitForPredicateParameter;
                    break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotPredicate:
                switch (_currToken.TokenKind)
                {
                case TokenKind.And:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.And);
                    break;

                case TokenKind.Or:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.Or);
                    break;

                case TokenKind.Comma:
                    _state = State.WaitForPredicateParameter;
                    break;

                case TokenKind.CloseRoundBracket:
                    if (_isGroup)
                    {
                        Exit();
                        break;
                    }
                    throw new UnexpectedTokenException(_currToken);

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotConcept:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Word:
                    switch (_currToken.KeyWordTokenKind)
                    {
                    case KeyWordTokenKind.Is:
                        ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.Is);
                        break;

                    default:
                        throw new UnexpectedTokenException(_currToken);
                    }
                    break;

                case TokenKind.More:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.More);
                    break;

                case TokenKind.MoreOrEqual:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.MoreOrEqual);
                    break;

                case TokenKind.Less:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.Less);
                    break;

                case TokenKind.LessOrEqual:
                    ProcessBinaryOperator(KindOfOperatorOfLogicalQueryNode.LessOrEqual);
                    break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotOperator:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Word:
                case TokenKind.Identifier:
                    ProcessWord();
                    break;

                case TokenKind.OpenRoundBracket:
                    ProcessGroup();
                    break;

                case TokenKind.Not:
                    ProcessUnaryOperator(KindOfOperatorOfLogicalQueryNode.Not);
                    break;

                case TokenKind.LogicalVar:
                {
                    var value = NameHelper.CreateName(_currToken.Content);

                    ProcessConceptOrQuestionVar(value);
                }
                break;

                case TokenKind.Number:
                {
                    _context.Recovery(_currToken);

                    var parser = new NumberParser(_context);
                    parser.Run();

#if DEBUG
                    //Log($"parser.Result = {parser.Result}");
#endif
                    var node = new LogicalQueryNode();
                    node.Kind  = KindOfLogicalQueryNode.Value;
                    node.Value = parser.Result;

                    var intermediateNode = new IntermediateAstNode(node);

                    AstNodesLinker.SetNode(intermediateNode, _nodePoint);

                    _state = State.GotConcept;
                }
                break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            case State.GotFuzzyLogicNonNumericSequenceItem:
                switch (_currToken.TokenKind)
                {
                case TokenKind.Word:
                case TokenKind.Identifier:
                {
                    var value = NameHelper.CreateName(_currToken.Content);

#if DEBUG
                    //Log($"value = {value}");
#endif

                    _fuzzyLogicNonNumericSequenceValue.AddIdentifier(value);

#if DEBUG
                    //Log($"_fuzzyLogicNonNumericSequenceValue = {_fuzzyLogicNonNumericSequenceValue}");
#endif
                }
                break;

                case TokenKind.More:
                case TokenKind.MoreOrEqual:
                case TokenKind.Less:
                case TokenKind.LessOrEqual:
                    _context.Recovery(_currToken);
                    _state = State.GotConcept;
                    break;

                default:
                    throw new UnexpectedTokenException(_currToken);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(_state), _state, null);
            }
        }