2016-05-17 03:04:11 +12:00
|
|
|
#pragma once
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifndef TPARSER_INCLUDED
|
|
|
|
#define TPARSER_INCLUDED
|
|
|
|
|
2016-04-14 22:15:09 +12:00
|
|
|
#include <memory>
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
#include "tcommon.h"
|
|
|
|
#include "tgrammar.h"
|
|
|
|
|
|
|
|
#undef DVAPI
|
|
|
|
#undef DVVAR
|
|
|
|
#ifdef TNZBASE_EXPORTS
|
|
|
|
#define DVAPI DV_EXPORT_API
|
|
|
|
#define DVVAR DV_EXPORT_VAR
|
|
|
|
#else
|
|
|
|
#define DVAPI DV_IMPORT_API
|
|
|
|
#define DVVAR DV_IMPORT_VAR
|
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace TSyntax {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
struct DVAPI SyntaxToken {
|
2016-06-15 18:43:10 +12:00
|
|
|
int m_pos, m_length;
|
|
|
|
TokenType m_type;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI Parser {
|
|
|
|
class Imp;
|
|
|
|
std::unique_ptr<Imp> m_imp;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Parser(const Grammar *grammar);
|
|
|
|
~Parser();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! parse the input string and create the corresponding calculator
|
|
|
|
//! (returns 0 if the text contains mistakes)
|
|
|
|
Calculator *parse(std::string text);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! return true if the last parsed string was correct
|
|
|
|
bool isValid() const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! return the last parsed string
|
|
|
|
std::string getText() const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! return the last error code (if the last parsed string was correct then
|
|
|
|
//! returns "")
|
|
|
|
std::string getError() const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! if getError() != "" returns the position of the last parsed token
|
|
|
|
//! the pair contains the indices of the first and the last characters of the
|
|
|
|
//! token
|
|
|
|
std::pair<int, int> getErrorPos() const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
enum SyntaxStatus { Correct, Incomplete, Error, ExtraIgnored };
|
|
|
|
SyntaxStatus checkSyntax(std::vector<SyntaxToken> &tokens, std::string text);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void getSuggestions(Grammar::Suggestions &suggestions, std::string text);
|
|
|
|
std::string getCurrentPatternString(std::string text);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
// not implemented
|
|
|
|
Parser(const Parser &);
|
|
|
|
Parser &operator=(const Parser &);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace TSyntax
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#endif
|