C# の .CS ファイルのステップ数を求める(超簡易版)

C# の .CS ファイルからステップ数を求める。
# $html … .html でソースを出力する
# $spcut …
# 空行、コメントを除いたソースを出力する
# コメントは // のみを除く対象とする
# $strFile … .CS ファイルのパス
# $html より $spcut を優先する
Param( [Switch] $html, [Switch] $spcut, [string] $strFile )
trap
{
break
}
Function OutputLine( [String] $s )
{
if( $html )
{
"<pre>" + $s + "</pre>"
}
else
{
" `t" + $s
}
}
Function SpaceLine( [String] $s, [String] $c )
{
if( $html )
{
"<pre style=`"padding-top:1em`"></pre>"
}
else
{
" `t"
}
}
Function ThroughtLine( [String] $s, [String] $c )
{
if( $html )
{
"<pre class=`"cut`">" + $s + "</pre>"
}
else
{
if( $c.Length -eq 0 )
{
"{1}`t{0}" -f $s, "<<"
}
else
{
"{1}`t{0}" -f $s, $c
}
}
}
Function CutProcess()
{
Process
{
if( $_ -match "^[\t ]*$" )
{
return
}
if( $_ -match "^[\t ]*//" )
{
return
}
$_
}
}
Function CountProcess
{
Begin
{
$lineComment = 0
$lineInstrument = 0
# メソッド
$strMethod = ""
$strMethod += "^[\t ]*"
$strMethod += "(?:(?:private|protected|public)[\t ]+)?"
$strMethod += "(?:static[\t ]+)?"
$strMethod += "(?:(?:void|bool|byte|short|int|long|string)[\t ]+)"
$strMethod += "(?:[_A-Za-z][0-9_A-Za-z]*[\t ]*\()"
# 変数
$strVariable = ""
$strVariable += "^[\t ]*"
$strVariable += "(?:(?:private|protected|public)[\t ]+)?"
$strVariable += "(?:(?:bool|byte|short|int|long|string))"
$strVariable += "(?:(?:[\t ]+)|(?:[\t ]*\[[\t ]*\][\t ]*))"
$strVariable += "(?:[_A-Za-z][0-9_A-Za-z]*[\t ]*);"
# 定数
# ; … 代入式なし
# = … 代入式あり
$strConstX = ""
$strConstX += "^[\t ]*"
$strConstX += "(?:(?:private|protected|public)[\t ]+)?"
$strConstX += "const[\t ]+"
$strConstX += "(?:(?:bool|byte|short|int|long|string)[\t ]+)"
$strConstX += "(?:[_A-Za-z][0-9_A-Za-z]*[\t ]*([;=]))"
if( $html )
{
"<DOCTYPE html>"
"<html lang=`"ja-JP`">"
"<head>"
"<meta charset=`"UTF-8`"/>"
"<style>"
"* { font-family: monospace; }"
"pre { padding:0em ; margin:0em }"
".cut { color: #c0c0c0; }"
"table"
"{"
" border-collapse:collapse;"
"}"
"table > tbody > tr > td"
"{"
" border: 1px solid gray;"
" padding: 0.5em;"
"}"
"</style>"
"</head>"
"<body>"
}
}
End
{
if( $html )
{
"<table>"
"<tbody>"
"<tr>"
"<td>{0}</td>" -f "コメント数"
"<td>{0}</td>" -f "ステップ数"
"<td>{0}</td>" -f "ファイル名"
"</tr>"
"<tr>"
"<td>{0}</td>" -f $lineComment
"<td>{0}</td>" -f $lineInstrument
"<td>{0}</td>" -f $strFile
"</tr>"
"</tbody>"
"</table>"
"</body>"
"</html>"
}
else
{
"{0,5}`t{1,5}`t{2}" -f $lineComment, $lineInstrument, $strFile
}
}
Process
{
# スペース
if( $_ -match "^[\t ]*$" )
{
SpaceLine $_
return
}
# コメント
if( $_ -match "^[\t ]*//" )
{
$lineComment ++
ThroughtLine $_
return
}
# using System
if( $_ -match "^[\t ]*using[\t ]+System" )
{
ThroughtLine $_
return
}
# namespace
if( $_ -match "^[\t ]*namespace" )
{
ThroughtLine $_
return
}
# class
if( $_ -match "^[\t ]*(?:(?:private|protected|public)[\t ]+)?(?:(?:partial|static)[\t ]+)?class[\t ]" )
{
ThroughtLine $_
return
}
# "{" or "}"
if( $_ -match "^[\t ]*[{}][\t ]*" )
{
ThroughtLine $_
return
}
# const 定数
if( $_ -match $strConstX )
{
$m = $Matches
if( $m[1] -eq ";" )
{
# 代入式なし
ThroughtLine $_
}
else
{
# 代入式あり
OutputLine $_
}
return
}
# method
if( $_ -match $strMethod )
{
ThroughtLine $_
return
}
# variable
if( $_ -match $strVariable )
{
ThroughtLine $_
return
}
# geter/seter
if( $_ -match "^[\t ]*[gs]et[\t ]*$" )
{
ThroughtLine $_
return
}
$lineInstrument ++
OutputLine $_
}
}
if( $spcut )
{
Get-Content $strFile | CutProcess
}
else
{
Get-Content $strFile | CountProcess
}