// -*- mode: C++; c-file-style: "cc-mode" -*-
//*************************************************************************
// DESCRIPTION: Verilator: Netlist (top level) functions
//
// Code available from: https://verilator.org
//
//*************************************************************************
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of either the GNU Lesser General Public License Version 3
// or the Perl Artistic License Version 2.0.
// SPDX-FileCopyrightText: 2003-2026 Wilson Snyder
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
//
//*************************************************************************
// Overview of files involved in parsing
//       V3Parse.h              External consumer interface to V3ParseImp
//       V3ParseImp             Internals to parser, common to across flex & bison
//         V3ParseGrammar       Wrapper that includes V3ParseBison
//           V3ParseBison       Bison output
//         V3ParseLex           Wrapper that includes lex output
//           V3Lexer.yy.cpp     Flex output
//*************************************************************************

#define VL_MT_DISABLED_CODE_UNIT 1

#include "config_build.h"
#include "verilatedos.h"

#include "V3ParseImp.h"

#include "V3Ast.h"
#include "V3Error.h"
#include "V3File.h"
#include "V3Global.h"
#include "V3LanguageWords.h"
#include "V3Os.h"
#include "V3ParseBison.h"  // Generated by bison
#include "V3PreShell.h"
#include "V3Stats.h"

#include <cctype>
#include <sstream>

VL_DEFINE_DEBUG_FUNCTIONS;

//======================================================================
// Globals

V3ParseImp* V3ParseImp::s_parsep = nullptr;

//######################################################################
// Parser constructor

V3ParseImp::~V3ParseImp() {
    for (auto& itr : m_stringps) VL_DO_DANGLING(delete itr, itr);
    m_stringps.clear();
    for (auto& itr : m_numberps) VL_DO_DANGLING(delete itr, itr);
    m_numberps.clear();
    lexDestroy();
    parserClear();
}

//######################################################################
// Parser utility methods

void V3ParseImp::importIfInStd(FileLine* fileline, const string& id, bool doImport) {
    // Keywords that auto-import to require use of verilated_std.vh.
    // OK if overly sensitive; will over-import and keep std:: around
    // longer than migt otherwise.
    if (v3Global.usesStdPackage()) return;  // Run once then short-circuit
    const bool identifierImportsStd = (id == "mailbox" || id == "process" || id == "randomize"
                                       || id == "semaphore" || id == "std");
    if (!identifierImportsStd) return;
    // Ignore Std:: used inside verilated_std.vh itself
    if (fileline->filename() == V3Options::getStdPackagePath()) return;
    if (AstPackage* const stdpkgp
        = v3Global.rootp()->stdPackagep()) {  // else e.g. --no-std-package
        UINFO(9, "import and keep std:: for " << fileline);
        v3Global.setUsesStdPackage();
        if (doImport) {
            AstPackageImport* const impp = new AstPackageImport{stdpkgp->fileline(), stdpkgp, "*"};
            unitPackage(stdpkgp->fileline())->addStmtsp(impp);
            for (AstNode* itemp = v3Global.rootp()->stdPackagep()->stmtsp(); itemp;
                 itemp = itemp->nextp()) {
                if (itemp->name() == "process") {
                    v3Global.rootp()->stdPackageProcessp(VN_AS(itemp, Class));
                    UASSERT_OBJ(v3Global.rootp()->stdPackageProcessp(), v3Global.rootp(),
                                "'std' package class should be found");
                    break;
                }
            }
        }
    }
}

AstNodeExpr* V3ParseImp::makePropertyCase(FileLine* flp, AstNodeExpr* exprp, AstCaseItem* itemsp) {
    AstNodeExpr* resultp = nullptr;
    AstNodeExpr* matchedp = nullptr;
    AstNodeExpr* defaultPropp = nullptr;
    FileLine* defaultFlp = flp;

    if (!itemsp) {
        flp->v3error("Property case statement with no items");
        exprp->deleteTree();
        return new AstConst{flp, AstConst::BitTrue{}};
    }

    for (AstCaseItem *nextp, *itemp = itemsp; itemp; itemp = nextp) {
        nextp = VN_AS(itemp->nextp(), CaseItem);
        AstNodeExpr* const propp = VN_AS(itemp->stmtsp()->unlinkFrBack(), NodeExpr);

        if (itemp->isDefault()) {
            if (defaultPropp) {
                itemp->v3error("Multiple default statements in property case statement");
                defaultPropp->deleteTree();
                exprp->deleteTree();
                return new AstConst{flp, AstConst::BitTrue{}};
            }
            defaultFlp = itemp->fileline();
            defaultPropp = propp;
            continue;
        }

        AstNodeExpr* itemMatchp = nullptr;
        for (AstNodeExpr *condNextp, *condp = itemp->condsp(); condp; condp = condNextp) {
            condNextp = VN_AS(condp->nextp(), NodeExpr);
            condp->unlinkFrBack();
            AstNodeExpr* const eqp
                = new AstEqCase{condp->fileline(), exprp->cloneTreePure(false), condp};
            itemMatchp = itemMatchp ? new AstLogOr{itemp->fileline(), itemMatchp, eqp} : eqp;
        }
        UASSERT_OBJ(itemMatchp, itemp, "Property case item without condition");
        AstNodeExpr* const guardp
            = matchedp
                  ? new AstLogAnd{itemp->fileline(), itemMatchp->cloneTreePure(false),
                                  new AstLogNot{itemp->fileline(), matchedp->cloneTreePure(false)}}
                  : itemMatchp->cloneTreePure(false);
        AstNodeExpr* const branchp = new AstImplication{itemp->fileline(), guardp, propp, true};
        resultp = resultp ? new AstSAnd{flp, resultp, branchp} : branchp;
        matchedp = matchedp ? new AstLogOr{itemp->fileline(), matchedp, itemMatchp} : itemMatchp;
    }
    itemsp->deleteTree();

    if (defaultPropp) {
        if (!matchedp) {
            exprp->deleteTree();
            return defaultPropp;
        }
        AstNodeExpr* const noMatchp
            = static_cast<AstNodeExpr*>(new AstLogNot{defaultFlp, matchedp->cloneTreePure(false)});
        AstNodeExpr* const branchp = new AstImplication{defaultFlp, noMatchp, defaultPropp, true};
        resultp = new AstSAnd{flp, resultp, branchp};
    }
    matchedp->deleteTree();
    exprp->deleteTree();
    return resultp;
}

