This commit is contained in:
Andrey Parhomenko 2024-02-26 02:22:09 +05:00
commit de5de7a79a
8 changed files with 179 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
pp

3
build.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/sh
#
go build ./cmd/pp

15
errors.go Normal file
View file

@ -0,0 +1,15 @@
package pp
import (
//"errors"
"fmt"
)
type UnexpectedError struct {
What string
Line int
}
func (err UnexpectedError) Error() string {
return fmt.Sprintf("%d: unexpected %s", err.Line, err.What)
}

8
go.mod Normal file
View file

@ -0,0 +1,8 @@
module vultras.su/util/pp
go 1.21.7
require (
github.com/d5/tengo/v2 v2.16.1 // indirect
vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 // indirect
)

4
go.sum Normal file
View file

@ -0,0 +1,4 @@
github.com/d5/tengo/v2 v2.16.1 h1:/N6dqiGu9toqANInZEOQMM8I06icdZnmb+81DG/lZdw=
github.com/d5/tengo/v2 v2.16.1/go.mod h1:XRGjEs5I9jYIKTxly6HCF8oiiilk5E/RYXOZ5b0DZC8=
vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 h1:acA77oEg7hDuUchyBvw7scG9gayVLFAC9/CGuqyfLSA=
vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0/go.mod h1:rYu/sFWE3vUkDSSJCZt+K1aVaso0MYcZ+tmxQd4whdQ=

109
main.go Normal file
View file

@ -0,0 +1,109 @@
package pp
import (
"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"context"
"strings"
"fmt"
)
type Preprocessor struct {
// The string will be inserted in the
// beginning of processed files.
head string
tags [2]string
modules *tengo.ModuleMap
}
// Evaluate the expression and return the value
// it would print.
func (pp *Preprocessor) Eval(data string) (string, error) {
const retHead = `
printf := func(format, ...vals) {
__ret__ += __Sprintf(format, vals...)
}
`
script := tengo.NewScript([]byte(retHead + pp.head + "\n" + data))
err := script.Add("__Sprintf", &tengo.UserFunction{
Value: func(args ...tengo.Object) (tengo.Object, error){
if len(args) < 1 {
return nil, tengo.ErrWrongNumArguments
}
format, ok := tengo.ToString(args[0])
if !ok {
return nil, tengo.ErrInvalidArgumentType{
Expected: "string",
}
}
gargs := make([]any, len(args) - 1)
for i := range gargs {
gargs[i] = tengo.ToInterface(args[i+1])
//fmt.Printf("shit: %q\n", gargs[i])
}
str := fmt.Sprintf(format, gargs...)
return tengo.FromInterface(str)
},
},
)
if err != nil {
return "", err
}
err = script.Add("__ret__", "")
if err != nil{
return "", err
}
compiled, err := script.RunContext(context.Background())
if err != nil {
return "", err
}
return compiled.Get("__ret__").String(), nil
}
// Get the new preprocessor with default options.
func NewPp() *Preprocessor {
pp := &Preprocessor{
tags: [2]string{
"<?tengo",
"?>",
},
modules: stdlib.GetModuleMap(stdlib.AllModuleNames()...)
}
return pp
}
func (pp *Preprocessor) Process(data string) (string, error) {
var b strings.Builder
last := 0
for {
idxStart := strings.Index(data[last:], pp.tags[0])
idxEnd := strings.Index(data[last:], pp.tags[1])
//fmt.Printf("cock %d %d %d\n", last, idxStart, idxEnd)
if idxStart < 0 {
if idxEnd >= 0 {
return "", UnexpectedError{
What: "end tag",
}
}
fmt.Fprint(&b, data[last:])
break
} else if idxEnd < 0 {
return "", UnexpectedError{
What: "start tag",
}
}
fmt.Fprint(&b, data[last:idxStart])
code := data[idxStart+len(pp.tags[0]):idxEnd]
str, err := pp.Eval(code)
if err != nil {
return "", err
}
fmt.Fprint(&b, str)
data = data[idxEnd + len(pp.tags[1])+1:]
}
return b.String(), nil
}

11
tests/index.md.pp Normal file
View file

@ -0,0 +1,11 @@
# The index testing
1 + 1 = <?tengo
printf("%d", 1+1)
printf("\ncock")
?>
## The shit after
checking <?tengo
printf("second shit")
?>

28
tool.go Normal file
View file

@ -0,0 +1,28 @@
package pp
import (
"vultras.su/core/cli/mtool"
"fmt"
"os"
"log"
"path/filepath"
)
var Tool = mtool.T("pp").Func(func(flags *mtool.Flags){
pp := NewPp()
filePaths := flags.Parse()
for _, filePath := range filePaths {
pth := filepath.FromSlash(filePath)
bts, err := os.ReadFile(pth)
if err != nil {
log.Println("read error:", err)
continue
}
str, err := pp.Process(string(bts))
if err != nil {
log.Println("pp error:", err)
continue
}
fmt.Print(str)
}
})