Skip to content

Commit

Permalink
nicc: regexparser: add some (?...) extensions
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Privat <[email protected]>
  • Loading branch information
privat committed Jun 15, 2024
1 parent 86e406a commit b30b070
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions contrib/nitcc/src/regexparser.nit
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ class RegexParser
abort
end

#
# There is some custom extensions that leverage the automaton engine.
#
# ```
# # (?=A)B intersection of A and B
# assert "(?=[ab])[bc]".parse_regex.is_equivalent("b".parse_regex)
# # (?!A)B remove A from B
# assert "(?![ab])[bc]".parse_regex.is_equivalent("c".parse_regex)
# # (??A) sortest A
# assert "(??a+)".parse_regex.is_equivalent("a".parse_regex)
# # (?+A) Longest A
# assert "(?+a?b?)".parse_regex.is_equivalent("a?b".parse_regex)
# ```
fun parse_char: Automaton
do
var c = current
Expand All @@ -99,6 +112,29 @@ class RegexParser
else if eof or c == '|' or c == ')' then
return new Automaton.epsilon
else if match('(') then
if match('?') then
if match('=') then
var r1 = parse
expect ')'
var r2 = parse_concat
return r2.intersect(r1)
else if match('!') then
var r1 = parse
expect ')'
var r2 = parse_concat
return r2.except(r1)
else if match('?') then
var r = parse
expect ')'
return r.shortest
else if match('+') then
var r = parse
expect ')'
return r.longest
else
syntax_error
end
end
var r = parse
expect ')'
return r
Expand Down

0 comments on commit b30b070

Please sign in to comment.