Waddle_Language/scripts/flatten.sh

60 lines
1.3 KiB
Bash
Raw Normal View History

#!/bin/bash
# Check if input file is provided
#if [ $# -eq 0 ]; then
# echo "Usage: $0 <input_file> [output_file]"
# exit 1
#fi
# Input and output file names
INPUT_FILE="./working/waddle.bnf"
OUTPUT_FILE="./working/waddle_flattened.bnf"
# Flatten multi-line BNF definitions
awk '
BEGIN {
in_definition = 0
current_line = ""
}
/^[[:space:]]*$/ {
# Empty line
if (in_definition) {
# Remove leading/trailing whitespace and collapse internal whitespace
gsub(/^[[:space:]]+|[[:space:]]+$/, "", current_line)
gsub(/[[:space:]]+/, " ", current_line)
print current_line
current_line = ""
in_definition = 0
}
next
}
/^[[:space:]]*import/ {
# Print import line as-is
print $0
next
}
{
in_definition = 1
# Append current line to definition, removing leading/trailing whitespace
if (current_line == "") {
current_line = $0
} else {
current_line = current_line " " $0
}
}
END {
# Handle last definition if exists
if (in_definition) {
gsub(/^[[:space:]]+|[[:space:]]+$/, "", current_line)
gsub(/[[:space:]]+/, " ", current_line)
print current_line
}
}
' "$INPUT_FILE" > "$OUTPUT_FILE"
echo "Flattened BNF saved to $OUTPUT_FILE"