srl_ruby 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ac4b0f8fd97b06c70f416ef86a1be451e66c120
4
- data.tar.gz: 57c2baed9fc6659842ca4b0df87686a5ce1fdb38
3
+ metadata.gz: aec2fc57a4a6eadb94d63cbb5b3edaaab988ceff
4
+ data.tar.gz: fb2a6b4007134fd657c749e9979610e562b13ff3
5
5
  SHA512:
6
- metadata.gz: 73b82f0ab75ed3a0426809ec69c00448f1212efbc4d4216a9293094080d9ec734421ebb62868c4b84858aef7408d75ab69fd61567d10f004f64b981383b4eddc
7
- data.tar.gz: de79ea767f4a30d454f1b266c5581796a9881e83e6452fbf7398d3f99cb938b5b1a3fd17b6804ef89af6c8f6e116df4a41a990cc9ab7606780489198bb645ab5
6
+ metadata.gz: 2319c820f448ffffc89ed753f39c724fa7c438ebaa5930b5ee2aa9c303f7dedd3f23523a865ddf1a13838ec70e2f10a717cce1c562b4f98f61050a99d77d1437
7
+ data.tar.gz: d975c484f19792cae40c23fbfc6294f606ffe49cff29ecf07519447585cf80e332e10e7ec1422846f501ae14dec2f8773c1fe0e04905ea9177214b60193a24ab
data/CHANGELOG.md CHANGED
@@ -6,6 +6,13 @@
6
6
  ### Fixed
7
7
  ### Security
8
8
 
9
+ ## [0.1.0] - 2018-03-08
10
+ ### Added
11
+ - Version bump. Added new API methods `SrlRuby#load_file` and `SrlRuby#parse`
12
+
13
+ ### Changed
14
+ - File `README.md` vastly improved with examples and usage snippet.
15
+
9
16
  ## [0.0.2] - 2018-03-07
10
17
  ### Added
11
18
  - File `CHANGELOG.md`. This file. Adopting `keepachangelog.com` recommended format.
