solidity/scripts/test_antlr_grammar.sh

158 lines
4.5 KiB
Bash
Raw Normal View History

2020-03-11 12:03:57 +00:00
#!/usr/bin/env bash
set -e
READLINK=readlink
if [[ "$OSTYPE" == "darwin"* ]]; then
READLINK=greadlink
fi
ROOT_DIR=$(${READLINK} -f "$(dirname "$0")"/..)
2020-03-11 12:03:57 +00:00
WORKDIR="${ROOT_DIR}/build/antlr"
ANTLR_JAR="${ROOT_DIR}/build/deps/antlr4.jar"
2020-08-12 01:05:53 +00:00
ANTLR_JAR_URI="https://www.antlr.org/download/antlr-4.8-complete.jar"
2020-03-11 12:03:57 +00:00
SGR_RESET="\033[0m"
SGR_BOLD="\033[1m"
SGR_GREEN="\033[32m"
SGR_RED="\033[31m"
SGR_BLUE="\033[34m"
vt_cursor_up() { echo -ne "\033[A"; }
vt_cursor_begin_of_line() { echo -ne "\r"; }
download_antlr4()
{
if [[ ! -e "$ANTLR_JAR" ]]
then
curl -o "${ANTLR_JAR}" "${ANTLR_JAR_URI}"
fi
}
prepare_workdir()
{
mkdir -p "${ROOT_DIR}/build/deps"
mkdir -p "${WORKDIR}"
mkdir -p "${WORKDIR}/src"
mkdir -p "${WORKDIR}/target"
}
prepare_workdir
download_antlr4
echo "Creating parser"
(
cd "${ROOT_DIR}"/docs/grammar
# Create lexer/parser from grammar
java -jar "${ANTLR_JAR}" SolidityParser.g4 SolidityLexer.g4 -o "${WORKDIR}/src/"
2020-03-11 12:03:57 +00:00
# Compile lexer/parser sources
javac -classpath "${ANTLR_JAR}" "${WORKDIR}/src/"*.java -d "${WORKDIR}/target/"
)
2020-03-11 12:03:57 +00:00
# Run tests
failed_count=0
test_file()
{
local SOL_FILE
SOL_FILE="$(${READLINK} -m "${1}")"
2020-03-11 12:03:57 +00:00
local cur=${2}
local max=${3}
2020-08-12 01:05:53 +00:00
local solOrYul=${4}
2020-03-11 12:03:57 +00:00
echo -e "${SGR_BLUE}[${cur}/${max}] Testing ${SOL_FILE}${SGR_RESET} ..."
local output
2020-08-12 01:05:53 +00:00
if [[ "${solOrYul}" == "sol" ]]; then
output=$(
grep -v "^==== ExternalSource:" "${SOL_FILE}" | java \
2020-08-12 01:05:53 +00:00
-classpath "${ANTLR_JAR}:${WORKDIR}/target/" \
"org.antlr.v4.gui.TestRig" \
Solidity \
sourceUnit 2>&1
2020-08-12 01:05:53 +00:00
)
else
output=$(
echo "assembly $(cat "${SOL_FILE}")" | java \
-classpath "${ANTLR_JAR}:${WORKDIR}/target/" \
"org.antlr.v4.gui.TestRig" \
Solidity \
assemblyStatement 2>&1
)
fi
2020-03-11 12:03:57 +00:00
vt_cursor_up
vt_cursor_begin_of_line
2020-08-12 01:05:53 +00:00
if grep -qE "^\/\/ ParserError" "${SOL_FILE}"; then
if [[ "${output}" != "" ]]
then
echo -e "${SGR_BLUE}[${cur}/${max}] Testing ${SOL_FILE}${SGR_RESET} ${SGR_BOLD}${SGR_GREEN}FAILED AS EXPECTED${SGR_RESET}"
else
echo -e "${SGR_BLUE}[${cur}/${max}] Testing ${SOL_FILE}${SGR_RESET} ${SGR_BOLD}${SGR_RED}SUCCEEDED DESPITE PARSER ERROR${SGR_RESET}"
echo "${output}"
failed_count=$((failed_count + 1))
exit 1
fi
2020-03-11 12:03:57 +00:00
else
2020-08-12 01:05:53 +00:00
if [[ "${output}" == "" ]]
then
echo -e "${SGR_BLUE}[${cur}/${max}] Testing ${SOL_FILE}${SGR_RESET} ${SGR_BOLD}${SGR_GREEN}OK${SGR_RESET}"
else
echo -e "${SGR_BLUE}[${cur}/${max}] Testing ${SOL_FILE}${SGR_RESET} ${SGR_BOLD}${SGR_RED}FAILED${SGR_RESET}"
echo "${output}"
failed_count=$((failed_count + 1))
exit 1
fi
2020-03-11 12:03:57 +00:00
fi
}
2020-08-12 01:05:53 +00:00
# we only want to use files that do not contain excluded parser errors, analysis errors or multi-source files.
2020-03-11 12:03:57 +00:00
SOL_FILES=()
while IFS='' read -r line
do
SOL_FILES+=("$line")
done < <(
grep --include "*.sol" -riL -E \
"^\/\/ (Syntax|Type|Declaration)Error|^\/\/ ParserError (1684|2837|3716|3997|5333|6275|6281|6933|7319)|^==== Source:" \
2020-03-11 12:03:57 +00:00
"${ROOT_DIR}/test/libsolidity/syntaxTests" \
"${ROOT_DIR}/test/libsolidity/semanticTests" |
# Skipping the unicode tests as I couldn't adapt the lexical grammar to recursively counting RLO/LRO/PDF's.
2021-03-29 13:58:16 +00:00
grep -v -E 'comments/.*_direction_override.*.sol' |
grep -v -E 'literals/.*_direction_override.*.sol' |
# Skipping a test with "revert E;" because ANTLR cannot distinguish it from
# a variable declaration.
grep -v -E 'revertStatement/non_called.sol' |
# Skipping a test with "let basefee := ..."
grep -v -E 'inlineAssembly/basefee_berlin_function.sol' |
# Skipping license error, unrelated to the grammar
grep -v -E 'license/license_double5.sol' |
grep -v -E 'license/license_hidden_unicode.sol' |
grep -v -E 'license/license_unicode.sol' |
# Skipping tests with 'something.address' as 'address' as the grammar fails on those
grep -v -E 'inlineAssembly/external_function_pointer_address.*.sol'
2020-03-11 12:03:57 +00:00
)
2020-08-12 01:05:53 +00:00
YUL_FILES=()
# Add all yul optimizer tests without objects and types.
while IFS='' read -r line
do
YUL_FILES+=("$line")
done < <(
grep -riL -E \
"object|\:[ ]*[uib]" \
"${ROOT_DIR}/test/libyul/yulOptimizerTests"
)
num_tests=$((${#SOL_FILES[*]} + ${#YUL_FILES[*]}))
2020-03-11 12:03:57 +00:00
test_count=0
for SOL_FILE in "${SOL_FILES[@]}"
do
test_count=$((test_count + 1))
2020-08-12 01:05:53 +00:00
test_file "${SOL_FILE}" ${test_count} $num_tests "sol"
done
for YUL_FILE in "${YUL_FILES[@]}"
do
test_count=$((test_count + 1))
test_file "${YUL_FILE}" ${test_count} $num_tests "yul"
2020-03-11 12:03:57 +00:00
done
2020-08-12 01:05:53 +00:00
echo "Summary: ${failed_count} of $num_tests sources failed."
2020-03-11 12:03:57 +00:00
exit ${failed_count}