Skip to content

Commit

Permalink
Use id in MemoTreeClose
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Mar 22, 2021
1 parent 8b7b1c8 commit ff7e552
Show file tree
Hide file tree
Showing 8 changed files with 980 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ gpeg
*.svg
*.dat
*.so
/benchmarks/apply
/benchmarks/apply_gpeg
/benchmarks/fullparse
/benchmarks/reparse
40 changes: 40 additions & 0 deletions benchmarks/apply_gpeg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"flag"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

var dir = flag.String("dir", "", "dir to apply command to")
var suffix = flag.String("suffix", "", "file suffix")

const cutoff = 100000

func main() {
flag.Parse()

target := flag.Args()[0]

err := filepath.Walk(*dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), *suffix) && info.Size() >= cutoff {
args := []string{path}
cmd := exec.Command(target, args...)
cmd.Stdout = os.Stdout
cmd.Run()
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
67 changes: 67 additions & 0 deletions benchmarks/reparse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"time"

"github.com/zyedidia/gpeg/memo"
"github.com/zyedidia/gpeg/pattern"
"github.com/zyedidia/gpeg/re"
"github.com/zyedidia/gpeg/vm"
)

var grammar = flag.String("grammar", "../grammars/json_memo.peg", "grammar to use")

func main() {
flag.Parse()

inputs := flag.Args()

peg, err := ioutil.ReadFile(*grammar)
if err != nil {
log.Fatal(err)
}

p := re.MustCompilePatt(string(peg))
prog := pattern.MustCompile(p)
code := vm.Encode(prog)

for _, input := range inputs {
data, err := ioutil.ReadFile(input)
if err != nil {
log.Fatal(err)
}
in := bytes.NewReader(data)

tbl := memo.NewTreeTable(512)
for i := 0; i < 1; i++ {
code.Exec(in, tbl)
loc := rand.Intn(len(data))
edit := memo.Edit{
Start: loc,
End: loc + 1,
Len: 1,
}
in.Reset(data)

tstart := time.Now()
tbl.ApplyEdit(edit)
code.Exec(in, tbl)
fmt.Println(len(data), time.Since(tstart).Microseconds())
}
// f, err := os.Create("out.svg")
// if err != nil {
// log.Fatal(err)
// }
// defer f.Close()
// viz.DrawMemo(tbl, len(data), f, 1000, 2000)
}
// fmt.Println("elapsed:", time.Since(tstart))
// fmt.Printf("match: %t, n: %d, len(ast): %d\n", match, n, len(ast))
// fmt.Printf("table entries: %d\n", tbl.Size())
}
Loading

0 comments on commit ff7e552

Please sign in to comment.