ehm: removed intermediate stuff

main
Antonio De Lucreziis 2 years ago
parent be72480f95
commit 9ebc4715ed

@ -1,69 +0,0 @@
(*script.Program)({
Statements: ([]script.Expression) (len=1) {
(*script.FunctionCall)({
Receiver: (*script.Identifier)({
Value: (string) (len=5) "match"
}),
Arguments: ([]script.ArgumentExpression) (len=2) {
(*script.BinaryOperation)({
Lhs: (*script.Identifier)({
Value: (string) (len=1) "x"
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.ListExpression)({
Values: ([]script.Expression) (len=2) {
(*script.BinaryOperation)({
Lhs: (*script.Quote)({
Value: (*script.Identifier)({
Value: (string) (len=4) "None"
})
}),
Operator: (string) (len=2) "->",
Rhs: (*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 0
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}),
(*script.BinaryOperation)({
Lhs: (*script.Quote)({
Value: (*script.ParenthesizedExpression)({
Inner: (*script.FunctionCall)({
Receiver: (*script.Identifier)({
Value: (string) (len=4) "Some"
}),
Arguments: ([]script.ArgumentExpression) (len=1) {
(*script.BinaryOperation)({
Lhs: (*script.Identifier)({
Value: (string) (len=5) "value"
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
})
})
}),
Operator: (string) (len=2) "->",
Rhs: (*script.BinaryOperation)({
Lhs: (*script.Identifier)({
Value: (string) (len=5) "value"
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
})
}
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
})
}
})

@ -1,93 +0,0 @@
(*script.Program)({
Statements: ([]script.Expression) (len=1) {
(*script.FunctionCall)({
Receiver: (*script.Identifier)({
Value: (string) (len=3) "for"
}),
Arguments: ([]script.ArgumentExpression) (len=3) {
(*script.BinaryOperation)({
Lhs: (*script.Identifier)({
Value: (string) (len=1) "n"
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.ParenthesizedExpression)({
Inner: (*script.FunctionCall)({
Receiver: (*script.Identifier)({
Value: (string) (len=5) "range"
}),
Arguments: ([]script.ArgumentExpression) (len=2) {
(*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 1
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 10
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
})
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.ListExpression)({
Values: ([]script.Expression) (len=2) {
(*script.BinaryOperation)({
Lhs: (*script.Identifier)({
Value: (string) (len=1) "m"
}),
Operator: (string) (len=2) ":=",
Rhs: (*script.BinaryOperation)({
Lhs: (*script.Identifier)({
Value: (string) (len=1) "n"
}),
Operator: (string) (len=1) "*",
Rhs: (*script.BinaryOperation)({
Lhs: (*script.Identifier)({
Value: (string) (len=1) "n"
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
})
}),
(*script.FunctionCall)({
Receiver: (*script.Identifier)({
Value: (string) (len=8) "printfln"
}),
Arguments: ([]script.ArgumentExpression) (len=2) {
(*script.BinaryOperation)({
Lhs: (*script.LiteralString)({
Value: (string) (len=8) "n^2 = %v"
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.Identifier)({
Value: (string) (len=1) "m"
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
})
}
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
})
}
})

@ -1,130 +0,0 @@
(*script.Program)({
Statements: ([]script.Expression) (len=1) {
(*script.FunctionCall)({
Receiver: (*script.Identifier)({
Value: (string) (len=3) "foo"
}),
Arguments: ([]script.ArgumentExpression) (len=3) {
(*script.BinaryOperation)({
Lhs: (*script.ListExpression)({
Values: ([]script.Expression) (len=3) {
(*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 1
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 2
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 3
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.DictExpression)({
Entries: ([]*script.DictEntry) (len=2) {
(*script.DictEntry)({
Key: (*script.Identifier)({
Value: (string) (len=1) "a"
}),
Value: (*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 1
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}),
(*script.DictEntry)({
Key: (*script.Identifier)({
Value: (string) (len=1) "b"
}),
Value: (*script.FunctionCall)({
Receiver: (*script.Identifier)({
Value: (string) (len=3) "foo"
}),
Arguments: ([]script.ArgumentExpression) (len=2) {
(*script.BinaryOperation)({
Lhs: (*script.Quote)({
Value: (*script.Identifier)({
Value: (string) (len=5) "other"
})
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.DictExpression)({
Entries: ([]*script.DictEntry) (len=1) {
(*script.DictEntry)({
Key: (*script.Identifier)({
Value: (string) (len=3) "bar"
}),
Value: (*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 456
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
})
}
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
})
})
}
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.ListExpression)({
Values: ([]script.Expression) (len=3) {
(*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 4
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 5
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
}),
(*script.BinaryOperation)({
Lhs: (*script.LiteralNumber)({
Value: (float64) 6
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
}),
Operator: (string) "",
Rhs: (script.Expression) <nil>
})
}
})
}
})

@ -1,130 +0,0 @@
# Script
Mini scripting language for querying the website "model".
## Syntax
### Values
```go
1
-1
0xFF
3.14
"some text"
'r' 'u' 'n' 'e'
```
### Property Access
```lua
-- Primitives
12
-3.14
"text"
-- Meta
#symbol
#(None)
#(Some 123)
-- Booleans
true
false
-- Lists
[1 2 3]
-- Dicts
{ a = 1, b = 2 }
{
a = 1
b = 2
}
-- Function call
fn 1 2 3
-- as long as it continues "inline" its fine and still a single expression
fn [1 2
3] {
a = 1
b = (foo #other {
bar = 456
})
} [
4 5 6
]
-- Arithmetic (no precedence)
-- anything matching the following regex is considered an operator
[+-*/%<>=&|^?#@]+
-- Control Flow (just builtin macros)
if cond trueCaseExpr
ifelse cond trueCaseExpr falseCaseExpr
match value [
case valuePattern1 case1Expr
case valuePattern2 case2Expr
...
default defaultExpr
]
for var items body
-- Functions
fn [a] [
printfln "a = %v" a
]
fn example [a b] [
printfln "a + b = %v" (a + b)
]
example 42 69
-- Example
build [
pipeline [
source "index.html"
layout "layout/base.html"
target "dist/index.html"
]
pipeline [
source "posts/{id}.html"
markdown
layout "layout/base.html"
target "dist/posts/{{ .Id }}/index.html"
]
pipeline [
source "posts/{id}.md"
frontmatter
sort #descending { key = "publish_date" }
slice { to = 10 }
template "layouts/list.html"
layout "layouts/base.html"
target "dist/posts/index.html"
]
pipeline [
source "posts/{id}.md"
frontmatter
sort #descending { key = "publish_date" }
slice { from = 10 }
chunk 10 {
template "layouts/list.html"
layout "layouts/base.html"
target "dist/posts/{{ .Chunk.Index }}/index.html"
}
]
pipeline [
source "posts/{id}.md"
frontmatter
categorize "tags" {
template "layouts/tag.html"
layout "layouts/base.html"
target "dist/tags/{{ .Category }}/index.html"
}
]
]
```

@ -1,149 +0,0 @@
package script
import (
"os"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
type Program struct {
Statements []Expression `parser:"Newline? ( @@ ( Newline @@ )* Newline? )?"`
}
type ArgumentExpression interface{}
type Expression interface{}
type FunctionReceiver interface{}
var _ FunctionReceiver = &Identifier{}
var _ FunctionReceiver = &ParenthesizedExpression{}
type FunctionCall struct {
Receiver FunctionReceiver `parser:"@@"`
Arguments []ArgumentExpression `parser:"@@+"`
}
type ParenthesizedExpression struct {
Inner Expression `parser:"'(' @@ ')'"`
}
type ListExpression struct {
Values []Expression `parser:"'[' ( Newline? @@ ( Newline? @@ )* Newline? )? ']'"`
}
type DictExpression struct {
Entries []*DictEntry `parser:"'{' ( Newline? @@ ( ( ',' | Newline ) @@ )* Newline? )? '}'"`
}
type DictEntry struct {
Key DictEntryKey `parser:"@@"`
Value Expression `parser:"'=' @@"`
}
type DictEntryKey interface{}
type Boolean bool
func (b *Boolean) Capture(values []string) error {
*b = values[0] == "true"
return nil
}
type LiteralBoolean struct {
Value Boolean `parser:"@('true' | 'false')"`
}
type LiteralString struct {
Value string `parser:"@String"`
}
type LiteralNumber struct {
Value float64 `parser:"@Number"`
}
type Identifier struct {
Value string `parser:"@Ident"`
}
type Quote struct {
Value Quotation `parser:"'#' @@"`
}
type PropertyAccess struct {
Receiver IdentifierOrParenthesis `parser:"@@"`
Accesses []IdentifierOrParenthesis `parser:"('.' @@)*"`
}
type Quotation interface{}
type ExpressionFactor interface{}
type BinaryOperation struct {
Lhs ExpressionFactor `parser:"@@"`
Operator string `parser:"( @Operator"`
Rhs Expression `parser:" @@ )?"`
}
type IdentifierOrParenthesis interface{}
var (
scriptLexer = lexer.MustSimple([]lexer.SimpleRule{
{Name: "Comment", Pattern: `--[^\n]*\n?`},
{Name: "String", Pattern: `"(\\"|[^"])*"`},
{Name: "Number", Pattern: `[-+]?(?:\d*\.)?\d+`},
{Name: "Ident", Pattern: `[a-zA-Z]\w*`},
{Name: "Meta", Pattern: `\#`},
// an operator is any combination of [+-*/%<>=:;,&|^?#@] expect for the string "="
{Name: "Operator", Pattern: `(=[\+\-\*/%<>=:&|\^?#@]+)|([\+\-\*/%<>:&|\^?#@][\+\-\*/%<>=:&|\^?#@]*)`},
{Name: "Punct", Pattern: `[\(\)\{\}\[\]=,]`},
{Name: "Newline", Pattern: `[ \t]*\n\s*`},
{Name: "Whitespace", Pattern: `[ \t]+`},
})
// Parser
Parser = participle.MustBuild[Program](
participle.Lexer(scriptLexer),
participle.Elide("Comment", "Whitespace"),
participle.Unquote("String"),
participle.Union[Expression](
&FunctionCall{},
&BinaryOperation{},
),
participle.Union[ArgumentExpression](
&BinaryOperation{},
),
participle.Union[ExpressionFactor](
&ListExpression{},
&DictExpression{},
&LiteralNumber{},
&LiteralString{},
&LiteralBoolean{},
&ParenthesizedExpression{},
&Quote{},
&Identifier{},
),
participle.Union[Quotation](
&Identifier{},
&ParenthesizedExpression{},
),
participle.Union[DictEntryKey](
&Identifier{},
&ParenthesizedExpression{},
),
participle.Union[FunctionReceiver](
&Identifier{},
&ParenthesizedExpression{},
),
participle.Union[IdentifierOrParenthesis](
&Identifier{},
&ParenthesizedExpression{},
),
participle.UseLookahead(2),
)
)
func Parse(source string) (*Program, error) {
return Parser.ParseString("", source, participle.Trace(os.Stdout))
// return Parser.ParseString("", source)
}

@ -1,378 +0,0 @@
package script_test
import (
"testing"
"github.com/aziis98/cabret/script"
"github.com/bradleyjkemp/cupaloy"
"gotest.tools/assert"
)
func TestParsing(t *testing.T) {
t.Run("Number", func(t *testing.T) {
program, err := script.Parse(`
-3.14
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.LiteralNumber{-3.14}},
},
},
)
})
t.Run("String", func(t *testing.T) {
program, err := script.Parse(`
"Hello, World!"
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.LiteralString{"Hello, World!"}},
},
},
)
})
t.Run("Symbol", func(t *testing.T) {
program, err := script.Parse(`
#symbol
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.Quote{Value: &script.Identifier{"symbol"}}},
},
},
)
})
t.Run("BinaryOperation", func(t *testing.T) {
program, err := script.Parse(`
1 < 2
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{
Lhs: &script.LiteralNumber{1},
Operator: "<",
Rhs: &script.BinaryOperation{Lhs: &script.LiteralNumber{2}},
},
},
},
)
})
t.Run("Boolean/True", func(t *testing.T) {
program, err := script.Parse(`
true
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.LiteralBoolean{true}},
},
},
)
})
t.Run("Boolean/False", func(t *testing.T) {
program, err := script.Parse(`
false
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.LiteralBoolean{false}},
},
},
)
})
t.Run("List", func(t *testing.T) {
program, err := script.Parse(`
[1 2 3]
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.ListExpression{
Values: []script.Expression{
&script.BinaryOperation{Lhs: &script.LiteralNumber{1}},
&script.BinaryOperation{Lhs: &script.LiteralNumber{2}},
&script.BinaryOperation{Lhs: &script.LiteralNumber{3}},
},
}},
},
},
)
})
t.Run("List", func(t *testing.T) {
program, err := script.Parse(`
[1 2 3
"a" "b" "c"
false]
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.ListExpression{
Values: []script.Expression{
&script.BinaryOperation{Lhs: &script.LiteralNumber{1}},
&script.BinaryOperation{Lhs: &script.LiteralNumber{2}},
&script.BinaryOperation{Lhs: &script.LiteralNumber{3}},
&script.BinaryOperation{Lhs: &script.LiteralString{"a"}},
&script.BinaryOperation{Lhs: &script.LiteralString{"b"}},
&script.BinaryOperation{Lhs: &script.LiteralString{"c"}},
&script.BinaryOperation{Lhs: &script.LiteralBoolean{false}},
},
}},
},
},
)
})
t.Run("Dict", func(t *testing.T) {
program, err := script.Parse(`
{}
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.DictExpression{}},
},
},
)
})
t.Run("Dict", func(t *testing.T) {
program, err := script.Parse(`
{ a = "1", b = "2" }
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.DictExpression{
Entries: []*script.DictEntry{
{
Key: &script.Identifier{"a"},
Value: &script.BinaryOperation{Lhs: &script.LiteralString{"1"}},
},
{
Key: &script.Identifier{"b"},
Value: &script.BinaryOperation{Lhs: &script.LiteralString{"2"}},
},
},
}},
},
},
)
})
t.Run("Dict", func(t *testing.T) {
program, err := script.Parse(`
{
a = "1"
b = "2", c = "3"
}
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.BinaryOperation{Lhs: &script.DictExpression{
Entries: []*script.DictEntry{
{
Key: &script.Identifier{"a"},
Value: &script.BinaryOperation{Lhs: &script.LiteralString{"1"}},
},
{
Key: &script.Identifier{"b"},
Value: &script.BinaryOperation{Lhs: &script.LiteralString{"2"}},
},
{
Key: &script.Identifier{"c"},
Value: &script.BinaryOperation{Lhs: &script.LiteralString{"3"}},
},
},
}},
},
},
)
})
t.Run("FunctionCall", func(t *testing.T) {
program, err := script.Parse(`
fn 1 2 3
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.FunctionCall{
Receiver: &script.Identifier{"fn"},
Arguments: []script.ArgumentExpression{
&script.BinaryOperation{Lhs: &script.LiteralNumber{1}},
&script.BinaryOperation{Lhs: &script.LiteralNumber{2}},
&script.BinaryOperation{Lhs: &script.LiteralNumber{3}},
},
},
},
},
)
})
t.Run("FunctionCall", func(t *testing.T) {
program, err := script.Parse(`
foo { a = 1, b = [1 2 3] } [
bar #test
2
]
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.FunctionCall{
Receiver: &script.Identifier{"foo"},
Arguments: []script.ArgumentExpression{
&script.BinaryOperation{Lhs: &script.DictExpression{
Entries: []*script.DictEntry{
{
Key: &script.Identifier{"a"},
Value: &script.BinaryOperation{Lhs: &script.LiteralNumber{1}},
},
{
Key: &script.Identifier{"b"},
Value: &script.BinaryOperation{Lhs: &script.ListExpression{
Values: []script.Expression{
&script.BinaryOperation{Lhs: &script.LiteralNumber{1}},
&script.BinaryOperation{Lhs: &script.LiteralNumber{2}},
&script.BinaryOperation{Lhs: &script.LiteralNumber{3}},
},
}},
},
},
}},
&script.BinaryOperation{Lhs: &script.ListExpression{
Values: []script.Expression{
&script.FunctionCall{
Receiver: &script.Identifier{"bar"},
Arguments: []script.ArgumentExpression{
&script.BinaryOperation{Lhs: &script.Quote{&script.Identifier{"test"}}},
},
},
&script.BinaryOperation{Lhs: &script.LiteralNumber{2}},
},
}},
},
},
},
},
)
})
t.Run("Operators", func(t *testing.T) {
program, err := script.Parse(`
if 1 <= 2 [ println "Yes" ]
`)
assert.NilError(t, err)
assert.DeepEqual(t, program,
&script.Program{
Statements: []script.Expression{
&script.FunctionCall{
Receiver: &script.Identifier{"if"},
Arguments: []script.ArgumentExpression{
&script.BinaryOperation{
Lhs: &script.LiteralNumber{1},
Operator: "<=",
Rhs: &script.BinaryOperation{
Lhs: &script.LiteralNumber{2},
},
},
&script.BinaryOperation{Lhs: &script.ListExpression{
Values: []script.Expression{
&script.FunctionCall{
Receiver: &script.Identifier{"println"},
Arguments: []script.ArgumentExpression{
&script.BinaryOperation{Lhs: &script.LiteralString{"Yes"}},
},
},
},
}},
},
},
},
},
)
})
t.Run("ComplexProgram", func(t *testing.T) {
program, err := script.Parse(`
match x [
#None -> 0
#(Some value) -> value
]
`)
assert.NilError(t, err)
cupaloy.SnapshotT(t, program)
})
t.Run("ComplexProgram", func(t *testing.T) {
program, err := script.Parse(`
for n (range 1 10) [
m := n * n
printfln "n^2 = %v" m
]
`)
assert.NilError(t, err)
cupaloy.SnapshotT(t, program)
})
t.Run("ComplexProgram", func(t *testing.T) {
program, err := script.Parse(`
foo [ 1 2 3 ] {
a = 1
b = foo #other { bar = 456 }
} [
4
5
6
]
`)
assert.NilError(t, err)
cupaloy.SnapshotT(t, program)
})
}
Loading…
Cancel
Save