From 7020e1a5091402e2a63d237ef038745d30a1569b Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Sat, 23 Feb 2019 11:31:06 -0800 Subject: [PATCH] Add some tooling to bring in Antlr4 for eventually parsing .otto files The thoughts are still swirling around in my head a bit, but given the current targets that Antlr4 supports, I fear that .otto file parsing will need to be handled in JavaScript or C++. --- Makefile | 42 ++++++++++++++++++++++++++++++++++++++++++ Otto.g4 | 4 ++++ build/.gitignore | 1 + contrib/.gitignore | 1 + scripts/prereqs.sh | 29 +++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 Makefile create mode 100644 Otto.g4 create mode 100644 build/.gitignore create mode 100644 contrib/.gitignore create mode 100644 scripts/prereqs.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c71bd94 --- /dev/null +++ b/Makefile @@ -0,0 +1,42 @@ + + +ANTLR_BIN=antlr-4.7.2-complete.jar +ANTLR=contrib/$(ANTLR_BIN) +GRAMMAR=Otto.g4 +################################################################################ +## Phony targets +all: help + +check: ## Run validation tests + +depends: prereqs $(ANTLR) ## Download all dependencies + +prereqs: scripts/prereqs.sh ## Check that this system has the necessary tools to build otto + @sh scripts/prereqs.sh + +clean: ## Clean all temporary/working files + rm -f $(ANTLR) + + +parser: depends $(GRAMMAR) ## Generate the parser code + @for target in JavaScript Go Cpp; do \ + java -cp $(ANTLR) org.antlr.v4.Tool \ + -Dlanguage=$$target \ + -o build/$$target \ + $(GRAMMAR); \ + echo "--> Generated $$target stubs"; \ + done; + +################################################################################ +## Non-phony targets +$(ANTLR): ## Download the latest ANTLR4 binary + (cd contrib && wget https://www.antlr.org/download/$(ANTLR_BIN)) + +################################################################################ + +# Cute hack thanks to: +# https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html +help: ## Display this help text + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +.PHONY: all check clean depends diff --git a/Otto.g4 b/Otto.g4 new file mode 100644 index 0000000..5ddb133 --- /dev/null +++ b/Otto.g4 @@ -0,0 +1,4 @@ +grammar Otto; +r : 'hello' ID ; // match keyword hello followed by an identifier +ID : [a-z]+ ; // match lower-case identifiers +WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/build/.gitignore @@ -0,0 +1 @@ +* diff --git a/contrib/.gitignore b/contrib/.gitignore new file mode 100644 index 0000000..d392f0e --- /dev/null +++ b/contrib/.gitignore @@ -0,0 +1 @@ +*.jar diff --git a/scripts/prereqs.sh b/scripts/prereqs.sh new file mode 100644 index 0000000..6c4567f --- /dev/null +++ b/scripts/prereqs.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +err() { + printf '\e[48;5;%dm' 196 + echo -n ">> ${1}" + printf '\e[0m \n' +} +ok() { + printf '\e[48;5;%dm' 034 + echo -n ">> ${1}" + printf '\e[0m \n' +} + +which wget &> /dev/null +if [ $? -ne 0 ]; then + err "Please ensure that \`wget\` is in your PATH" + exit 1; +else + ok "wget is present" +fi; + +which java &> /dev/null +if [ $? -ne 0 ]; then + err "Please ensure that \`java\` is in your PATH" + exit 1; +else + ok "Java is present" +fi; +