/*
* Copyright (C) 2008 Kamil Dudka <xdudka00@stud.fit.vutbr.cz>
*
* This file is part of vyp08 (compiler and interpreter of VYP08 language).
*
* vyp08 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* vyp08 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with vyp08. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SCANNER_H
#define SCANNER_H
#include "config.h"
#ifndef BUILDING_DOX
# include <iostream>
# include <string>
#endif
/**
* token type enumeration
*/
enum EToken {
ETOKEN_NULL = 0, ///< null token (not valid)
// tokens carrying a value with them
ETOKEN_ID, ///< identifier
ETOKEN_NUMBER_INT, ///< integral literal
ETOKEN_NUMBER_DOUBLE, ///< decimal literal
ETOKEN_STRING, ///< string literal
// operators
ETOKEN_OP_LCBR, ///< {
ETOKEN_OP_RCBR, ///< }
ETOKEN_OP_LPAR, ///< (
ETOKEN_OP_RPAR, ///< )
ETOKEN_OP_STAR, ///< *
ETOKEN_OP_SLASH, ///< /
ETOKEN_OP_PLUS, ///< +
ETOKEN_OP_MINUS, ///< -
ETOKEN_OP_LESS, ///< <
ETOKEN_OP_LESS_EQ, ///< <=
ETOKEN_OP_GREATER, ///< >
ETOKEN_OP_GREATER_EQ, ///< >=
ETOKEN_OP_ASSIGN, ///< :=
ETOKEN_OP_COMMA, ///< ,
ETOKEN_OP_SEMICOLON, ///< ;
// keywords
ETOKEN_KW_AND, ///< and
ETOKEN_KW_DIV, ///< div
ETOKEN_KW_DOUBLE, ///< double
ETOKEN_KW_ELSE, ///< else
ETOKEN_KW_EQ, ///< eq
ETOKEN_KW_IF, ///< if
ETOKEN_KW_INT, ///< int
ETOKEN_KW_NEQ, ///< neq
ETOKEN_KW_NOT, ///< not
ETOKEN_KW_OR, ///< or
ETOKEN_KW_STRING, ///< string
ETOKEN_KW_VAR, ///< var
ETOKEN_KW_VOID, ///< void
ETOKEN_KW_WHILE, ///< while
};
/// send token type to stream in human-readable form
std::ostream& operator<<(std::ostream &, EToken);
/**
* token as value type
*/
struct Token {
EToken type; ///< type of token
int lineno; ///< line number in source code
int numberInt; ///< value - only for ETOKEN_NUMBER_INT
double numberDouble; ///< value - only for ETOKEN_NUMBER_DOUBLE
std::string text; ///< value - only for ETOKEN_ID and ETOKEN_STRING
Token():
type(ETOKEN_NULL),
lineno(0),
numberInt(0),
numberDouble(0.0)
{
}
Token(EToken type_, int lineno_, int numberInt_, double numberDouble_, const std::string &text_):
type(type_),
lineno(lineno_),
numberInt(numberInt_),
numberDouble(numberDouble_),
text(text_)
{
}
};
/// send token to stream in human-readable form
std::ostream& operator<<(std::ostream &, const Token &);
/**
* the most common base class for particular compiler modules
*/
class IErrorSensitive {
public:
virtual ~IErrorSensitive() { }
/**
* Return true if any error was detected.
*/
virtual bool hasError() const = 0;
};
/**
* pure virtual scanner interface
*/
class IScanner: public IErrorSensitive {
public:
virtual ~IScanner() { }
/**
* read next token
* @param token Where to store token to.
* @return Return true if token is read, false on EOF.
*/
virtual bool readNext(Token &token) = 0;
};
/**
* simple (static only) factory for lexical scanner creation
*/
class ScannerFactory {
public:
/**
* simple factory method
* @param input stream to read from
* @param fileName Name (or alias) of input file (which appears in error msgs)
* @return Return on heap allocated instance of scanner
* @attention Caller is responsible to destroy scanner object.
*/
static IScanner* createScanner(std::istream &input, std::string fileName);
private:
ScannerFactory();
};
#endif /* SCANNER_H */