void V3ParseImp::lexPpline(const char* textp) {
    // Handle lexer `line directive
    // FileLine* const prevFl = lexFileline();
    string newFilename;
    int newLineno = -1;
    int enterExit = 0;
    lexFileline()->lineDirectiveParse(textp, newFilename /*ref*/, newLineno /*ref*/,
                                      enterExit /*ref*/);
    if (enterExit == 1) {  // Enter
        FileLine* const prevFl = lexFileline()->copyOrSameFileLine();  // Without applyIgnores
        FileLine* const newFl
            = new FileLine{prevFl};  // Not copyOrSameFileLine as need to keep old value
        lexFileline(newFl);
        lexFileline()->parent(prevFl);
    } else if (enterExit == 2) {  // Exit
        const FileLine* const prevFl = lexFileline();
        if (FileLine* upFl = lexFileline()->parent()) {
            // Must copy upFl as may be existing nodes that use the FileLine value
            lexFileline(new FileLine{upFl});  // Restore warning state to upper file
            lexFileline()->contentLinenoFrom(prevFl);
        }
    }
    if (enterExit != -1) {  // Line/fn change
        lexFileline()->filename(newFilename);
        lexFileline()->lineno(newLineno);
        lexFileline()->applyIgnores();
    }
}

void V3ParseImp::lexTimescaleParse(FileLine* fl, const char* textp) {
    // Parse `timescale of <number><units> / <number><units>
    VTimescale unit;
    VTimescale prec;
    VTimescale::parseSlashed(fl, textp, unit /*ref*/, prec /*ref*/);
    m_timeLastUnit = v3Global.opt.timeComputeUnit(unit);
    m_timeLastPrec = v3Global.opt.timeComputePrec(prec);
}
AstPragma* V3ParseImp::createTimescale(FileLine* fl, bool unitSet, double unitVal, bool precSet,
                                       double precVal) {
    VTimescale unit{VTimescale::NONE};
    if (unitSet) {
        bool bad;
        unit = VTimescale{unitVal, bad /*ref*/};
        if (bad) {
            UINFO(1, "Value = " << unitVal);
            fl->v3error("timeunit illegal value");
        }
    }
    VTimescale prec{VTimescale::NONE};
    if (precSet) {
        bool bad;
        prec = VTimescale{precVal, bad /*ref*/};
        if (bad) {
            UINFO(1, "Value = " << precVal);
            fl->v3error("timeprecision illegal value");
        }
    }
    v3Global.rootp()->timeprecisionMerge(fl, prec);
    if (unit.isNone()) {
        return nullptr;
    } else {
        unit = v3Global.opt.timeComputeUnit(unit);
        return new AstPragma{fl, unit};
    }
}

void V3ParseImp::lexVerilatorCmtLintSave(const FileLine* fl) { m_lexLintState.push_back(*fl); }

void V3ParseImp::lexVerilatorCmtLintRestore(FileLine* fl) {
    if (m_lexLintState.empty()) {
        fl->v3error("/*verilator lint_restore*/ without matching save");
        return;
    }
    fl->warnStateFrom(m_lexLintState.back());
    m_lexLintState.pop_back();
}

void V3ParseImp::lexVerilatorCmtLint(FileLine* fl, const char* textp, bool turnOff) {
    const char* sp = textp;
    while (*sp && !std::isspace(*sp)) ++sp;
    while (*sp && std::isspace(*sp)) ++sp;
    while (*sp && !std::isspace(*sp)) ++sp;
    while (*sp && std::isspace(*sp)) ++sp;
    string msg = sp;
    string::size_type pos = msg.find('*');
    if (pos != string::npos) msg.erase(pos);
    // Use parsep()->lexFileline() as want to affect later FileLine's warnings
    const string err = parsep()->lexFileline()->warnOffParse(msg, turnOff);
    if (!err.empty())
        fl->v3error("Unknown verilator lint message code: '" << err << "', in '" << textp << "'");
}

