pstr is a simple regex engine written in Go from scratch for educational purposes. It can parse basic regular expressions, convert them into a Nondeterministic Finite Automaton (NFA), and perform matching on input strings.
- Basic Regex Parsing: Supports literals,
( )groups,[ ]character classes, and quantifiers like*,+,?, and{m,n}. - NFA Engine: Converts parsed regex tokens into an NFA state machine.
- String Matching: Checks if an input string is valid according to the generated NFA.
- Interactive CLI: A simple command-line interface to test regex patterns in real-time.
- Exposed API: An API endpoint to check regex patterns programmatically.
- Go 1.24 or newer
-
Clone the repository:
git clone https://github.com/rubuy-74/pstr cd pstr -
Run the interactive CLI: You can use the provided shell script:
./run.sh
Or run it directly with Go:
go run cmd/pstr/main.go
-
Test a pattern: The application will prompt you to enter a regex and then a string to check against it.
Example Interaction:
Enter regex > (a|b)*c Enter string to check > ababc Congratulations, the string is VALID
-
Run the API server:
go run cmd/pstr/main.go
The server will start on port
3000. -
Send a POST request: You can use
curlor any API client to send aPOSTrequest to the/checkendpoint.Example with
curl:curl -X POST -H "Content-Type: application/json" -d '{"regex": "(a|b)*c", "string": "ababc"}' http://localhost:3000/check
Expected Response (Success):
{ "valid": true }Expected Response (Error):
{ "error": "failed to parse regex", "message": "missing left operand for | operator at position 0" }
Note: This testing section was created using Cursor AI to provide comprehensive test coverage and reliability verification.
The project includes extensive test suites to ensure reliability and prevent crashes on edge cases. All tests verify that the regex engine handles invalid inputs gracefully instead of crashing.
go test ./...- Runs all tests in all packages
- Shows only pass/fail status
- Fast and clean output
go test ./... -v- Shows detailed output for each test
- Great for debugging and seeing what's being tested
- Shows individual test case results
go test ./internal/parser/
go test ./internal/state_machine/
go test ./internal/go test ./... -run="TestEmptyInputValidation"
go test ./... -run="TestProcessRepeatEdgeCases"
go test ./... -run="TestPanicRecovery"go test ./... -cover- Shows test coverage percentage
- Helps identify untested code
go test ./... -race- Detects race conditions in concurrent code
- Important for production code
go test ./... -bench=.- Runs benchmark tests (if you have any)
- Measures performance
go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.out- Generates detailed coverage report
- Opens HTML report in browser
The project includes comprehensive test suites covering:
- Integration Tests: Complete pipeline testing with edge cases
- Parser Tests: Original functionality + reliability tests
- State Machine Tests: NFA creation and validation
- Reliability Tests: Edge cases that previously caused crashes
- Panic Recovery Tests: Ensures no crashes on invalid inputs
- Memory Safety Tests: Validates safe memory access
Total Test Cases: 50+ individual test cases covering all reliability fixes.
- Empty string handling
- Whitespace-only inputs
- Invalid operator usage
processRepeat()with no preceding tokensprocessOr()with missing operandsprocessBrackets()with empty/invalid contentprocessGroup()with empty/unclosed groupsToNFA()with empty token lists
Token.ToNFA()with invalid type assertions- Safe type assertion handling
- Panic recovery verification
- Proper error propagation
- Meaningful error messages
- Graceful failure handling
- Malformed regex patterns
- Unclosed brackets/groups/ranges
- Invalid range syntax
- Complex combinations
- Daily Development:
go test ./... - Debugging Issues:
go test ./... -v - Before Commits:
go test ./... -race -cover - CI/CD Pipeline:
go test ./... -v -cover
βββ cmd/
β βββ pstr/
β βββ main.go # API endpoint and CLI entry point
βββ internal/
β βββ models/
β β βββ state/
β β β βββ state.go # NFA state data structures
β β βββ token/
β β β βββ token.go # Regex token data structures
β β βββ token_type/
β β βββ token_type.go # Enum for token types
β βββ parser/
β β βββ parser.go # Regex string to token parsing
β β βββ parser_test.go # Tests for the parser
β β βββ reliability_test.go # Reliability and edge case tests
β βββ state_machine/
β β βββ state_machine.go # Token to NFA conversion and matching logic
β β βββ state_machine_test.go # State machine tests
β βββ integration_test.go # End-to-end integration tests
β βββ utils/
β βββ utils.go # Utility functions
βββ go.mod # Go module definition
βββ run.sh # Script to run the CLI
βββ TODO.md # Project goals and references
This project is licensed under the MIT License.
Made with β€οΈ by rubuy-74