The JavaScript generation (make) fails because the parser encounters # tokens when parsing inline comments within code. The tokenizer treats # as TOKEN_SHARP when it should skip the rest of the line.
The issue is NOT in StringReader or Vim9 for loops (as initially suspected).
The real problem occurs when the tokenizer encounters a comment character # in the middle of parsing an expression or statement.
var lines: list<string> = lines->map((_, l) => substitute(l, '^[ \t]*$', '', ''))When parsing this line with the next line being a comment:
# Only peek within current line, don't cross line boundariesThe parser tries to continue parsing the expression but encounters the # character, which the tokenizer returns as TOKEN_SHARP.
ERROR: Syntax error at line 161, col 13: Unexpected token in expression: "#" (type 37)
The parser's ParsePrimary() method doesn't handle TOKEN_SHARP, causing an exception.
-
Filter comments in StringReader.new() - FAILED
- Vim9 for loops seemed to cause hangs with complex logic
- Actually worked fine; problem was elsewhere
-
Skip comments in Get() tokenizer method - FAILED
- Tried both recursive and loop-based approaches
- Infinite loops or timeouts occurred
-
Pre-filter in jscompiler.vim - FAILED
- Vim9 for/filter/lambda expressions appeared problematic
- Actually worked in isolation; problem was with vim9parser.vim itself
-
Pre-filter in jscompile.sh script - FAILED
- Script execution also timed out
The proper fix requires one of:
In ParsePrimary(), when encountering TOKEN_SHARP:
- Skip to end of current line
- Call Get() again to get the next real token
- Continue parsing
Challenge: Requires careful state management in the tokenizer
Modify Get() to loop internally until a non-comment token is found.
Challenge: Must avoid infinite loops and handle edge cases (EOF, EOL)
Process the entire input to extract comment positions before tokenizing.
Challenge: Requires coordination between tokenizer and parser
See test/ directory:
test_comment_handling.vim- Basic comment parsingtest_stringreader_filtering.vim- StringReader filteringtest_vim9_filtering_issue.vim- Vim9 script patternstest_comment_hang_detection.vim- Size-based hang detectiontest_full_file_hang.vim- Full file StringReader testtest_parse_full_file.vim- REPRODUCTION CASE
The Vim9 language constructs (for loops, filter(), map()) are NOT the problem. The issue is purely in how the tokenizer handles comment characters encountered during expression parsing.
Focus on Option A: Making TOKEN_SHARP handling in the parser robust by:
- Detecting TOKEN_SHARP in ParsePrimary()
- Safely skipping to next line and getting next token
- Testing with all comment patterns in the test suite