=== removed file 'q' --- q 2015-08-20 11:26:21 +0000 +++ q 1970-01-01 00:00:00 +0000 @@ -1,2 +0,0 @@ -#!/bin/zsh -exec swipl -q -g main -s $0:h/query.pl =(cat) === modified file 'query.pl' --- query.pl 2015-08-20 11:26:21 +0000 +++ query.pl 2015-08-19 20:11:44 +0000 @@ -18,31 +18,21 @@ query_default_text([]) --> []. query_content([name(Name)|T]) --> query_name(Name), query_content(T). +query_content([query(Query)|T]) --> query_exp(Query), query_content(T). query_content([expr(Expr)|T]) --> `'`, query_awk(Expr), `'`, query_content(T). -query_content([query(Query, Filters, Default)|T]) --> - query_exp(Query, Filters, Default), query_content(T). query_content([]) --> []. -query_default([string(String)|T]) --> awk_string(String), query_default(T). +%query_default([text(Text)|T]) --> query_default_text(Text), query_default(T). query_default([expr(Expr)|T]) --> `'`, query_awk(Expr), `'`, query_default(T). -% query_default([query(Query)|T]) --> query_exp(Query), query_default(T). -query_default([query(Query, Filters, Default)|T]) --> - query_exp(Query, Filters, Default), query_default(T). +query_default([query(Query)|T]) --> query_exp(Query), query_default(T). +query_default([query(Query, Default)|T]) --> + query_exp(Query, Default), query_default(T). query_default([]) --> []. -query_exp(Content, Filters, Default) --> - `<`, - query_content(Content), - query_exp_filters(Filters), - query_exp_default(Default), - `>`. - -query_exp_filters([H|T]) --> - `|`, query_name(H), query_exp_filters(T). -query_exp_filters([]) --> []. - -query_exp_default(none) --> []. -query_exp_default(Default) --> `:`, query_default(Default). +query_exp(Content) --> `<`, query_content(Content), `>`. + +query_exp(Content, Default) --> + `<`, query_content(Content), `:`, query_default(Default), `>`. awk_string(String) --> `"`, awk_string_content(String), `"`. @@ -84,8 +74,11 @@ awk_code(R,R,L,L) --> []. % awk_code(L) --> awk_code(L,[],delim,_). -query_awk([query(Query, Filters, Default)|T],delim) --> - query_exp(Query, Filters, Default), +query_awk([query(Query, Default)|T],delim) --> + query_exp(Query, Default), + query_awk(T, expr). +query_awk([query(Query)|T],delim) --> + query_exp(Query), query_awk(T, expr). query_awk([code(Code)|T], Last) --> awk_code(Code, [], Last, Next), @@ -96,46 +89,35 @@ %%% -out_awk([code(Code)|T]) --> Code, out_awk(T). -out_awk([query(Query, Filters, Default)|T]) --> - out_query(Query, Filters, Default), out_awk(T). -out_awk([]) --> []. - -out_query(Query, Filters, none) --> - out_filters(Filters, out_f_get(Query)). -out_query(Query, Filters, Default) --> - `(find(`, out_query_content(Query), `)?`, - out_filters(Filters, out_f_found), - `:`, out_default(Default), `)`. - -out_f_get(Query) --> - `get(`, out_query_content(Query), `)`. - -out_f_found --> `found`. - -out_filters([H|T], Pred) --> - H, `(`, out_filters(T, Pred), `)`. -out_filters([], Pred) --> call(Pred). - -out_query_content([name(Name)|T]) --> `"`, Name, `"`, out_query_content(T). -out_query_content([expr(Expr)|T]) --> `(`, out_awk(Expr), `)`, out_query_content(T). -out_query_content([query(Query, Filters, Default)|T]) --> - out_query(Query, Filters, Default), out_query_content(T). -out_query_content([]) --> []. - -out_default([]) --> `""`. -out_default([H|T]) --> out_default_aux([H|T]). -out_default_aux([string(String)|T]) --> awk_string(String), out_default_aux(T). -out_default_aux([expr(Expr)|T]) --> `(`, out_awk(Expr), `)`, out_default_aux(T). -out_default_aux([query(Query, Filters, Default)|T]) --> - out_query(Query, Filters, Default), out_default_aux(T). -out_default_aux([]) --> []. +awk_out([code(Code)|T]) --> Code, awk_out(T). +awk_out([query(Query)|T]) --> `get(`, query_out(Query), `)`, awk_out(T). +awk_out([query(Query, Default)|T]) --> + `(find(`, query_out(Query), `)?found:`, default_out(Default), `)`, awk_out(T). +awk_out([]) --> []. + +query_out([name(Name)|T]) --> `"`, Name, `"`, query_out(T). +query_out([query(Query)|T]) --> `get(`, query_out(Query), `)`, query_out(T). +query_out([query(Query, Default)|T]) --> + `(find(`, query_out(Query), `)?found:`, default_out(Default), `)`, query_out(T). +query_out([expr(Expr)|T]) --> `(`, awk_out(Expr), `)`, query_out(T). +query_out([]) --> []. + +default_out([]) --> `""`. +default_out([H|T]) --> default_out_aux([H|T]). +%default_out_aux([text(Text)|T]) --> Text, default_out_aux(T). +default_out_aux([query(Query)|T]) --> + `get(`, query_out(Query), `)`, default_out_aux(T). +default_out_aux([query(Query, Default)|T]) --> + `(find(`, query_out(Query), `)?found:`, default_out(Default), `)`, + default_out_aux(T). +default_out_aux([expr(Expr)|T]) --> `(`, awk_out(Expr), `)`, default_out_aux(T). +default_out_aux([]) --> []. %%% main([InFile]) :- - (phrase_from_file(query_awk(Awk), InFile) -> true ; throw(parsing_failed)), - (phrase(out_awk(Awk), Out) -> true ; throw(formatting_failed(Awk))), + phrase_from_file(query_awk(Awk), InFile), + phrase(awk_out(Awk), Out), format('~s', [Out]). -main([]) :- main(["/dev/stdin"]). +main([]) :- main("/dev/stdin"). === removed file 'query.test' --- query.test 2015-08-20 11:26:21 +0000 +++ query.test 1970-01-01 00:00:00 +0000 @@ -1,98 +0,0 @@ -true ->>>= 0 - -false ->>>= 1 - -./q -<<< -BEGIN { -} ->>> -BEGIN { -} ->>>= 0 - -./q -<<< - foo() ->>> - foo(get("bar")) ->>>= 0 - -./q -<<< - x[>] = ->>> - x[get("y"get("z"))] = get("a"(b())) ->>>= 0 - -./q -<<< - y = "" ->>> - y = "" ->>>= 0 - -./q -<<< - y = (y) () ->>> - y = (y) ((find("c")?found:"")) ->>>= 0 - -./q -<<< - y = (y) () ->>> - y = (y) ((find("d")?found:(0))) ->>>= 0 - -./q -<<< - y = (y) (>) ->>> - y = (y) ((find("e")?found:get("f"))) ->>>= 0 - -./q -<<< - y = (y) (>>) ->>> - y = (y) ((find("g")?found:(find("h")?found:get("i")))) ->>>= 0 - -./q -<<< - y = (y) ( - ()'>>>) ->>> - y = (y) ((find("g")?found:(find("h")?found:(find("i")?found:(get("j") - ((find("k")?found:(0)))))))) ->>>= 0 - -./q -<<< - y = (y) () - ()'>>>) ->>> - y = (y) ((find("g")?found:(find("h")?found:(find("i")?found:((get("j")) - ((find("k")?found:(0)))))))) ->>>= 0 - -./q -<<< - y = (y) () ->>> - y = (y) ((find("a")?C(found):("r\n"))) ->>>= 0 - -./q -<<< - y = (y) () ->>> - y = (y) ((find("a")?C(found):"r\n")) ->>>= 0 - -./q -<<< - y = (y) () ->>> - y = (y) (C(get("a"))) ->>>= 0