-
Notifications
You must be signed in to change notification settings - Fork 49
Break out of quantification loop if there is no forward progress #560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
8584cf4
799ee98
df8f2a6
84e30b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,8 @@ extension Processor { | |
var ints: [Int] | ||
|
||
var values: [Any] | ||
|
||
var positions: [Input.Index] | ||
} | ||
} | ||
|
||
|
@@ -66,6 +68,12 @@ extension Processor.Registers { | |
values[i.rawValue] = newValue | ||
} | ||
} | ||
subscript(_ i: PositionRegister) -> Input.Index { | ||
get { positions[i.rawValue] } | ||
set { | ||
positions[i.rawValue] = newValue | ||
} | ||
} | ||
subscript(_ i: ElementRegister) -> Input.Element { | ||
elements[i.rawValue] | ||
} | ||
|
@@ -89,6 +97,8 @@ extension Processor.Registers { | |
} | ||
|
||
extension Processor.Registers { | ||
static let sentinelIndex = "".startIndex | ||
|
||
init( | ||
_ program: MEProgram, | ||
_ sentinel: String.Index | ||
|
@@ -120,11 +130,15 @@ extension Processor.Registers { | |
|
||
self.values = Array( | ||
repeating: SentinelValue(), count: info.values) | ||
self.positions = Array( | ||
repeating: Processor.Registers.sentinelIndex, | ||
count: info.positions) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Future: Why the start index instead of the provided sentinel? Should or could we construct an invalid index and at least assert that we're never loading that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
mutating func reset(sentinel: Input.Index) { | ||
self.ints._setAll(to: 0) | ||
self.values._setAll(to: SentinelValue()) | ||
self.positions._setAll(to: Processor.Registers.sentinelIndex) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,4 +208,40 @@ extension RegexTests { | |
expectProgram(for: "[abc]", semanticLevel: .unicodeScalar, doesNotContain: [.matchBitset]) | ||
expectProgram(for: "[abc]", semanticLevel: .unicodeScalar, contains: [.consumeBy]) | ||
} | ||
|
||
func testQuantificationForwardProgressCompile() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm ok with these for now, but do note that the kinds of tests can become extremely fragile and are likely to get dropped if control flow is ever overhauled. Make sure anything important is represented by both functional tests and more precise unit testing. E.g. you can test the |
||
// Unbounded quantification + non forward progressing inner nodes | ||
// Expect to emit the position checking instructions | ||
expectProgram(for: #"(?:(?=a)){1,}"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\b)*"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:(?#comment))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:|)+"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|)+"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|(?i-i:))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|(?#comment))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|(?#comment)(?i-i:))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|(?i))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
|
||
// Bounded quantification, don't emit position checking | ||
expectProgram(for: #"(?:(?=a)){1,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\b)?"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:(?#comment)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:|){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|(?i-i:)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|(?#comment)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|(?#comment)(?i-i:)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(?:\w|(?i)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
|
||
// Inner node is a quantification that does not guarantee forward progress | ||
expectProgram(for: #"(a*)*"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(a?)*"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(a{,5})*"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"((\b){,4})*"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"((\b){1,4})*"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"((|){1,4})*"#, contains: [.moveCurrentPosition, .condBranchSamePosition]) | ||
// Inner node is a quantification that guarantees forward progress | ||
expectProgram(for: #"(a+)*"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
expectProgram(for: #"(a{1,})*"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition]) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.