From d3ca783562b6ef5d662006e83829dfdc1805fae2 Mon Sep 17 00:00:00 2001 From: Danny McClanahan <1305167+cosmicexplorer@users.noreply.github.com> Date: Sat, 16 Nov 2024 00:00:38 -0500 Subject: [PATCH] annotate delimiter pairing logic --- lib/coffeescript/lexer.js | 11 ++++++++--- src/lexer.coffee | 12 ++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/coffeescript/lexer.js b/lib/coffeescript/lexer.js index b00e651c40..c067570dea 100644 --- a/lib/coffeescript/lexer.js +++ b/lib/coffeescript/lexer.js @@ -1103,7 +1103,7 @@ // here. `;` and newlines are both treated as a `TERMINATOR`, we distinguish // parentheses that indicate a method call from regular parentheses, and so on. literalToken() { - var match, message, origin, prev, ref, ref1, ref2, ref3, ref4, ref5, skipToken, tag, token, value; + var match, message, origin, prev, ref, ref1, ref2, ref3, ref4, ref5, skipToken, tag, value; if (match = OPERATOR.exec(this.chunk)) { [value] = match; if (CODE.test(value)) { @@ -1187,14 +1187,19 @@ } } } - token = this.makeToken(tag, value); + // Match up paired delimiters. switch (value) { + // Upon opening a pair, provide the requisite close token, and record the "origin" as + // a separate token. case '(': case '{': case '[': + // TODO: this concept of "origin" is somewhat overloaded and makes it difficult to introspect + // a token stream. Is it the source of a generated token, or the "parent" node for + // a context-sensitive match like paired delimiters? this.ends.push({ tag: INVERSES[value], - origin: token + origin: this.makeToken(tag, value) }); break; case ')': diff --git a/src/lexer.coffee b/src/lexer.coffee index 80135b9db5..d2a3cd35da 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -815,10 +815,18 @@ exports.Lexer = class Lexer tag = 'INDEX_START' switch prev[0] when '?' then prev[0] = 'INDEX_SOAK' - token = @makeToken tag, value + + # Match up paired delimiters. switch value - when '(', '{', '[' then @ends.push {tag: INVERSES[value], origin: token} + # Upon opening a pair, provide the requisite close token, and record the "origin" as + # a separate token. + when '(', '{', '[' + # TODO: this concept of "origin" is somewhat overloaded and makes it difficult to introspect + # a token stream. Is it the source of a generated token, or the "parent" node for + # a context-sensitive match like paired delimiters? + @ends.push {tag: INVERSES[value], origin: @makeToken tag, value} when ')', '}', ']' then @pair value + @tokens.push @makeToken tag, value value.length