void V3ParseImp::lexVerilatorCmtBad(FileLine* fl, const char* textp) {
    string cmtparse = textp;
    if (cmtparse.substr(0, std::strlen("/*verilator")) == "/*verilator") {
        cmtparse.replace(0, std::strlen("/*verilator"), "");
    }
    while (std::isspace(cmtparse[0])) cmtparse.replace(0, 1, "");
    string cmtname;
    for (int i = 0; std::isalnum(cmtparse[i]); i++) cmtname += cmtparse[i];
    if (!v3Global.opt.isFuture(cmtname)) {
        fl->v3warn(BADVLTPRAGMA, "Unknown verilator comment: '" << textp << "'");
    }
}

void V3ParseImp::lexErrorPreprocDirective(FileLine* fl, const char* textp) {
    // Find all `preprocessor spelling candidates
    // Can't make this static as might get more defines later when read cells
    VSpellCheck speller;
    for (V3LanguageWords::const_iterator it = V3LanguageWords::begin();
         it != V3LanguageWords::end(); ++it) {
        const string ppDirective = it->first;
        if (ppDirective[0] == '`') speller.pushCandidate(ppDirective);
    }
    V3PreShell::candidateDefines(&speller);
    const string suggest = speller.bestCandidateMsg(textp);
    fl->v3error("Define or directive not defined: '"
                << textp << "'\n"
                << (suggest.empty() ? "" : fl->warnMore() + suggest));
}

string V3ParseImp::lexParseTag(const char* textp) {
    string tmp = textp + std::strlen("/*verilator tag ");
    string::size_type pos = tmp.rfind("*/");
    if (pos != string::npos) tmp.erase(pos);
    return tmp;
}

double V3ParseImp::lexParseTimenum(const char* textp) {
    const size_t length = std::strlen(textp);
    char* const strgp = new char[length + 1];
    char* dp = strgp;
    const char* sp = textp;
    for (; std::isdigit(*sp) || *sp == '_' || *sp == '.'; ++sp) {
        if (*sp != '_') *dp++ = *sp;
    }
    *dp++ = '\0';
    const double d = strtod(strgp, nullptr);
    const string suffix{sp};

    double divisor = 1;
    if (suffix == "s") {
        divisor = 1;
    } else if (suffix == "ms") {
        divisor = 1e3;
    } else if (suffix == "us") {
        divisor = 1e6;
    } else if (suffix == "ns") {
        divisor = 1e9;
    } else if (suffix == "ps") {
        divisor = 1e12;
    } else if (suffix == "fs") {
        divisor = 1e15;
    } else {
        // verilog.l checks the suffix for us, so this is an assert
        v3fatalSrc("Unknown time suffix " << suffix);
    }

    VL_DO_DANGLING(delete[] strgp, strgp);
    return d / divisor;
}

//######################################################################
// Parser tokenization

size_t V3ParseImp::ppInputToLex(char* buf, size_t max_size) {
    size_t got = 0;
    while (got < max_size  // Haven't got enough
           && !m_ppBuffers.empty()) {  // And something buffered
        string front = m_ppBuffers.front();
        m_ppBuffers.pop_front();
        size_t len = front.length();
        if (len > (max_size - got)) {  // Front string too big
            const string remainder = front.substr(max_size - got);
            front.resize(max_size - got);
            m_ppBuffers.push_front(remainder);  // Put back remainder for next time
            len = (max_size - got);
        }
        std::memcpy(buf + got, front.c_str(), len);
        got += len;
    }
    if (debug() >= 9) {
        const string out = std::string{buf, got};
        cout << "   inputToLex  got=" << got << " '" << out << "'\n";
    }
    // Note returns 0 at EOF
    return got;
}

void V3ParseImp::preprocDumps(std::ostream& os, bool forInputs) {
    const bool noblanks
        = forInputs || (v3Global.opt.preprocOnly() && v3Global.opt.preprocNoLine());
    const bool nolines = forInputs;
    bool anyNonVerilog = false;
    for (auto& buf : m_ppBuffers) {
        if (noblanks) {
            bool blank = true;
            for (const char ch : buf) {
                if (!std::isspace(ch) && ch != '\n') {
                    blank = false;
                    break;
                }
            }
            if (blank) continue;
            if (nolines && buf.rfind("`line ", 0) == 0) continue;
            // Ignores comments, but over-positive match search should be ok
            if (forInputs && buf.rfind("`verilator_config", 0) == 0) anyNonVerilog = true;
        }
        os << buf;
    }
    if (forInputs && anyNonVerilog) os << "\n`verilog\n";
}

