Skip to content

Commit 3a80a64

Browse files
author
Hana Dusíková
committed
lookahead support (part 1)
1 parent 4dfd660 commit 3a80a64

File tree

9 files changed

+142
-25
lines changed

9 files changed

+142
-25
lines changed

include/ctre/actions/look.inc.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef CTRE__ACTIONS__LOOKAHEAD__HPP
2+
#define CTRE__ACTIONS__LOOKAHEAD__HPP
3+
4+
// lookahead positive start
5+
template <auto V, typename... Ts, size_t Counter> static constexpr auto apply(pcre::start_lookahead_positive, ctll::term<V>, pcre_context<ctll::list<Ts...>, pcre_parameters<Counter>>) {
6+
return pcre_context{ctll::list<look_start<lookahead_positive<>>, Ts...>(), pcre_parameters<Counter>()};
7+
}
8+
9+
// lookahead positive end
10+
template <auto V, typename Look, typename... Ts, size_t Counter> static constexpr auto apply(pcre::look_finish, ctll::term<V>, pcre_context<ctll::list<Look, look_start<lookahead_positive<>>, Ts...>, pcre_parameters<Counter>>) {
11+
return pcre_context{ctll::list<lookahead_positive<Look>, Ts...>(), pcre_parameters<Counter>()};
12+
}
13+
14+
// lookahead positive end (sequence)
15+
template <auto V, typename... Look, typename... Ts, size_t Counter> static constexpr auto apply(pcre::look_finish, ctll::term<V>, pcre_context<ctll::list<ctre::sequence<Look...>, look_start<lookahead_positive<>>, Ts...>, pcre_parameters<Counter>>) {
16+
return pcre_context{ctll::list<lookahead_positive<Look...>, Ts...>(), pcre_parameters<Counter>()};
17+
}
18+
19+
// lookahead negative start
20+
template <auto V, typename... Ts, size_t Counter> static constexpr auto apply(pcre::start_lookahead_negative, ctll::term<V>, pcre_context<ctll::list<Ts...>, pcre_parameters<Counter>>) {
21+
return pcre_context{ctll::list<look_start<lookahead_negative<>>, Ts...>(), pcre_parameters<Counter>()};
22+
}
23+
24+
// lookahead negative end
25+
template <auto V, typename Look, typename... Ts, size_t Counter> static constexpr auto apply(pcre::look_finish, ctll::term<V>, pcre_context<ctll::list<Look, look_start<lookahead_negative<>>, Ts...>, pcre_parameters<Counter>>) {
26+
return pcre_context{ctll::list<lookahead_negative<Look>, Ts...>(), pcre_parameters<Counter>()};
27+
}
28+
29+
// lookahead negative end (sequence)
30+
template <auto V, typename... Look, typename... Ts, size_t Counter> static constexpr auto apply(pcre::look_finish, ctll::term<V>, pcre_context<ctll::list<ctre::sequence<Look...>, look_start<lookahead_negative<>>, Ts...>, pcre_parameters<Counter>>) {
31+
return pcre_context{ctll::list<lookahead_negative<Look...>, Ts...>(), pcre_parameters<Counter>()};
32+
}
33+
34+
#endif

include/ctre/atoms.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct accept { };
1010
struct start_mark { };
1111
struct end_mark { };
1212
struct end_cycle_mark { };
13+
struct end_lookahead_mark { };
1314
template <size_t Id> struct numeric_mark { };
1415

1516
// actual AST of regexp
@@ -39,10 +40,14 @@ template <size_t Index, typename Name, typename... Content> struct capture_with_
3940

4041
template <size_t Index> struct back_reference { };
4142
template <typename Name> struct back_reference_with_name { };
42-
43+
44+
template <typename Type> struct look_start { };
45+
46+
template <typename... Content> struct lookahead_positive { };
47+
template <typename... Content> struct lookahead_negative { };
4348

4449
struct assert_begin { };
45-
struct assert_end { };
50+
struct assert_end { };
4651

4752
}
4853

include/ctre/evaluation.hpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, c
145145
} else {
146146
return evaluate(begin, current, end, captures, ctll::list<HeadContent, Tail...>());
147147
}
148-
149148
}
150149

151150
// matching empty in patterns
@@ -361,6 +360,34 @@ constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, c
361360
return not_matched;
362361
}
363362

363+
// end of lookahead
364+
template <typename R, typename Iterator, typename EndIterator, typename... Tail>
365+
constexpr CTRE_FORCE_INLINE R evaluate(const Iterator, Iterator current, const EndIterator, R captures, ctll::list<end_lookahead_mark>) noexcept {
366+
return captures.set_end_mark(current).matched();
367+
}
368+
369+
// lookahead positive
370+
template <typename R, typename Iterator, typename EndIterator, typename... Content, typename... Tail>
371+
constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, const EndIterator end, R captures, ctll::list<lookahead_positive<Content...>, Tail...>) noexcept {
372+
373+
if (auto lookahead_result = evaluate(begin, current, end, captures, ctll::list<sequence<Content...>, end_lookahead_mark>())) {
374+
captures = lookahead_result.unmatch();
375+
return evaluate(begin, current, end, captures, ctll::list<Tail...>());
376+
} else {
377+
return not_matched;
378+
}
379+
}
380+
381+
// lookahead negative
382+
template <typename R, typename Iterator, typename EndIterator, typename... Content, typename... Tail>
383+
constexpr CTRE_FORCE_INLINE R evaluate(const Iterator begin, Iterator current, const EndIterator end, R captures, ctll::list<lookahead_negative<Content...>, Tail...>) noexcept {
384+
385+
if (auto lookahead_result = evaluate(begin, current, end, captures, ctll::list<sequence<Content...>, end_lookahead_mark>())) {
386+
return not_matched;
387+
} else {
388+
return evaluate(begin, current, end, captures, ctll::list<Tail...>());
389+
}
390+
}
364391

365392
}
366393

include/ctre/pcre.gram

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ caret={^}
1818
minus={-}
1919
angle_open={<}
2020
angle_close={>}
21+
equal_sign={=}
22+
exclamation_mark={!}
2123

2224
escape_alphanum={b,c,h,i,j,k,l,m,o,p,q,v,y,z,A,B,C,E,F,G,H,I,J,K,L,M,O,P,Q,U,V,X,Y,Z,1,2,3,4,5,6,7,8,9}
2325
hexdec={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,A,B,C,D,E,F}
2426
octal={0,1,2,3,4,5,6,7}
25-
nonspecial_characters={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,\,,:}
26-
nonspecial_characters_in_class={_,+,*,(,),.,$,\{,\},?}
27+
nonspecial_characters={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,\,,:,!,=}
28+
nonspecial_characters_in_class={_,+,*,(,),.,$,\{,\},?,!,=}
2729
alpha_characters={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,_}
2830
alphanum_characters={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,_}
2931
set_control_chars={:,],-}
@@ -60,6 +62,8 @@ number2->epsilon | num,[push_number],<number2>
6062
preblock->open,[prepare_capture],<block>
6163

6264
block-><content_in_capture>,[make_capture],close
65+
block->questionmark,[reset_capture],equal_sign,[start_lookahead_positive],<content_in_capture>,[look_finish],close
66+
block->questionmark,[reset_capture],exclamation_mark,[start_lookahead_negative],<content_in_capture>,[look_finish],close
6367
block->questionmark,[reset_capture],colon,<content_in_capture>,close
6468
block->questionmark,angle_open,<block_name>,angle_close,<content_in_capture>,[make_capture_with_name],close
6569

0 commit comments

Comments
 (0)