/// <summary>
        /// Returns the next token to be used by the parser.
        /// </summary>
        /// <returns></returns>
        public PrologToken Next()
        {
            if (_peeked == true)
            {
                _current = _lookahead;
                _peeked  = false;
                return(_current);
            }
            PrologToken tok = new PrologToken(PrologToken.EOF, _line, _col);
            string      val = "";

            while (_ch != EOF)
            {
                /* comment encountered */
                if (_ch == '%')
                {
                    while (_input.Read() != '\n')
                    {
                    }
                    NextCharacter();
                    continue;
                }

                if (_ch == '/')
                {
                    _ch = (char)_input.Peek();
                    if (_ch == '*')
                    {
                        // Comment started
                        _input.Read();
                        while (true)
                        {
                            _ch = (char)_input.Peek();
                            if (_ch == '*')
                            {
                                _input.Read();
                                _ch = (char)_input.Peek();
                                if (_ch == '/')
                                {
                                    _input.Read();
                                    NextCharacter();
                                    break;
                                }
                                else
                                {
                                    //_input.Read();
                                }
                            }
                            else
                            {
                                _input.Read();
                            }
                        }
                    }
                    else
                    {
                        _ch = '/';
                    }
                }


                if (_ch == '_')
                {
                    tok.Kind        = PrologToken.ATOM;
                    tok.StringValue = _ch.ToString();
                    _current        = tok;
                    NextCharacter();
                    return(tok);
                }
                /* Recognizes: numbers */
                if (Char.IsDigit(_ch))
                {
                    tok.Kind = PrologToken.ATOM;

                    while (Char.IsDigit(_ch))
                    {
                        val += _ch.ToString();
                        NextCharacter();
                    }
                    tok.IntValue    = Int32.Parse(val);
                    tok.StringValue = val;
                    _current        = tok;
                    return(tok);
                }
                /* Recognizes: identifiers, not, mod */
                else if (Char.IsLetter(_ch))
                {
                    tok.Kind = PrologToken.ATOM;
                    while (Char.IsLetterOrDigit(_ch) || _ch == '_')
                    {
                        val += _ch.ToString();
                        NextCharacter();
                    }
                    if (Char.IsUpper(val, 0) || val[0] == '_' || val == "_")
                    {
                        tok.Kind = PrologToken.VARIABLE;
                    }
                    tok.StringValue = val;
                    _current        = tok;
                    return(tok);
                }
                else if (Char.IsPunctuation(_ch) || Char.IsSymbol(_ch))
                {
                    tok.Kind = PrologToken.ATOM;
                    val     += _ch.ToString();
                    switch (val)
                    {
                    case ",":
                        tok.Kind = PrologToken.COMMA;
                        NextCharacter();
                        break;

                    case "|":
                        tok.Kind = PrologToken.LIST_SEP;
                        NextCharacter();
                        break;

                    case "(":
                        tok.Kind = PrologToken.LPAREN;
                        NextCharacter();
                        break;

                    case ")":
                        tok.Kind = PrologToken.RPAREN;
                        NextCharacter();
                        break;

                    case "[":
                        tok.Kind = PrologToken.LBRACKET;
                        NextCharacter();
                        break;

                    case "]":
                        tok.Kind = PrologToken.RBRACKET;
                        NextCharacter();
                        break;

                    case "!":
                        tok.Kind = PrologToken.STRING;
                        NextCharacter();
                        break;

                    case ".":
                        tok.Kind = PrologToken.DOT;
                        NextCharacter();
                        break;

                    case "'":
                        tok.Kind = PrologToken.STRING;
                        NextCharacter();
                        while (_ch != '\'')
                        {
                            val += _ch.ToString();
                            NextCharacter();
                        }
                        NextCharacter();
                        //val = val.Remove(0,1);
                        val            += "'";
                        tok.StringValue = val;
                        _current        = tok;
                        break;

                    case "\"":
                        tok.Kind = PrologToken.STRING;
                        NextCharacter();
                        while (_ch != '"')
                        {
                            val += _ch.ToString();
                            NextCharacter();
                        }
                        NextCharacter();
                        //val = val.Remove(0,1);
                        val            += "\"";
                        tok.StringValue = val;
                        _current        = tok;
                        break;
                        //default:
                        //    Error("Invalid token: " + val);
                        //    break;
                    }
                    if (tok.Kind != PrologToken.ATOM)
                    {
                        tok.StringValue = val;
                        if (tok.Kind == PrologToken.STRING)
                        {
                            tok.Kind = PrologToken.ATOM;
                        }
                        _current = tok;
                        return(tok);
                    }
                    val = "";
                    while ((Char.IsPunctuation(_ch) || Char.IsSymbol(_ch)) && _ch != '(')
                    {
                        val += _ch.ToString();
                        NextCharacter();
                    }
                    tok.StringValue = val;
                    _current        = tok;
                    return(tok);
                }
                NextCharacter();
            }
            _current = tok;
            return(tok);
        }
