Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions benchmark/compare.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "../src/crystal-dfa"
require "benchmark"

rx1, rx2 = nil, nil
rx1 = nil
rx2 = nil
expression = /(?:x+x+)+y/
string = "xxxxxxxxxxxxxy"
# expression = /"([^"\\]|\\.)*"/
Expand All @@ -13,26 +14,27 @@ string = "xxxxxxxxxxxxxy"
puts
puts %{building "#{expression}" with Regex (PCRE)}
puts Benchmark.measure { rx1 = Regex.new(expression.source) }
rx1 = rx1.not_nil!
rx1ok = rx1.not_nil!

puts %{building "#{expression}" with RegExp (own impl}
puts Benchmark.measure { rx2 = rx1.cr }
rx2 = rx2.not_nil!
puts Benchmark.measure { rx2 = DFA::RegExp.new(expression.source) } # rx1ok.cr }
rx2ok = rx2.not_nil!

puts
puts %{matching "#{string}" a first time with Regex (PCRE)}
puts Benchmark.measure { rx1.match string }
pp rx1.match string
puts Benchmark.measure { rx1ok.match string }
pp rx1ok.match string
puts
puts %{matching "#{string}" a first time with RegExp (own impl}
puts Benchmark.measure { rx2.match string }
pp rx2.match string
puts Benchmark.measure { rx2ok.match string }
pp rx2ok.match string
puts

Benchmark.measure { rx1.not_nil!.match string }
Benchmark.measure { rx2.not_nil!.match string }
Benchmark.measure { rx1ok.match string }
Benchmark.measure { rx2ok.match string }

Benchmark.ips do |x|
x.report("Regex (PCRE) matching : #{string}") { rx1.not_nil!.match string }
x.report("RegExp (own impl) matching : #{string}") { rx2.not_nil!.match string }
x.report("Regex (PCRE) matching : #{string}") { rx2ok.match string }
x.report("RegExp (own impl) matching : #{string}") { rx2ok.match string }
end
puts
4 changes: 4 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ authors:
crystal: 0.26.1

license: MIT

targets:
benchmark:
main: benchmark/compare.cr
6 changes: 3 additions & 3 deletions spec/crystal-dfa/nfa_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe DFA::NFA do
DFA::NFA.create_nfa(ast).should eq expected
end

it "creates a state for a ConcateNode" do
context "creates a state for a ConcateNode" do
it "works for the binary case" do
ast = DFA::AST::ConcatNode.new [
DFA::AST::LiteralNode.new('a').as(DFA::AST::ASTNode),
Expand All @@ -31,7 +31,7 @@ describe DFA::NFA do
end
end

it "creates a state for an AlternationNode" do
context "creates a state for an AlternationNode" do
it "works for the binary case" do
ast = DFA::AST::AlternationNode.new [
DFA::AST::LiteralNode.new('a').as(DFA::AST::ASTNode),
Expand Down Expand Up @@ -99,7 +99,7 @@ describe DFA::NFA do
DFA::NFA.create_nfa(ast).should eq expected
end

it "creates a state for a CharacterClassNode([a-z]) One-or-More" do
context "creates a state for a CharacterClassNode([a-z]) One-or-More" do
it "creates a state for the simple range case [a-z]" do
ast = DFA::AST::CharacterClassNode.new(false, Array(String).new, [('a'..'z')])
expected = r_state('a', 'z')
Expand Down
5 changes: 2 additions & 3 deletions src/core_ext/range.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ module IntersectionMethods(T)
end

macro included

def disjoin(other : self)
[other] + (self - other)
#[self] + (other - self)
# [self] + (other - self)
end
end

Expand Down Expand Up @@ -47,7 +46,7 @@ module IntersectionMethods(T)
end
end

struct Tuple(T)
struct Tuple(*T)
include IntersectionMethods(T)

def -(other : self)
Expand Down
22 changes: 11 additions & 11 deletions src/crystal-dfa/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,23 @@ module DFA
end

class NameParslet < PrefixParslet
def parse(parser, token)
def parse(parser, token) : AST::ASTNode
AST::LiteralNode.new(token[:value].not_nil!)
end
end

class AnyCharacterParslet < PrefixParslet
def parse(parser, token)
def parse(parser, token) : AST::ASTNode
AST::CharacterClassNode.new(false, Array(String).new, ANY_CHAR_RANGES)
end
end

class SpecialCharacterClassParslet < PrefixParslet
def parse(parser, token)
def parse(parser, token) : AST::ASTNode
_next = parser.consume
raise "unexpected end of input" unless _next

value = _next[:type] == :LITERAL ? _next[:value] : # translate specia characters back to
value = _next[:type] == :LITERAL ? _next[:value] : # translate specia characters back to
# their string representation because
# we won't interprete them inside a
# characterclass
Expand All @@ -180,7 +180,7 @@ Lexer::IDENTIFIERS.key_for(_next[:type])
end

class GroupParslet < PrefixParslet
def parse(parser, token)
def parse(parser, token) : AST::ASTNode
# ignore non capturing group designators
if (_peek = parser.peek) &&
_peek[:type] == :QSTM
Expand All @@ -199,7 +199,7 @@ Lexer::IDENTIFIERS.key_for(_next[:type])
end

class CharacterRangeParslet < InfixParslet
def parse(parser, left, token)
def parse(parser, left, token) : AST::ASTNode
parser.consume(:MINUS)
right = parser.parseExpression(precedence)

Expand All @@ -218,7 +218,7 @@ Lexer::IDENTIFIERS.key_for(_next[:type])
end

class CharacterClassParslet < PrefixParslet
def parse(parser, token)
def parse(parser, token) : AST::ASTNode
negate = (peek = parser.peek) &&
(peek[:type] == :NEGATE) &&
parser.consume ? true : false
Expand Down Expand Up @@ -266,7 +266,7 @@ Lexer::IDENTIFIERS.key_for(_next[:type])
end

class ConcatParslet < InfixParslet
def parse(parser, left : AST::ASTNode, token)
def parse(parser, left : AST::ASTNode, token) : AST::ASTNode
exp = AST::ConcatNode.new([left.as(AST::ASTNode)])

_next = parser.parseExpression(Precedence[:LITERAL] - 1).as(AST::ASTNode?)
Expand All @@ -282,7 +282,7 @@ Lexer::IDENTIFIERS.key_for(_next[:type])
end

class CurlyQuantifierParslet < InfixParslet
def parse(parser, left : AST::ASTNode, token)
def parse(parser, left : AST::ASTNode, token) : AST::ASTNode
parser.consume(:LCURLY)

values = parse_quantifications(
Expand Down Expand Up @@ -318,14 +318,14 @@ Lexer::IDENTIFIERS.key_for(_next[:type])
end

class QuantifierParslet(T) < InfixParslet
def parse(parser, left : AST::ASTNode, token)
def parse(parser, left : AST::ASTNode, token) : AST::ASTNode
parser.consume
T.new(left)
end
end

class AlternationParslet < InfixParslet
def parse(parser, left : AST::ASTNode, token)
def parse(parser, left : AST::ASTNode, token) : AST::ASTNode
exp = AST::AlternationNode.new([left.as(AST::ASTNode)])
while (peek = parser.peek) && (peek[:type] == :PIPE)
parser.consume
Expand Down