data/README.md CHANGED
@@ -8,33 +8,97 @@
8
8
  This project implements a [Simple Regex Language](https://simple-regex.com) parser and interpreter in Ruby.
9
9
 
10
10
  ## What is SRL?
11
- SRL is a small language lets you write regular expressions
12
- with a readable syntax that bears some resemblance with English.
13
- Here are a couple of hyperlinks of interest:
14
- [Main SRL website](https://simple-regex.com)
15
- [SRL libraries](https://github.com/SimpleRegex)
11
+ [SRL](https://github.com/SimpleRegex) is a small language that lets you write pattern matching expressions
12
+ in a readable syntax that bears some resemblance with English.
16
13
 
14
+ As an example, let's assume that our mission is to create a regular expression
15
+ that matches the time in 12 hour clock format.
16
+ Here is a SRL expression that matches the _hh:mm AM/PM_ time format:
17
+ ```
18
+ digit from 0 to 1,
19
+ digit optional,
20
+ literally ":",
21
+ digit from 0 to 5, digit,
22
+ literally " ",
23
+ one of "AP", literally "M"
24
+ ```
17
25
 
18
- ## An example
19
- Let's assume that we want to create a regular expression that recognizes time in a day in the 24 hours format hh:mm:ss.
20
- In SRL:
26
+ ## Why SRL?
27
+ Even without knowing SRL, a reader can easily grasp the details of the above SRL expression.
28
+ This is where SRL shines over the traditional regular expressions: high readability.
29
+ For instance, a regex for the clock format problem may look like this:
21
30
  ```
22
- digit from 0 to 2, digit,
23
- (literally ':', digit from 0 to 5, digit) twice
31
+ /[0-1]?\d:[0-5]\d [AP]M/
24
32
  ```
25
33
 
26
- If one runs the `srl_ruby` command-line like this:
34
+ In terms of terseness, regexes are hard to beat. But it is also a well-known fact: regexes can be really hard to write and even harder to read ('decipher' verb is closer to reality).
35
+ Alas, the path of creating and maintaining regexes can be full of frustration.
27
36
 
37
+ There comes SRL. The intent is to let developers define self-documenting patterns with an easy syntax.
38
+ And then let your computer translate SRL expressions into regular expressions.
39
+ `srl_ruby` allows you to craft self-documenting patterns in SRL and then generate
40
+ their Ruby regular expression representation.
41
+
42
+
43
+ Ah, by the way, our clock format pattern isn't completely correct. It will match invalid times like 19:34 PM.
44
+ The problem arises when the first digit in the hour field is a one: in that case the second digit can only be
45
+ zero or one.
46
+
47
+ Let's fix the issue in SRL:
28
48
  ```
29
- srl_ruby "digit from 0 to 2, digit, (literally ':', digit from 0 to 5, digit) twice"
30
- ```
31
- It replies with:
49
+ any of (
50
+ (literally "0" optional, digit),
51
+ (literally "1", one of "01")
52
+ ),
53
+ literally ":",
54
+ digit from 0 to 5, digit,
55
+ literally " ",
56
+ one of "AP", literally "M"
32
57
  ```
33
- SRL input: digit from 0 to 2, digit, (literally ':', digit from 0 to 5, digit) twice
34
- Resulting Regexp: /[0-2]\d(?::[0-5]\d){2}/
58
+
59
+ And there is the equivalent regex found by `srl_ruby`:
60
+ /(?:(?:0?\d)|(?:1[01])):[0-5]\d [AP]M/
61
+
62
+ ### More about Simple Regex Language syntax and examples
63
+ Here are a couple of hyperlinks of interest:
64
+ [Main SRL website](https://simple-regex.com)
65
+ [SRL libraries](https://github.com/SimpleRegex)
66
+
67
+
68
+ ## Usage
69
+ The following snippet...
70
+
71
+ ```ruby
72
+ require 'srl_ruby' # Load srl_ruby library
73
+
74
+
75
+ # Here is a multiline SRL expression that matches dates
76
+ # in yyyy-mm-dd format
77
+ some_srl = <<-END_SRL
78
+ any of (literally "19", literally "20"), digit twice,
79
+ literally "-",
80
+ any of (
81
+ (literally "0", digit),
82
+ (literally "1", one of "012")
83
+ ),
84
+ literally "-",
85
+ any of (
86
+ (literally "0", digit),
87
+ (one of "12", digit),
88
+ (literally "3", one of "01")
89
+ )
90
+ END_SRL
91
+
92
+ # Next line launches the SRL parser, it returns the corresponding regex literal
93
+ result = SrlRuby.parse(some_srl)
94
+
95
+ puts 'Equivalent regexp: /' + result + '/'
35
96
  ```
36
97
 
37
- In other words, it translates a readable SRL expression into its regexp equivalent.
98
+ ... produces the following output:
99
+ ```
100
+ Equivalent regexp: /(?:19|20)\d{2}-(?:(?:0\d)|(?:1[012]))-(?:(?:0\d)|(?:[12]\d)|(?:3[01]))/
101
+ ```
38
102
 
39
103
  ## Installation
40
104
 
data/bin/srl_ruby CHANGED
@@ -1,12 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- require_relative '../lib/srl_ruby/tokenizer'
3
- require_relative '../lib/srl_ruby/grammar'
4
- require_relative '../lib/srl_ruby/ast_builder'
2
+ require_relative '../lib/srl_ruby/srl_ruby'
5
3
 
6
- def print_title(aTitle)
7
- puts aTitle
8
- puts '=' * aTitle.size
9
- end
10
4
 
11
5
  # Parse the input expression in command-line
12
6
  if ARGV.empty?
@@ -29,30 +23,6 @@ END_MSG
29
23
  exit(1)
30
24
  end
31
25
  puts 'SRL input: ' + ARGV[0]
32
-
33
- # Create a Rley facade object
34
- engine = Rley::Engine.new
35
-
36
- ########################################
37
- # Step 1. Load a grammar for calculator
38
- engine.use_grammar(SrlRuby::Grammar)
39
-
40
- lexer = SrlRuby::Tokenizer.new(ARGV[0])
41
- result = engine.parse(lexer.tokens)
42
-
43
- unless result.success?
44
- # Stop if the parse failed...
45
- puts "Parsing of '#{ARGV[0]}' failed"
46
- puts "Reason: #{result.failure_reason.message}"
47
- exit(1)
48
- end
49
-
50
- # Generate an abstract syntax tree (AST) from the parse result
51
- engine.configuration.repr_builder = SrlRuby::ASTBuilder
52
- ast_ptree = engine.convert(result)
53
-
54
- # Now output the regexp literal
55
- root = ast_ptree.root
56
- puts "Resulting Regexp: /#{root.to_str}/" # Output the expression result
26
+ puts 'Resulting Regexp: /' + SrlRuby.parse(ARGV[0]) + '/'
57
27
 
58
28
  # End of file
@@ -1,3 +1,3 @@
1
1
  module SrlRuby
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
data/lib/srl_ruby.rb CHANGED
@@ -1,4 +1,46 @@
1
1
  require_relative './srl_ruby/version'
2
+ require_relative './srl_ruby/tokenizer'
3
+ require_relative './srl_ruby/grammar'
4
+ require_relative './srl_ruby/ast_builder'
2
5
 
3
6
  module SrlRuby # This module is used as a namespace
4
- end
7
+ # Load the SRL expression contained in filename.
8
+ # Returns the literal regular expression representation
9
+ # as a Ruby String.
10
+ # @param filename [String] file name to parse.
11
+ def self.load_file(filename)
12
+ source = nil
13
+ File.open(filename, 'r') { |f| source = f.read }
14
+ return source if source.nil? || source.empty?
15
+
16
+ return parse(source)
17
+ end
18
+
19
+ # Parse the SRL expression into its literal regexp and return it.
20
+ # @param source [String] the SRL source to parse and convert.
21
+ def self.parse(source)
22
+ # Create a Rley facade object
23
+ engine = Rley::Engine.new
24
+
25
+ # Step 1. Load SRL grammar
26
+ engine.use_grammar(SrlRuby::Grammar)
27
+
28
+ lexer = SrlRuby::Tokenizer.new(source)
29
+ result = engine.parse(lexer.tokens)
30
+
31
+ unless result.success?
32
+ # Stop if the parse failed...
33
+ line1 = "Parsing of '#{source}' failed\n"
34
+ line2 = "Reason: #{result.failure_reason.message}"
35
+ raise StandardError, line1 + line2
36
+ end
37
+
38
+ # Generate an abstract syntax tree (AST) from the parse result
39
+ engine.configuration.repr_builder = SrlRuby::ASTBuilder
40
+ ast_ptree = engine.convert(result)
41
+
42
+ # Now output the regexp literal
43
+ root = ast_ptree.root
44
+ return root.to_str
45
+ end
46
+ end # module
data/srl_ruby.gemspec CHANGED
@@ -26,7 +26,7 @@ module PkgExtending
26
26
  end
27
27
 
28
28
  def self.pkg_documentation(aPackage)
29
- aPackage.rdoc_options << '--charset=UTF-8 --exclude="examples|features|spec"'
29
+ aPackage.rdoc_options << '--charset=UTF-8 --exclude="examples|spec"'
30
30
  aPackage.extra_rdoc_files = ['README.md']
31
31
  end
32
32
  end # module
@@ -37,10 +37,11 @@ Gem::Specification.new do |spec|
37
37
  spec.authors = ['Dimitri Geshef']
38
38
  spec.email = ['famished.tiger@yahoo.com']
39
39
 
40
- spec.summary = %q(A parser for the Simple Regex Language, written in Ruby.)
41
- spec.description = <<-END_DESCR
42
- srl_ruby is a Ruby gem implementing a parser for Simple Regex Language (SRL).
43
- It reads then converts SRL input into Ruby Strings or Regexp.
40
+ spec.description = %q(A parser for the Simple Regex Language (SRL).)
41
+ spec.summary = <<-END_DESCR
42
+ srl_ruby is a gem implementing a parser for Simple Regex Language (SRL).
43
+ It translates patterns expressed in SRL into plain Ruby Regexp objects
44
+ or regex literals.
44
45
  END_DESCR
45
46
  spec.homepage = 'https://github.com/famished-tiger/SRL-Ruby'
46
47
  spec.license = 'MIT'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: srl_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Geshef
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-07 00:00:00.000000000 Z
11
+ date: 2018-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rley
@@ -66,8 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
- description: "srl_ruby is a Ruby gem implementing a parser for Simple Regex Language
70
- (SRL).\nIt reads then converts SRL input into Ruby Strings or Regexp. \n"
69
+ description: A parser for the Simple Regex Language (SRL).
71
70
  email:
72
71
  - famished.tiger@yahoo.com
73
72
  executables:
@@ -126,7 +125,7 @@ licenses:
126
125
  metadata: {}
127
126
  post_install_message:
128
127
  rdoc_options:
129
- - --charset=UTF-8 --exclude="examples|features|spec"
128
+ - --charset=UTF-8 --exclude="examples|spec"
130
129
  require_paths:
131
130
  - lib
132
131
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -144,7 +143,9 @@ rubyforge_project:
144
143
  rubygems_version: 2.6.13
145
144
  signing_key:
146
145
  specification_version: 4
147
- summary: A parser for the Simple Regex Language, written in Ruby.
146
+ summary: srl_ruby is a gem implementing a parser for Simple Regex Language (SRL).
147
+ It translates patterns expressed in SRL into plain Ruby Regexp objects or regex
148
+ literals.
148
149
  test_files:
149
150
  - spec/integration_spec.rb
150
151
  - spec/regex/character_spec.rb