Next() 공개 메소드

Returns the next token to be used by the parser.
public Next ( ) : PrologToken
리턴 PrologToken
예제 #1
0
        public void CurrentTest()
        {
            Write("current.txt", "scary Variable  ");
            StreamReader stream = new StreamReader("C:\\current.txt");
            scanner = new PrologScanner(stream);
            scanner.Next();
            Assert.AreEqual(PrologToken.ATOM, scanner.Current.Kind);

            scanner.Next();
            Assert.AreEqual(PrologToken.VARIABLE, scanner.Current.Kind);

            stream.Close();
        }
        public PrologCodeUnit Parse(TextReader input)
        {
            _scanner = new PrologScanner(input);
            _errors.Clear();

            PrologCodeTerm term = null;

            while (true)
            {
                term = ReadTerm(1200);
                if (_scanner.Next().Kind != PrologToken.DOT)
                {
                    _errors.Add(new PrologCompilerError("P0001", "Unexpected end of term", "", false, _scanner.Current.Line, _scanner.Current.Column));
                    break;
                }
                if (term is PrologCodeHeadlessClause)
                {
                    ProcessHeadlessClause(term);
                }
                else
                {
                    _codeUnit.Terms.Add(term);
                }
                if (_scanner.Lookahead.Kind == PrologToken.EOF)
                {
                    break;
                }
            }
            return(_codeUnit);
        }
예제 #3
0
 public void DotTest()
 {
     Write("dot.txt", " . ");
     StreamReader stream = new StreamReader("C:\\dot.txt");
     scanner = new PrologScanner(stream);
     scanner.Next();
     Assert.AreEqual(PrologToken.DOT, scanner.Current.Kind);
     stream.Close();
 }
예제 #4
0
 public void CommaTest()
 {
     Write("comma.txt", " ,  ");
     StreamReader stream = new StreamReader("C:\\comma.txt");
     scanner = new PrologScanner(stream);
     scanner.Next();
     Assert.AreEqual(PrologToken.COMMA, scanner.Current.Kind);
     stream.Close();
 }
예제 #5
0
 public void AtomTest()
 {
     Write("atom.txt", " atom  ");
     StreamReader stream = new StreamReader("C:\\atom.txt");
     scanner = new PrologScanner(stream);
     scanner.Next();
     Assert.AreEqual(PrologToken.ATOM, scanner.Current.Kind);
     stream.Close();
 }
예제 #6
0
        public void FactTokensRule()
        {
            Write("fact_tokens.txt", "male(ali,hodroj,X,'Fine not really!',[A|B]).");
            StreamReader stream = new StreamReader("C:\\fact_tokens.txt");
            scanner = new PrologScanner(stream);
            // male
            scanner.Next();
            Assert.AreEqual(PrologToken.ATOM, scanner.Current.Kind);

            // (
            scanner.Next();
            Assert.AreEqual(PrologToken.LPAREN, scanner.Current.Kind);

            // ali
            scanner.Next();
            Assert.AreEqual(PrologToken.ATOM, scanner.Current.Kind);

            // ,
            scanner.Next();
            Assert.AreEqual(PrologToken.COMMA, scanner.Current.Kind);

            // hodroj
            scanner.Next();
            Assert.AreEqual(PrologToken.ATOM, scanner.Current.Kind);

            // ,
            scanner.Next();
            Assert.AreEqual(PrologToken.COMMA, scanner.Current.Kind);

            // X
            scanner.Next();
            Assert.AreEqual(PrologToken.VARIABLE, scanner.Current.Kind);

            // ,
            scanner.Next();
            Assert.AreEqual(PrologToken.COMMA, scanner.Current.Kind);

            // 'Fine Not Really!'
            scanner.Next();
            Assert.AreEqual(PrologToken.ATOM, scanner.Current.Kind);

            // ,
            scanner.Next();
            Assert.AreEqual(PrologToken.COMMA, scanner.Current.Kind);

            // [
            scanner.Next();
            Assert.AreEqual(PrologToken.LBRACKET, scanner.Current.Kind);

            // A
            scanner.Next();
            Assert.AreEqual(PrologToken.VARIABLE, scanner.Current.Kind);

            // |
            scanner.Next();
            Assert.AreEqual(PrologToken.LIST_SEP, scanner.Current.Kind);

            // B
            scanner.Next();
            Assert.AreEqual(PrologToken.VARIABLE, scanner.Current.Kind);

            // ]
            scanner.Next();
            Assert.AreEqual(PrologToken.RBRACKET, scanner.Current.Kind);

            // )
            scanner.Next();
            Assert.AreEqual(PrologToken.RPAREN, scanner.Current.Kind);

            // .
            scanner.Next();
            Assert.AreEqual(PrologToken.DOT, scanner.Current.Kind);

            stream.Close();
        }
