Tcl SpiceGenTcl package (v0.65)

::SpiceGenTcl::NgspiceTop, Main, Index

ClassesTop, Main, Index

Parser [::SpiceGenTcl::Ngspice]Top, Main, Index

Method summary
constructorConstructor for the class.
buildTopNetlistBuilds top netlist corresponding to parsed netlist file.
configureConfigure properties.
readAndParseCalls methods readFile and buildTopNetlist in a sequence.
readFileReads netlist file and prepare for parsing: remove redundant white space characters, collapse continuation lines and remove comments lines.
Properties

Readable: -filepath, -topnetlist

Writable: -filepath, -topnetlist

constructor [::SpiceGenTcl::Ngspice::Parser]Parser, Top, Main, Index

Creates object of class Parser that do parsing of valid Ngspice netlist.

Parser create OBJNAME name filepath
Parser new name filepath
Details
Parameters
nameName of the object.
filepathPath to file that should be parsed.
method constructor {name filepath} {

    # Creates object of class `Parser` that do parsing of valid Ngspice netlist.
    #   name - name of the object
    #   filepath - path to file that should be parsed
    set Name $name
    my configure -filepath $filepath
    set ElemsMethods [dcreate b CreateBehSource c CreateCap d CreateDio e CreateVCVS f CreateCCCS g CreateVCCS h CreateCCVS i CreateVIsource j CreateJFET k CreateCoupling l CreateInd m CreateMOSFET n CreateVeriloga q CreateBJT r CreateRes s CreateVSwitch v CreateVIsource w CreateCSwitch x CreateSubcktInst z CreateMESFET]
    set DotsMethods [dcreate ac CreateAc dc CreateDc func CreateFunc global CreateGlobal ic CreateIc include CreateInclude model CreateModel nodeset CreateNodeset op CreateOp options CreateOptions param CreateParam sens CreateSens sp CreateSp temp CreateTemp tran CreateTran params CreateParam lib CreateLib option CreateOptions]
    set SupModelsTypes {r c l sw csw d npn pnp njf pjf nmos pmos nmf pmf}
    my configure -topnetlist [::SpiceGenTcl::Netlist new [file tail $filepath]]
    set ModelTemplate {oo::class create @type@ {
        superclass ::SpiceGenTcl::Model
        constructor {name args} {
            set paramsNames [list @paramsList@]
            next $name @type@ [my argsPreprocess $paramsNames {*}$args]
        }
    }}
    set SubcircuitTemplate {oo::class create @classname@ {
        superclass ::SpiceGenTcl::Subcircuit
        constructor {} {
            set pins @pins@
            set params @params@
            next @subname@ $pins $params
        }
    }}
    set NamespacePath ::SpiceGenTcl::Ngspice
}

buildTopNetlist [::SpiceGenTcl::Ngspice::Parser]Parser, Top, Main, Index

Builds top netlist corresponding to parsed netlist file

OBJECT buildTopNetlist
Details
method buildTopNetlist {} {

    # Builds top netlist corresponding to parsed netlist file
    if {![info exists FileData]} {
        error "Parser object '[my configure -name]' doesn't have prepared data"
    }
    set allLines $FileData
    set topNetlist [my configure -topnetlist]
    my GetSubcircuitLines
    # parse found subcircuits definitions first
    if {[info exists SubcktsBoundaries]} {
        set subcktsBoundaries $SubcktsBoundaries
        dict for {subcktName subcktBounds} $subcktsBoundaries {
            my BuildSubcktFromDef $subcktName $subcktBounds
            lappend lines2remove {*}[lseq [@ $subcktBounds 0] [@ $subcktBounds 1]]
        }
        set allLines [lremove $allLines {*}$lines2remove]
    }
    my BuildNetlist $allLines $topNetlist
    return
}

readAndParse [::SpiceGenTcl::Ngspice::Parser]Parser, Top, Main, Index

Calls methods readFile and buildTopNetlist in a sequence