void V3ParseImp::parseFile(
    FileLine* fileline, const string& modfilename, bool inLibrary, bool inLibMap,
    const string& libname,
    const string& errmsg,  // "" for no error, make fake node
    const std::string& notFoundName) {  // name for AstNotFoundModule - modfilename will be used if
                                        // notFoundName is empty
    const string nondirname = V3Os::filenameNonDir(modfilename);
    const string modname = V3Os::filenameNonDirExt(modfilename);

    UINFO(2, __FUNCTION__ << ": " << modname << (inLibrary ? " [LIB]" : "")
                          << (inLibMap ? " [LIBMAP]" : ""));
    m_lexFileline = new FileLine{fileline};
    m_lexFileline->newContent();
    m_bisonLastFileline = m_lexFileline;
    m_inLibrary = inLibrary;
    m_inLibMap = inLibMap;
    m_libname = libname;

    // Preprocess into m_ppBuffer
    const bool ok = V3PreShell::preproc(fileline, modfilename, m_filterp, this, errmsg);
    if (!ok) {
        if (errmsg != "") return;  // Threw error already
        // Create fake node for later error reporting
        AstNodeModule* const nodep = new AstNotFoundModule{
            fileline, notFoundName.empty() ? modname : notFoundName, libname};
        v3Global.rootp()->addModulesp(nodep);
        return;
    }

    if (v3Global.opt.preprocOnly() || v3Global.opt.keepTempFiles()) {
        // Create output file with all the preprocessor output we buffered up
        const string vppfilename = v3Global.opt.hierTopDataDir() + "/" + v3Global.opt.prefix()
                                   + "__" + nondirname + ".vpp";
        std::ofstream* ofp = nullptr;
        std::ostream* osp;
        if (v3Global.opt.preprocOnly()) {
            osp = &cout;
        } else {
            osp = ofp = V3File::new_ofstream(vppfilename);
        }
        if (osp->fail()) {
            fileline->v3error("Can't write file: " + vppfilename);
            return;
        }
        if (v3Global.opt.dumpDefines()) {
            V3PreShell::dumpDefines(*osp);
        } else {
            preprocDumps(*osp, false);
        }
        if (ofp) {
            ofp->close();
            VL_DO_DANGLING(delete ofp, ofp);
        }
    }

    V3Stats::addStatSum(V3Stats::STAT_SOURCE_CHARS, m_ppBytes);
    if ((debug() || v3Global.opt.dumpLevel("inputs"))
        && modfilename != V3Options::getStdPackagePath()
        && modfilename != V3Options::getStdWaiverPath())
        dumpInputsFile();

    // Parse it
    if (!v3Global.opt.preprocOnly() || v3Global.opt.preprocResolve()) {
        lexFile(modfilename);
    } else {
        m_ppBuffers.clear();
    }
}

void V3ParseImp::dumpInputsFile() {
    // Create output file with joined preprocessor output we buffered up,
    // Useful for debug to feed back into Verilator
    static bool s_append = false;
    const string vppfilename
        = v3Global.opt.hierTopDataDir() + "/" + v3Global.opt.prefix() + "__inputs.vpp";
    std::ofstream* ofp = V3File::new_ofstream(vppfilename, s_append);
    if (ofp->fail()) {
        v3error("Can't write file: " + vppfilename);
        return;
    }
    if (!s_append) {
        s_append = true;
        UINFO(1, "Writing all preprocessed output to " << vppfilename);
        *ofp << "// Dump of all post-preprocessor input\n";
        *ofp << "// Blank lines and `line directives have been removed\n";
        *ofp << "//\n";
        V3Stats::infoHeader(*ofp, "// ");
        *ofp << '\n';
        for (const auto& pair : v3Global.opt.allArgs()) {
            if (!pair.second) continue;
            *ofp << "// verilator fargs";
            for (const std::string& arg : pair.first) {
                // Apply some quoting, pretty basic, update as needed ...
                std::string quoted;
                bool quoteIt = false;
                for (const char c : arg) {
                    if (c == '"' || c == '\\') {
                        quoteIt = true;
                        quoted += '\\';
                    } else if (std::isspace(c)) {
                        quoteIt = true;
                    }
                    quoted += c;
                }
                *ofp << " " << (quoteIt ? '"' + quoted + '"' : arg);
            }
            *ofp << "\n";
        }
        *ofp << "\n";
    }
    preprocDumps(*ofp, true);
    ofp->close();
    VL_DO_DANGLING(delete ofp, ofp);
}

void V3ParseImp::dumpTokensAhead(int line) {  // LCOV_EXCL_START
    int i = 0;
    UINFO(1, "dumpTokensAhead @ " << line << " [ ] " << yylval);
    for (const V3ParseBisonYYSType& t : m_tokensAhead) {
        UINFO(1, "dumpTokensAhead @ " << line << " [" << i << "] " << t);
        ++i;
    }
}  // LCOV_EXCL_STOP

