Repository navigation
fix(syntax): keep chunked parses intact and nested paren probes fast - #894
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes from the second review of
shell/syntax.1. Unclosed-heredoc nil delimiter.
doHeredocsreadr.HdocDelim.Valuewithout checking for nil. At 19ff56a I could not trigger a nil delimiter with the reported input (a missing delimiter word always setsp.errfirst), so this adds the guard as a defensive fix. A regression test parses the reported inputs in POSIX and Bash mode.2. Chunked reader changed the parse (regression from #820). There were two root causes:
detachSourceReplay, it dropped the enclosing probe's source view. After that,sourceBuffer()fell back top.bs, which with a chunked reader holds only the last chunk. So the outer arithmetic salvage lost its text.(( $((1)) ((echo a); echo b); ))parsed as(($((1))))with no error. Detaching now restores the enclosing view.loadReplay(backquote recovery) appended intop.bs[:0]. Inside a probe,p.bsaliases the shared replay buffer, so this overwrote input that later probes read. For example,(( `"` ))became(( ))` )). This only showed with a string reader.loadReplaynow copies.New
reader_differential_test.goparses fixed and generated inputs with both a string reader andiotest.OneByteReaderand requires identical errors and printed ASTs. Over 60k generated Bash inputs, what still differs is exactly the set that already differed at ddcd090 (before #820). All of it is backquote recovery outside probes, whererecoverableBackquoteSourceonly sees the current chunk. That is pre-existing and out of scope here, so the generated alphabet leaves backquotes out.3. Superlinear parse of nested
$( ((/$((. Each start offset was probed only once, so a per-offset memo of the probe decision would never hit. The cost came from three things:arithmSuffixWordbuilt a fresh sub-parser for the same salvaged tails about n² times.ArithmCmd.Source/ArithmExp.Source, which is about n³ bytes.Fixes:
probeMemo(newprobe_memo.go) memoizes nested no-probe$((/((parses across probe clones, keyed by offset and lexer state. On a hit it replays the nesting counters and moves the lexer past the construct.Sourcestrings, which no parser decision reads.go test ./shell/syntaxdropped from 27 s to 9 s locally.TestProbeMemoMatchesFreshParserequires identical results with the memo on and off; I also ran it locally on 200k generated inputs.TestParseNestedParenAmbiguityIsFastrequires depth 3200 to parse within a deadline. A residual non-linear term remains: copying the source text that each nested node keeps, which is inherent to the AST.4. Duplicate pattern package. Not changed.
shell/internal/pattern/*.goandinternal/shellpattern/*.goare byte-identical, but another session is changing pattern logic in both right now, so deduplicating here would conflict. Pattern fixes must go into both copies until one becomes a thin wrapper over the other.No SPEC changes: there are no boundary, policy or API changes.
Checks:
go test ./shell/...andmake lintare clean.FuzzParsePrintandFuzzParseAdversarialeach ran 60 s with no failures and left no new testdata.