tsunami

log in
history

ANTLR: helper PS scripts

Luke Breuer
2008-12-22 23:24 UTC

init.ps1
function read($f) { [System.IO.File]::ReadAllText("$PWD$f").Trim() }
#function t($rule) { read test.syntax | .\test.exe $rule }
function t($s) 
{ 
    if ($s)
    {
        $s | .\test.exe
    }
    else
    {
        read test.syntax | .\test.exe
    }
}
function m { C:\Docs\c#\Time\antlr\m.cmd }
function s($t) 
{ 
    if ($t) { [System.IO.File]::WriteAllText("$PWD\test.syntax", $t) }
    else    { cat "$PWD\test.syntax" -A }
}
del alias:cat -EA SilentlyContinue

function Join($delim = "")
{
    $a = $input | % { $_ }
    
    if ($a)
    {
        [string]::Join($delim, $a)
    }
}
token.ps1
. .\init.ps1

function UnescapeAndConvert
{
    $input | % { [int][Regex]::Unescape($_)[0] }
}

function GetTokenCharacters($marker)
{
    $section = [Regex]::Match((read "tsunami.g"), "(?s)// $marker BEGIN.*?// $marker END")
    $tokens = [Regex]::Matches($section, "(?m)^(?<token>[A-Z]+)\s*:.*")
    $tokens | 
        % { @{ Token = $_.Groups["token"]; Matches = [Regex]::Matches($_.Value, "(?<=^([^']*|'[^']*')*')\?.") } } | 
        % { $t = $_.Token; $_.Matches | % { @{ Token = $t; Char = $_.Value } } } #| 
        #sort { $_.Token } -unique
}

function ToUniRegex($char)
{
    "\u" + $char.ToString("X").PadLeft(4, '0')
}

# OTHER
$chars = GetTokenCharacters("NOT_OTHER") | % { $_.Char }

$template = @"
// the following characters are excluded: [{0}]
OTHER   : ({{0}})+;
"@ -f (($chars | Join) -replace '-', '-')

$spaces = @(-1) + @($chars | UnescapeAndConvert | sort) + @(0x100)
$iter = $spaces.Length - 2

$OTHER = 0..$iter | 
    % { "'{0}'..'{1}'" -f (ToUniRegex($spaces[$_] + 1)), (ToUniRegex($spaces[$_ + 1] - 1)) } |
    % { $a = @() } { $a += $_ } { $template -f ($a | Join " |`n           ") }

# ESCAPED
$chars = GetTokenCharacters("ESCAPE_ABLE")

$ESCAPED = @"
// the following characters can be escaped: [{0}]
ESCAPED : {1};
"@ -f (($chars | % { $_.Char } | Join) -replace '-', '-'), ($chars | % { "BACKSLASH {0,-9}" -f $_.Token } | Join " |`n          ")

$OTHER
""
$ESCAPED
upload-debug.ps1
. .\init.ps1

del t.err -EA SilentlyContinue
t 2>t.err
$errors = [Regex]::Matches((read("t.err")), '(?im)(?<=^Test.exe : ).*') | 
    % { $_.Value } | 
    Join "`r`n" | 
    % { $_.Trim() }

if (!$errors) { $errors = "(no errors)" }

$markup = @"
==grammar{{
$(read tsunami.g)
}}
==test input{{
$(read test.syntax)
}}
==testing grammar against input{{
$errors
}}"@


$markup = Convert-Tabs-to-Spaces $markup.Replace("'", "''") 8;
$markup = $markup -replace "(?=``|`"|\)", "\"

sqlcmd -S asp3 -E -d time_database -Q "update Item set data = '$markup' where item_pk = 529"
upload-ps1.ps1
. .\init.ps1

$markup = dir [itu]*.ps1 | 
    % { "==$($_.Name)`n{{{{`n{0}`n}}}}`n" -f ((read $_.Name) -replace "(?=}}(}})*[^}]|==|``|`")", "\") } | 
    join

$cn = New-Object System.Data.SqlClient.SqlConnection "Server=ASP3;Database=time_database;Integrated Security=True"
$cn.Open()
$cmd = New-Object System.Data.SqlClient.SqlCommand 
$cmd.CommandText = "update Item set data = @data where item_pk = 530"
$cmd.Connection = $cn
$param = New-Object System.Data.SqlClient.SqlParameter "data", $markup
[void]$cmd.Parameters.Add($param)
[void]$cmd.ExecuteNonQuery()
$cn.Dispose()