예제 #7
0
 public void VariableTest()
 {
     Write("var.txt", "Father  ");
     StreamReader stream = new StreamReader("C:\\var.txt");
     scanner = new PrologScanner(stream);
     scanner.Next();
     Assert.AreEqual(PrologToken.VARIABLE, scanner.Current.Kind);
     stream.Close();
 }
예제 #8
0
        public void StringTest()
        {
            Write("string.txt", " 'This is a string'  ");

            StreamReader stream = new StreamReader("C:\\string.txt");
            scanner = new PrologScanner(stream);
            scanner.Next();
            Assert.AreEqual(PrologToken.ATOM, scanner.Current.Kind);
            stream.Close();
        }
예제 #9
0
 public void RParenTest()
 {
     Write("rparen.txt", "     )  ");
     StreamReader stream = new StreamReader("C:\\rparen.txt");
     scanner = new PrologScanner(stream);
     scanner.Next();
     Assert.AreEqual(PrologToken.RPAREN, scanner.Current.Kind);
     stream.Close();
 }
예제 #10
0
 public void RBracketTest()
 {
     Write("rbra.txt", " ]  ");
     StreamReader stream = new StreamReader("C:\\rbra.txt");
     scanner = new PrologScanner(stream);
     scanner.Next();
     Assert.AreEqual(PrologToken.RBRACKET, scanner.Current.Kind);
     stream.Close();
 }
예제 #11
0
 public void ListSepTest()
 {
     Write("sep.txt", " |  ");
     StreamReader stream = new StreamReader("C:\\sep.txt");
     scanner = new PrologScanner(stream);
     scanner.Next();
     Assert.AreEqual(PrologToken.LIST_SEP, scanner.Current.Kind);
     stream.Close();
 }
