Port of schuchert.wikispaces.com


PowerShell5-Tokenize_Expression-First_Stab_At_Parentheses

PowerShell5-Tokenize_Expression-First_Stab_At_Parentheses

Up

First Stab at parenthesis

There are two ways in which our tokenizer might encounter parenthesis. The first is to group a lower-precedence operator, as in:

The next is in the use of function calls.:

For now, we want to pull out the () as tokens. We’ll allow something at a higher level to determine the context.

        @{expression = '(a)'; expected = @('(', 'a', ')')}
        @{expression = '(())'; expected = @('(', '(',')', ')')}
    [-] Should convert (()) to ( ( ) ) 102ms
      Expected string length 1 but was 4. Strings differ at index 1.
      Expected: {(}
      But was:  {(())}
      ------------^
      22:             $result[$i] | Should be $expected[$i]
      at Invoke-LegacyAssertion, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.8\Functions\Assertions\Should.ps1: line 190
      at <ScriptBlock>, C:\Users\Brett\src\shunting_yard_powershell_3\Tokenizer.Tests.ps1: line 22
            if (-not $this.recordIfMatches([ref]$expression, '^([()])', $result)) {
                    if (-not $this.recordIfMatches([ref]$expression, '^([\d\w]+)', $result)) {
                        $this.recordIfMatches([ref]$expression, '^([^\d\w\s]+)', $result)
                    }
                }
            }
    static [Array]$regex = @( '^([()])', '^([\d\w]+)', '^([^\d\w\s]+)' )
    [ArrayList]interpret([String]$expression) {
        $result = [ArrayList]::new()

        while ($expression.Length -ne 0) {
            $expression = $expression -replace ('^\s+', '')
            foreach ($r in [Tokenizer]::regex) {
                if ($this.recordIfMatches([ref]$expression, $r, $result)) {
                    break
                }
            }
        }

        return $result
    }

Up


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.