A code formatter for LPC (Lars Pensjö C) language, similar to gofmt for Go.
lpcfmt parses LPC source code and reformats it with consistent indentation, spacing, and style. It can be used as a command-line tool to format individual files or entire directories.
Tested against real-world code: 100% pass rate on 18+ test files from the FluffOS driver repository.
- Automatic code formatting with consistent style
- Preserves comments and preprocessor directives
- Handles all LPC language constructs:
- Inheritance statements
- Variable declarations
- Function definitions
- Control flow structures
- Expressions and operators
- Classes and structs
# Format a single file and output to stdout
lpcfmt myfile.lpc
# Format a file in place
lpcfmt -w myfile.lpc
# List files that need formatting
lpcfmt -l myfile.lpc
# Show diff of formatting changes
lpcfmt -d myfile.lpc
# Format from stdin
cat myfile.lpc | lpcfmt
# Format all LPC files in a directory
lpcfmt -w path/to/directory/
# Show version
lpcfmt --version-w- Write result to source file instead of stdout-l- List files whose formatting differs from lpcfmt's-d- Display diffs instead of rewriting files--version- Print version information
# Generate Go parser from ANTLR grammar using Gradle
./gradlew generateGoParser
# Build the formatter
go build -o lpcfmt ./cmd/lpcfmt
# Or use Windows batch file
gradlew.bat generateGoParser# Build with CMake (experimental, not actively maintained)
mkdir build && cd build
cmake ..
makeThe LPC grammar is defined in ANTLR4 format:
lpc.g4- Main grammarliteral.g4- Literal tokens (strings, numbers)lpcid.g4- Identifier rules
inherit "/inherit/master/valid";
nosave int has_error=0;
public string get_last_error(){return last_error;}
inherit "/inherit/master/valid";
nosave int has_error = 0;
public string get_last_error()
{
return last_error;
}
The formatter includes comprehensive test coverage:
# Run all tests
go test ./internal/formatter/
# Run with verbose output
go test -v ./internal/formatter/
# Run integration tests against FluffOS code
go test -v ./internal/formatter/ -run TestFluffOS- 48+ test files from real-world LPC code
- Auto-discovery of all
.cand.lpcfiles intests/directory - Idempotency testing ensures stable formatting
- Multiple sources: FluffOS, Dead Souls, Lima, Nightmare, NT7, XKX100
- Validates: syntax preservation, comment retention, formatting stability
See tests/README.md for test file organization and TESTING.md for detailed testing documentation.
See CLAUDE.md for a comprehensive development guide including:
- ANTLR parser generation
- Complete testing instructions
- Project architecture
- Development workflow
- Troubleshooting guide
The project structure:
lpc.g4,literal.g4,lpcid.g4- ANTLR grammar definitionscmd/lpcfmt/- Go CLI implementationinternal/formatter/- Core formatting logic and testsformatter.go- Main formatter interfaceantlr_formatter.go- ANTLR-based formatter implementation
parser/- Generated Go parser code (from ANTLR, auto-generated)tests/- Test LPC filestests/fluffos/- Real FluffOS driver test filestests/mudlibs/- MUD library test files
build.gradle,gradlew- Gradle wrapper for parser generationmain.cpp- C++ implementation (experimental, not actively maintained)
Contributions are welcome! Please:
- Add tests for new features
- Ensure all tests pass:
go test ./... - Follow the existing code style
- Update documentation as needed
See LICENSE file for details.