void V3ParseImp::lexFile(const string& modname) {
    // Prepare for lexing
    UINFO(3, "Lexing " << modname);
    s_parsep = this;
    lexFileline()->warnResetDefault();  // Reenable warnings on each file
    lexDestroy();  // Restart from clean slate.
    lexNew();

    // Lex it
    if (bisonParse()) v3fatal("Cannot continue\n");
}

void V3ParseImp::tokenPull() {
    // Pull token from lex into the pipeline
    // This corrupts yylval, must save/restore if required
    // Fetch next token from prefetch or real lexer
    VL_RESTORER(yylval);
    yylexReadTok();  // sets yylval
    UINFO(9, "tokenPulled " << yylval);
    m_tokensAhead.push_back(yylval);
    if (debug() >= 10) dumpTokensAhead(__LINE__);
}

const V3ParseBisonYYSType* V3ParseImp::tokenPeekp(size_t depth) {
    // Look ahead "depth" number of tokens in the input stream
    // Returns pointer to token, which is no longer valid after changing m_tokensAhead
    while (m_tokensAhead.size() <= depth) tokenPull();
    return &m_tokensAhead.at(depth);
}

size_t V3ParseImp::tokenPipeScanIdInst(size_t depthIn) {
    // Search around IEEE module_instantiation/interface_instantiation/program_instantiation
    // Return location of following token, or input if not found
    // yaID/*module_identifier*/ [ '#' '('...')' ] yaID/*name_of_instance*/ [ '['...']' ] '(' ...
    // yaID/*module_identifier*/ [ '#' id|etc ] yaID/*name_of_instance*/ [ '['...']' ] '(' ...
    size_t depth = depthIn;
    depth = tokenPipeScanParam(depth, true);

    if (tokenPeekp(depth)->token != yaID__LEX) return depthIn;
    ++depth;

    depth = tokenPipeScanBracket(depth);  // [ '['..']' ]*
    if (tokenPeekp(depth)->token != '(') return depthIn;

    return depth;
}

size_t V3ParseImp::tokenPipeScanIdType(size_t depthIn) {
    // Search around IEEE data type identifier
    // Return location of following token, or input if not found
    // tokenPipeScanIdCell has precedence over this search
    //   yaID/*type_identifier*/ [ '#' '('...')' ] [{ '['...']' }] yaID/*identifier*/
    // assignment_pattern_expression:
    //   yaID/*type_identifier*/ [ '#' '('...')' ] [{ '['...']' }] yP_TICKBRA
    // class_type parameter_value_assignment  // often followed by ) as in e.g. ClsA#(ClsB#(...))
    //   yaID/*type_identifier*/ '#' '('...')'  [^ '::']
    // and caller must check does NOT match tokenPipeScanIdCell
    size_t depth = depthIn;
    // UINFO(9, "tokenPipeScanType START d="
    //       << depth << " " << V3ParseImp::tokenName(tokenPeekp(depth)->token));
    if (tokenPeekp(depth)->token == '#' && tokenPeekp(depth + 1)->token == '(') {
        depth = tokenPipeScanParam(depth, false);
        // Not :: as then it's a yaID__CC, we'll parse that in tokenPipeScanId
        if (tokenPeekp(depth)->token != yP_COLONCOLON) return depth;
    }

    depth = tokenPipeScanBracket(depth);  // [ '['..']' ]*

    if (tokenPeekp(depth)->token != yaID__LEX && tokenPeekp(depth)->token != yP_TICKBRA)
        return depthIn;
    ++depth;
    // UINFO(9, "tokenPipeScanType MATCH");

    return depth;
}

size_t V3ParseImp::tokenPipeScanBracket(size_t inDepth) {
    // Return location of following token, or input if not found
    // [ '['...']' ]*
    int depth = inDepth;
    int bra = 0;
    while (tokenPeekp(depth)->token == '[') {
        do {  // Scan brackets
            const int tok = tokenPeekp(depth)->token;
            if (tok == 0) {  // LCOV_EXCL_BR_LINE
                UINFO(9, "tokenPipeScanBracket hit EOF; probably syntax error to come");
                return inDepth;  // LCOV_EXCL_LINE
            } else if (tok == '[') {
                ++bra;
                ++depth;
            } else if (bra && tok == ']') {
                --bra;
                ++depth;
            } else if (bra) {
                ++depth;
            }
        } while (bra);
    }
    return depth;
}

size_t V3ParseImp::tokenPipeScanParam(size_t inDepth, bool forCell) {
    // Search around IEEE parameter_value_assignment to see if :: follows
    // Return location of following token, or input if not found
    // yaID [ '#(' ... ')' ]
    // if forCell: yaID [ '#' number/etc ]
    int depth = inDepth;
    if (tokenPeekp(depth)->token != '#') return inDepth;
    ++depth;

    if (tokenPeekp(depth)->token != '(') {
        if (!forCell) return inDepth;
        // For module cells, we can have '#' and a number, or, annoyingly an idDotted
        const int ntoken = tokenPeekp(depth)->token;
        if (ntoken == yaINTNUM || ntoken == yaFLOATNUM || ntoken == yaTIMENUM
            || ntoken == yaID__LEX) {
            ++depth;
            return depth;
        }
        return inDepth;  // Miss
    }
    ++depth;
    int parens = 1;  // Count first (
    while (true) {
        const int tok = tokenPeekp(depth)->token;
        if (tok == 0) {  // LCOV_EXCL_BR_LINE
            UINFO(9, "tokenPipeScanParam hit EOF; probably syntax error to come");
            return inDepth;  // LCOV_EXCL_LINE
        } else if (tok == '(') {
            ++parens;
        } else if (tok == ')') {
            --parens;
            if (parens == 0) {
                ++depth;
                break;
            }
        }
        ++depth;
    }
    return depth;
}

size_t V3ParseImp::tokenPipeScanParens(size_t depth) {
    // Search around IEEE type_reference to see if is expression
    // Return location of following token, or input if not found
    // yTYPE__ETC '(' ... ')'  ['==' '===' '!=' '!===']
    //             ^- depth
    // yRANDOMIZE yWITH '(' ... ')'  ['{']
    //                   ^- depth
    if (tokenPeekp(depth)->token != '(') return depth;
    depth += 1;  // Past the (
    int parens = 1;  // Count first (
    while (true) {
        const int tok = tokenPeekp(depth)->token;
        if (tok == 0) {  // LCOV_EXCL_BR_LINE
            UINFO(9, "tokenPipeScanTypeParens hit EOF; probably syntax error to come");
            break;  // LCOV_EXCL_LINE
        } else if (tok == '(') {
            ++parens;
        } else if (tok == ')') {
            --parens;
            if (parens == 0) {
                ++depth;
                break;
            }
        }
        ++depth;
    }
    return depth;
}

size_t V3ParseImp::tokenPipeScanEqNew(size_t depth) {
    // Search around IEEE class_new to see if is expression
    // Return location of following token, or input if not found
    // '=' { packageClassScopeNoId } yNEW__LEX
    UINFO(9, "tokenPipelineScanEqNew tok=" << yylval.token);
    UASSERT(yylval.token == '=', "Start with '='");
    while (true) {
        const int tok = tokenPeekp(depth)->token;
        if (tok == 0) {  // LCOV_EXCL_BR_LINE
            UINFO(9, "tokenPipeScanEqNew hit EOF; probably syntax error to come");
            break;  // LCOV_EXCL_LINE
        } else if (tok == yNEW__LEX) {
            break;
        } else if (tok == yaID__LEX) {
            ++depth;  // yaID__LEX
            depth = tokenPipeScanParam(depth, false);
            if (tokenPeekp(depth)->token != yP_COLONCOLON) return 0;
            ++depth;  // yP_COLONCOLON
            continue;
        } else {
            return 0;  // Miss
        }
        ++depth;
    }
    return depth;
}

bool V3ParseImp::tokenPipeScanTaggedFollowsPrimary(size_t depth) {
    // Check if token at depth is a primary-starting token
    // Used to disambiguate tagged union expressions
    // Returns true if the token starts a primary expression (IEEE A.8.4)
    // Note: '.' and '.*' are pattern-specific, NOT primaries
    const int tok = tokenPeekp(depth)->token;
    if (tok == '(') return true;  // Parenthesized expression
    if (tok == '{') return true;  // Concatenation
    if (tok == yP_TICKBRA) return true;  // Assignment pattern '{
    if (tok == yaINTNUM) return true;  // Integer literal
    if (tok == yaFLOATNUM) return true;  // Float literal
    if (tok == yaTIMENUM) return true;  // Time literal
    if (tok == yaSTRING) return true;  // String literal
    if (tok == yNULL) return true;  // null
    if (tok == yTHIS) return true;  // this
    if (tok == '$') return true;  // $
    if (tok == yaID__LEX) return true;  // Identifier (raw)
    if (tok == yaID__ETC) return true;  // Identifier
    if (tok == yaID__CC) return true;  // Identifier with ::
    if (tok == yaID__aTYPE) return true;  // Type identifier
    if (tok == yaID__aINST) return true;  // Instance identifier
    if (tok == yTAGGED__LEX) return true;  // Nested tagged (raw)
    if (tok == yTAGGED) return true;  // Nested tagged
    if (tok == yTAGGED__NONPRIMARY) return true;  // Nested tagged (no primary)
    return false;
}

int V3ParseImp::tokenPipelineId(int token) {
    const V3ParseBisonYYSType* nexttokp = tokenPeekp(0);  // First char after yaID
    const int nexttok = nexttokp->token;
    UINFO(9, "tokenPipelineId tok=" << yylval.token);
    UASSERT(yylval.token == yaID__LEX, "Start with ID");
    if (nexttok == yP_COLONCOLON) return yaID__CC;
    VL_RESTORER(yylval);  // Remember value, as about to read ahead
    if (m_tokenLastBison.token != '@' && m_tokenLastBison.token != '#'
        && m_tokenLastBison.token != '.') {
        if (tokenPipeScanIdInst(0)) return yaID__aINST;
        if (tokenPipeScanIdType(0)) return yaID__aTYPE;
    }
    if (nexttok == '#') {  // e.g. class_type parameter_value_assignment '::'
        const size_t depth = tokenPipeScanParam(0, false);
        if (tokenPeekp(depth)->token == yP_COLONCOLON) return yaID__CC;
    }
    return token;
}

