Skip to content

Commit 40965c6

Browse files
committed
Removed class WsjcppYamlParserStatus (moved variables inside WsjcppYaml)
1 parent e068c59 commit 40965c6

File tree

3 files changed

+158
-151
lines changed

3 files changed

+158
-151
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 137 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ WsjcppYamlNode::WsjcppYamlNode(
7777
m_nItemType = nItemType;
7878
m_nValueQuotes = WSJCPP_YAML_QUOTES_NONE;
7979
m_nNameQuotes = WSJCPP_YAML_QUOTES_NONE;
80+
m_nDiffIntent = 0;
8081
TAG = "WsjcppYamlNode";
8182
}
8283

@@ -605,6 +606,19 @@ std::string WsjcppYamlNode::getForLogFormat() {
605606

606607
// ---------------------------------------------------------------------
607608

609+
void WsjcppYamlNode::setNodeDiffIntent(int nDiffIntent) {
610+
m_nDiffIntent = nDiffIntent;
611+
}
612+
613+
// ---------------------------------------------------------------------
614+
615+
int WsjcppYamlNode::getNodeDiffIntent() {
616+
return m_nDiffIntent;
617+
}
618+
619+
// ---------------------------------------------------------------------
620+
// WsjcppYamlParsebleLine
621+
608622
WsjcppYamlParsebleLine::WsjcppYamlParsebleLine(int nLine) {
609623
TAG = "WsjcppYamlParsebleLine(line:" + std::to_string(nLine) + ")";
610624
m_nLineNumber = nLine;
@@ -940,20 +954,6 @@ std::string WsjcppYamlParsebleLine::removeStringSingleQuotes(const std::string &
940954
return sRet;
941955
}
942956

943-
// ---------------------------------------------------------------------
944-
// WsjcppYamlParserStatus
945-
946-
void WsjcppYamlParserStatus::logUnknownLine(const std::string &sPrefix) {
947-
WsjcppLog::warn(sPrefix, "\n"
948-
" error:\n"
949-
" desc: \"unknown_line\"\n"
950-
" line_number: " + std::to_string(pCurItem->getPlaceInFile().getNumberOfLine()) + "\n"
951-
" line: \"" + placeInFile.getLine() + "\"\n"
952-
" intent: " + std::to_string(nIntent) + "\n"
953-
" filename: \"" + pCurItem->getPlaceInFile().getFilename() + "\""
954-
);
955-
}
956-
957957
// ---------------------------------------------------------------------
958958
// WsjcppYamlCursor
959959

@@ -1261,44 +1261,40 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
12611261
}
12621262

12631263
std::vector<std::string> vLines = this->splitToLines(sBuffer);
1264-
WsjcppYamlParserStatus st;
1265-
st.pCurItem = m_pRoot; // TODO recreate again new root element
1266-
st.placeInFile.setFilename(sFileName);
1267-
st.nIntent = 0;
1268-
m_pRoot->setPlaceInFile(st.placeInFile);
1264+
m_pParseCurrentItem = m_pRoot;
1265+
m_parsePlaceInFile.setFilename(sFileName);
1266+
m_nParseCurrentIntent = 0;
1267+
m_pRoot->setPlaceInFile(m_parsePlaceInFile);
12691268

12701269
for (int nLine = 0; nLine < vLines.size(); nLine++) {
1271-
st.placeInFile.setLine(vLines[nLine]);
1270+
m_parsePlaceInFile.setLine(vLines[nLine]);
12721271
// WsjcppLog::info(TAG, "Line(" + std::to_string(nLine) + ") '" + st.sLine + "'");
1273-
st.placeInFile.setNumberOfLine(nLine);
1274-
st.line = WsjcppYamlParsebleLine(nLine);
1275-
if (!st.line.parseLine(st.placeInFile.getLine(), sError)) {
1272+
m_parsePlaceInFile.setNumberOfLine(nLine);
1273+
m_parseLine = WsjcppYamlParsebleLine(nLine);
1274+
if (!m_parseLine.parseLine(m_parsePlaceInFile.getLine(), sError)) {
12761275
return false;
12771276
}
12781277

1279-
bool isEmptyName = st.line.isEmptyName();
1280-
bool isEmptyValue = st.line.isEmptyValue();
1281-
bool isArrayItem = st.line.isArrayItem();
1282-
int nLineIntent = st.line.getIntent();
1283-
int nDiffIntent = nLineIntent - st.nIntent;
1278+
bool isEmptyName = m_parseLine.isEmptyName();
1279+
bool isEmptyValue = m_parseLine.isEmptyValue();
1280+
bool isArrayItem = m_parseLine.isArrayItem();
1281+
int nLineIntent = m_parseLine.getIntent();
1282+
int nDiffIntent = nLineIntent - m_nParseCurrentIntent;
12841283

1285-
if (st.line.isEmptyLine()) {
1286-
1287-
1288-
if (st.pCurItem != nullptr) {
1289-
1290-
if (st.pCurItem->isArray() || st.pCurItem->isMap() || st.pCurItem->isUndefined()) {
1291-
WsjcppYamlNode *pItem = new WsjcppYamlNode(
1292-
st.pCurItem, st.placeInFile,
1284+
if (m_parseLine.isEmptyLine()) {
1285+
if (m_pParseCurrentItem != nullptr) {
1286+
if (m_pParseCurrentItem->isArray() || m_pParseCurrentItem->isMap() || m_pParseCurrentItem->isUndefined()) {
1287+
WsjcppYamlNode *pNode = new WsjcppYamlNode(
1288+
m_pParseCurrentItem, m_parsePlaceInFile,
12931289
WSJCPP_YAML_NODE_EMPTY
12941290
);
1295-
st.pCurItem->appendElement(pItem);
1296-
} else if (st.pCurItem->getParent() != nullptr && (st.pCurItem->getParent()->isArray() || st.pCurItem->getParent()->isMap())) {
1297-
WsjcppYamlNode *pItem = new WsjcppYamlNode(
1298-
st.pCurItem->getParent(), st.placeInFile,
1291+
m_pParseCurrentItem->appendElement(pNode);
1292+
} else if (m_pParseCurrentItem->getParent() != nullptr && (m_pParseCurrentItem->getParent()->isArray() || m_pParseCurrentItem->getParent()->isMap())) {
1293+
WsjcppYamlNode *pNode = new WsjcppYamlNode(
1294+
m_pParseCurrentItem->getParent(), m_parsePlaceInFile,
12991295
WSJCPP_YAML_NODE_EMPTY
13001296
);
1301-
st.pCurItem->getParent()->appendElement(pItem);
1297+
m_pParseCurrentItem->getParent()->appendElement(pNode);
13021298
} else {
13031299
WsjcppLog::throw_err(TAG, "Empty element can be added only to map or to array");
13041300
}
@@ -1307,165 +1303,189 @@ bool WsjcppYaml::parse(const std::string &sFileName, const std::string &sBuffer,
13071303
}
13081304

13091305
while (nDiffIntent < 0) {
1310-
st.pCurItem = st.pCurItem->getParent();
1311-
st.nIntent = st.nIntent - 2;
1312-
nDiffIntent = nLineIntent - st.nIntent;
1313-
if (st.pCurItem == nullptr) {
1306+
int nNodeDiffIntent = m_pParseCurrentItem->getNodeDiffIntent();
1307+
if (nNodeDiffIntent == 0) {
1308+
sError = "Node diff intent cann't be 0 ";
1309+
return false;
1310+
}
1311+
m_pParseCurrentItem = m_pParseCurrentItem->getParent();
1312+
m_nParseCurrentIntent = m_nParseCurrentIntent - nNodeDiffIntent;
1313+
nDiffIntent = nLineIntent - m_nParseCurrentIntent;
1314+
if (m_pParseCurrentItem == nullptr) {
13141315
sError = "Current item is nullptr";
13151316
return false;
13161317
}
13171318
}
13181319

13191320
if (nDiffIntent == 0) {
1320-
if (st.line.isEmptyName()) {
1321+
if (m_parseLine.isEmptyName()) {
13211322
if ( ! isEmptyValue && isArrayItem) {
1322-
process_sameIntent_emptyName_hasValue_arrayItem(st);
1323+
process_sameIntent_emptyName_hasValue_arrayItem();
13231324
} else if (! isEmptyValue && ! isArrayItem) {
1324-
process_sameIntent_emptyName_hasValue_noArrayItem(st);
1325+
process_sameIntent_emptyName_hasValue_noArrayItem();
13251326
} else if (isEmptyValue && isArrayItem) {
1326-
process_sameIntent_emptyName_emptyValue_arrayItem(st);
1327+
process_sameIntent_emptyName_emptyValue_arrayItem();
13271328
} else if (isEmptyValue && ! isArrayItem) {
1328-
process_sameIntent_emptyName_emptyValue_noArrayItem(st);
1329+
process_sameIntent_emptyName_emptyValue_noArrayItem();
13291330
} else {
1330-
st.logUnknownLine(TAG);
1331+
logUnknownParseLine();
13311332
}
1332-
} else if ( ! st.line.isEmptyName()) {
1333+
} else if ( ! m_parseLine.isEmptyName()) {
13331334
if ( ! isEmptyValue && isArrayItem) {
1334-
process_sameIntent_hasName_hasValue_arrayItem(st);
1335+
process_sameIntent_hasName_hasValue_arrayItem();
13351336
} else if ( ! isEmptyValue && ! isArrayItem) {
1336-
process_sameIntent_hasName_hasValue_noArrayItem(st);
1337+
process_sameIntent_hasName_hasValue_noArrayItem();
13371338
} else if (isEmptyValue && isArrayItem) {
1338-
process_sameIntent_hasName_emptyValue_arrayItem(st);
1339+
process_sameIntent_hasName_emptyValue_arrayItem();
13391340
} else if (isEmptyValue && ! isArrayItem) {
1340-
process_sameIntent_hasName_emptyValue_noArrayItem(st);
1341+
process_sameIntent_hasName_emptyValue_noArrayItem();
13411342
} else {
1342-
st.logUnknownLine(TAG);
1343+
logUnknownParseLine();
13431344
}
13441345
} else {
1345-
st.logUnknownLine(TAG);
1346+
logUnknownParseLine();
13461347
}
13471348
} else {
1348-
st.logUnknownLine(TAG);
1349+
logUnknownParseLine();
13491350
}
13501351
}
13511352
return true;
13521353
}
13531354

13541355
// ---------------------------------------------------------------------
13551356

1356-
void WsjcppYaml::process_sameIntent_hasName_emptyValue_arrayItem(WsjcppYamlParserStatus &st) {
1357-
st.logUnknownLine("process_sameIntent_hasName_emptyValue_arrayItem");
1357+
void WsjcppYaml::process_sameIntent_hasName_emptyValue_arrayItem() {
1358+
WsjcppLog::warn(TAG, "process_sameIntent_hasName_emptyValue_arrayItem");
1359+
this->logUnknownParseLine();
13581360
}
13591361

13601362
// ---------------------------------------------------------------------
13611363

1362-
void WsjcppYaml::process_sameIntent_hasName_emptyValue_noArrayItem(WsjcppYamlParserStatus &st) {
1364+
void WsjcppYaml::process_sameIntent_hasName_emptyValue_noArrayItem() {
13631365
WsjcppYamlNode *pItem = new WsjcppYamlNode(
1364-
st.pCurItem, st.placeInFile,
1366+
m_pParseCurrentItem, m_parsePlaceInFile,
13651367
WSJCPP_YAML_NODE_UNDEFINED
13661368
);
1367-
if (st.line.getValueQuotes() != WSJCPP_YAML_QUOTES_NONE) {
1369+
if (m_parseLine.getValueQuotes() != WSJCPP_YAML_QUOTES_NONE) {
13681370
pItem->doValue();
1369-
pItem->setValue(st.line.getValue(), st.line.getValueQuotes());
1371+
pItem->setValue(m_parseLine.getValue(), m_parseLine.getValueQuotes());
13701372
}
1371-
pItem->setName(st.line.getName(), st.line.getNameQuotes());
1372-
pItem->setComment(st.line.getComment());
1373-
st.pCurItem->setElement(st.line.getName(), pItem);
1374-
st.pCurItem = pItem;
1375-
st.nIntent = st.nIntent + 2;
1373+
pItem->setName(m_parseLine.getName(), m_parseLine.getNameQuotes());
1374+
pItem->setComment(m_parseLine.getComment());
1375+
m_pParseCurrentItem->setElement(m_parseLine.getName(), pItem);
1376+
m_pParseCurrentItem = pItem;
1377+
int nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent;
1378+
m_nParseCurrentIntent = m_nParseCurrentIntent + nDiffIntent;
13761379
}
13771380

13781381
// ---------------------------------------------------------------------
13791382

1380-
void WsjcppYaml::process_sameIntent_hasName_hasValue_arrayItem(WsjcppYamlParserStatus &st) {
1381-
if (st.pCurItem->isUndefined()) {
1382-
st.pCurItem->doArray();
1383+
void WsjcppYaml::process_sameIntent_hasName_hasValue_arrayItem() {
1384+
if (m_pParseCurrentItem->isUndefined()) {
1385+
m_pParseCurrentItem->doArray();
13831386
}
13841387
WsjcppYamlNode *pMapItem = new WsjcppYamlNode(
1385-
st.pCurItem, st.placeInFile,
1388+
m_pParseCurrentItem, m_parsePlaceInFile,
13861389
WSJCPP_YAML_NODE_MAP
13871390
);
1388-
st.pCurItem->appendElement(pMapItem);
1389-
st.pCurItem = pMapItem;
1390-
st.nIntent = st.nIntent + 2;
1391+
m_pParseCurrentItem->appendElement(pMapItem);
1392+
m_pParseCurrentItem = pMapItem;
1393+
int nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent;
1394+
m_nParseCurrentIntent = m_nParseCurrentIntent + nDiffIntent;
13911395

13921396
WsjcppYamlNode *pItem = new WsjcppYamlNode(
1393-
st.pCurItem, st.placeInFile,
1397+
m_pParseCurrentItem, m_parsePlaceInFile,
13941398
WSJCPP_YAML_NODE_VALUE
13951399
);
1396-
pItem->setComment(st.line.getComment());
1397-
pItem->setValue(st.line.getValue(), st.line.getValueQuotes());
1398-
pItem->setName(st.line.getName(), st.line.getNameQuotes());
1399-
pMapItem->setElement(st.line.getName(), pItem);
1400-
st.pCurItem = pItem;
1401-
st.nIntent = st.nIntent + 2;
1400+
pItem->setComment(m_parseLine.getComment());
1401+
pItem->setValue(m_parseLine.getValue(), m_parseLine.getValueQuotes());
1402+
pItem->setName(m_parseLine.getName(), m_parseLine.getNameQuotes());
1403+
pMapItem->setElement(m_parseLine.getName(), pItem);
1404+
m_pParseCurrentItem = pItem;
1405+
nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent;
1406+
m_nParseCurrentIntent = m_nParseCurrentIntent + nDiffIntent;
14021407
}
14031408

14041409
// ---------------------------------------------------------------------
14051410

1406-
void WsjcppYaml::process_sameIntent_hasName_hasValue_noArrayItem(WsjcppYamlParserStatus &st) {
1411+
void WsjcppYaml::process_sameIntent_hasName_hasValue_noArrayItem() {
14071412
WsjcppYamlNode *pItem = new WsjcppYamlNode(
1408-
st.pCurItem, st.placeInFile,
1413+
m_pParseCurrentItem, m_parsePlaceInFile,
14091414
WSJCPP_YAML_NODE_VALUE
14101415
);
1411-
pItem->setComment(st.line.getComment());
1412-
pItem->setValue(st.line.getValue(), st.line.getValueQuotes());
1413-
pItem->setName(st.line.getName(), st.line.getNameQuotes());
1414-
st.pCurItem->setElement(st.line.getName(), pItem);
1415-
st.pCurItem = pItem;
1416-
st.nIntent = st.nIntent + 2;
1416+
pItem->setComment(m_parseLine.getComment());
1417+
pItem->setValue(m_parseLine.getValue(), m_parseLine.getValueQuotes());
1418+
pItem->setName(m_parseLine.getName(), m_parseLine.getNameQuotes());
1419+
m_pParseCurrentItem->setElement(m_parseLine.getName(), pItem);
1420+
m_pParseCurrentItem = pItem;
1421+
int nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent;
1422+
m_nParseCurrentIntent = m_nParseCurrentIntent + nDiffIntent;
14171423
}
14181424

14191425
// ---------------------------------------------------------------------
14201426

1421-
void WsjcppYaml::process_sameIntent_emptyName_hasValue_arrayItem(WsjcppYamlParserStatus &st) {
1422-
if (st.pCurItem->isUndefined()) {
1423-
st.pCurItem->doArray();
1427+
void WsjcppYaml::process_sameIntent_emptyName_hasValue_arrayItem() {
1428+
if (m_pParseCurrentItem->isUndefined()) {
1429+
m_pParseCurrentItem->doArray();
14241430
}
14251431
WsjcppYamlNode *pItem = new WsjcppYamlNode(
1426-
st.pCurItem, st.placeInFile,
1432+
m_pParseCurrentItem, m_parsePlaceInFile,
14271433
WSJCPP_YAML_NODE_VALUE
14281434
);
1429-
pItem->setComment(st.line.getComment());
1430-
pItem->setValue(st.line.getValue(), st.line.getValueQuotes());
1431-
st.pCurItem->appendElement(pItem);
1432-
st.pCurItem = pItem;
1433-
st.nIntent = st.nIntent + 2;
1435+
pItem->setComment(m_parseLine.getComment());
1436+
pItem->setValue(m_parseLine.getValue(), m_parseLine.getValueQuotes());
1437+
m_pParseCurrentItem->appendElement(pItem);
1438+
m_pParseCurrentItem = pItem;
1439+
int nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent;
1440+
m_nParseCurrentIntent = m_nParseCurrentIntent + nDiffIntent;
14341441
}
14351442

14361443
// ---------------------------------------------------------------------
14371444

1438-
void WsjcppYaml::process_sameIntent_emptyName_hasValue_noArrayItem(WsjcppYamlParserStatus &st) {
1439-
st.logUnknownLine("TODO process_sameIntent_emptyName_hasValue_noArrayItem");
1445+
void WsjcppYaml::process_sameIntent_emptyName_hasValue_noArrayItem() {
1446+
WsjcppLog::warn(TAG, "TODO process_sameIntent_emptyName_hasValue_noArrayItem");
1447+
this->logUnknownParseLine();
1448+
14401449
}
14411450

14421451
// ---------------------------------------------------------------------
14431452

1444-
void WsjcppYaml::process_sameIntent_emptyName_emptyValue_arrayItem(WsjcppYamlParserStatus &st) {
1445-
if (st.pCurItem->isUndefined()) {
1446-
st.pCurItem->doArray();
1453+
void WsjcppYaml::process_sameIntent_emptyName_emptyValue_arrayItem() {
1454+
if (m_pParseCurrentItem->isUndefined()) {
1455+
m_pParseCurrentItem->doArray();
14471456
}
14481457
WsjcppYamlNode *pItem = new WsjcppYamlNode(
1449-
st.pCurItem, st.placeInFile,
1458+
m_pParseCurrentItem, m_parsePlaceInFile,
14501459
WSJCPP_YAML_NODE_VALUE
14511460
);
1452-
pItem->setComment(st.line.getComment());
1453-
pItem->setValue(st.line.getValue(), st.line.getValueQuotes());
1454-
st.pCurItem->appendElement(pItem);
1455-
st.pCurItem = pItem;
1456-
st.nIntent = st.nIntent + 2;
1461+
pItem->setComment(m_parseLine.getComment());
1462+
pItem->setValue(m_parseLine.getValue(), m_parseLine.getValueQuotes());
1463+
m_pParseCurrentItem->appendElement(pItem);
1464+
m_pParseCurrentItem = pItem;
1465+
int nDiffIntent = m_parseLine.getIntent() - m_nParseCurrentIntent;
1466+
m_nParseCurrentIntent = m_nParseCurrentIntent + nDiffIntent;
14571467
}
14581468

14591469
// ---------------------------------------------------------------------
14601470

1461-
void WsjcppYaml::process_sameIntent_emptyName_emptyValue_noArrayItem(WsjcppYamlParserStatus &st) {
1462-
WsjcppYamlNode *pItem = new WsjcppYamlNode(
1463-
st.pCurItem, st.placeInFile,
1471+
void WsjcppYaml::process_sameIntent_emptyName_emptyValue_noArrayItem() {
1472+
WsjcppYamlNode *pNode = new WsjcppYamlNode(
1473+
m_pParseCurrentItem, m_parsePlaceInFile,
14641474
WSJCPP_YAML_NODE_EMPTY
14651475
);
1466-
pItem->setComment(st.line.getComment());
1467-
st.pCurItem->appendElement(pItem);
1476+
pNode->setComment(m_parseLine.getComment());
1477+
m_pParseCurrentItem->appendElement(pNode);
14681478
}
14691479

14701480
// ---------------------------------------------------------------------
14711481

1482+
void WsjcppYaml::logUnknownParseLine() {
1483+
WsjcppLog::warn(TAG, "\n"
1484+
" error:\n"
1485+
" desc: \"unknown_line\"\n"
1486+
" line_number: " + std::to_string(m_pParseCurrentItem->getPlaceInFile().getNumberOfLine()) + "\n"
1487+
" line: \"" + m_parsePlaceInFile.getLine() + "\"\n"
1488+
" intent: " + std::to_string(m_nParseCurrentIntent) + "\n"
1489+
" filename: \"" + m_pParseCurrentItem->getPlaceInFile().getFilename() + "\""
1490+
);
1491+
}

0 commit comments

Comments
 (0)