Skip to content

Instantly share code, notes, and snippets.

@dualbus
Created October 4, 2013 06:28
Show Gist options
  • Save dualbus/6821774 to your computer and use it in GitHub Desktop.
Save dualbus/6821774 to your computer and use it in GitHub Desktop.
#!/usr/bin/awk -f
BEGIN {
state = "base";
}
{
length_of_line = length;
for (i = 1; i <= length_of_line + 1; i++) {
if (i > length_of_line) {
character = "\n";
} else {
character = substr($0, i, 1);
}
switch (state) {
case "base":
switch (character) {
case "/":
state = "first-slash";
break;
case "\"":
state = "quoted-string"
printf "%s", character
break;
default:
printf "%s", character
}
break;
case "first-slash":
switch (character) {
case "/":
state = "double-slash"
break;
case "*":
state = "comment"
break;
default:
state = "base"
printf "/%s", character
}
break;
case "double-slash":
if (character == "\n") {
state = "base"
printf "%s", character
}
break;
case "comment":
if (character == "*") {
state = "maybe-end-comment";
}
break
case "maybe-end-comment":
switch (character) {
case "/":
state = "base"
break;
case "*":
break;
default:
state = "comment"
}
break;
case "quoted-string":
switch (character) {
case "\"":
state = "base"
break;
case "\\":
state = "escape-character"
break;
default:
state = "quoted-string"
}
printf "%s", character
break;
case "escape-character":
state = "quoted-string"
printf "%s", character
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment