-
Notifications
You must be signed in to change notification settings - Fork 3
285 lines (245 loc) · 10.7 KB
/
antlr-lint.yml
File metadata and controls
285 lines (245 loc) · 10.7 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
name: ANTLR Grammar Lint
# This workflow runs the antlr-v4-linter on all ANTLR grammar files (.g4)
# to ensure they follow best practices and coding standards.
#
# The linter will check for:
# - Naming conventions (rules, tokens, labels)
# - Grammar complexity issues
# - Documentation requirements
# - Performance optimizations
# - Syntax and structural problems
# - Token management best practices
on:
# Trigger on push events that modify grammar files (only on main/master branch)
push:
branches:
- main
- master
paths:
- '**/*.g4'
- '.github/workflows/antlr-lint.yml' # Re-run if workflow itself changes
# Trigger on pull requests that modify grammar files
pull_request:
paths:
- '**/*.g4'
- '.github/workflows/antlr-lint.yml'
# Allow manual triggering from Actions tab
workflow_dispatch:
inputs:
verbose:
description: 'Enable verbose output'
required: false
default: 'false'
type: choice
options:
- 'true'
- 'false'
jobs:
lint-grammars:
name: Lint ANTLR Grammars
runs-on: ubuntu-latest
# Define the dialects to check
# Add new dialects here as they are added to the repository
strategy:
matrix:
include:
- dialect: redshift
path: redshift
# Future dialects can be added like:
# - dialect: postgres
# path: postgres
# - dialect: mysql
# path: mysql
steps:
# Step 1: Checkout the parser repository containing grammar files
- name: 📥 Checkout parser repository
uses: actions/checkout@v4
with:
path: parser
# Step 2: Checkout the antlr-v4-linter tool repository
- name: 📥 Checkout antlr-v4-linter tool
uses: actions/checkout@v4
with:
repository: bytebase/antlr-v4-linter
path: antlr-v4-linter
# Step 3: Set up Python environment
- name: 🐍 Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
cache: 'pip'
cache-dependency-path: 'antlr-v4-linter/pyproject.toml'
# Step 4: Install the antlr-v4-linter tool
- name: 📦 Install antlr-v4-linter
run: |
echo "Installing antlr-v4-linter and its dependencies..."
cd antlr-v4-linter
# Install in editable mode for development
pip install -e .
# Verify installation was successful
echo "Verifying installation..."
which antlr-lint
antlr-lint --version || echo "Version command not available"
# Show available commands
echo "Available commands:"
antlr-lint --help
# Step 5: Create or check for configuration file
- name: ⚙️ Setup linter configuration
working-directory: parser
run: |
# Check if a custom configuration exists for this dialect
if [ -f "${{ matrix.path }}/antlr-lint.json" ]; then
echo "✅ Found custom configuration for ${{ matrix.dialect }}"
echo "CONFIG_FILE=${{ matrix.path }}/antlr-lint.json" >> $GITHUB_ENV
elif [ -f "antlr-lint.json" ]; then
echo "✅ Found global configuration"
echo "CONFIG_FILE=antlr-lint.json" >> $GITHUB_ENV
else
echo "ℹ️ No configuration found, using defaults"
echo "CONFIG_FILE=" >> $GITHUB_ENV
fi
# Step 6: Run the linter on grammar files with detailed output
- name: 🔍 Lint ${{ matrix.dialect }} grammar files
working-directory: parser
run: |
echo "========================================="
echo "Linting ANTLR grammar files for: ${{ matrix.dialect }}"
echo "Path: ${{ matrix.path }}"
echo "========================================="
echo ""
# Initialize counters
total_files=0
failed_files=0
passed_files=0
# Create a temporary file to store all issues
issues_file=$(mktemp)
# Process each .g4 file
for file in $(find ${{ matrix.path }} -name "*.g4" -type f | sort); do
total_files=$((total_files + 1))
echo "📄 Checking: $file"
echo "----------------------------------------"
# Prepare config option if config file exists
config_opt=""
if [ -n "$CONFIG_FILE" ]; then
config_opt="--config $CONFIG_FILE"
fi
# Add verbose flag if requested
verbose_opt=""
if [ "${{ github.event.inputs.verbose }}" = "true" ]; then
verbose_opt="--verbose"
fi
# Run linter and capture output
output_file=$(mktemp)
antlr-lint lint $verbose_opt $config_opt "$file" 2>&1 | tee "$output_file"
# Check if there are any errors or warnings in the output
if grep -E "ERROR|WARNING" "$output_file" > /dev/null; then
echo "❌ FAILED: Issues detected"
failed_files=$((failed_files + 1))
# Parse the output for GitHub annotations
# Look for the table format with Location, Severity, Rule, and Message
while IFS= read -r line; do
# Look for lines with the format: │ 827:1 │ ERROR │ S001 │ Message...
if echo "$line" | grep -E "^│ [0-9]+:[0-9]+" > /dev/null; then
# Extract location (line:column)
location=$(echo "$line" | sed -n 's/^│ *\([0-9]*:[0-9]*\).*/\1/p' | tr -d ' ')
line_num=$(echo "$location" | cut -d: -f1)
col_num=$(echo "$location" | cut -d: -f2)
# Extract severity
if echo "$line" | grep -i "ERROR" > /dev/null; then
severity="error"
elif echo "$line" | grep -i "WARNING" > /dev/null; then
severity="warning"
else
severity="notice"
fi
# Extract rule code
rule=$(echo "$line" | sed -n 's/.*│ *\([A-Z][0-9]*\) *│.*/\1/p')
# Extract message - everything after the rule code
message=$(echo "$line" | sed -n 's/.*│ [A-Z][0-9]* *│ *\(.*\) *│$/\1/p' | sed 's/ *$//')
# Output GitHub annotation
if [ -n "$col_num" ] && [ "$col_num" != "1" ]; then
echo "::${severity} file=${file},line=${line_num},col=${col_num}::[$rule] ${message}"
else
echo "::${severity} file=${file},line=${line_num}::[$rule] ${message}"
fi
# Store for summary
echo "${severity}: ${file}:${line_num} - [$rule] ${message}" >> "$issues_file"
fi
done < "$output_file"
# If no specific line annotations were found, create a general file-level annotation
if ! grep -q "::" "$output_file"; then
echo "::error file=${file}::ANTLR grammar linting failed. Check the workflow logs for details."
fi
else
echo "✅ PASSED: No issues found"
passed_files=$((passed_files + 1))
fi
rm -f "$output_file"
echo ""
done
# Summary statistics
echo "========================================="
echo "📊 Linting Summary for ${{ matrix.dialect }}"
echo "========================================="
echo "Total files checked: ${total_files}"
echo "✅ Passed: ${passed_files}"
echo "❌ Failed: ${failed_files}"
echo ""
# If there were failures, show a summary of issues
if [ $failed_files -gt 0 ]; then
echo "📋 Issues Summary:"
echo "----------------------------------------"
cat "$issues_file" | sort | uniq
echo ""
echo "❌ Grammar linting failed for ${{ matrix.dialect }}"
echo "Please fix the issues above and try again."
rm -f "$issues_file"
exit 1
else
echo "✅ All grammar files for ${{ matrix.dialect }} passed linting!"
rm -f "$issues_file"
fi
# Step 7: Upload linter results as artifacts (useful for debugging)
- name: 📤 Upload linting results
if: failure()
uses: actions/upload-artifact@v4
with:
name: linting-results-${{ matrix.dialect }}
path: |
parser/${{ matrix.path }}/*.g4
parser/antlr-lint.json
parser/${{ matrix.path }}/antlr-lint.json
retention-days: 7
# Step 8: Create job summary
- name: 📝 Create job summary
if: always()
working-directory: parser
run: |
echo "# 🔍 ANTLR Grammar Lint Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "## Dialect: \`${{ matrix.dialect }}\`" >> $GITHUB_STEP_SUMMARY
echo "**Path:** \`${{ matrix.path }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### 📄 Grammar Files Checked:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| File | Status |" >> $GITHUB_STEP_SUMMARY
echo "|------|--------|" >> $GITHUB_STEP_SUMMARY
# Get config file path from environment
config_opt=""
if [ -f "${{ matrix.path }}/antlr-lint.json" ]; then
config_opt="--config ${{ matrix.path }}/antlr-lint.json"
elif [ -f "antlr-lint.json" ]; then
config_opt="--config antlr-lint.json"
fi
for file in $(find ${{ matrix.path }} -name "*.g4" -type f | sort); do
# Check if this file had issues (ignore exit code)
output=$(antlr-lint lint $config_opt "$file" 2>&1 || true)
if echo "$output" | grep -E "ERROR|WARNING" > /dev/null; then
echo "| \`${file}\` | ❌ Failed |" >> $GITHUB_STEP_SUMMARY
else
echo "| \`${file}\` | ✅ Passed |" >> $GITHUB_STEP_SUMMARY
fi
done
echo "" >> $GITHUB_STEP_SUMMARY
echo "---" >> $GITHUB_STEP_SUMMARY
echo "*Workflow run at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')*" >> $GITHUB_STEP_SUMMARY