miller/tools/build-dsl
John Kerl 268a96d002
Export library code in pkg/ (#1391)
* Export library code in `pkg/`

* new doc page
2023-09-10 17:15:13 -04:00

60 lines
1.7 KiB
Bash
Executable file

#!/bin/bash
# ================================================================
# Reads the Miller DSL grammar file and generates Go code.
#
# This is not run on every build / commit / etc.
#
# It's intended to be run manually by the developer, as needed when mlr.bnf
# changes for example.
#
# Resulting auto-generated .go files should then be checked into source control.
#
# With verbose, *.txt files are created with information about LR1 conflicts
# etc. Please don't commit them.
#
# As of mid-2021 this takes easily 5-10 minutes to run.
# ================================================================
set -euo pipefail
verbose="true"
if [ $# -eq 1 ]; then
if [ "$1" == "-v" ]; then
verbose="true"
elif [ "$1" == "-q" ]; then
verbose="true"
fi
fi
# Build the bin/gocc executable:
go install github.com/goccmack/gocc
go mod tidy
bingocc="$HOME/go/bin/gocc"
if [ ! -x "$bingocc" ]; then
exit 1
fi
rm -f pkg/parsing/*.txt
if [ "$verbose" = "true" ]; then
lr1="pkg/parsing/LR1_conflicts.txt"
$bingocc -v -o ./pkg/parsing -p mlr/pkg/parsing pkg/parsing/mlr.bnf || expand -2 $lr1
else
$bingocc -o ./pkg/parsing -p mlr/pkg/parsing pkg/parsing/mlr.bnf
fi
# Code-gen directories:
# pkg/parsing/errors/
# pkg/parsing/lexer/
# pkg/parsing/parser/
# pkg/parsing/token/
# pkg/parsing/util/
# Override GOCC codegen with customized error handling
cp pkg/parsing/errors.go.template pkg/parsing/errors/errors.go
# We might need a manual replace of os.ReadFile by ioutil.ReadFile in autogen code. Note we don't
# use latest-and-greatest Go compiler version in our go.mod since we want to build on Centos which
# can be trailing-edge in that regard.
for x in pkg/parsing/*/*.go; do gofmt -w $x; done