From f154a05140a2eb540bc1e4c0ededbe72c4ba9ed4 Mon Sep 17 00:00:00 2001 From: Gunnlaugur Thor Briem Date: Thu, 3 Sep 2026 14:58:23 +0000 Subject: [PATCH 1/5] fix: a quoted scope in an operand position does not parse `'Alpha':[Book.xlsx]Gamma!A1` and `'Alpha:Beta':Gamma!A1` fail to parse: "Unexpected context_quote token". Their first name is a quoted scope with no `!` after it, so it is not a scope but an operand, and `parse` has no production for one there. The lexer types it as a scope on the chance that a second sheet name follows. Where nothing merges it, `mergeRefTokens` now corrects it to `ref_named`, which is what Excel reads there and what the unquoted `a` of `a:'b'!A1` already gets, so the two ways of writing the operation agree. A scope that does have its `!` keeps the scope type whether or not it merged, since a scope containing a `:` never merges with the name or table after it, as in `'Sheet1:Sheet2'!name`. Untouched: an unquoted scope in the same position, `1` in `1:'dec'!B11`, which the lexer leaves as `context` and which still does not parse. A number is not a name, so it wants a different answer. --- lib/mergeRefTokens.ts | 11 +++++++++++ lib/tokenize-3d.spec.ts | 10 ++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/mergeRefTokens.ts b/lib/mergeRefTokens.ts index bd211db..7d71ee8 100644 --- a/lib/mergeRefTokens.ts +++ b/lib/mergeRefTokens.ts @@ -193,6 +193,11 @@ const matcher = ( return best; }; +function isBangAfter (tokenlist: Token[], i: number): boolean { + const next = tokenlist[i + 1]; + return !!next && next.type === OPERATOR && next.value === '!'; +} + function commonMergeRefTokens (tokenlist: Token[], xlsx: boolean): Token[] { const finalTokens = []; // this seeks backwards because it's really the range part @@ -217,6 +222,12 @@ function commonMergeRefTokens (tokenlist: Token[], xlsx: boolean): Token[] { i -= valid - 1; } } + // A quoted scope with no `!` after it is a name, not a scope; the lexer types it as a scope + // on the chance that a second sheet name follows. One that does have its `!` keeps the scope + // type even unmerged, since a scope containing a `:` never merges with the name after it. + if (token.type === CONTEXT_QUOTE && !isBangAfter(tokenlist, i)) { + token = { ...token, type: REF_NAMED }; + } finalTokens[finalTokens.length] = token; } return finalTokens.reverse(); diff --git a/lib/tokenize-3d.spec.ts b/lib/tokenize-3d.spec.ts index 51bbdf6..8b03bd0 100644 --- a/lib/tokenize-3d.spec.ts +++ b/lib/tokenize-3d.spec.ts @@ -132,7 +132,7 @@ describe('lexer: 3d ranges', () => { { type: REF_RANGE, value: 'A1' } ]); expect(tokenize("'Alpha:Beta':Gamma!A1")).toEqual([ - { type: CONTEXT_QUOTE, value: "'Alpha:Beta'" }, + { type: REF_NAMED, value: "'Alpha:Beta'" }, { type: OPERATOR, value: ':' }, { type: REF_RANGE, value: 'Gamma!A1' } ]); @@ -200,7 +200,7 @@ describe('lexer: 3d ranges', () => { expect(tokenize("Alpha:'Beta:Gamma':Delta!A1")).toEqual([ { type: REF_NAMED, value: 'Alpha' }, { type: OPERATOR, value: ':' }, - { type: CONTEXT_QUOTE, value: "'Beta:Gamma'" }, + { type: REF_NAMED, value: "'Beta:Gamma'" }, { type: OPERATOR, value: ':' }, { type: REF_RANGE, value: 'Delta!A1' } ]); @@ -317,8 +317,10 @@ describe('lexer: 3d ranges', () => { { type: OPERATOR, value: '!' }, { type: REF_RANGE, value: 'A1' } ]); + // Nothing merges the first scope, because a workbook specifier is not allowed in the + // second. What is left has no `!` after it, so it is the operand Excel reads as a name. expect(tokenize("'Alpha':[Book.xlsx]Gamma!A1")).toEqual([ - { type: CONTEXT_QUOTE, value: "'Alpha'" }, + { type: REF_NAMED, value: "'Alpha'" }, { type: OPERATOR, value: ':' }, { type: REF_RANGE, value: '[Book.xlsx]Gamma!A1' } ]); @@ -380,7 +382,7 @@ describe('lexer: 3d ranges', () => { { type: REF_RANGE, value: 'A1' } ]); expect(tokenize("'Alpha':'[Book.xlsx]Gamma'!A1")).toEqual([ - { type: CONTEXT_QUOTE, value: "'Alpha'" }, + { type: REF_NAMED, value: "'Alpha'" }, { type: OPERATOR, value: ':' }, { type: REF_RANGE, value: "'[Book.xlsx]Gamma'!A1" } ]); From f022b88b3515bc1da20e1f9dd331fd6ac91131e6 Mon Sep 17 00:00:00 2001 From: Gunnlaugur Thor Briem Date: Thu, 3 Sep 2026 15:08:04 +0000 Subject: [PATCH 2/5] Pin that the corrected type parses The fix is a token type, and only the lexer specs covered it. These assert the outcome that motivated it: both formulas reach an AST, with the quoted scope as the name operand of the range operator. --- lib/parse.spec.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/parse.spec.ts b/lib/parse.spec.ts index d9deaa6..850d0eb 100644 --- a/lib/parse.spec.ts +++ b/lib/parse.spec.ts @@ -137,6 +137,25 @@ describe('parser', () => { isParsed("'A1:B2'!C3", { type: 'ReferenceIdentifier', value: "'A1:B2'!C3", kind: 'range' }); }); + test('a quoted scope with no bang after it is a name operand', () => { + isParsed("'Alpha':[Book.xlsx]Gamma!A1", { + type: 'BinaryExpression', + operator: ':', + arguments: [ + { type: 'ReferenceIdentifier', value: "'Alpha'", kind: 'name' }, + { type: 'ReferenceIdentifier', value: '[Book.xlsx]Gamma!A1', kind: 'range' } + ] + }); + isParsed("'Alpha:Beta':Gamma!A1", { + type: 'BinaryExpression', + operator: ':', + arguments: [ + { type: 'ReferenceIdentifier', value: "'Alpha:Beta'", kind: 'name' }, + { type: 'ReferenceIdentifier', value: 'Gamma!A1', kind: 'range' } + ] + }); + }); + test('"$" is not allowed on an unquoted sheet name', () => { isInvalidExpr('=SUM($Jan:$Mar!A1)'); isParsed("'$Jan:$Mar'!A1", { type: 'ReferenceIdentifier', value: "'$Jan:$Mar'!A1", kind: 'range' }); From a7e5d7f7308702a8ff391b6a9ae074b8f35fedcd Mon Sep 17 00:00:00 2001 From: Gunnlaugur Thor Briem Date: Thu, 3 Sep 2026 15:24:10 +0000 Subject: [PATCH 3/5] Say that the parse test pins fx's reading, not Excel's The test asserted `'Alpha:Beta':Gamma!A1` as the name `Alpha:Beta` joined to `Gamma!A1`, under a name that read as a claim about Excel. Excel's reading of that form is unmeasured, and for the nearest measured neighbour, a quoted first name followed by an unquoted second, Excel reads the sheet range instead. What the test is really for is that the formula reaches an AST at all, where the quoted first name used to stop the parser. Renamed to say it parses that way, with the scope of the claim in a comment. --- lib/parse.spec.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/parse.spec.ts b/lib/parse.spec.ts index 850d0eb..b07a900 100644 --- a/lib/parse.spec.ts +++ b/lib/parse.spec.ts @@ -137,7 +137,11 @@ describe('parser', () => { isParsed("'A1:B2'!C3", { type: 'ReferenceIdentifier', value: "'A1:B2'!C3", kind: 'range' }); }); - test('a quoted scope with no bang after it is a name operand', () => { + // These pin fx's reading, not Excel's, which is unmeasured for both forms. Excel reads a + // quoted first name followed by an *unquoted* second as the sheet range, so its answer here + // may well differ; what matters below is that the formula reaches an AST at all, since the + // quoted first name used to arrive as a scope and stop the parser. + test('a quoted scope with no bang after it parses as a name operand', () => { isParsed("'Alpha':[Book.xlsx]Gamma!A1", { type: 'BinaryExpression', operator: ':', From 92fc49d97e46152e79c5988172b8eb83e74f5beb Mon Sep 17 00:00:00 2001 From: Gunnlaugur Thor Briem Date: Thu, 3 Sep 2026 16:07:28 +0000 Subject: [PATCH 4/5] Excel reads these forms as the range operator as well The test name and its comment left the reading as fx's own. Excel takes both formulas the same way and stores them with the second name quoted, `'Alpha:Beta':'Gamma'!A1` and `Alpha:'[1]Gamma'!A1`. Neither resolves: a defined name cannot contain a colon, and `Alpha` is not a name in that workbook, so both are #NAME?. In the second form the workbook bracket is what rules out a sheet range; the quote on the first name is redundant and Excel drops it. --- lib/parse.spec.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/parse.spec.ts b/lib/parse.spec.ts index b07a900..d0b335c 100644 --- a/lib/parse.spec.ts +++ b/lib/parse.spec.ts @@ -137,11 +137,10 @@ describe('parser', () => { isParsed("'A1:B2'!C3", { type: 'ReferenceIdentifier', value: "'A1:B2'!C3", kind: 'range' }); }); - // These pin fx's reading, not Excel's, which is unmeasured for both forms. Excel reads a - // quoted first name followed by an *unquoted* second as the sheet range, so its answer here - // may well differ; what matters below is that the formula reaches an AST at all, since the - // quoted first name used to arrive as a scope and stop the parser. - test('a quoted scope with no bang after it parses as a name operand', () => { + // Excel reads both as the range operator over the quoted name, and stores them with the + // second name quoted: `'Alpha:Beta':'Gamma'!A1` and `Alpha:'[1]Gamma'!A1`. In the second it + // is the workbook bracket that rules out a sheet range, not the quote, which Excel drops. + test('a quoted scope with no bang after it is a name operand', () => { isParsed("'Alpha':[Book.xlsx]Gamma!A1", { type: 'BinaryExpression', operator: ':', From 265280e23d3e5ef30a4334765f2bf066c879b70f Mon Sep 17 00:00:00 2001 From: Gunnlaugur Thor Briem Date: Thu, 3 Sep 2026 16:25:48 +0000 Subject: [PATCH 5/5] Trim the retyping comment to the rule it implements Two clauses went: the lexer's speculative typing, which the description already gives as the cause, and a justification for the `!` exclusion that was wrong. It said a scope containing a `:` never merges with the name after it, which holds under `xlsx` only; in the default mode `'Sheet1:Sheet2'!name` merges into one reference. What is left is the rule the guard implements. --- lib/mergeRefTokens.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/mergeRefTokens.ts b/lib/mergeRefTokens.ts index 7d71ee8..49c4e25 100644 --- a/lib/mergeRefTokens.ts +++ b/lib/mergeRefTokens.ts @@ -222,9 +222,8 @@ function commonMergeRefTokens (tokenlist: Token[], xlsx: boolean): Token[] { i -= valid - 1; } } - // A quoted scope with no `!` after it is a name, not a scope; the lexer types it as a scope - // on the chance that a second sheet name follows. One that does have its `!` keeps the scope - // type even unmerged, since a scope containing a `:` never merges with the name after it. + // A quoted scope with no `!` after it is a name, not a scope. One that has its `!` stays a + // scope, merged or not. if (token.type === CONTEXT_QUOTE && !isBangAfter(tokenlist, i)) { token = { ...token, type: REF_NAMED }; }