예제 #12
0
        public BinaryTree Term(int n)
        {
            int        m   = 0;
            BinaryTree ast = null;

            _scanner.Next();
            switch (_scanner.Current.Kind)
            {
            case PrologToken.LPAREN:
                ast = Term(1200);
                _scanner.Next();
                if (_scanner.Current.Kind != PrologToken.RPAREN)
                {
                    _errors.Add(new PrologCompilerError("P0003", "Expected ) after term", "", false, _scanner.Current.Line, _scanner.Current.Column));
                }
                break;

            // Handle list terms here...
            case PrologToken.LBRACKET:
                ArrayList listArguments = new ArrayList();
                // Peek after [ token
                if (_scanner.Lookahead.Kind == PrologToken.RBRACKET)
                {
                    _scanner.Next();
                    // return a nil atom: []
                    ast = new BinaryList();     // empty list
                    break;
                }
                listArguments.Add(Term(999));
                _scanner.Next();

                // if , is encountered
                while (_scanner.Current.Kind == PrologToken.COMMA)
                {
                    listArguments.Add(Term(999));
                    _scanner.Next();
                }
                if (_scanner.Current.Kind == PrologToken.LIST_SEP)
                {
                    listArguments.Add(Term(999));
                    _scanner.Next();
                }
                else
                {
                    listArguments.Add(new BinaryList());
                }
                if (_scanner.Current.Kind != PrologToken.RBRACKET)
                {
                    _errors.Add(new PrologCompilerError("P0004", "Unterminated list, expected ]", "", false, _scanner.Current.Line, _scanner.Current.Column));
                }

                int        i    = listArguments.Count - 1;
                BinaryList list = new BinaryList((BinaryTree)listArguments[i - 1], (BinaryTree)listArguments[i]);
                i -= 2;
                while (i > -1)
                {
                    list = new BinaryList((BinaryTree)listArguments[i--], list);
                }
                ast = list;
                break;

            case PrologToken.RPAREN:
            case PrologToken.RBRACKET:
            case PrologToken.COMMA:
            case PrologToken.DOT:
                _errors.Add(new PrologCompilerError("P0005", "Unexpected closure of term", "", false, _scanner.Current.Line, _scanner.Current.Column));
                return(null);

            case PrologToken.ATOM:
            case PrologToken.VARIABLE:
                string atomName = _scanner.Current.StringValue;
                if (_scanner.Lookahead.Kind == PrologToken.LPAREN)
                {
                    ArrayList arguments = new ArrayList();
                    _scanner.Next();
                    arguments.Add(Term(1200));

                    _scanner.Next();
                    while (_scanner.Current.Kind == PrologToken.COMMA)
                    {
                        arguments.Add(Term(1200));
                        _scanner.Next();
                    }
                    if (_scanner.Current.Kind != PrologToken.RPAREN)
                    {
                        _errors.Add(new PrologCompilerError("P0005", "Unexpected closure of term", "", false, _scanner.Current.Line, _scanner.Current.Column));
                        return(null);
                    }
                    ast = new BinaryTree(atomName, arguments);
                    break;
                }
                if (_operators.IsOperator(_scanner.Current.StringValue))
                {
                    PrologOperator op = _operators.GetOperator(_scanner.Current.StringValue);
                    if (op.IsPrefix)
                    {
                        // prefix operator
                        if (n < op.PrefixPrecedence)
                        {
                            ParserError("prefix precedence error", _scanner.Current.Line, _scanner.Current.Column);
                            return(null);
                        }
                        switch (_scanner.Lookahead.Kind)
                        {
                        case PrologToken.LPAREN:
                        case PrologToken.LBRACKET:
                            ast = new BinaryTree(op.Name, Term(op.PrefixRightPrecedence));
                            m   = op.PrefixPrecedence;
                            Right(n, m, ref ast);
                            break;

                        case PrologToken.RPAREN:
                        case PrologToken.RBRACKET:
                        case PrologToken.DOT:
                        case PrologToken.LIST_SEP:
                            if (n < m)
                            {
                                _errors.Add(new PrologCompilerError("P0006", "Unexpected atom '" + _scanner.Lookahead.StringValue + "'", "", false, _scanner.Current.Line, _scanner.Current.Column));
                                return(null);
                            }
                            ast = new BinaryTree(_scanner.Current.StringValue);
                            Right(n, m, ref ast);
                            break;

                        case PrologToken.ATOM:
                            if (_operators.IsOperator(_scanner.Lookahead.StringValue))
                            {
                                PrologOperator atomOp = _operators.GetOperator(_scanner.Lookahead.StringValue);
                                if (atomOp.IsInfix && m <= atomOp.InfixLeftPrecedence)
                                {
                                    if (n < m)
                                    {
                                        ParserError("n < m", _scanner.Lookahead.Line, _scanner.Current.Column);
                                        return(null);
                                    }
                                    ast = new BinaryTree(_scanner.Lookahead.StringValue);
                                    Right(n, m, ref ast);
                                    break;
                                }
                                else if (atomOp.IsPostfix && m <= atomOp.PostfixLeftPrecedence)
                                {
                                    if (n < m)
                                    {
                                        ParserError("n < m", _scanner.Current.Line, _scanner.Current.Column);
                                        return(null);
                                    }
                                    ast = new BinaryTree(_scanner.Current.StringValue);
                                    Right(n, m, ref ast);
                                    break;
                                }
                                // Just added on 6/23/2006. Might not fix anything.
                                else
                                {
                                    ast = new BinaryTree(_scanner.Current.StringValue, null, Term(op.InfixRightPrecedence));
                                    m   = op.PrefixPrecedence;
                                    Right(n, m, ref ast);
                                    break;
                                }
                            }
                            else
                            {
                                ast = new BinaryTree(_scanner.Current.StringValue, null, Term(op.InfixRightPrecedence));
                                m   = op.PrefixPrecedence;
                                Right(n, m, ref ast);
                            }
                            break;

                        default:
                            ParserError("Unknown internal error", _scanner.Current.Line, _scanner.Current.Column);
                            return(null);
                        }
                    }
                }
                else
                {
                    ast = new BinaryTree(_scanner.Current.StringValue);
                }
                break;

            default:
                ParserError("Unknown internal error", _scanner.Current.Line, _scanner.Current.Column);
                return(null);
            }
            Right(n, m, ref ast);
            return(ast);
        }
예제 #13
0
        public PrologCodeUnit Parse(TextReader input)
        {
            _scanner = new PrologScanner(input);
            _errors.Clear();

            PrologCodeTerm term = null;
            while(true)
            {

                term = ReadTerm(1200);
                if (_scanner.Next().Kind != PrologToken.DOT)
                {
                    _errors.Add(new PrologCompilerError("P0001", "Unexpected end of term", "", false, _scanner.Current.Line, _scanner.Current.Column));
                    break;
                }
                if (term is PrologCodeHeadlessClause)
                {
                    ProcessHeadlessClause(term);
                }
                else
                {
                    _codeUnit.Terms.Add(term);
                }
                if (_scanner.Lookahead.Kind == PrologToken.EOF)
                    break;

            }
            return _codeUnit;
        }