Skip to content

Conversation

@davydog187
Copy link
Contributor

Summary

This PR enhances the Lua parser with three major improvements to support more idiomatic Lua 5.3 syntax patterns. All changes include comprehensive unit tests.

Changes

1. Function Call Syntactic Sugar

Implements Lua's syntactic sugar for function calls without parentheses:

  • func "string"func("string")
  • func{table}func({table})
  • Works for both regular function calls and method calls

Examples:

require "debug"           -- instead of require("debug")
print{1, 2, 3}           -- instead of print({1, 2, 3})
obj:method "str"         -- instead of obj:method("str")
config{debug = true}     -- instead of config({debug = true})

2. Empty Statement Support

The parser now correctly handles semicolons as empty statements:

  • Multiple consecutive semicolons: ;;; print "test" ;;
  • Semicolons after comments
  • Semicolons at start of code, between statements, and in blocks
  • Empty programs consisting only of semicolons

3. Comments in Expressions

The parser now skips comments that appear within expressions:

return x ~= -- force SETLINE before nil
nil

This enables more idiomatic Lua code with inline documentation.

Implementation Details

Parser modifications:

  • Modified parse_block_acc to skip semicolons at the block level
  • Removed recursive semicolon handling from parse_stmt_inner to avoid infinite recursion
  • Added comment skipping to parse_prefix for expression-level comments
  • Enhanced parse_infix to recognize string/table tokens after function names
  • Updated method call parsing to support syntactic sugar patterns

Test coverage:

  • 5 tests for function call syntactic sugar (test/lua/parser/expr_test.exs)
  • 4 tests for comments in expressions (test/lua/parser/expr_test.exs)
  • 8 tests for empty statements (test/lua/parser/statement_test.exs)

Testing

All 1120 tests pass, including 17 new parser tests.

mix test
# 50 doctests, 14 properties, 1120 tests, 0 failures

The Lua 5.3 test suite's constructs.lua file now parses successfully (it fails at runtime due to missing the debug module, which is expected).

Related

This builds on the work from #132 (load function implementation) and improves parser compatibility with the Lua 5.3 test suite.

🤖 Generated with Claude Code

Dave Lucia and others added 3 commits February 11, 2026 07:54
This commit implements three key parser enhancements to support more Lua 5.3 syntax:

1. **Function Call Syntactic Sugar**
   - Implement `func "string"` → `func("string")` pattern
   - Implement `func{table}` → `func({table})` pattern
   - Support for both regular function calls and method calls
   - Examples: `require "debug"`, `print{1,2,3}`, `obj:method "str"`

2. **Empty Statement Support**
   - Handle semicolons as empty statements (`;` `;;` etc)
   - Support multiple consecutive semicolons
   - Handle semicolons after comments correctly
   - Semicolons can appear at start of code, between statements, in blocks

3. **Comments in Expressions**
   - Parser now skips comments that appear within expressions
   - Supports comments after operators: `x ~= -- comment\nnil`
   - Enables more idiomatic Lua code with inline documentation

**Testing**
- Added 17 new parser tests covering all new functionality
- All tests now properly use ExUnit instead of ad-hoc mix run -e
- Tests organized into:
  - 5 tests for function call syntactic sugar (expr_test.exs)
  - 4 tests for comments in expressions (expr_test.exs)
  - 8 tests for empty statements (statement_test.exs)

**Implementation Details**
- Modified parse_block_acc to skip semicolons at block level
- Removed recursive semicolon handling from parse_stmt_inner
- Added comment skipping to parse_prefix for expression-level comments
- Enhanced parse_infix to recognize string/table tokens after function names
- Updated method call parsing to support syntactic sugar patterns

All 1120 tests passing.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Remove unreachable error handling code for Lua.Compiler.compile/1
since the compiler never returns errors in the current implementation.

This fixes 3 dialyzer errors:
- pattern_match: {:error, _reason} can never match
- pattern_match_cov: variable_error pattern is unreachable
- unused_fun: format_compile_error/1 is never called

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Parser always returns errors as strings, so the fallback
clause that calls inspect/1 is unreachable.

Fixes dialyzer pattern_match_cov warning.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@davydog187 davydog187 merged commit b3b2332 into main Feb 11, 2026
2 checks passed
@davydog187 davydog187 deleted the implement-load-function branch February 11, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant