コード例 #1
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);
        }
コード例 #2
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();
 }
コード例 #3
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();
 }
コード例 #4
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();
 }
コード例 #5
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();
        }
コード例 #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 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;
        }