Quick Start¶
Get up and running with Grammar School in minutes.
Installation¶
Your First DSL¶
Let's create a simple greeting DSL that demonstrates the core concepts.
from grammar_school import Grammar, method
class GreetingDSL(Grammar):
@method
def greet(self, name, message="Hello"):
print(f"{message}, {name}!")
# Use the DSL
dsl = GreetingDSL()
dsl.execute('greet(name="Alice")')
# Output: Hello, Alice!
dsl.execute('greet(name="Bob", message="Hi")')
# Output: Hi, Bob!
package main
import (
"context"
"fmt"
"grammar-school/go/gs"
)
type GreetingDSL struct{}
func (d *GreetingDSL) Greet(args gs.Args) error {
name := args["name"].Str
message := "Hello"
if msg, ok := args["message"]; ok {
message = msg.Str
}
fmt.Printf("%s, %s!\n", message, name)
return nil
}
func main() {
dsl := &GreetingDSL{}
parser := &MyParser{} // Implement gs.Parser interface
engine, _ := gs.NewEngine("", dsl, parser)
engine.Execute(context.Background(), `greet(name="Alice")`)
// Output: Hello, Alice!
engine.Execute(context.Background(), `greet(name="Bob", message="Hi")`)
// Output: Hi, Bob!
}
How It Works¶
- Define Methods - Create methods marked with
@method(Python) that contain your implementation - Create Grammar - Instantiate your Grammar class (Python) or Engine (Go) with your DSL
- Execute - Parse and execute DSL code - methods run directly when called
Next Steps¶
- Learn about Core Concepts
- Explore the Python API or Go API
- Check out Examples