void V3ParseImp::tokenPipeline() {
    // called from bison's "yylex", has a "this"
    if (m_tokensAhead.empty()) tokenPull();  // corrupts yylval
    yylval = m_tokensAhead.front();
    m_tokensAhead.pop_front();
    int token = yylval.token;
    // If a paren, read another
    if (token == '('  //
        || token == ':'  //
        || token == '='  //
        || token == yCONST__LEX  //
        || token == yGLOBAL__LEX  //
        || token == yLOCAL__LEX  //
        || token == yNEW__LEX  //
        || token == ySTATIC__LEX  //
        || token == yTAGGED__LEX  //
        || token == yTYPE__LEX  //
        || token == yVIRTUAL__LEX  //
        || token == yWITH__LEX  //
        || token == yaID__LEX  //
    ) {
        if (debugFlex() >= 6) UINFO(0, "tokenPipeline: reading ahead");
        const V3ParseBisonYYSType curValue = yylval;  // Remember value, as about to read ahead
        const V3ParseBisonYYSType* nexttokp = tokenPeekp(0);
        const int nexttok = nexttokp->token;
        yylval = curValue;
        // Now potentially munge the current token
        if (token == '(' && isStrengthToken(nexttok)) {
            token = yP_PAR__STRENGTH;
        } else if (token == ':') {
            if (nexttok == yBEGIN) {
                token = yP_COLON__BEGIN;
            } else if (nexttok == yFORK) {
                token = yP_COLON__FORK;
            }
        } else if (token == '=') {
            if (nexttokp->token == yNEW__LEX || tokenPipeScanEqNew(0)) {
                token = yP_EQ__NEW;
            }  // else still '='
        } else if (token == yCONST__LEX) {
            if (nexttok == yREF) {
                token = yCONST__REF;
            } else {
                token = yCONST__ETC;
            }
        } else if (token == yGLOBAL__LEX) {
            if (nexttok == yCLOCKING) {
                token = yGLOBAL__CLOCKING;
            } else if (v3Global.opt.pedantic()) {
                token = yGLOBAL__ETC;
            }
            // Avoid 2009 "global" conflicting with old code when we can
            else {
                token = yaID__LEX;
                yylval.strp = V3ParseImp::parsep()->newString("global");
            }
        } else if (token == yLOCAL__LEX) {
            if (nexttok == yP_COLONCOLON) {
                token = yLOCAL__COLONCOLON;
            } else {
                token = yLOCAL__ETC;
            }
        } else if (token == yNEW__LEX) {
            if (nexttok == '(') {
                token = yNEW__PAREN;
            } else {
                token = yNEW__ETC;
            }
        } else if (token == ySTATIC__LEX) {
            if (nexttok == yCONSTRAINT) {
                token = ySTATIC__CONSTRAINT;
            } else {
                token = ySTATIC__ETC;
            }
        } else if (token == yTYPE__LEX) {
            VL_RESTORER(yylval);  // Remember value, as about to read ahead
            const size_t depth = tokenPipeScanParens(0);
            const int postToken = tokenPeekp(depth)->token;
            // v-- token                v-- postToken
            // yTYPE__EQ '(' .... ')' EQ_OPERATOR yTYPE_ETC '(' ... ')'
            if (postToken == yP_EQUAL || postToken == yP_NOTEQUAL || postToken == yP_CASEEQUAL
                || postToken == yP_CASENOTEQUAL) {
                token = yTYPE__EQ;
            } else {
                token = yTYPE__ETC;
            }
        } else if (token == yVIRTUAL__LEX) {
            if (nexttok == yCLASS) {
                token = yVIRTUAL__CLASS;
            } else if (nexttok == yINTERFACE) {
                token = yVIRTUAL__INTERFACE;
            } else if (nexttok == yaID__ETC  //
                       || nexttok == yaID__LEX) {
                // || nexttok == yaID__aINTERFACE  // but we may not know interfaces yet.
                token = yVIRTUAL__anyID;
            } else {
                token = yVIRTUAL__ETC;
            }
        } else if (token == yTAGGED__LEX) {
            // Look past 'tagged id' to see if primary follows
            // Check for all ID token types (both raw and processed forms)
            if (nexttok == yaID__LEX || nexttok == yaID__ETC || nexttok == yaID__aTYPE
                || nexttok == yaID__aINST) {
                VL_RESTORER(yylval);  // Remember value, as about to read ahead
                // Check token after the identifier
                if (tokenPipeScanTaggedFollowsPrimary(1)) {
                    token = yTAGGED;  // Has value expression following
                } else {
                    token = yTAGGED__NONPRIMARY;  // No primary follows
                }
            } else {
                // No identifier follows, not a tagged expression (e.g., 'union tagged {')
                token = yTAGGED;
            }
        } else if (token == yWITH__LEX) {
            if (nexttok == '(') {
                VL_RESTORER(yylval);  // Remember value, as about to read ahead
                const size_t depth = tokenPipeScanParens(0);
                const int postToken = tokenPeekp(depth)->token;
                // v-- token           v-- postToken
                // yWITH '(' .... ')' '{'
                if (postToken == '{') {
                    token = yWITH__PAREN_CUR;
                } else {
                    token = yWITH__PAREN;
                }
            } else if (nexttok == '[') {
                token = yWITH__BRA;
            } else if (nexttok == '{') {
                token = yWITH__CUR;
            } else {
                token = yWITH__ETC;
            }
        } else if (token == yaID__LEX) {
            token = tokenPipelineId(token);
        }
        // If add to above "else if", also add to "if (token" further above
    }
    yylval.token = token;
    // effectively returns yylval
}

