Skip to content

Commit 800e0ed

Browse files
Consume BOMs in parser document state
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent a24c58a commit 800e0ed

13 files changed

Lines changed: 107 additions & 54 deletions

src/functions/private/ConvertFrom-YamlByteOrderMark.ps1

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/functions/private/New-YamlReaderContext.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function New-YamlReaderContext {
3737
)
3838

3939
$text = $Yaml.Replace("`r`n", "`n").Replace("`r", "`n")
40-
$text = ConvertFrom-YamlByteOrderMark -Text $text
4140
Assert-YamlText -Yaml $text
4241
$lines = [System.Text.RegularExpressions.Regex]::Split($text, "`n")
4342
$lineStarts = [int[]]::new($lines.Count)

src/functions/private/Read-YamlBlockMapping.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ function Read-YamlBlockMapping {
4949
$lineNumber = $Context.LineIndex
5050
$line = $Context.Lines[$lineNumber]
5151
$trimmed = $line.TrimStart(' ', "`t")
52-
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
52+
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)' -or
53+
(Test-YamlDocumentByteOrderMark -Context $Context -RequireDocumentStart)) {
5354
break
5455
}
5556
if ($trimmed.Length -eq 0 -or $trimmed.StartsWith('#', [System.StringComparison]::Ordinal)) {

src/functions/private/Read-YamlBlockNode.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function Read-YamlBlockNode {
4444
-HasUnknownTag $PendingUnknownTag -Anchor $PendingAnchor
4545
}
4646
$line = $Context.Lines[$Context.LineIndex]
47-
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
47+
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)' -or
48+
(Test-YamlDocumentByteOrderMark -Context $Context -RequireDocumentStart)) {
4849
$mark = New-YamlMark -Index $Context.LineStarts[$Context.LineIndex] -Line $Context.LineIndex -Column 0
4950
return New-YamlEmptyScalar -Context $Context -Depth $Depth -Mark $mark -Tag $PendingTag `
5051
-HasUnknownTag $PendingUnknownTag -Anchor $PendingAnchor
@@ -119,7 +120,8 @@ function Read-YamlBlockNode {
119120
$Context.LineIndex++
120121
Skip-YamlBlockTrivia -Context $Context
121122
if ($Context.LineIndex -ge $Context.Lines.Count -or
122-
$Context.Lines[$Context.LineIndex] -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
123+
$Context.Lines[$Context.LineIndex] -match '^(?:---|\.\.\.)(?:[ \t]|$)' -or
124+
(Test-YamlDocumentByteOrderMark -Context $Context -RequireDocumentStart)) {
123125
$mark = New-YamlMark -Index ($Context.LineStarts[$lineNumber] + $contentColumn) `
124126
-Line $lineNumber -Column $contentColumn
125127
return New-YamlEmptyScalar -Context $Context -Depth $Depth -Mark $mark -Tag $tag `

src/functions/private/Read-YamlBlockScalar.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ function Read-YamlBlockScalar {
9797
$decodedLength = 0
9898
while ($Context.LineIndex -lt $Context.Lines.Count) {
9999
$line = $Context.Lines[$Context.LineIndex]
100-
Assert-YamlNoByteOrderMark -Text $line -Mark $start
101-
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
100+
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)' -or
101+
(Test-YamlDocumentByteOrderMark -Context $Context -RequireDocumentStart)) {
102102
break
103103
}
104+
Assert-YamlNoByteOrderMark -Text $line -Mark $start
104105
if ($Context.LineIndex -eq $Context.Lines.Count - 1 -and
105106
$line.Length -eq 0 -and
106107
$Context.Text.EndsWith("`n", [System.StringComparison]::Ordinal)) {

src/functions/private/Read-YamlBlockSequence.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ function Read-YamlBlockSequence {
4747
} else {
4848
$line = $Context.Lines[$Context.LineIndex]
4949
$trimmed = $line.TrimStart(' ', "`t")
50-
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
50+
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)' -or
51+
(Test-YamlDocumentByteOrderMark -Context $Context -RequireDocumentStart)) {
5152
break
5253
}
5354
if ($trimmed.Length -eq 0 -or $trimmed.StartsWith('#', [System.StringComparison]::Ordinal)) {
@@ -80,7 +81,9 @@ function Read-YamlBlockSequence {
8081
} else {
8182
$nextLine = $Context.Lines[$Context.LineIndex]
8283
$nextIndent = Get-YamlIndent -Line $nextLine -LineNumber $Context.LineIndex -Context $Context
83-
if ($nextIndent -le $Indent -or $nextLine -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
84+
if ($nextIndent -le $Indent -or
85+
$nextLine -match '^(?:---|\.\.\.)(?:[ \t]|$)' -or
86+
(Test-YamlDocumentByteOrderMark -Context $Context -RequireDocumentStart)) {
8487
$mark = New-YamlMark -Index ($Context.LineStarts[$line] + $itemColumn) -Line $line `
8588
-Column $itemColumn
8689
$item = New-YamlEmptyScalar -Context $Context -Depth ($Depth + 1) -Mark $mark

src/functions/private/Read-YamlDirectiveBlock.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function Read-YamlDirectiveBlock {
3030
$directive = $Context.Lines[$Context.LineIndex]
3131
$mark = New-YamlMark -Index $Context.LineStarts[$Context.LineIndex] `
3232
-Line $Context.LineIndex -Column 0
33+
Assert-YamlNoByteOrderMark -Text $directive -Mark $mark
3334
if ($directive -cmatch '^%YAML(?:[ \t]|$)') {
3435
if ($yamlDirectiveSeen -or $directive -cnotmatch (
3536
'^%YAML[ \t]+([0-9]+)\.([0-9]+)(?:[ \t]+#.*)?$'
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function Read-YamlDocumentByteOrderMark {
2+
<#
3+
.SYNOPSIS
4+
Consumes a byte order mark while scanning an actual document prefix.
5+
#>
6+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
7+
'PSUseShouldProcessForStateChangingFunctions', '',
8+
Justification = 'Advances an in-memory parser context.'
9+
)]
10+
[CmdletBinding()]
11+
param (
12+
[Parameter(Mandatory)]
13+
[pscustomobject] $Context,
14+
15+
[switch] $RequireDocumentStart
16+
)
17+
18+
if ($Context.LineIndex -ge $Context.Lines.Count) {
19+
return
20+
}
21+
$index = $Context.LineStarts[$Context.LineIndex]
22+
$atStreamStart = $index -eq 0
23+
if (-not $atStreamStart -and
24+
-not (Test-YamlDocumentByteOrderMark -Context $Context `
25+
-RequireDocumentStart:$RequireDocumentStart)) {
26+
return
27+
}
28+
if (-not $Context.Lines[$Context.LineIndex].StartsWith(
29+
[string] [char] 0xFEFF,
30+
[System.StringComparison]::Ordinal
31+
)) {
32+
return
33+
}
34+
35+
$Context.Text = $Context.Text.Remove($index, 1)
36+
$Context.Lines[$Context.LineIndex] = $Context.Lines[$Context.LineIndex].Substring(1)
37+
for ($line = $Context.LineIndex + 1; $line -lt $Context.LineStarts.Count; $line++) {
38+
$Context.LineStarts[$line]--
39+
}
40+
}

src/functions/private/Read-YamlPlainScalar.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ function Read-YamlPlainScalar {
7070
while ($firstComment -lt 0 -and $Context.LineIndex -lt $Context.Lines.Count) {
7171
$line = $Context.Lines[$Context.LineIndex]
7272
$trimmed = $line.TrimStart(' ', "`t")
73-
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
73+
if ($line -match '^(?:---|\.\.\.)(?:[ \t]|$)' -or
74+
(Test-YamlDocumentByteOrderMark -Context $Context -RequireDocumentStart)) {
7475
break
7576
}
7677
if ($trimmed.Length -eq 0) {

src/functions/private/Read-YamlStreamCore.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ function Read-YamlStreamCore {
4545
-MaxAliases $MaxAliases -MaxScalarLength $MaxScalarLength `
4646
-MaxTagLength $MaxTagLength -MaxTotalTagLength $MaxTotalTagLength `
4747
-MaxNumericLength $MaxNumericLength
48-
$text = $context.Text
4948
$lines = $context.Lines
5049
$lineStarts = $context.LineStarts
5150
$documents = [System.Collections.Generic.List[object]]::new()
5251
$implicitDocumentSeen = $false
5352

5453
while ($context.LineIndex -lt $lines.Count) {
54+
Read-YamlDocumentByteOrderMark -Context $context
5555
Skip-YamlBlockTrivia -Context $context
5656
if ($context.LineIndex -ge $lines.Count) {
5757
break
@@ -76,7 +76,8 @@ function Read-YamlStreamCore {
7676

7777
if ($context.LineIndex -ge $lines.Count) {
7878
if ($directiveSeen) {
79-
$mark = New-YamlMark -Index $text.Length -Line ([Math]::Max(0, $lines.Count - 1)) -Column 0
79+
$mark = New-YamlMark -Index $context.Text.Length `
80+
-Line ([Math]::Max(0, $lines.Count - 1)) -Column 0
8081
throw (New-YamlException -Start $mark -End $mark -ErrorId 'YamlDirectiveWithoutDocument' -Message (
8182
'YAML directives must be followed by an explicit document start marker.'
8283
))
@@ -113,7 +114,8 @@ function Read-YamlStreamCore {
113114
$context.LineIndex++
114115
Skip-YamlBlockTrivia -Context $context
115116
if ($context.LineIndex -ge $lines.Count -or
116-
$lines[$context.LineIndex] -match '^(?:---|\.\.\.)(?:[ \t]|$)') {
117+
$lines[$context.LineIndex] -match '^(?:---|\.\.\.)(?:[ \t]|$)' -or
118+
(Test-YamlDocumentByteOrderMark -Context $context -RequireDocumentStart)) {
117119
$mark = New-YamlMark -Index ($lineStarts[$markerLine] + 3) -Line $markerLine -Column 3
118120
$document = New-YamlEmptyScalar -Context $context -Depth 1 -Mark $mark
119121
} else {
@@ -162,6 +164,8 @@ function Read-YamlStreamCore {
162164
}
163165
$documents.Add($document)
164166

167+
Skip-YamlBlockTrivia -Context $context
168+
Read-YamlDocumentByteOrderMark -Context $context -RequireDocumentStart
165169
Skip-YamlBlockTrivia -Context $context
166170
$explicitEnd = $false
167171
if ($context.LineIndex -lt $lines.Count -and

0 commit comments

Comments
 (0)