forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_antlr_grammar.sh
executable file
·147 lines (132 loc) · 3.75 KB
/
test_antlr_grammar.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
set -e
READLINK=readlink
if [[ "$OSTYPE" == "darwin"* ]]; then
READLINK=greadlink
fi
ROOT_DIR=$(${READLINK} -f "$(dirname "$0")"/..)
WORKDIR="${ROOT_DIR}/build/antlr"
ANTLR_JAR="${ROOT_DIR}/build/deps/antlr4.jar"
ANTLR_JAR_URI="https://www.antlr.org/download/antlr-4.8-complete.jar"
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
if [[ ! -f "${WORKDIR}/target/SolidityParser.class" ]] || \
[ "${GRAMMAR_FILE}" -nt "${WORKDIR}/target/SolidityParser.class" ]
then
echo "Creating parser"
(
cd "${ROOT_DIR}"/docs/grammar
# Create lexer/parser from grammar
java -jar "${ANTLR_JAR}" Solidity.g4 SolidityLexer.g4 -o "${WORKDIR}/src/"
# Compile lexer/parser sources
javac -classpath "${ANTLR_JAR}" "${WORKDIR}/src/"*.java -d "${WORKDIR}/target/"
)
fi
# Run tests
failed_count=0
test_file()
{
local SOL_FILE
SOL_FILE="$(${READLINK} -m "${1}")"
local cur=${2}
local max=${3}
local solOrYul=${4}
echo -e "${SGR_BLUE}[${cur}/${max}] Testing ${SOL_FILE}${SGR_RESET} ..."
local output
if [[ "${solOrYul}" == "sol" ]]; then
output=$(
java \
-classpath "${ANTLR_JAR}:${WORKDIR}/target/" \
"org.antlr.v4.gui.TestRig" \
Solidity \
sourceUnit <"${SOL_FILE}" 2>&1
)
else
output=$(
echo "assembly $(cat "${SOL_FILE}")" | java \
-classpath "${ANTLR_JAR}:${WORKDIR}/target/" \
"org.antlr.v4.gui.TestRig" \
Solidity \
assemblyStatement 2>&1
)
fi
vt_cursor_up
vt_cursor_begin_of_line
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
else
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
fi
}
# we only want to use files that do not contain excluded parser errors, analysis errors or multi-source files.
SOL_FILES=()
while IFS='' read -r line
do
SOL_FILES+=("$line")
done < <(
grep -riL -E \
"^\/\/ (Syntax|Type|Declaration)Error|^\/\/ ParserError (2837|3716|3997|5333|6275|6281|6933|7319)|^==== Source:" \
"${ROOT_DIR}/test/libsolidity/syntaxTests" \
"${ROOT_DIR}/test/libsolidity/semanticTests" \
)
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[*]}))
test_count=0
for SOL_FILE in "${SOL_FILES[@]}"
do
test_count=$((test_count + 1))
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"
done
echo "Summary: ${failed_count} of $num_tests sources failed."
exit ${failed_count}