parent
6918ff603f
commit
42ebc151c3
1 changed files with 163 additions and 145 deletions
308
sh/core/code.awk
308
sh/core/code.awk
|
@ -1,5 +1,7 @@
|
||||||
# code
|
# code
|
||||||
|
|
||||||
|
# functions
|
||||||
|
|
||||||
function alias_eval(alias, name, type) {
|
function alias_eval(alias, name, type) {
|
||||||
text = alias "() { "
|
text = alias "() { "
|
||||||
if (type == "function") {
|
if (type == "function") {
|
||||||
|
@ -62,176 +64,192 @@ function trim(string) {
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# begin
|
||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
RE_ANY = "(.*)"
|
|
||||||
RE_BEGIN = "^"
|
|
||||||
RE_CONST = "([_A-Z][_0-9A-Z]*)"
|
|
||||||
RE_SET = "=.*"
|
|
||||||
RE_SPACE = "[[:space:]]"
|
|
||||||
RE_TSK = "(FIXME|TODO)"
|
|
||||||
RE_VAR = "([_a-z][_0-9a-z]*)"
|
|
||||||
|
|
||||||
RE_SPACES = RE_SPACE "*"
|
RE_ANY = "(.*)"
|
||||||
|
RE_BEGIN = "^"
|
||||||
|
RE_CONST = "([_A-Z][_0-9A-Z]*)"
|
||||||
|
RE_SET = "=.*"
|
||||||
|
RE_SPACE = "[[:space:]]"
|
||||||
|
RE_TSK = "(FIXME|TODO)"
|
||||||
|
RE_VAR = "([_a-z][_0-9a-z]*)"
|
||||||
|
|
||||||
RE_END = RE_SPACES "$"
|
RE_SPACES = RE_SPACE "*"
|
||||||
RE_FUNC = RE_SPACES "\\(" RE_SPACES "\\)" RE_SPACES "{"
|
|
||||||
|
|
||||||
re["alias"] = RE_BEGIN "#=" RE_SPACES RE_VAR RE_END
|
RE_END = RE_SPACES "$"
|
||||||
re["binary"] = RE_BEGIN "#\\|" RE_SPACES RE_VAR RE_END
|
RE_FUNC = RE_SPACES "\\(" RE_SPACES "\\)" RE_SPACES "{"
|
||||||
RE_CLOSE = RE_BEGIN "}" RE_SPACES RE_END
|
|
||||||
re["command"] = RE_BEGIN "#/" RE_SPACES RE_VAR RE_END
|
re["alias"] = RE_BEGIN "#=" RE_SPACES RE_VAR RE_END
|
||||||
RE_COMMENT = RE_BEGIN "#" RE_ANY RE_END
|
re["binary"] = RE_BEGIN "#\\|" RE_SPACES RE_VAR RE_END
|
||||||
re["constant"] = RE_BEGIN RE_CONST RE_SET RE_END
|
RE_CLOSE = RE_BEGIN "}" RE_SPACES RE_END
|
||||||
RE_DOC = RE_BEGIN RE_SPACES "#" RE_SPACE RE_ANY RE_END
|
re["command"] = RE_BEGIN "#/" RE_SPACES RE_VAR RE_END
|
||||||
re["function"] = RE_BEGIN RE_VAR RE_FUNC RE_END
|
RE_COMMENT = RE_BEGIN "#" RE_ANY RE_END
|
||||||
RE_MODULE = RE_BEGIN "#\\." RE_SPACES RE_ANY RE_END
|
re["constant"] = RE_BEGIN RE_CONST RE_SET RE_END
|
||||||
RE_SHEBANG = RE_BEGIN "#!" RE_SPACES RE_ANY RE_END
|
RE_DOC = RE_BEGIN RE_SPACES "#" RE_SPACE RE_ANY RE_END
|
||||||
RE_TASK = RE_BEGIN RE_SPACES "#" RE_SPACES RE_TSK RE_ANY RE_END
|
re["function"] = RE_BEGIN RE_VAR RE_FUNC RE_END
|
||||||
re["variable"] = RE_BEGIN RE_VAR RE_SET RE_END
|
RE_MODULE = RE_BEGIN "#\\." RE_SPACES RE_ANY RE_END
|
||||||
|
RE_SHEBANG = RE_BEGIN "#!" RE_SPACES RE_ANY RE_END
|
||||||
|
RE_TASK = RE_BEGIN RE_SPACES "#" RE_SPACES RE_TSK RE_ANY RE_END
|
||||||
|
re["variable"] = RE_BEGIN RE_VAR RE_SET RE_END
|
||||||
|
|
||||||
|
f = ""
|
||||||
|
match_alias = 0
|
||||||
|
match_command = 0
|
||||||
|
match_module = 0
|
||||||
|
match_task = 0
|
||||||
|
reset()
|
||||||
|
|
||||||
f = ""
|
|
||||||
match_alias = 0
|
|
||||||
match_command = 0
|
|
||||||
match_module = 0
|
|
||||||
match_task = 0
|
|
||||||
reset()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# main
|
||||||
|
|
||||||
{
|
{
|
||||||
if (action == "filter") {
|
|
||||||
if (match($0, re[target])) {
|
if (action == "filter") {
|
||||||
unique[extract($0, target)] = ""
|
if (match($0, re[target])) {
|
||||||
|
unique[extract($0, target)] = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (action == "tasks") {
|
||||||
|
line++
|
||||||
|
if (match($0, RE_MODULE)) {
|
||||||
|
line = 1
|
||||||
|
if (output_tasks) {
|
||||||
|
print ""
|
||||||
|
print output_module
|
||||||
|
print output_tasks
|
||||||
|
output_tasks = ""
|
||||||
}
|
}
|
||||||
} else if (action == "tasks") {
|
doc = ""
|
||||||
line++
|
match_task = 0
|
||||||
if (match($0, RE_MODULE)) {
|
output_module = ". " extract($0, "module")
|
||||||
line = 1
|
} else if (match($0, RE_TASK)) {
|
||||||
if (output_tasks) {
|
if (target) {
|
||||||
print ""
|
if (target == extract($0, "task")) {
|
||||||
print output_module
|
|
||||||
print output_tasks
|
|
||||||
output_tasks = ""
|
|
||||||
}
|
|
||||||
doc = ""
|
|
||||||
match_task = 0
|
|
||||||
output_module = ". " extract($0, "module")
|
|
||||||
} else if (match($0, RE_TASK)) {
|
|
||||||
if (target) {
|
|
||||||
if (target == extract($0, "task")) {
|
|
||||||
match_task = 1
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
match_task = 1
|
match_task = 1
|
||||||
}
|
}
|
||||||
append(line ": " $0)
|
|
||||||
} else if (match($0, RE_COMMENT)) {
|
|
||||||
append(line ": " $0)
|
|
||||||
} else {
|
} else {
|
||||||
if (match_task) {
|
match_task = 1
|
||||||
output_tasks = output_tasks "\n" doc
|
|
||||||
}
|
|
||||||
doc = ""
|
|
||||||
match_task = 0
|
|
||||||
}
|
}
|
||||||
} else if (action == "eval") {
|
append(line ": " $0)
|
||||||
if (match($0, re[target])) {
|
} else if (match($0, RE_COMMENT)) {
|
||||||
append(extract($0, target))
|
append(line ": " $0)
|
||||||
} else if (match($0, re["function"])) {
|
} else {
|
||||||
split(doc, array, "\n")
|
if (match_task) {
|
||||||
for (item in array) {
|
output_tasks = output_tasks "\n" doc
|
||||||
alias_eval(array[item], extract($0, "function"), "function")
|
}
|
||||||
|
doc = ""
|
||||||
|
match_task = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (action == "eval") {
|
||||||
|
if (match($0, re[target])) {
|
||||||
|
append(extract($0, target))
|
||||||
|
} else if (match($0, re["function"])) {
|
||||||
|
split(doc, array, "\n")
|
||||||
|
for (item in array) {
|
||||||
|
alias_eval(array[item], extract($0, "function"), "function")
|
||||||
|
}
|
||||||
|
reset()
|
||||||
|
} else {
|
||||||
|
reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (action == "function") {
|
||||||
|
if (match($0, re["command"])) {
|
||||||
|
append(extract($0, "command"))
|
||||||
|
} else if (match($0, re["function"])) {
|
||||||
|
split(doc, array, "\n")
|
||||||
|
for (item in array) {
|
||||||
|
if (array[item] == target) {
|
||||||
|
print extract($0, "function")
|
||||||
|
exit
|
||||||
}
|
}
|
||||||
reset()
|
}
|
||||||
|
reset()
|
||||||
|
} else {
|
||||||
|
reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (action == "doc") {
|
||||||
|
# doc
|
||||||
|
if (match($0, RE_SHEBANG)) {
|
||||||
|
append("! " extract($0, "shebang"))
|
||||||
|
} else if (match($0, RE_DOC)) {
|
||||||
|
if (f) {
|
||||||
|
append($0)
|
||||||
|
} else {
|
||||||
|
append(extract($0, "doc"))
|
||||||
|
}
|
||||||
|
} else if (match($0, re["alias"])) {
|
||||||
|
matched = extract($0, "alias")
|
||||||
|
append("= " matched)
|
||||||
|
if (matched == target) {
|
||||||
|
match_alias = 1
|
||||||
|
}
|
||||||
|
} else if (match($0, re["command"])) {
|
||||||
|
matched = extract($0, "command")
|
||||||
|
append("/ " matched)
|
||||||
|
if (matched == target) {
|
||||||
|
match_command = 1
|
||||||
|
}
|
||||||
|
# set
|
||||||
|
} else if (match($0, re["constant"])) {
|
||||||
|
matched = extract($0, "constant")
|
||||||
|
if (matched == target) {
|
||||||
|
output(matched, "constant")
|
||||||
} else {
|
} else {
|
||||||
reset()
|
reset()
|
||||||
}
|
}
|
||||||
} else if (action == "function") {
|
} else if (match($0, re["variable"])) {
|
||||||
if (match($0, re["command"])) {
|
matched = extract($0, "variable")
|
||||||
append(extract($0, "command"))
|
if (matched == target) {
|
||||||
} else if (match($0, re["function"])) {
|
output(matched, "variable")
|
||||||
split(doc, array, "\n")
|
|
||||||
for (item in array) {
|
|
||||||
if (array[item] == target) {
|
|
||||||
print extract($0, "function")
|
|
||||||
exit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
reset()
|
|
||||||
} else {
|
} else {
|
||||||
reset()
|
reset()
|
||||||
}
|
}
|
||||||
} else if (action == "doc") {
|
# others
|
||||||
# doc
|
} else if (match($0, RE_MODULE)) {
|
||||||
if (match($0, RE_SHEBANG)) {
|
reset()
|
||||||
append("! " extract($0, "shebang"))
|
if (extract($0, "module") == target) {
|
||||||
} else if (match($0, RE_DOC)) {
|
match_module = 1
|
||||||
if (f) {
|
}
|
||||||
append($0)
|
} else if (match($0, re["function"])) {
|
||||||
} else {
|
f = extract($0, "function")
|
||||||
append(extract($0, "doc"))
|
} else if (match($0, RE_CLOSE)) {
|
||||||
}
|
if (match_alias) {
|
||||||
} else if (match($0, re["alias"])) {
|
print "= " target
|
||||||
matched = extract($0, "alias")
|
output(f, "function")
|
||||||
append("= " matched)
|
} else if (match_command) {
|
||||||
if (matched == target) {
|
print "/ " target
|
||||||
match_alias = 1
|
output(f, "function")
|
||||||
}
|
} else if (f == target) {
|
||||||
} else if (match($0, re["command"])) {
|
output(f, "function")
|
||||||
matched = extract($0, "command")
|
|
||||||
append("/ " matched)
|
|
||||||
if (matched == target) {
|
|
||||||
match_command = 1
|
|
||||||
}
|
|
||||||
# set
|
|
||||||
} else if (match($0, re["constant"])) {
|
|
||||||
matched = extract($0, "constant")
|
|
||||||
if (matched == target) {
|
|
||||||
output(matched, "constant")
|
|
||||||
} else {
|
|
||||||
reset()
|
|
||||||
}
|
|
||||||
} else if (match($0, re["variable"])) {
|
|
||||||
matched = extract($0, "variable")
|
|
||||||
if (matched == target) {
|
|
||||||
output(matched, "variable")
|
|
||||||
} else {
|
|
||||||
reset()
|
|
||||||
}
|
|
||||||
# others
|
|
||||||
} else if (match($0, RE_MODULE)) {
|
|
||||||
reset()
|
|
||||||
if (extract($0, "module") == target) {
|
|
||||||
match_module = 1
|
|
||||||
}
|
|
||||||
} else if (match($0, re["function"])) {
|
|
||||||
f = extract($0, "function")
|
|
||||||
} else if (match($0, RE_CLOSE)) {
|
|
||||||
if (match_alias) {
|
|
||||||
print "= " target
|
|
||||||
output(f, "function")
|
|
||||||
} else if (match_command) {
|
|
||||||
print "/ " target
|
|
||||||
output(f, "function")
|
|
||||||
} else if (f == target) {
|
|
||||||
output(f, "function")
|
|
||||||
} else {
|
|
||||||
f = ""
|
|
||||||
reset()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (match_module) {
|
f = ""
|
||||||
output(target, "module")
|
reset()
|
||||||
} else {
|
}
|
||||||
reset()
|
} else {
|
||||||
}
|
if (match_module) {
|
||||||
|
output(target, "module")
|
||||||
|
} else {
|
||||||
|
reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
# end
|
||||||
|
|
||||||
END {
|
END {
|
||||||
if (action == "filter") {
|
|
||||||
for (item in unique) {
|
if (action == "filter") {
|
||||||
print item
|
for (item in unique) {
|
||||||
}
|
print item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue