Commit 98ff7943 by Maarten L. Hekkelman

backup

parent 24fa80ba
...@@ -139,7 +139,7 @@ class file_t ...@@ -139,7 +139,7 @@ class file_t
// setValidator(nullptr); // setValidator(nullptr);
parser_type p(is, *this); parser_type p(is, *this);
p.parseFile(); p.parse_file();
// if (saved != nullptr) // if (saved != nullptr)
// { // {
......
...@@ -48,16 +48,16 @@ class parse_error : public std::runtime_error ...@@ -48,16 +48,16 @@ class parse_error : public std::runtime_error
class sac_parser class sac_parser
{ {
public: public:
using DatablockIndex = std::map<std::string, std::size_t>; using datablock_index = std::map<std::string, std::size_t>;
sac_parser(std::istream &is) sac_parser(std::istream &is, bool init = true)
: mData(is) : m_source(is)
{ {
m_validate = true; m_validate = true;
m_line_nr = 1; m_line_nr = 1;
m_bol = true; m_bol = true;
// if (init) if (init)
m_lookahead = get_next_token(); m_lookahead = get_next_token();
} }
...@@ -157,6 +157,7 @@ class sac_parser ...@@ -157,6 +157,7 @@ class sac_parser
case CIFToken::STOP: return "STOP"; case CIFToken::STOP: return "STOP";
case CIFToken::Tag: return "Tag"; case CIFToken::Tag: return "Tag";
case CIFToken::Value: return "Value"; case CIFToken::Value: return "Value";
default: return "Invalid token parameter";
} }
} }
...@@ -182,41 +183,42 @@ class sac_parser ...@@ -182,41 +183,42 @@ class sac_parser
case CIFValue::TextField: return "TextField"; case CIFValue::TextField: return "TextField";
case CIFValue::Inapplicable: return "Inapplicable"; case CIFValue::Inapplicable: return "Inapplicable";
case CIFValue::Unknown: return "Unknown"; case CIFValue::Unknown: return "Unknown";
default: return "Invalid type parameter";
} }
} }
// getNextChar takes a char from the buffer, or if it is empty // get_next_char takes a char from the buffer, or if it is empty
// from the istream. This function also does carriage/linefeed // from the istream. This function also does carriage/linefeed
// translation. // translation.
int getNextChar() int get_next_char()
{ {
int result; int result;
if (mBuffer.empty()) if (m_buffer.empty())
result = mData.get(); result = m_source.get();
else else
{ {
result = mBuffer.top(); result = m_buffer.top();
mBuffer.pop(); m_buffer.pop();
} }
// very simple CR/LF translation into LF // very simple CR/LF translation into LF
if (result == '\r') if (result == '\r')
{ {
int lookahead = mData.get(); int lookahead = m_source.get();
if (lookahead != '\n') if (lookahead != '\n')
mBuffer.push(lookahead); m_buffer.push(lookahead);
result = '\n'; result = '\n';
} }
mTokenValue += static_cast<char>(result); m_token_value += static_cast<char>(result);
if (result == '\n') if (result == '\n')
++m_line_nr; ++m_line_nr;
if (VERBOSE >= 6) if (VERBOSE >= 6)
{ {
std::cerr << "getNextChar => "; std::cerr << "get_next_char => ";
if (iscntrl(result) or not isprint(result)) if (iscntrl(result) or not isprint(result))
std::cerr << int(result) << std::endl; std::cerr << int(result) << std::endl;
else else
...@@ -228,21 +230,21 @@ class sac_parser ...@@ -228,21 +230,21 @@ class sac_parser
void retract() void retract()
{ {
assert(not mTokenValue.empty()); assert(not m_token_value.empty());
char ch = mTokenValue.back(); char ch = m_token_value.back();
if (ch == '\n') if (ch == '\n')
--m_line_nr; --m_line_nr;
mBuffer.push(ch); m_buffer.push(ch);
mTokenValue.pop_back(); m_token_value.pop_back();
} }
int restart(int start) int restart(int start)
{ {
int result = 0; int result = 0;
while (not mTokenValue.empty()) while (not m_token_value.empty())
retract(); retract();
switch (start) switch (start)
...@@ -277,12 +279,12 @@ class sac_parser ...@@ -277,12 +279,12 @@ class sac_parser
int state = State::Start, start = State::Start; int state = State::Start, start = State::Start;
m_bol = false; m_bol = false;
mTokenValue.clear(); m_token_value.clear();
mTokenType = CIFValue::Unknown; mTokenType = CIFValue::Unknown;
while (result == CIFToken::Unknown) while (result == CIFToken::Unknown)
{ {
auto ch = getNextChar(); auto ch = get_next_char();
switch (state) switch (state)
{ {
...@@ -318,7 +320,7 @@ class sac_parser ...@@ -318,7 +320,7 @@ class sac_parser
{ {
state = State::Start; state = State::Start;
retract(); retract();
mTokenValue.clear(); m_token_value.clear();
} }
else else
m_bol = (ch == '\n'); m_bol = (ch == '\n');
...@@ -329,7 +331,7 @@ class sac_parser ...@@ -329,7 +331,7 @@ class sac_parser
{ {
state = State::Start; state = State::Start;
m_bol = true; m_bol = true;
mTokenValue.clear(); m_token_value.clear();
} }
else if (ch == kEOF) else if (ch == kEOF)
result = CIFToken::Eof; result = CIFToken::Eof;
...@@ -351,8 +353,8 @@ class sac_parser ...@@ -351,8 +353,8 @@ class sac_parser
state = State::TextField; state = State::TextField;
else if (ch == ';') else if (ch == ';')
{ {
assert(mTokenValue.length() >= 2); assert(m_token_value.length() >= 2);
mTokenValue = mTokenValue.substr(1, mTokenValue.length() - 3); m_token_value = m_token_value.substr(1, m_token_value.length() - 3);
mTokenType = CIFValue::TextField; mTokenType = CIFValue::TextField;
result = CIFToken::Value; result = CIFToken::Value;
} }
...@@ -378,10 +380,10 @@ class sac_parser ...@@ -378,10 +380,10 @@ class sac_parser
result = CIFToken::Value; result = CIFToken::Value;
mTokenType = CIFValue::String; mTokenType = CIFValue::String;
if (mTokenValue.length() < 2) if (m_token_value.length() < 2)
error("Invalid quoted string token"); error("Invalid quoted string token");
mTokenValue = mTokenValue.substr(1, mTokenValue.length() - 2); m_token_value = m_token_value.substr(1, m_token_value.length() - 2);
} }
else if (ch == quoteChar) else if (ch == quoteChar)
; ;
...@@ -493,7 +495,7 @@ class sac_parser ...@@ -493,7 +495,7 @@ class sac_parser
case State::Value: case State::Value:
if (ch == '_') if (ch == '_')
{ {
std::string s = toLowerCopy(mTokenValue); std::string s = toLowerCopy(m_token_value);
if (s == "global_") if (s == "global_")
result = CIFToken::GLOBAL; result = CIFToken::GLOBAL;
...@@ -518,12 +520,12 @@ class sac_parser ...@@ -518,12 +520,12 @@ class sac_parser
retract(); retract();
result = CIFToken::Value; result = CIFToken::Value;
if (mTokenValue == ".") if (m_token_value == ".")
mTokenType = CIFValue::Inapplicable; mTokenType = CIFValue::Inapplicable;
else if (mTokenValue == "?") else if (m_token_value == "?")
{ {
mTokenType = CIFValue::Unknown; mTokenType = CIFValue::Unknown;
mTokenValue.clear(); m_token_value.clear();
} }
} }
break; break;
...@@ -539,7 +541,7 @@ class sac_parser ...@@ -539,7 +541,7 @@ class sac_parser
else else
result = CIFToken::SAVE; result = CIFToken::SAVE;
mTokenValue.erase(mTokenValue.begin(), mTokenValue.begin() + 5); m_token_value.erase(m_token_value.begin(), m_token_value.begin() + 5);
} }
break; break;
...@@ -556,7 +558,7 @@ class sac_parser ...@@ -556,7 +558,7 @@ class sac_parser
if (mTokenType != CIFValue::Unknown) if (mTokenType != CIFValue::Unknown)
std::cerr << ' ' << get_value_name(mTokenType); std::cerr << ' ' << get_value_name(mTokenType);
if (result != CIFToken::Eof) if (result != CIFToken::Eof)
std::cerr << " " << std::quoted(mTokenValue); std::cerr << " " << std::quoted(m_token_value);
std::cerr << std::endl; std::cerr << std::endl;
} }
...@@ -572,10 +574,10 @@ class sac_parser ...@@ -572,10 +574,10 @@ class sac_parser
} }
public: public:
bool parseSingleDatablock(const std::string &datablock) bool parse_single_datablock(const std::string &datablock)
{ {
// first locate the start, as fast as we can // first locate the start, as fast as we can
auto &sb = *mData.rdbuf(); auto &sb = *m_source.rdbuf();
enum enum
{ {
...@@ -653,20 +655,20 @@ class sac_parser ...@@ -653,20 +655,20 @@ class sac_parser
if (found) if (found)
{ {
produceDatablock(datablock); produce_datablock(datablock);
m_lookahead = get_next_token(); m_lookahead = get_next_token();
parseDataBlock(); parse_datablock();
} }
return found; return found;
} }
DatablockIndex indexDatablocks() datablock_index index_datablocks()
{ {
DatablockIndex index; datablock_index index;
// first locate the start, as fast as we can // first locate the start, as fast as we can
auto &sb = *mData.rdbuf(); auto &sb = *m_source.rdbuf();
enum enum
{ {
...@@ -748,7 +750,7 @@ class sac_parser ...@@ -748,7 +750,7 @@ class sac_parser
else if (isspace(ch)) else if (isspace(ch))
{ {
if (not datablock.empty()) if (not datablock.empty())
index[datablock] = mData.tellg(); index[datablock] = m_source.tellg();
state = start; state = start;
} }
...@@ -763,18 +765,18 @@ class sac_parser ...@@ -763,18 +765,18 @@ class sac_parser
return index; return index;
} }
bool parseSingleDatablock(const std::string &datablock, const DatablockIndex &index) bool parse_single_datablock(const std::string &datablock, const datablock_index &index)
{ {
bool result = false; bool result = false;
auto i = index.find(datablock); auto i = index.find(datablock);
if (i != index.end()) if (i != index.end())
{ {
mData.seekg(i->second); m_source.seekg(i->second);
produceDatablock(datablock); produce_datablock(datablock);
m_lookahead = get_next_token(); m_lookahead = get_next_token();
parseDataBlock(); parse_datablock();
result = true; result = true;
} }
...@@ -782,21 +784,21 @@ class sac_parser ...@@ -782,21 +784,21 @@ class sac_parser
return result; return result;
} }
void parseFile() void parse_file()
{ {
while (m_lookahead != CIFToken::Eof) while (m_lookahead != CIFToken::Eof)
{ {
switch (m_lookahead) switch (m_lookahead)
{ {
case CIFToken::GLOBAL: case CIFToken::GLOBAL:
parseGlobal(); parse_global();
break; break;
case CIFToken::DATA: case CIFToken::DATA:
produceDatablock(mTokenValue); produce_datablock(m_token_value);
match(CIFToken::DATA); match(CIFToken::DATA);
parseDataBlock(); parse_datablock();
break; break;
default: default:
...@@ -807,7 +809,7 @@ class sac_parser ...@@ -807,7 +809,7 @@ class sac_parser
} }
protected: protected:
void parseGlobal() void parse_global()
{ {
match(CIFToken::GLOBAL); match(CIFToken::GLOBAL);
while (m_lookahead == CIFToken::Tag) while (m_lookahead == CIFToken::Tag)
...@@ -817,7 +819,7 @@ class sac_parser ...@@ -817,7 +819,7 @@ class sac_parser
} }
} }
void parseDataBlock() void parse_datablock()
{ {
std::string cat; std::string cat;
...@@ -836,11 +838,11 @@ class sac_parser ...@@ -836,11 +838,11 @@ class sac_parser
while (m_lookahead == CIFToken::Tag) while (m_lookahead == CIFToken::Tag)
{ {
std::string catName, itemName; std::string catName, itemName;
std::tie(catName, itemName) = splitTagName(mTokenValue); std::tie(catName, itemName) = splitTagName(m_token_value);
if (cat.empty()) if (cat.empty())
{ {
produceCategory(catName); produce_category(catName);
cat = catName; cat = catName;
} }
else if (not iequals(cat, catName)) else if (not iequals(cat, catName))
...@@ -853,11 +855,11 @@ class sac_parser ...@@ -853,11 +855,11 @@ class sac_parser
while (m_lookahead == CIFToken::Value) while (m_lookahead == CIFToken::Value)
{ {
produceRow(); produce_row();
for (auto tag : tags) for (auto tag : tags)
{ {
produceItem(cat, tag, mTokenValue); produce_item(cat, tag, m_token_value);
match(CIFToken::Value); match(CIFToken::Value);
} }
} }
...@@ -869,25 +871,25 @@ class sac_parser ...@@ -869,25 +871,25 @@ class sac_parser
case CIFToken::Tag: case CIFToken::Tag:
{ {
std::string catName, itemName; std::string catName, itemName;
std::tie(catName, itemName) = splitTagName(mTokenValue); std::tie(catName, itemName) = splitTagName(m_token_value);
if (not iequals(cat, catName)) if (not iequals(cat, catName))
{ {
produceCategory(catName); produce_category(catName);
cat = catName; cat = catName;
produceRow(); produce_row();
} }
match(CIFToken::Tag); match(CIFToken::Tag);
produceItem(cat, itemName, mTokenValue); produce_item(cat, itemName, m_token_value);
match(CIFToken::Value); match(CIFToken::Value);
break; break;
} }
case CIFToken::SAVE: case CIFToken::SAVE:
parseSaveFrame(); parse_save_frame();
break; break;
default: default:
...@@ -897,7 +899,7 @@ class sac_parser ...@@ -897,7 +899,7 @@ class sac_parser
} }
} }
virtual void parseSaveFrame() virtual void parse_save_frame()
{ {
error("A regular CIF file should not contain a save frame"); error("A regular CIF file should not contain a save frame");
} }
...@@ -914,10 +916,10 @@ class sac_parser ...@@ -914,10 +916,10 @@ class sac_parser
// production methods, these are pure virtual here // production methods, these are pure virtual here
virtual void produceDatablock(const std::string &name) = 0; virtual void produce_datablock(const std::string &name) = 0;
virtual void produceCategory(const std::string &name) = 0; virtual void produce_category(const std::string &name) = 0;
virtual void produceRow() = 0; virtual void produce_row() = 0;
virtual void produceItem(const std::string &category, const std::string &item, const std::string &value) = 0; virtual void produce_item(const std::string &category, const std::string &item, const std::string &value) = 0;
protected: protected:
enum State enum State
...@@ -939,16 +941,16 @@ class sac_parser ...@@ -939,16 +941,16 @@ class sac_parser
SAVE SAVE
}; };
std::istream &mData; std::istream &m_source;
// Parser state // Parser state
bool m_validate; bool m_validate;
uint32_t m_line_nr; uint32_t m_line_nr;
bool m_bol; bool m_bol;
CIFToken m_lookahead; CIFToken m_lookahead;
std::string mTokenValue; std::string m_token_value;
CIFValue mTokenType; CIFValue mTokenType;
std::stack<int> mBuffer; std::stack<int> m_buffer;
}; };
// -------------------------------------------------------------------- // --------------------------------------------------------------------
...@@ -971,12 +973,12 @@ class parser_t : public sac_parser ...@@ -971,12 +973,12 @@ class parser_t : public sac_parser
{ {
} }
void produceDatablock(const std::string &name) override void produce_datablock(const std::string &name) override
{ {
std::tie(m_datablock, std::ignore) = m_file.emplace(name); std::tie(m_datablock, std::ignore) = m_file.emplace(name);
} }
void produceCategory(const std::string &name) override void produce_category(const std::string &name) override
{ {
if (VERBOSE >= 4) if (VERBOSE >= 4)
std::cerr << "producing category " << name << std::endl; std::cerr << "producing category " << name << std::endl;
...@@ -984,7 +986,7 @@ class parser_t : public sac_parser ...@@ -984,7 +986,7 @@ class parser_t : public sac_parser
std::tie(m_category, std::ignore) = m_datablock->emplace(name); std::tie(m_category, std::ignore) = m_datablock->emplace(name);
} }
void produceRow() override void produce_row() override
{ {
if (VERBOSE >= 4) if (VERBOSE >= 4)
std::cerr << "producing row for category " << m_category->name() << std::endl; std::cerr << "producing row for category " << m_category->name() << std::endl;
...@@ -994,7 +996,7 @@ class parser_t : public sac_parser ...@@ -994,7 +996,7 @@ class parser_t : public sac_parser
// m_row.lineNr(m_line_nr); // m_row.lineNr(m_line_nr);
} }
void produceItem(const std::string &category, const std::string &item, const std::string &value) override void produce_item(const std::string &category, const std::string &item, const std::string &value) override
{ {
if (VERBOSE >= 4) if (VERBOSE >= 4)
std::cerr << "producing _" << category << '.' << item << " -> " << value << std::endl; std::cerr << "producing _" << category << '.' << item << " -> " << value << std::endl;
...@@ -1002,7 +1004,7 @@ class parser_t : public sac_parser ...@@ -1002,7 +1004,7 @@ class parser_t : public sac_parser
if (not iequals(category, m_category->name())) if (not iequals(category, m_category->name()))
error("inconsistent categories in loop_"); error("inconsistent categories in loop_");
m_row[item] = mTokenValue; m_row[item] = m_token_value;
} }
protected: protected:
...@@ -1012,24 +1014,7 @@ class parser_t : public sac_parser ...@@ -1012,24 +1014,7 @@ class parser_t : public sac_parser
row_handle_type m_row; row_handle_type m_row;
}; };
// class Parser : public SacParser // --------------------------------------------------------------------
// {
// public:
// Parser(std::istream &is, File &f, bool init = true);
// virtual void produceDatablock(const std::string &name);
// virtual void produceCategory(const std::string &name);
// virtual void produceRow();
// virtual void produceItem(const std::string &category, const std::string &item, const std::string &value);
// protected:
// File &mFile;
// Datablock *mDataBlock;
// Datablock::iterator m_category;
// Row mRow;
// };
// // --------------------------------------------------------------------
// class DictParser : public Parser // class DictParser : public Parser
// { // {
...@@ -1040,7 +1025,7 @@ class parser_t : public sac_parser ...@@ -1040,7 +1025,7 @@ class parser_t : public sac_parser
// void loadDictionary(); // void loadDictionary();
// private: // private:
// virtual void parseSaveFrame(); // virtual void parse_save_frame();
// bool collectItemTypes(); // bool collectItemTypes();
// void linkItems(); // void linkItems();
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#pragma once #pragma once
#include <cif++/v2/item.hpp> #include <cif++/v2/item.hpp>
#include <cif++/v2/condition.hpp>
namespace cif::v2 namespace cif::v2
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment