diff options
Diffstat (limited to 'js/scripting-lang/parser_analysis.md')
-rw-r--r-- | js/scripting-lang/parser_analysis.md | 143 |
1 files changed, 0 insertions, 143 deletions
diff --git a/js/scripting-lang/parser_analysis.md b/js/scripting-lang/parser_analysis.md deleted file mode 100644 index 8a25df1..0000000 --- a/js/scripting-lang/parser_analysis.md +++ /dev/null @@ -1,143 +0,0 @@ -# Parser Implementation Analysis - -## Grammar vs Implementation Comparison - -### 1. Assignment Parsing Issue - -**Grammar Rule:** -``` -assignment = identifier ":" assignment_value -assignment_value = function_call | simple_expression -``` - -**Current Implementation Problem:** -- The parser calls `walk()` to get the value in assignments -- `walk()` consumes the function name before assignment parsing can detect it as a function call -- This causes `result : add 3 4;` to be parsed incorrectly - -**Expected Parse Tree:** -``` -assignment( - identifier("result"), - function_call("add", [number(3), number(4)]) -) -``` - -**Actual Parse Tree (from debug output):** -``` -assignment( - identifier("result"), - number(3) -) -// Plus separate nodes: number(4) -``` - -### 2. Function Call Detection - -**Grammar Rule:** -``` -function_call = identifier argument_list -argument_list = expression { expression } -``` - -**Current Implementation Issues:** -- Function call detection is in the wrong place in the parser -- Assignment parsing calls `walk()` which consumes tokens before function call detection -- The condition `tokens[current + 1].type !== TokenType.ASSIGNMENT` prevents function calls in assignments - -**Debug Evidence:** -``` -[DEBUG] Assignment parsing: { - "current": { "type": "NUMBER", "value": "3" }, - "next": { "type": "NUMBER", "value": "4" } -} -``` -The parser is already at the `3` token, meaning `add` has been consumed. - -### 3. Case Expression Parsing - -**Grammar Rule:** -``` -case_expression = "case" value_list "of" case_list -case_branch = pattern ":" result -``` - -**Current Implementation Status:** -✅ **Working correctly** - Case expressions parse and structure correctly -- Patterns are parsed correctly: `0 0`, `0 _`, `_ 0`, `_ _` -- Results are parsed correctly: string literals, numbers, identifiers -- The AST structure matches the grammar - -### 4. Arithmetic Expression Parsing - -**Grammar Rule:** -``` -arithmetic_expression = arithmetic_operator expression expression -``` - -**Current Implementation Status:** -✅ **Working correctly** - Prefix notation is handled correctly -- `+ x y` parses as `PlusExpression(left: x, right: y)` -- Operator precedence is handled correctly - -### 5. IO Statement Parsing - -**Grammar Rule:** -``` -io_statement = io_operator [ expression ] -io_operator = "..in" | "..out" -``` - -**Current Implementation Status:** -✅ **Working correctly** - IO statements parse correctly -- `..in` and `..out` are recognized as special tokens -- Expressions after `..out` are parsed correctly - -## Root Cause Analysis - -The fundamental issue is in the parser architecture: - -1. **Assignment parsing calls `walk()`** to get the value -2. **`walk()` consumes tokens** before assignment parsing can analyze them -3. **Function call detection happens too late** in the parsing process - -## Proposed Solutions - -### Solution 1: Restructure Assignment Parsing -Don't call `walk()` for assignment values. Instead, directly parse the value: - -```javascript -// Instead of: const value = walk(); -// Do: Parse value directly based on next tokens -``` - -### Solution 2: Add Function Call Detection to walk() -Make `walk()` detect function calls when it encounters an identifier followed by arguments: - -```javascript -if (token.type === TokenType.IDENTIFIER) { - // Check for function call first - if (isFunctionCall()) { - return parseFunctionCall(); - } - // Then check for assignment - if (isAssignment()) { - return parseAssignment(); - } -} -``` - -### Solution 3: Use Lookahead -Implement a lookahead mechanism to peek at tokens without consuming them: - -```javascript -function peek(n = 1) { - return tokens[current + n]; -} -``` - -## Recommended Approach - -**Solution 1** is the most straightforward. The assignment parsing should directly parse the value without calling `walk()`, allowing it to distinguish between function calls and simple expressions. - -This would fix the core issue and make the parser match the grammar specification. \ No newline at end of file |