bool V3ParseImp::isStrengthToken(int tok) {
    return tok == ygenSTRENGTH || tok == ySUPPLY0 || tok == ySUPPLY1 || tok == ySTRONG0
           || tok == ySTRONG1 || tok == yPULL0 || tok == yPULL1 || tok == yWEAK0 || tok == yWEAK1
           || tok == yHIGHZ0 || tok == yHIGHZ1;
}

void V3ParseImp::tokenPipelineSym() {
    // If an id, change the type based on symbol table
    // Note above sometimes converts yGLOBAL to a yaID__LEX
    tokenPipeline();  // sets yylval
    int token = yylval.token;
    if (token == yRANDOMIZE) importIfInStd(yylval.fl, "randomize", false);
    if (token == yaID__ETC || token == yaID__CC || token == yaID__LEX || token == yaID__aTYPE) {
        importIfInStd(yylval.fl, *(yylval.strp), true);
        if (token == yaID__LEX) {
            if (VString::startsWith(*(yylval.strp), "PATHPULSE__024")) {
                token = yaID__PATHPULSE;
            } else {
                token = yaID__ETC;
            }
        }
    }
    m_afterColonColon = token == yP_COLONCOLON;
    yylval.token = token;
    // effectively returns yylval
}

int V3ParseImp::tokenToBison() {
    // Called as global since bison doesn't have our pointer
    tokenPipelineSym();  // sets yylval
    m_bisonLastFileline = yylval.fl;
    if (m_tokenLastBison.token == '!'
        && (yylval.token == '&' || yylval.token == '|' || yylval.token == '^'
            || yylval.token == yP_NAND || yylval.token == yP_NOR || yylval.token == yP_XNOR)) {
        m_tokenLastBison.fl->v3warn(NOTREDOP,
                                    "Logical not directly before reduction operator is illegal\n"
                                        << m_tokenLastBison.fl->warnMore()
                                        << "... Suggest use parentheses, e.g. '!(|expr)'");
    }
    m_tokenLastBison = yylval;

    if (debug() >= 6 || debugFlex() >= 6
        || debugBison() >= 6) {  // --debugi-flex and --debugi-bison
        if (debug() >= 10) dumpTokensAhead(__LINE__);
        cout << "tokenToBison  " << yylval << endl;
    }
    return yylval.token;
}

//======================================================================
// V3ParseBisonYYSType functions

std::ostream& operator<<(std::ostream& os, const V3ParseBisonYYSType& rhs) {
    os << "TOKEN {";
    if (VL_UNCOVERABLE(!rhs.fl))
        os << "%E-null-fileline";
    else
        os << rhs.fl->filenameLetters() << rhs.fl->asciiLineCol();
    os << "}";
    os << "=" << rhs.token << " " << V3ParseImp::tokenName(rhs.token);
    if (rhs.token == yaID__ETC  //
        || rhs.token == yaID__CC  //
        || rhs.token == yaID__LEX  //
        || rhs.token == yaID__aINST  //
        || rhs.token == yaID__aTYPE) {
        os << " strp='" << *(rhs.strp) << "'";
    }
    return os;
}

//======================================================================
// V3Parse functions

V3Parse::V3Parse(AstNetlist* rootp, VInFilter* filterp) {
    m_impp = new V3ParseImp{rootp, filterp};
}
V3Parse::~V3Parse() {  //
    VL_DO_CLEAR(delete m_impp, m_impp = nullptr);
}
void V3Parse::parseFile(FileLine* fileline, const string& modname, bool inLibrary, bool inLibMap,
                        const string& libname, const string& errmsg,
                        const std::string& notFoundName) {
    m_impp->parseFile(fileline, modname, inLibrary, inLibMap, libname, errmsg, notFoundName);
}
void V3Parse::ppPushText(V3ParseImp* impp, const string& text) {
    if (text != "") impp->ppPushText(text);
}
void V3Parse::candidatePli(VSpellCheck* spellerp) { V3ParseImp::candidatePli(spellerp); }