Example #2
0
        /// <summary>
        /// Returns the next token to be used by the parser.
        /// </summary>
        /// <returns></returns>
        public PrologToken Next()
        {
            if(_peeked == true)
            {
                _current = _lookahead;
                _peeked = false;
                return _current;
            }
            PrologToken tok = new PrologToken(PrologToken.EOF, _line, _col);
            string val = "";
            while(_ch != EOF)
            {
                /* comment encountered */
                if(_ch == '%')
                {
                    while(_input.Read() != '\n')
                    {

                    }
                    NextCharacter();
                    continue;
                }

                if (_ch == '/')
                {
                    _ch = (char)_input.Peek();
                    if (_ch == '*')
                    {
                        // Comment started
                        _input.Read();
                        while (true)
                        {
                            _ch = (char)_input.Peek();
                            if (_ch == '*')
                            {
                                _input.Read();
                                _ch = (char)_input.Peek();
                                if (_ch == '/')
                                {
                                    _input.Read();
                                    NextCharacter();
                                    break;
                                }
                                else
                                {
                                    //_input.Read();
                                }
                            }
                            else
                            {
                                _input.Read();
                            }
                        }
                    }
                    else
                    {
                        _ch = '/';
                    }
                }

                if(_ch == '_')
                {
                    tok.Kind = PrologToken.ATOM;
                    tok.StringValue = _ch.ToString();
                    _current = tok;
                    NextCharacter();
                    return tok;
                }
                /* Recognizes: numbers */
                if(Char.IsDigit(_ch))
                {
                    tok.Kind = PrologToken.ATOM;

                    while(Char.IsDigit(_ch))
                    {
                        val += _ch.ToString();
                        NextCharacter();
                    }
                    tok.IntValue = Int32.Parse(val);
                    tok.StringValue = val;
                    _current = tok;
                    return tok;
                }
                    /* Recognizes: identifiers, not, mod */
                else if(Char.IsLetter(_ch))
                {
                    tok.Kind = PrologToken.ATOM;
                    while(Char.IsLetterOrDigit(_ch) || _ch == '_')
                    {
                        val += _ch.ToString();
                        NextCharacter();
                    }
                    if(Char.IsUpper(val, 0) || val[0] == '_' || val == "_")
                    {
                        tok.Kind = PrologToken.VARIABLE;

                    }
                    tok.StringValue = val;
                    _current = tok;
                    return tok;
                }
                else if(Char.IsPunctuation(_ch) || Char.IsSymbol(_ch))
                {

                    tok.Kind = PrologToken.ATOM;
                    val += _ch.ToString();
                    switch(val)
                    {

                        case ",":
                            tok.Kind = PrologToken.COMMA;
                            NextCharacter();
                            break;
                        case "|":
                            tok.Kind = PrologToken.LIST_SEP;
                            NextCharacter();
                            break;
                        case "(":
                            tok.Kind = PrologToken.LPAREN;
                            NextCharacter();
                            break;
                        case ")":
                            tok.Kind = PrologToken.RPAREN;
                            NextCharacter();
                            break;
                        case "[":
                            tok.Kind = PrologToken.LBRACKET;
                            NextCharacter();
                            break;
                        case "]":
                            tok.Kind = PrologToken.RBRACKET;
                            NextCharacter();
                            break;
                        case "!":
                            tok.Kind = PrologToken.STRING;
                            NextCharacter();
                            break;
                        case ".":
                            tok.Kind = PrologToken.DOT;
                            NextCharacter();
                            break;
                        case "'":
                            tok.Kind = PrologToken.STRING;
                            NextCharacter();
                            while(_ch != '\'')
                            {
                                val += _ch.ToString();
                                NextCharacter();
                            }
                            NextCharacter();
                            //val = val.Remove(0,1);
                            val += "'";
                            tok.StringValue = val;
                            _current = tok;
                            break;
                        case "\"":
                            tok.Kind = PrologToken.STRING;
                            NextCharacter();
                            while (_ch != '"')
                            {
                                val += _ch.ToString();
                                NextCharacter();
                            }
                            NextCharacter();
                            //val = val.Remove(0,1);
                            val += "\"";
                            tok.StringValue = val;
                            _current = tok;
                            break;
                        //default:
                        //    Error("Invalid token: " + val);
                        //    break;

                    }
                    if(tok.Kind != PrologToken.ATOM)
                    {
                        tok.StringValue = val;
                        if(tok.Kind == PrologToken.STRING)
                            tok.Kind = PrologToken.ATOM;
                        _current = tok;
                        return tok;
                    }
                    val = "";
                    while((Char.IsPunctuation(_ch) || Char.IsSymbol(_ch)) && _ch != '(')
                    {
                        val += _ch.ToString();
                        NextCharacter();
                    }
                    tok.StringValue = val;
                    _current = tok;
                    return tok;
                }
                NextCharacter();
            }
            _current = tok;
            return tok;
        }