mirror of https://github.com/deuill/fawkss.git
Complete work on simple variable definitions.
This commit is contained in:
parent
0a43eddd20
commit
9a7431b232
65
fawkss
65
fawkss
|
@ -3,6 +3,71 @@
|
|||
# Fawkss is a CSS preprocessor for people who dislike CSS preprocessors. It
|
||||
# implements a subset of the SCSS syntax while remaining relatively simple.
|
||||
|
||||
# ------------------
|
||||
# Built-in functions
|
||||
# ------------------
|
||||
|
||||
# Returns the index of the last occurence of a substring `s` in `str`, or -1
|
||||
# if the substring was not found.
|
||||
function lastindex(str, s) {
|
||||
match(str, ".*" s)
|
||||
return max(RSTART + RLENGTH - 1, -1)
|
||||
}
|
||||
|
||||
# Finds and returns greatest between two numbers.
|
||||
function max(x, y) {
|
||||
return (x > y) ? x : y
|
||||
}
|
||||
|
||||
# Trims spaces off both ends of a string.
|
||||
function trim(str) {
|
||||
match(str, /[:space:]*[^[:space:]]+[:space:]*/)
|
||||
return substr(str, RSTART, RLENGTH)
|
||||
}
|
||||
|
||||
# ----------------------
|
||||
# Pattern matching rules
|
||||
# ----------------------
|
||||
|
||||
# Match variable definitions.
|
||||
$0 ~ /\$[a-zA-Z0-9_]+[ ]*:/ {
|
||||
# Split text in tokens.
|
||||
split($0, token, ":")
|
||||
|
||||
# Get variable name and value.
|
||||
name = trim(substr(token[1], index(token[1], "$")))
|
||||
value = trim(substr(token[2], 0, lastindex(token[2], ";") - 1))
|
||||
|
||||
# Assign variable to global variables table.
|
||||
variables[name] = value
|
||||
|
||||
next
|
||||
}
|
||||
|
||||
# ------------
|
||||
# File parsing
|
||||
# ------------
|
||||
|
||||
# Match variable uses.
|
||||
$0 ~ /\$[a-zA-Z0-9_]+/ {
|
||||
# Replace each variable used with it's concrete value.
|
||||
while (match($0, /\$[a-zA-Z0-9_]+/)) {
|
||||
name = substr($0, RSTART, RLENGTH)
|
||||
|
||||
# Throw error and exit if variable used has not been declared.
|
||||
if (variables[name] == "") {
|
||||
printf "ERROR: Use of undeclared variable '%s' on line %d\n", name, NR | "cat >&2"
|
||||
exit
|
||||
}
|
||||
|
||||
$0 = substr($0, 0, RSTART - 1) variables[name] substr($0, RSTART + RLENGTH, length($0))
|
||||
}
|
||||
|
||||
print
|
||||
next
|
||||
}
|
||||
|
||||
# All other non-matching lines are printed out as-is.
|
||||
{
|
||||
print
|
||||
}
|
|
@ -18,15 +18,28 @@ COLOR_RED="\033[31m"
|
|||
COLOR_GREEN="\033[32m"
|
||||
COLOR_RESET="\033[0m"
|
||||
|
||||
# Initialize summary count array.
|
||||
SUMMARY=(0 0 0)
|
||||
|
||||
# Helpers for extracting sections of test files.
|
||||
read -d '' SECTION_TEST <<-'_EOF_'
|
||||
/--- TEST ---/ { test = 1; next }
|
||||
/--- EXPECTED ---/ { exit }
|
||||
test { print }
|
||||
_EOF_
|
||||
|
||||
read -d '' SECTION_EXPECTED <<-'_EOF_'
|
||||
/--- EXPECTED ---/ { expected = 1; next }
|
||||
/--- END ---/ { exit }
|
||||
expected { print }
|
||||
_EOF_
|
||||
|
||||
# --------------
|
||||
# TEST EXECUTION
|
||||
# --------------
|
||||
|
||||
echo -e ">> ${COLOR_WHITE}Executing tests...${COLOR_RESET}"
|
||||
|
||||
# Initialize summary count array.
|
||||
summary=(0 0 0)
|
||||
|
||||
for file in ${@:-${ROOT}/spec/*.fkss}
|
||||
do
|
||||
# Check if file exists and is readable.
|
||||
|
@ -37,27 +50,10 @@ do
|
|||
fi
|
||||
|
||||
# Increase count of total tests run.
|
||||
summary[0]=$((${summary[0]} + 1))
|
||||
|
||||
# Compile test file and get actual and expected output.
|
||||
read -d '' generate_actual <<-'_EOF_'
|
||||
/--- TEST ---/ { test=1; next }
|
||||
/--- EXPECTED ---/ { print "x"; exit }
|
||||
test { print }
|
||||
_EOF_
|
||||
|
||||
actual=$(awk "${generate_actual}" "${file}")
|
||||
|
||||
read -d '' generate_expected <<-'_EOF_'
|
||||
/--- EXPECTED ---/ { expected=1; next }
|
||||
/--- END ---/ { print "x"; exit }
|
||||
expected { print }
|
||||
_EOF_
|
||||
|
||||
expected=$(awk "${generate_expected}" "${file}")
|
||||
SUMMARY[0]=$((${SUMMARY[0]} + 1))
|
||||
|
||||
# Get diff between expected and actual output.
|
||||
result=$(colordiff -ud <(echo "${expected%x}") <(echo "${actual%x}") | tail -n +3)
|
||||
result=$(colordiff -ud <(awk "${SECTION_EXPECTED}" "${file}") <(exec ${FAWKSS_SCRIPT} <(awk "${SECTION_TEST}" "${file}")) | tail -n +3)
|
||||
|
||||
# Echo per-test results.
|
||||
echo -ne ">> ${COLOR_WHITE}Testing file '$(basename ${file})'...${COLOR_RESET} "
|
||||
|
@ -65,13 +61,13 @@ do
|
|||
if [ "x${result}" != "x" ]
|
||||
then
|
||||
# Increase total of failed tests run.
|
||||
summary[1]=$((${summary[1]} + 1))
|
||||
SUMMARY[1]=$((${SUMMARY[1]} + 1))
|
||||
|
||||
echo -e "${COLOR_RED}FAIL${COLOR_RESET}"
|
||||
echo "${result}"
|
||||
else
|
||||
# Increase total of successful tests run.
|
||||
summary[2]=$((${summary[2]} + 1))
|
||||
SUMMARY[2]=$((${SUMMARY[2]} + 1))
|
||||
|
||||
echo -e "${COLOR_GREEN}OK${COLOR_RESET}"
|
||||
fi
|
||||
|
@ -81,6 +77,6 @@ done
|
|||
|
||||
# Echo total results.
|
||||
echo -ne ">> ${COLOR_WHITE}Summary:${COLOR_RESET} "
|
||||
echo -ne "Total ${COLOR_WHITE}${summary[0]}${COLOR_RESET}, "
|
||||
echo -ne "Failed ${COLOR_RED}${summary[1]}${COLOR_RESET}, "
|
||||
echo -e "Successful ${COLOR_GREEN}${summary[2]}${COLOR_RESET}"
|
||||
echo -ne "Total ${COLOR_WHITE}${SUMMARY[0]}${COLOR_RESET}, "
|
||||
echo -ne "Failed ${COLOR_RED}${SUMMARY[1]}${COLOR_RESET}, "
|
||||
echo -e "Successful ${COLOR_GREEN}${SUMMARY[2]}${COLOR_RESET}"
|
|
@ -5,15 +5,18 @@
|
|||
--- TEST ---
|
||||
|
||||
/**
|
||||
* This should not appear in the final source code.
|
||||
* This will appear in the final source code.
|
||||
*/
|
||||
|
||||
// Neither should this.
|
||||
// But this won't.
|
||||
|
||||
:root {} // This rule should appear, but the comment shouldn't.
|
||||
|
||||
--- EXPECTED ---
|
||||
|
||||
/**
|
||||
* This will appear in the final source code.
|
||||
*/
|
||||
|
||||
|
||||
:root {}
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
|
||||
--- TEST ---
|
||||
|
||||
$width: 100%;
|
||||
$__height1: 10px;
|
||||
$12color: black;
|
||||
$width : 100%;
|
||||
$__height1:10px;
|
||||
$12color: black;
|
||||
|
||||
.test-element {
|
||||
color: $12color;
|
||||
height: $__height1;
|
||||
color:$12color;
|
||||
height: $__height1;
|
||||
width: $width;
|
||||
}
|
||||
|
||||
|
@ -18,8 +18,8 @@ $12color: black;
|
|||
|
||||
|
||||
.test-element {
|
||||
color: black;
|
||||
height: 10px;
|
||||
color:black;
|
||||
height: 10px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue