Skip to content

Commit

Permalink
Implement inline comment rule, make tests a bit more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
deuill committed Feb 28, 2016
1 parent 9a7431b commit 27c845a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
75 changes: 62 additions & 13 deletions fawkss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Built-in functions
# ------------------

# This block contains global helper functions, used across different rules, as
# defined below.

# 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) {
Expand All @@ -25,12 +28,16 @@ function trim(str) {
return substr(str, RSTART, RLENGTH)
}

# ----------------------
# Pattern matching rules
# ----------------------
# ----------------
# Rule definitions
# ----------------

# This block contains rule definitions used across Fawkss. A rule is defined as
# an exclusive match against a single line which always contines on to the next
# line. As such, rules are not composable.

# Match variable definitions.
$0 ~ /\$[a-zA-Z0-9_]+[ ]*:/ {
# Variable definition rule.
$0 ~ /^[ ]*\$[a-zA-Z0-9_]+[ ]*:/ {
# Split text in tokens.
split($0, token, ":")

Expand All @@ -40,17 +47,48 @@ $0 ~ /\$[a-zA-Z0-9_]+[ ]*:/ {

# Assign variable to global variables table.
variables[name] = value

next
}

# ------------
# File parsing
# ------------
# -----------------
# Line manipulation
# -----------------

# This block contains line manipulation rules which modify lines to be printed.

# Match inline comments.
$0 ~ /\/\// {
# Remove any special cases from the line.
while (match($0, /['"][^\/\/]*\/\/[^'"]*['"]/)) {
special[len += 1] = RSTART ":" substr($0, RSTART, RLENGTH)
$0 = substr($0, 0, RSTART - 1) substr($0, RSTART + RLENGTH, length($0))
}

# Remove inline comments from line.
while (match($0, /[ ]*\/\/.*$/)) {
$0 = substr($0, 0, RSTART - 1) substr($0, RSTART + RLENGTH, length($0))
}

# Reinsert special cases in their predefined positions.
for (i = len; i != 0; i--) {
pos = substr(special[i], 0, index(special[i], ":") - 1)

# Do not attempt to reinsert special case string if string has been
# truncated to less the original position of the string.
if (pos > length($0)) {
continue
}

str = substr(special[i], index(special[i], ":") + 1, length(special[i]))
$0 = substr($0, 0, pos - 1) str substr($0, pos, length($0))
}

len = 0
}

# Match variable uses.
$0 ~ /\$[a-zA-Z0-9_]+/ {
# Replace each variable used with it's concrete value.
$0 ~ /^.+:[ ]*\$[a-zA-Z0-9_]+[ ]*/ {
# Replace each variable used with its concrete value.
while (match($0, /\$[a-zA-Z0-9_]+/)) {
name = substr($0, RSTART, RLENGTH)

Expand All @@ -62,12 +100,23 @@ $0 ~ /\$[a-zA-Z0-9_]+/ {

$0 = substr($0, 0, RSTART - 1) variables[name] substr($0, RSTART + RLENGTH, length($0))
}
}

# --------
# Printing
# --------

# Do not print more than two consecutive newlines.
!NF && newlines += 1 {
if (newlines < 2) {
print
}

print
next
}

# All other non-matching lines are printed out as-is.
# Print non-blank line and reset newline count.
{
newlines = 0
print
}
5 changes: 4 additions & 1 deletion spec/01-comments.fkss
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
// But this won't.

:root {} // This rule should appear, but the comment shouldn't.
.test::before{content:'// This should appear here.'} // But not this.
.test::after{content:"// Also this.", url: "//trip//me"} // You "get" the point.

--- EXPECTED ---

/**
* This will appear in the final source code.
*/


:root {}
.test::before{content:'// This should appear here.'}
.test::after{content:"// Also this.", url: "//trip//me"}

--- END ---
7 changes: 2 additions & 5 deletions spec/02-variables.fkss
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ $__height1:10px;

.test-element {
color:$12color;
height: $__height1;
width: $width;
height: $__height1; width: $width;
}

--- EXPECTED ---


.test-element {
color:black;
height: 10px;
width: 100%;
height: 10px; width: 100%;
}

--- END ---

0 comments on commit 27c845a

Please sign in to comment.