srl_ruby 0.4.3 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +278 -22
- data/CHANGELOG.md +43 -1
- data/Gemfile +2 -0
- data/LICENSE.txt +1 -1
- data/README.md +1 -1
- data/Rakefile +3 -0
- data/appveyor.yml +15 -14
- data/bin/srl2ruby +17 -12
- data/bin/srl2ruby_cli_parser.rb +6 -6
- data/features/lib/step_definitions/srl_testing_steps.rb +2 -0
- data/features/lib/support/env..rb +2 -0
- data/lib/regex/abstract_method.rb +2 -0
- data/lib/regex/alternation.rb +3 -3
- data/lib/regex/anchor.rb +3 -0
- data/lib/regex/atomic_expression.rb +2 -0
- data/lib/regex/capturing_group.rb +8 -3
- data/lib/regex/char_class.rb +4 -2
- data/lib/regex/char_range.rb +4 -3
- data/lib/regex/char_shorthand.rb +3 -0
- data/lib/regex/character.rb +7 -2
- data/lib/regex/compound_expression.rb +2 -0
- data/lib/regex/concatenation.rb +3 -1
- data/lib/regex/expression.rb +6 -1
- data/lib/regex/lookaround.rb +3 -1
- data/lib/regex/match_option.rb +2 -0
- data/lib/regex/monadic_expression.rb +2 -0
- data/lib/regex/multiplicity.rb +9 -9
- data/lib/regex/non_capturing_group.rb +3 -1
- data/lib/regex/polyadic_expression.rb +4 -0
- data/lib/regex/quantifiable.rb +3 -1
- data/lib/regex/raw_expression.rb +2 -0
- data/lib/regex/repetition.rb +2 -0
- data/lib/regex/wildcard.rb +2 -5
- data/lib/srl_ruby/ast_builder.rb +12 -1
- data/lib/srl_ruby/grammar.rb +48 -46
- data/lib/srl_ruby/regex_repr.rb +2 -0
- data/lib/srl_ruby/tokenizer.rb +14 -8
- data/lib/srl_ruby/version.rb +3 -1
- data/lib/srl_ruby.rb +2 -0
- data/spec/acceptance/srl_test_suite_spec.rb +2 -0
- data/spec/acceptance/support/rule_file_ast_builder.rb +2 -0
- data/spec/acceptance/support/rule_file_grammar.rb +7 -5
- data/spec/acceptance/support/rule_file_nodes.rb +2 -0
- data/spec/acceptance/support/rule_file_parser.rb +2 -0
- data/spec/acceptance/support/rule_file_tokenizer.rb +14 -8
- data/spec/regex/atomic_expression_spec.rb +2 -0
- data/spec/regex/character_spec.rb +12 -6
- data/spec/regex/match_option_spec.rb +2 -0
- data/spec/regex/monadic_expression_spec.rb +2 -0
- data/spec/regex/multiplicity_spec.rb +4 -0
- data/spec/regex/repetition_spec.rb +2 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/srl_ruby/srl_ruby_spec.rb +2 -0
- data/spec/srl_ruby/tokenizer_spec.rb +3 -1
- data/spec/srl_ruby_spec.rb +8 -6
- data/srl_ruby.gemspec +7 -5
- metadata +16 -18
- data/lib/srl_ruby/srl_token.rb +0 -23
- data/spec/acceptance/support/rule_file_token.rb +0 -22
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
@@ -5,7 +5,7 @@ the 'srl_test' directory, which is subject to its own license.
|
|
5
5
|
|
6
6
|
The MIT License (MIT)
|
7
7
|
|
8
|
-
Copyright (c) 2018 Dimitri Geshef
|
8
|
+
Copyright (c) 2018-2019 Dimitri Geshef
|
9
9
|
|
10
10
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
11
11
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ With **srl_ruby** you can easily design your patterns in SRL and let *srl_ruby*
|
|
15
15
|
- Ruby API for integrating a SRL parser or compiler with your code.
|
16
16
|
- 100% pure Ruby with clean design (_not a port from some other language_)
|
17
17
|
- Minimal runtime dependency ([Rley](https://rubygems.org/gems/rley) gem). Won't drag a bunch of gems...
|
18
|
-
- Compatibility: works with 2.
|
18
|
+
- Compatibility: works with Ruby 2.3+ MRI, JRuby
|
19
19
|
- Portability: tested on both Linux and Windows,...
|
20
20
|
|
21
21
|
## Installation
|
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'bundler/gem_tasks'
|
2
4
|
require 'rspec/core/rake_task' # Rspec as testing tool
|
3
5
|
require 'cucumber/rake/task' # Cucumber as testing tool
|
@@ -8,6 +10,7 @@ RSpec::Core::RakeTask.new do |spec|
|
|
8
10
|
end
|
9
11
|
|
10
12
|
Cucumber::Rake::Task.new do |_|
|
13
|
+
# Comment
|
11
14
|
end
|
12
15
|
|
13
16
|
# Combine RSpec and Cucumber tests
|
data/appveyor.yml
CHANGED
@@ -1,27 +1,28 @@
|
|
1
1
|
version: '{build}'
|
2
|
-
max_jobs:
|
2
|
+
max_jobs: 5
|
3
3
|
environment:
|
4
4
|
matrix:
|
5
|
-
- Ruby_version:
|
6
|
-
- Ruby_version: 21-x64
|
7
|
-
- Ruby_version: 22
|
8
|
-
- Ruby_version: 22-x64
|
9
|
-
- Ruby_version: 23
|
10
|
-
- Ruby_version: 23-x64
|
11
|
-
- Ruby_version: 24
|
12
|
-
- Ruby_version: 24-x64
|
13
|
-
- Ruby_version: 25
|
5
|
+
- Ruby_version: 26-x64
|
14
6
|
- Ruby_version: 25-x64
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
7
|
+
- Ruby_version: 24-x64
|
8
|
+
- Ruby_version: 23-x64
|
9
|
+
- Ruby_version: 26
|
10
|
+
- Ruby_version: 25
|
11
|
+
- Ruby_version: 24
|
12
|
+
- Ruby_version: 23
|
19
13
|
|
20
14
|
install:
|
21
15
|
- set PATH=C:\Ruby%Ruby_version%\bin;%PATH%
|
16
|
+
- gem update --system
|
17
|
+
- gem install bundler
|
22
18
|
- bundle install --retry=3 --clean --force
|
23
19
|
|
24
20
|
build: off
|
25
21
|
|
22
|
+
before_test:
|
23
|
+
- ruby -v
|
24
|
+
- gem -v
|
25
|
+
- bundle -v
|
26
|
+
|
26
27
|
test_script:
|
27
28
|
- bundle exec rake
|
data/bin/srl2ruby
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
require 'erb'
|
3
5
|
require 'rubygems'
|
4
6
|
require 'pathname'
|
@@ -7,10 +9,14 @@ require_relative '../lib/srl_ruby'
|
|
7
9
|
require_relative 'srl2ruby_cli_parser'
|
8
10
|
|
9
11
|
class Srl2RubyProg
|
10
|
-
DefaultSRLExtension = 'srl'
|
11
|
-
DefDirname = '/templates'
|
12
|
-
DefTemplate = 'base.erb'
|
12
|
+
DefaultSRLExtension = 'srl'
|
13
|
+
DefDirname = '/templates'
|
14
|
+
DefTemplate = 'base.erb'
|
15
|
+
|
16
|
+
# @return [Hash]
|
13
17
|
attr_reader(:cli_options)
|
18
|
+
|
19
|
+
# @return [ERB]
|
14
20
|
attr_reader(:template)
|
15
21
|
|
16
22
|
def initialize(prog_name, args)
|
@@ -23,11 +29,12 @@ class Srl2RubyProg
|
|
23
29
|
def run!(file_names)
|
24
30
|
if cli_options.include?(:pattern)
|
25
31
|
srl_source = cli_options[:pattern]
|
26
|
-
process_srl(srl_source)
|
32
|
+
process_srl(srl_source)
|
27
33
|
end
|
28
34
|
file_names.each do |srl_file|
|
29
35
|
fname = validate_filename(srl_file)
|
30
36
|
next unless file_exist?(fname)
|
37
|
+
|
31
38
|
puts "Parsing file '#{fname}'"
|
32
39
|
srl_source = File.read(fname)
|
33
40
|
process_srl(srl_source)
|
@@ -46,18 +53,16 @@ class Srl2RubyProg
|
|
46
53
|
fname = cli_options[:template]
|
47
54
|
exists = File.exist?(fname)
|
48
55
|
if exists
|
49
|
-
t_filepath = Dir.getwd
|
56
|
+
t_filepath = "#{Dir.getwd}/#{fname}"
|
50
57
|
else
|
51
|
-
t_filepath = def_template_dir
|
58
|
+
t_filepath = "#{def_template_dir}/#{fname}"
|
52
59
|
exit(1) unless file_exist?(t_filepath)
|
53
60
|
end
|
54
61
|
else
|
55
|
-
t_filepath = def_template_dir
|
62
|
+
t_filepath = "#{def_template_dir}/#{DefTemplate}"
|
56
63
|
end
|
57
64
|
path = Pathname.new(t_filepath)
|
58
|
-
|
59
|
-
|
60
|
-
erb_template
|
65
|
+
ERB.new(path.read, nil, '<>')
|
61
66
|
end
|
62
67
|
|
63
68
|
def validate_filename(raw_fname)
|
@@ -76,7 +81,7 @@ class Srl2RubyProg
|
|
76
81
|
|
77
82
|
exists
|
78
83
|
end
|
79
|
-
|
84
|
+
|
80
85
|
def process_srl(srl_source)
|
81
86
|
result = SrlRuby.parse(srl_source)
|
82
87
|
destination = $stdout
|
@@ -85,7 +90,7 @@ class Srl2RubyProg
|
|
85
90
|
destination = File.open(filepath, 'w')
|
86
91
|
end
|
87
92
|
puts "Writing to file '#{filepath}'" unless destination == $stdout
|
88
|
-
destination.puts emit_code(template, srl_source, result)
|
93
|
+
destination.puts emit_code(template, srl_source, result)
|
89
94
|
end
|
90
95
|
|
91
96
|
def emit_code(template, srl_source, regexp)
|
data/bin/srl2ruby_cli_parser.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'optparse' # Use standard OptionParser class for command-line parsing
|
2
4
|
|
3
5
|
# A command-line option parser for the srl2ruby compiler.
|
@@ -13,7 +15,7 @@ class Srl2RubyCLIParser < OptionParser
|
|
13
15
|
|
14
16
|
heading
|
15
17
|
separator 'Options:'
|
16
|
-
add_p_option
|
18
|
+
add_p_option
|
17
19
|
add_o_option
|
18
20
|
add_t_option
|
19
21
|
separator ''
|
@@ -35,7 +37,7 @@ class Srl2RubyCLIParser < OptionParser
|
|
35
37
|
end
|
36
38
|
|
37
39
|
def description
|
38
|
-
|
40
|
+
<<-DESCR
|
39
41
|
Description:
|
40
42
|
Parses a SRL file and compiles it into a Ruby Regexp literal.
|
41
43
|
Simple Regex Language (SRL) website: https://simple-regex.com
|
@@ -45,8 +47,6 @@ Examples:
|
|
45
47
|
#{program_name} -p 'begin with literally "Hello world!"'
|
46
48
|
#{program_name} example.srl -o example_re.rb -t srl_and_ruby.erb
|
47
49
|
DESCR
|
48
|
-
|
49
|
-
descr
|
50
50
|
end
|
51
51
|
|
52
52
|
def heading
|
@@ -63,14 +63,14 @@ DESCR
|
|
63
63
|
@parsed_options[:output] = pathname
|
64
64
|
end
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
def add_p_option
|
68
68
|
explanation = 'One-liner SRL pattern.'
|
69
69
|
|
70
70
|
on '-p', '--pattern SRL_PATT', explanation do |pattern_arg|
|
71
71
|
@parsed_options[:pattern] = pattern_arg.gsub(/^'|'$/, '')
|
72
72
|
end
|
73
|
-
end
|
73
|
+
end
|
74
74
|
|
75
75
|
def add_t_option
|
76
76
|
explanation = <<-EXPLANATION
|
data/lib/regex/alternation.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: alternation.rb
|
2
4
|
|
3
5
|
require_relative 'polyadic_expression' # Access the superclass
|
@@ -17,9 +19,7 @@ module Regex # This module is used as a namespace
|
|
17
19
|
# Purpose: Return the String representation of the concatented expressions.
|
18
20
|
def text_repr
|
19
21
|
result_children = children.map(&:to_str)
|
20
|
-
|
21
|
-
|
22
|
-
return result
|
22
|
+
"(?:#{result_children.join('|')})"
|
23
23
|
end
|
24
24
|
end # class
|
25
25
|
end # module
|
data/lib/regex/anchor.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: anchor.rb
|
2
4
|
|
3
5
|
require_relative 'atomic_expression' # Access the superclass
|
@@ -24,6 +26,7 @@ module Regex # This module is used as a namespace
|
|
24
26
|
# Constructor
|
25
27
|
# @param aKind [String] Lexeme representation of the anchor
|
26
28
|
def initialize(aKind)
|
29
|
+
super()
|
27
30
|
@kind = valid_kind(aKind)
|
28
31
|
end
|
29
32
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: capturing_group.rb
|
2
4
|
|
3
5
|
require_relative 'monadic_expression' # Access the superclass
|
@@ -6,6 +8,8 @@ module Regex # This module is used as a namespace
|
|
6
8
|
# An association between a capture variable and an expression
|
7
9
|
# the subject text in the same serial arrangement
|
8
10
|
class CapturingGroup < MonadicExpression
|
11
|
+
# rubocop: disable Style/OptionalBooleanParameter
|
12
|
+
|
9
13
|
# The capture variable id. It is a Fixnum when the capture group gets
|
10
14
|
# a sequence number,
|
11
15
|
# a String when it is an user-defined name
|
@@ -40,12 +44,13 @@ module Regex # This module is used as a namespace
|
|
40
44
|
atomic = no_backtrack ? '?>' : ''
|
41
45
|
if child.is_a?(Regex::NonCapturingGroup)
|
42
46
|
# Minor optimization
|
43
|
-
|
47
|
+
suffix = child.child.to_str
|
44
48
|
else
|
45
|
-
|
49
|
+
suffix = child.to_str
|
46
50
|
end
|
47
|
-
|
51
|
+
"(#{atomic}#{prefix}#{suffix})"
|
48
52
|
end
|
53
|
+
# rubocop: enable Style/OptionalBooleanParameter
|
49
54
|
end # class
|
50
55
|
end # module
|
51
56
|
|
data/lib/regex/char_class.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: char_class.rb
|
2
4
|
|
3
5
|
require_relative 'polyadic_expression' # Access the superclass
|
@@ -22,13 +24,13 @@ module Regex # This module is used as a namespace
|
|
22
24
|
# Conversion method re-definition.
|
23
25
|
# Purpose: Return the String representation of the character class.
|
24
26
|
def text_repr
|
25
|
-
result_children = children.inject('') do |sub_result, child|
|
27
|
+
result_children = children.inject(+'') do |sub_result, child|
|
26
28
|
if child.kind_of?(Regex::Character) && Metachars.include?(child.codepoint)
|
27
29
|
sub_result << '\\' # Escape meta-character...
|
28
30
|
end
|
29
31
|
sub_result << child.to_str
|
30
32
|
end
|
31
|
-
result =
|
33
|
+
result = "[#{negated ? '^' : ''}#{result_children}]"
|
32
34
|
|
33
35
|
return result
|
34
36
|
end
|
data/lib/regex/char_range.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: char_range.rb
|
2
4
|
|
3
5
|
require_relative 'polyadic_expression' # Access the superclass
|
@@ -32,9 +34,7 @@ module Regex # This module is used as a namespace
|
|
32
34
|
# Conversion method re-definition.
|
33
35
|
# Purpose: Return the String representation of the concatented expressions.
|
34
36
|
def text_repr
|
35
|
-
|
36
|
-
|
37
|
-
return result
|
37
|
+
"#{lower.to_str}-#{upper.to_str}"
|
38
38
|
end
|
39
39
|
|
40
40
|
private
|
@@ -43,6 +43,7 @@ module Regex # This module is used as a namespace
|
|
43
43
|
def validated_range(theLowerBound, theUpperBound)
|
44
44
|
msg = 'Character range error: lower bound is greater than upper bound.'
|
45
45
|
raise StandardError, msg if theLowerBound.codepoint > theUpperBound.codepoint
|
46
|
+
|
46
47
|
return [theLowerBound, theUpperBound]
|
47
48
|
end
|
48
49
|
end # class
|
data/lib/regex/char_shorthand.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: char_shorthand.rb
|
2
4
|
|
3
5
|
require_relative 'atomic_expression' # Access the superclass
|
@@ -24,6 +26,7 @@ module Regex # This module is used as a namespace
|
|
24
26
|
|
25
27
|
# Constructor
|
26
28
|
def initialize(aShortname)
|
29
|
+
super()
|
27
30
|
@shortname = valid_shortname(aShortname)
|
28
31
|
end
|
29
32
|
|
data/lib/regex/character.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: character.rb
|
2
4
|
|
3
5
|
require_relative 'atomic_expression' # Access the superclass
|
@@ -25,8 +27,8 @@ module Regex # This module is used as a namespace
|
|
25
27
|
'\7' => 7
|
26
28
|
}.freeze
|
27
29
|
|
28
|
-
MetaChars = '\^$.|+?*()[]{}'
|
29
|
-
MetaCharsInClass = '\^[]-'
|
30
|
+
MetaChars = '\^$.|+?*()[]{}'
|
31
|
+
MetaCharsInClass = '\^[]-' # Characters with special meaning in char. class
|
30
32
|
|
31
33
|
# The integer value that uniquely identifies the character.
|
32
34
|
attr_reader(:codepoint)
|
@@ -57,6 +59,7 @@ module Regex # This module is used as a namespace
|
|
57
59
|
# RegAn::Character.new('\n') # Represents a newline
|
58
60
|
# RegAn::Character.new('\u03a3') # Represents a Σ
|
59
61
|
def initialize(aValue)
|
62
|
+
super()
|
60
63
|
case aValue
|
61
64
|
when String
|
62
65
|
if aValue.size == 1
|
@@ -103,6 +106,7 @@ module Regex # This module is used as a namespace
|
|
103
106
|
def self.esc2codepoint(esc_seq)
|
104
107
|
msg = "Escape sequence #{esc_seq} does not begin with a backslash (\)."
|
105
108
|
raise StandardError, msg unless esc_seq[0] == '\\'
|
109
|
+
|
106
110
|
result = (esc_seq.length == 2) ? digram2codepoint(esc_seq) : esc_number2codepoint(esc_seq)
|
107
111
|
|
108
112
|
return result
|
@@ -155,6 +159,7 @@ module Regex # This module is used as a namespace
|
|
155
159
|
# Otherwise the character corresponding to the codepoint is returned.
|
156
160
|
def text_repr
|
157
161
|
return char if lexeme.nil?
|
162
|
+
|
158
163
|
return lexeme.dup
|
159
164
|
end
|
160
165
|
|
data/lib/regex/concatenation.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: concatenation.rb
|
2
4
|
|
3
5
|
require_relative 'polyadic_expression' # Access the superclass
|
@@ -17,7 +19,7 @@ module Regex # This module is used as a namespace
|
|
17
19
|
# Conversion method re-definition.
|
18
20
|
# Purpose: Return the String representation of the concatented expressions.
|
19
21
|
def text_repr
|
20
|
-
outcome = children.inject('') do |result, child|
|
22
|
+
outcome = children.inject(+'') do |result, child|
|
21
23
|
result << child.to_str
|
22
24
|
end
|
23
25
|
|
data/lib/regex/expression.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: expression.rb
|
2
4
|
|
3
5
|
require_relative 'abstract_method'
|
@@ -5,7 +7,10 @@ require_relative 'abstract_method'
|
|
5
7
|
module Regex # This module is used as a namespace
|
6
8
|
# Abstract class. The generalization of any valid regular (sub)expression.
|
7
9
|
class Expression
|
10
|
+
# @return [NilClass, Anchor]
|
8
11
|
attr_accessor :begin_anchor
|
12
|
+
|
13
|
+
# @return [NilClass, Anchor]
|
9
14
|
attr_accessor :end_anchor
|
10
15
|
|
11
16
|
# Constructor
|
@@ -31,7 +36,7 @@ module Regex # This module is used as a namespace
|
|
31
36
|
# Template method.
|
32
37
|
# @return [String] text representation of the expression.
|
33
38
|
def to_str
|
34
|
-
result = ''
|
39
|
+
result = +''
|
35
40
|
result << prefix
|
36
41
|
result << text_repr
|
37
42
|
result << suffix
|