commit ec600d17b132f4ffc4ae6942752738210051a582
parent a345b6074a9574a5e5b08384118f37f12aa4b916
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date: Thu, 3 Sep 2015 15:53:24 +0200
Add cuts to speed up failure on incorrect input, don't allow unescaped nested double-quote.
Diffstat:
| M | query.pl | | | 42 | +++++++++++++++++++++++++++++++----------- |
1 file changed, 31 insertions(+), 11 deletions(-)
diff --git a/query.pl b/query.pl
@@ -5,16 +5,19 @@
%%% :- use_module(library(dcg/basics)).
query_name([H|T]) -->
- [H],
- { code_type(H, csymf) },
+ [H], { code_type(H, csymf) },
query_name_next(T).
query_name_next([H|T]) -->
- [H],
- { H = 0'.; code_type(H, csym) },
+ [H], { H = 0'.; code_type(H, csym) },
+ !,
query_name_next(T).
query_name_next([]) --> [].
-query_default_text([H|T]) --> [H], {\+memberchk(H,`'<>`)}, query_default_text(T).
+query_default_text([H|T]) -->
+ [H],
+ {\+memberchk(H,`'<>`)},
+ !,
+ query_default_text(T).
query_default_text([]) --> [].
query_content([name(Name)|T]) --> query_name(Name), query_content(T).
@@ -47,21 +50,30 @@ query_exp_default(Default) --> `:`, query_default(Default).
awk_string(String) --> `"`, awk_string_content(String), `"`.
awk_string_content([0'\\,H|T],R) -->
- [0'\\,H], !, awk_string_content(T,R).
+ [0'\\,H],
+ !,
+ awk_string_content(T,R).
awk_string_content([H|T],R) -->
- [H], { H \= 0'/ }, awk_string_content(T,R).
+ [H], { H\=0'\\, 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).
+ [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([H|T],R) -->
+ [H], {assertion(H \= 0'\n)},
+ !,
+ awk_comment(T,R).
awk_comment(L) --> awk_comment(L,[]).
awk_code([0'"|T0],R,_,F) -->
@@ -80,7 +92,10 @@ awk_code([H|T],R,_,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).
+ [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,_).
@@ -119,7 +134,7 @@ 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_content([query(Query, Filters, Default)|T]) -->
out_query(Query, Filters, Default), out_query_content(T).
out_query_content([]) --> [].
@@ -133,9 +148,14 @@ out_default_aux([]) --> [].
%%%
+
main([InFile]) :-
(phrase_from_file(query_awk(Awk), InFile) -> true ; throw(parsing_failed)),
(phrase(out_awk(Awk), Out) -> true ; throw(formatting_failed(Awk))),
format('~s', [Out]).
main([]) :- main(["/dev/stdin"]).
+% current_input(In), set_stream(In, tty(false)),
+% (phrase_from_stream(query_awk(Awk), In) -> true ; throw(parsing_failed)),
+% (phrase(out_awk(Awk), Out) -> true ; throw(formatting_failed(Awk))),
+% format('~s', [Out]).