Skip to content

Commit

Permalink
Remove javascript parser
Browse files Browse the repository at this point in the history
This commit removes the javascript parsers, a feature this is no longer
necessary now that the Zui client uses the /compile endpoint to parse
Zed queries.

This commit also updates the Go parser dependencies (pigeon and
goimport) to latest versions.
  • Loading branch information
mattnibs committed Apr 4, 2024
1 parent 2a487ee commit 3be401c
Show file tree
Hide file tree
Showing 11 changed files with 4,610 additions and 37,745 deletions.
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ install:
installdev:
@go install -ldflags='$(LDFLAGS)' ./cmd/...

PEG_GEN := $(addprefix compiler/parser/parser., go js es.js)
$(PEG_GEN): compiler/parser/Makefile compiler/parser/support.js compiler/parser/parser.peg
compiler/parser/parser.go: compiler/parser/Makefile compiler/parser/support.go compiler/parser/parser.peg
$(MAKE) -C compiler/parser

# This rule is best for edit-compile-debug cycle of peg development. It should
# properly trigger rebuilds of peg-generated code, but best to run
# "make -C compiler/parser" when changing versions of pigeon, pegjs, or JavaScript
# dependencies.
# "make -C compiler/parser" when changing versions of pigeon.
.PHONY: peg peg-run
peg: $(PEG_GEN)
peg: compiler/parser/parser.go

peg-run: $(PEG_GEN)
go run ./cmd/zc -repl
Expand Down
33 changes: 0 additions & 33 deletions cmd/zed/dev/compile/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"strings"

