#!/bin/bash

# ================================================================
# Reads the Miller DSL grammar file and generates Go code.
# ================================================================

set -euo pipefail

verbose="false"
if [ $# -eq 1 ]; then
  if [ "$1" == "-v" ]; then
    verbose="true"
  fi
fi

export GOPATH=$(pwd)

# Build the bin/gocc executable:
go get github.com/goccmack/gocc

rm -f src/miller/parsing/*.txt
if [ "$verbose" = "true" ]; then
  lr1="src/miller/parsing/LR1_conflicts.txt"
  bin/gocc -v -o src/miller/parsing src/miller/parsing/mlr.bnf || expand -2 $lr1
else
  bin/gocc -o src/miller/parsing src/miller/parsing/mlr.bnf
fi

# Code-gen directories:
#   src/miller/parsing/errors/
#   src/miller/parsing/lexer/
#   src/miller/parsing/parser/
#   src/miller/parsing/token/
#   src/miller/parsing/util/

# Override GOCC codegen with customized error handling
cp src/miller/parsing/errors.go.template src/miller/parsing/errors/errors.go

for x in src/miller/parsing/*/*.go; do gofmt -w $x; done
