11#include " SpinParser.h"
22
3- #define KEY_ELEMENT_SEP " : "
3+ #define KEY_ELEMENT_SEP ' : '
44
55SpinParser::SpinParser ()
66{
@@ -71,17 +71,11 @@ QStringList SpinParser::spinFileTree(QString file, QString libpath)
7171 QStringList nodes;
7272 foreach (key, keys) {
7373 value = db[key];
74- QStringList levels = key.split (" /" );
75- int lcount = levels.count ()-1 ;
76- QStringList keyel = levels[lcount].split (KEY_ELEMENT_SEP);
77- if (keyel.count () < 2 )
78- continue ;
79- if (QString (keyel[0 ]).compare (keyel[1 ]) == 0 ) {
80- // qDebug() << key;
81- objectInfo (value, subnode, subfile);
82- // for(int n = 0; n < lcount; n++) subfile = " " + subfile;
83- nodes.append (subfile);
84- }
74+ QStringList tags = value.split (" \t " );
75+ if (tags.count () < 4 || !tags[3 ].startsWith (SpinKinds[K_OBJECT].letter ))
76+ continue ; // short list or not an object
77+ objectInfo (value, subnode, subfile);
78+ nodes.append (subfile);
8579 }
8680 // Indent later. Just sort for now.
8781#ifdef QT5
@@ -93,7 +87,6 @@ QStringList SpinParser::spinFileTree(QString file, QString libpath)
9387 QString ns = nodes.at (n);
9488 spinFiles.append (ns.mid (ns.lastIndexOf (" :" )+1 ));
9589 }
96-
9790 return spinFiles;
9891}
9992
@@ -421,9 +414,6 @@ void SpinParser::match_dat (QString p, int line)
421414
422415void SpinParser::match_object (QString p, int line)
423416{
424- QString tag = " " ;
425- QString subfile = " " ;
426- QString subnode = " " ;
427417 int len = p.indexOf (" :" );
428418 if (p.indexOf (" :=" ) > 0 )
429419 len = 0 ;
@@ -440,15 +430,38 @@ void SpinParser::match_object (QString p, int line)
440430 if (s.contains (" [" ,Qt::CaseInsensitive))
441431 p = p.mid (0 ,p.indexOf (" [" ,0 ,Qt::CaseInsensitive));
442432 p = p.trimmed ();
443- tag = s+" \t " +currentFile+" \t " +p+" \t " +SpinKinds[K_OBJECT].letter ;
444- objectInfo (tag, subnode, subfile);
433+
434+ QString key = objectNode+KEY_ELEMENT_SEP+s;
435+ QString tag = s+" \t " +currentFile+" \t " +p+" \t " +SpinKinds[K_OBJECT].letter ;
436+ QString dummy, subnode, subfile, parent, curr = key;
437+
438+ objectInfo (tag, subnode, subfile, parent);
445439 QString file = checkFile (subfile);
446- if (QFile::exists (file) == false )
447- return ;
448- QString key = objectNode+" /" +subnode+KEY_ELEMENT_SEP+s;
449- if (db.contains (key) == false )
440+
441+ // file is missing, ignore object
442+ if (!QFile::exists (file)) return ;
443+
444+ // avoid circular references, do this by scanning all existing
445+ // parents of this particular key (*/*/*:*)
446+ for (;;) {
447+ int index;
448+ // invalid match, ignore object
449+ if (parent == file) {
450+ qDebug () << " circular reference: " << s << currentFile;
451+ return ;
452+ }
453+ // cut off key at KEY_ELEMENT_SEP
454+ if ((index = curr.lastIndexOf (KEY_ELEMENT_SEP)) == -1 ) break ;
455+ curr.resize (index);
456+ // replace last / with KEY_ELEMENT_SEP
457+ if ((index = curr.lastIndexOf (' /' )) == -1 ) break ;
458+ curr[index] = QChar (KEY_ELEMENT_SEP);
459+ // extract parent file
460+ objectInfo (db[curr], dummy, dummy, parent);
461+ }
462+ if (!db.contains (key))
450463 db.insert (key,tag+" \t " +QString (" %1" ).arg (line));
451- // qDebug() << objectNode+"/"+subnode << " :: " << tag;
464+
452465 findSpinTags (subfile,objectNode+" /" +subnode);
453466 }
454467}
@@ -531,10 +544,16 @@ void SpinParser::match_var (QString p, int line)
531544}
532545
533546int SpinParser::objectInfo (QString tag, QString &name, QString &file)
547+ {
548+ return objectInfo (tag, name, file, file);
549+ }
550+
551+ int SpinParser::objectInfo (QString tag, QString &name, QString &file, QString &parent)
534552{
535553 QStringList list = tag.split (" \t " ,QString::KeepEmptyParts);
536554 if (list.length () == 0 )
537555 return 0 ;
556+
538557 name = QString (list[0 ]).trimmed ();
539558 file = QString (list[2 ]).trimmed ();
540559 file = file.mid (file.indexOf (" \" " )+1 );
@@ -544,6 +563,9 @@ int SpinParser::objectInfo(QString tag, QString &name, QString &file)
544563 if (file.contains (" .spin" ,Qt::CaseInsensitive) == false )
545564 file += " .spin" ;
546565
566+ if (parent != file)
567+ parent = QString (list[1 ]).trimmed ();
568+
547569 return list.length ();
548570}
549571
0 commit comments