commit 6d5104ab82a0569523454cfd9ae88da3ce3138ab
parent 54d63266203d6c9419f958845e3fe8170a21273c
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date: Sat, 25 Jul 2015 06:11:12 +0200
very basic implementation of query postprocessor in prolog
Diffstat:
3 files changed, 97 insertions(+), 1 deletion(-)
diff --git a/bin/aat.awk b/bin/aat.awk
@@ -363,7 +363,7 @@ function aat_process(str) {
match_dbg("query", str)
processed = processed "<" substr(str, 1, RLENGTH) ">"
str = substr(str, RLENGTH+1)
- } else if(match(str, /^\|[a-zA-Z]/)) {
+ } else if(match(str, /^\|[a-zA-Z_]/)) {
# filters ahead
match_dbg("filter", str)
break
diff --git a/query.pl b/query.pl
@@ -0,0 +1,92 @@
+#!/usr/bin/swipl -q -g main -s
+% vim: ft=prolog textwidth=80 tabstop=4 softtabstop=4 shiftwidth=4 expandtab
+
+:- use_module(library(pure_input)).
+%%% :- use_module(library(dcg/basics)).
+
+query_name([H|T]) -->
+ [H],
+ { code_type(H, csymf) },
+ query_name_next(T).
+query_name_next([H|T]) -->
+ [H],
+ { H = 0'.; code_type(H, csym) },
+ query_name_next(T).
+query_name_next([]) --> [].
+
+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_exp(Content) --> `<`, query_content(Content), `>`.
+
+awk_string(String) --> `"`, awk_string_content(String), `"`.
+
+awk_string_content([0'\\,H|T],R) -->
+ [0'\\,H], !, awk_string_content(T,R).
+awk_string_content([H|T],R) -->
+ [H], { H \= 0'/ }, awk_string_content(T,R).
+awk_string_content(R,R) --> [].
+awk_string_content(L) --> awk_string_content(L,[]).
+
+awk_regex_content([0'\\,H|T],R) -->
+ [0'\\,H], !, awk_regex_content(T,R).
+awk_regex_content([H|T],R) -->
+ [H], { H \= 0'" }, awk_regex_content(T,R).
+awk_regex_content(R,R) --> [].
+awk_regex_content(L) --> awk_regex_content(L,[]).
+
+awk_comment([0'\n|R],R) --> `\n`, !.
+awk_comment([H|T],R) --> [H], {assertion(H \= 0'\n)}, awk_comment(T,R).
+awk_comment(L) --> awk_comment(L,[]).
+
+awk_code([0'"|T0],R,_,F) -->
+ `"`, !, awk_string_content(T0,T1), `"`, { T1=[0'"|T2] }, awk_code(T2,R,expr,F).
+awk_code([0'/|T0],R,Prev,F) -->
+ { memberchk(Prev, [delim, op]) },
+ `/`, !, awk_regex_content(T0,T1), `/`, { T1=[0'/|T2] }, awk_code(T2,R,expr,F).
+awk_code([0'#|T],R,Prev,F) -->
+ `#`, !, awk_comment(T,T1), awk_code(T1,R,Prev,F).
+awk_code([H|T],R,Prev,F) -->
+ [H], { code_type(H, white) }, !, awk_code(T,R,Prev,F).
+awk_code([H|T],R,_,F) -->
+ [H], { memberchk(H, `\n,;([{=`) }, !, awk_code(T,R,delim,F).
+awk_code([H|T],R,_,F) -->
+ [H], { memberchk(H, `!+-*/%^&|?:>~`) }, !, awk_code(T,R,op,F).
+% awk_code([0'<|T],R,Prev,F) -->
+% `<`, !, { Prev \= delim }, awk_code(T,R,op,F).
+awk_code([H|T],R,Prev,F) -->
+[H], { H \= 0'', H \= 0'@, (H \= 0'< ; Prev \= delim ) }, awk_code(T,R,expr,F).
+awk_code(R,R,L,L) --> [].
+% awk_code(L) --> awk_code(L,[],delim,_).
+
+query_awk([query(Query)|T],delim) -->
+ query_exp(Query),
+ query_awk(T, expr).
+query_awk([code(Code)|T], Last) -->
+ awk_code(Code, [], Last, Next),
+ { Code \= [] },
+ query_awk(T, Next).
+query_awk([], _) --> [].
+query_awk(L) --> query_awk(L,delim).
+
+%%%
+
+awk_out([code(Code)|T]) --> Code, awk_out(T).
+awk_out([query(Query)|T]) --> `get(`, query_out(Query), `)`, 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([expr(Expr)|T]) --> `(`, awk_out(Expr), `)`, query_out(T).
+query_out([]) --> [].
+
+%%%
+
+main([InFile]) :-
+ phrase_from_file(query_awk(Awk), InFile),
+ phrase(awk_out(Awk), Out),
+ format('~s', [Out]).
+
+main([]) :- main("/dev/stdin").
diff --git a/query_example.awk b/query_example.awk
@@ -0,0 +1,4 @@
+BEGIN {
+ foo(<bar>)
+ x[<y<z>>] = <a'b()'>
+}