OBJECT readAndParse
Details
method readAndParse {} {

    # Calls methods `readFile` and `buildTopNetlist` in a sequence
    my readFile
    my buildTopNetlist
    return
}

readFile [::SpiceGenTcl::Ngspice::Parser]Parser, Top, Main, Index

Reads netlist file and prepare for parsing: remove redundant white space characters, collapse continuation lines and remove comments lines

OBJECT readFile
Details
method readFile {} {

    # Reads netlist file and prepare for parsing: remove redundant white space characters, collapse continuation
    # lines and remove comments lines
    set file [open [my configure -filepath] r]
    set fileData [split [read $file] "\n"]
    close $file
    set fileData [lrange $fileData 1 end]
    # replace all sequences of white space characters with single space character
    foreach line $fileData {
        set processedLine [regsub -all {[[:space:]]+} [string trim $line] { }]
        if {$processedLine ne {}} {
            lappend lines [string tolower $processedLine]
        }
    }
    # find continuations and move them to line that is the start of continuation
    set fileData $lines
    set contFlag false
    # create a dictionary like `2 {3 4 5} 6 {7} 8 {} 9 {} 10 {} {11 12} 13 {}...` where keys are start lines,
    # indexes and values are the lists of lines indexes that are the continuation of the line with key index
    # also, skip the first line of the netlist
    for {set i 0} {$i<[llength $fileData]} {incr i} {
        set line [@ $fileData $i]
        if {[string index $line 0] eq "+"} {
            if {$contFlag} {
                dict lappend continLinesIndex $startIndex $i
                continue
            }
            dict append continLinesIndex [= {$i-1}]
            set startIndex [= {$i-1}]
            dict lappend continLinesIndex $startIndex $i
            set contFlag true
        } else {
            set contFlag false
            dict append continLinesIndex $i
        }
    }
    # append continuation lines to start line of each continuation list with first symbol removal (it is assumed
    # to be `+` symbol)
    if {[info exists continLinesIndex]} {
        dict map {lineIndex contLinesIndexes} $continLinesIndex {
            if {$contLinesIndexes eq {}} {
                lappend finalList [@ $fileData $lineIndex]
            } else {
                set locList [@ $fileData $lineIndex]
                foreach contLineIndex $contLinesIndexes {
                    lappend locList [string trim [string range [@ $fileData $contLineIndex] 1 end]]
                }
                lappend finalList [join $locList]
            }
        }
    } else {
        set finalList $fileData
    }
    # remove all comments that start with `*` symbol
    set finalList [lsearch -all -inline -not -glob $finalList {\**}]
    # remove all end of line comments that starts with symbols \;, \$ and //
    foreach line $finalList {
        if {[set index [string first {;} $line]]!=-1} {
            set line [string trim [string range $line 0 [= {$index-1}]]]
        } elseif {[set index [string first {$} $line]]!=-1} {
            set line [string trim [string range $line 0 [= {$index-1}]]]
        } elseif {[set index [string first {//} $line]]!=-1} {
            set line [string trim [string range $line 0 [= {$index-1}]]]
        }
        lappend tempList $line
    }
    set finalList $tempList
    # remove control section that starts with .control and ends with .endc
    for {set i 0} {$i<[llength $finalList]} {incr i} {
        set line [@ $finalList $i]
        if {[regexp {^\.control\s?} $line]} {
            set controlStart $i
        } elseif {[regexp {^\.endc\s?} $line]} {
            set controlEnd $i
        }
        if {[info exists controlStart] && [info exists controlEnd]} {
            break
        }
    }
    if {[info exists controlStart] && [info exists controlEnd]} {
        set finalList [lreplace $finalList $controlStart $controlEnd]
    }
    # remove spaces after `{` and before `}`, replace `= `, ` = ` and ` =` with single `=`
    foreach line $finalList {
        lappend finalList1 [string map [list "\{ " "\{" " \}" "\}" " = " "=" " =" "=" "= " "="] $line]
    }
    set FileData $finalList1
    return
}