"github.com/brimdata/zed/cmd/zed/dev"
Expand Down Expand Up @@ -52,7 +51,6 @@ func init() {

type Command struct {
*root.Command
js bool
pigeon bool
proc bool
canon bool
Expand All @@ -65,7 +63,6 @@ type Command struct {

func New(parent charm.Command, f *flag.FlagSet) (charm.Command, error) {
c := &Command{Command: parent.(*root.Command)}
f.BoolVar(&c.js, "js", false, "run JavaScript version of peg parser")
f.BoolVar(&c.pigeon, "pigeon", false, "run pigeon version of peg parser")
f.BoolVar(&c.proc, "proc", false, "run pigeon version of peg parser and marshal into ast.Op")
f.BoolVar(&c.semantic, "s", false, "display semantically analyzed AST (implies -proc)")
Expand Down Expand Up @@ -97,9 +94,6 @@ func (c *Command) Run(args []string) error {
return charm.NeedHelp
}
c.n = 0
if c.js {
c.n++
}
if c.pigeon {
c.n++
}
Expand Down Expand Up @@ -153,14 +147,6 @@ func (c *Command) header(msg string) {
}

func (c *Command) parse(z string, lk *lake.Root) error {
if c.js {
s, err := parsePEGjs(z)
if err != nil {
return err
}
c.header("pegjs")
fmt.Println(s)
}
if c.pigeon {
s, err := parsePigeon(z)
if err != nil {
Expand Down Expand Up @@ -244,15 +230,6 @@ Failed to run node on ./compiler/parser/run.js. The "-js" flag is for PEG
development and should only be used when running "zed dev compile" in the root
directory of the Zed repository.`

func runNode(dir, line string) ([]byte, error) {
cmd := exec.Command("node", "./compiler/parser/run.js", "-e", "start")
if dir != "" {
cmd.Dir = dir
}
cmd.Stdin = strings.NewReader(line)
return cmd.Output()
}

func normalize(b []byte) (string, error) {
var v interface{}
err := json.Unmarshal(b, &v)
Expand All @@ -263,16 +240,6 @@ func normalize(b []byte) (string, error) {
return string(out), err
}

func parsePEGjs(z string) (string, error) {
b, err := runNode("", z)
if err != nil {
// parse errors don't cause this... this is only
// caused by a problem running node.
return "", errors.New(strings.TrimSpace(nodeProblem))
}
return normalize(b)
}

func astFmt(seq ast.Seq, canon bool) (string, error) {
if canon {
return zfmt.AST(seq), nil
Expand Down
27 changes: 3 additions & 24 deletions compiler/parser/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
GOIMPORTS_VERSION = v0.0.0-20200204192400-7124308813f3
PEGJS_VERSION = 0.10.0
PIGEON_VERSION = v1.0.1-0.20190520151103-9fec3898cef8
ROLLUP_PLUGIN_COMMONJS_VERSION = 14.0.0
ROLLUP_VERSION = 2.23.1
GOIMPORTS_VERSION = v0.19.0
PIGEON_VERSION = v1.2.1

# GNU cpp needs -std=gnu90 to prevent errors on \uHHHH sequences, and Clang cpp
# with -std=gnu90 needs -Wno-unicode to prevent warnings on \uHHHH sequences.
cpp = cpp -E -P -std=gnu90 -Wno-unicode
deps = $(CURDIR)/deps
npm = npm --global --prefix $(deps)

all: parser.go parser.js parser.es.js
all: parser.go

.PHONY: parser.go
parser.go:
Expand All @@ -22,20 +18,3 @@ ifeq "$(shell go version -m $(deps)/bin/pigeon 2>&1 | fgrep $(PIGEON_VERSION))"
endif
$(cpp) -DGO parser.peg | $(deps)/bin/pigeon -o $@
$(deps)/bin/goimports -w $@

.PHONY: parser.js
parser.js:
ifeq "$(shell $(deps)/bin/pegjs --version 2>&1 | fgrep $(PEGJS_VERSION))" ""
$(npm) install pegjs@$(PEGJS_VERSION)
endif
$(cpp) parser.peg | $(deps)/bin/pegjs --allowed-start-rules start,Expr -o $@

.PHONY: parser.es.js
parser.es.js: parser.js
ifeq "$(shell $(deps)/bin/rollup --version 2>&1 | fgrep $(ROLLUP_VERSION))" ""
$(npm) install rollup@$(ROLLUP_VERSION)
endif
ifeq "$(shell $(npm) list @rollup/plugin-commonjs 2>&1 | fgrep $(ROLLUP_PLUGIN_COMMONJS_VERSION))" ""
$(npm) install @rollup/plugin-commonjs@$(ROLLUP_PLUGIN_COMMONJS_VERSION)
endif
$(deps)/bin/rollup --format es --plugin commonjs $^ -o $@
28 changes: 6 additions & 22 deletions compiler/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,17 @@

This directory contains the Zed parser implemented in PEG.

There is a single PEG input file that works with both
[pigeon](https://github.com/mna/pigeon), which is Go based, and
[pegjs](https://github.com/pegjs/pegjs), which is JavaScript based. This allows us
to embed a Zed compiler into either JavaScript or Go.

The single parser file is run through the C pre-processor allowing
macro and ifdef logic to create the two variants of PEG.

## Install

You need pegjs, pigeon, and goimports to build the parsers. To install
them, run:

```
go get github.com/mna/pigeon golang.org/x/tools/cmd/goimports
npm install -g pegjs
```
There is a single PEG input file that works with
[pigeon](https://github.com/mna/pigeon) to generate the Go parser.

## Build

To build the parsers, just run make:
To build the parser, just run make:

`make`

This will run the C pre-processor to make the two PEG files and run
pigeon and pegjs to create the two parsers.
This will ensure the required libraries are installed and then produce the Go
parser (parser.go).

## Testing

Expand All @@ -41,7 +26,6 @@ is with this `make` command at the root of this repository:
```
make peg
```
This will ensure the PEG-generated JavaScript and Go parsers are up to date
with `parser.peg`
This will ensure the PEG-generated Go parser is up to date with `parser.peg`

To update the parser and launch the `zc -repl`, your can run `make peg-run`.
Loading

0 comments on commit 3be401c

Please sign in to comment.