filter_lexer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/filter_lexer.gemspec +32 -0
- data/lib/filter_lexer/exceptions.rb +28 -0
- data/lib/filter_lexer/formatters/sql.rb +143 -0
- data/lib/filter_lexer/nodes.rb +92 -0
- data/lib/filter_lexer/syntax.treetop +169 -0
- data/lib/filter_lexer/version.rb +3 -0
- data/lib/filter_lexer.rb +57 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4eb20adfd043c015d21dbc1639c4669945131cef
|
4
|
+
data.tar.gz: a0f2855582ae1094e6ef329b82d04f4b8b03fcec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ccc25baabaa2ca2af95d5b7653dd996ab674c6822708bef2ff92db4fd26d6c7b25dad79c1c2fd0e71cd472763c05abbf78c2af6b5f7c8693b0837e325c7c9d65
|
7
|
+
data.tar.gz: 71f924d03e1af187be0e270c0ceab349f092cb02cb8ff0fcdcfe3ba09864f82c693595ddd4b01c82fb821e5a7a1e2ec6755558a31a0699eeff14f065b12330a8
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
Gemfile.lock
|
31
|
+
.ruby-version
|
32
|
+
.ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Michon van Dooren
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Michon van Dooren
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# FilterLexer
|
2
|
+
|
3
|
+
This is a simple treetop implementation for a basic SQL-like filtering syntax.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'filter_lexer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```sh
|
22
|
+
gem install filter_lexer
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To parse a filter, simply do the following:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
tree = FilterLexer::Parser.parse('foo == "BAR"')
|
31
|
+
```
|
32
|
+
|
33
|
+
If the parsing succeeds, a tree is returned. This tree can be output (for development purposes) using `FilterLexer::Parser.output_tree`.
|
34
|
+
|
35
|
+
If the parsing fails, a `ParseException` is raised containing the detials of the failure.
|
36
|
+
|
37
|
+
Simple complete example showing success and failure:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
examples = [
|
41
|
+
'foo == "BAR"',
|
42
|
+
'foo == "BAR" &&',
|
43
|
+
]
|
44
|
+
|
45
|
+
examples.each do |example|
|
46
|
+
begin
|
47
|
+
tree = FilterLexer::Parser.parse(example)
|
48
|
+
puts "Parsed #{example}"
|
49
|
+
FilterLexer::Parser.output_tree(tree)
|
50
|
+
rescue FilterLexer::ParseException
|
51
|
+
puts $!.message
|
52
|
+
puts $!.context
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Development
|
58
|
+
|
59
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
60
|
+
|
61
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MaienM/FilterLexer.
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
70
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "filter_lexer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'filter_lexer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'filter_lexer'
|
8
|
+
spec.version = FilterLexer::VERSION
|
9
|
+
spec.authors = ['Michon van Dooren']
|
10
|
+
spec.email = ['michon1992@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{A basic lexer that supports basic sql-like filtering logic.}
|
13
|
+
spec.description = %q{Simple treetop lexer that supports writing filters in an SQL-like manner.}
|
14
|
+
spec.homepage = 'http://github.com/MaienM/FilterLexer'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
19
|
+
else
|
20
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
spec.add_development_dependency 'rspec'
|
31
|
+
spec.add_dependency 'treetop', '~> 1.6'
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module FilterLexer
|
2
|
+
class ParseException < Exception
|
3
|
+
def initialize(parser)
|
4
|
+
@index = parser.failure_index
|
5
|
+
@input = parser.input
|
6
|
+
end
|
7
|
+
|
8
|
+
def message
|
9
|
+
return "Parse error at index #{@index}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def context
|
13
|
+
i1 = @index - 40
|
14
|
+
i1 = 0 if i1 < 0
|
15
|
+
i2 = @index + 40
|
16
|
+
i2 = @input.size if i2 > @input.size
|
17
|
+
|
18
|
+
context = @input.slice(i1..i2)
|
19
|
+
if i1 > 0
|
20
|
+
context = "...#{context}"
|
21
|
+
i1 -= 3
|
22
|
+
end
|
23
|
+
context = "#{context}..." if i2 < @input.size
|
24
|
+
|
25
|
+
return context + "\r\n" + ' ' * (@index - i1) + '^'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
module FilterLexer
|
2
|
+
class Expression
|
3
|
+
def to_query
|
4
|
+
return "#{query_string} #{query_variables}"
|
5
|
+
end
|
6
|
+
|
7
|
+
def query_string
|
8
|
+
return self.elements.map { |e| e.query_string }.join(' ')
|
9
|
+
end
|
10
|
+
|
11
|
+
def query_variables
|
12
|
+
return self.elements.map { |e| e.query_variables }.flatten
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Group
|
17
|
+
def query_string
|
18
|
+
return "(#{self.elements.first.query_string})"
|
19
|
+
end
|
20
|
+
|
21
|
+
def query_variables
|
22
|
+
return self.elements.first.query_variables
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Filter
|
27
|
+
def query_string
|
28
|
+
return "#{elements[0].sql} #{elements[1].sql} ?"
|
29
|
+
end
|
30
|
+
|
31
|
+
def query_variables
|
32
|
+
return [elements[2].sql]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Operator
|
37
|
+
def query_string
|
38
|
+
return self.sql
|
39
|
+
end
|
40
|
+
|
41
|
+
def query_variables
|
42
|
+
return []
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class OrOperator
|
47
|
+
def sql
|
48
|
+
return 'OR'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class AndOperator
|
53
|
+
def sql
|
54
|
+
return 'AND'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class EQOperator
|
59
|
+
def sql
|
60
|
+
return 'IS' if parent.value_class < ValueSpecial
|
61
|
+
return '='
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class NEQOperator
|
66
|
+
def sql
|
67
|
+
return 'IS NOT' if parent.value_class < ValueSpecial
|
68
|
+
return '<>'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class LTOperator
|
73
|
+
def sql
|
74
|
+
return '<'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class LEOperator
|
79
|
+
def sql
|
80
|
+
return '<='
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class GTOperator
|
85
|
+
def sql
|
86
|
+
return '>='
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class GEOperator
|
91
|
+
def sql
|
92
|
+
return '>='
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class NotLikeOperator
|
97
|
+
def sql
|
98
|
+
return 'NOT LIKE'
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class LikeOperator
|
103
|
+
def sql
|
104
|
+
return 'LIKE'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class BooleanLiteralFalse
|
109
|
+
def sql
|
110
|
+
return 'FALSE'
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class BooleanLiteralTrue
|
115
|
+
def sql
|
116
|
+
return 'TRUE'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class NullLiteral
|
121
|
+
def sql
|
122
|
+
return 'NULL'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
class StringLiteral
|
127
|
+
def sql
|
128
|
+
return self.text_value
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class NumberLiteral
|
133
|
+
def sql
|
134
|
+
return self.text_value
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class Identifier
|
139
|
+
def sql
|
140
|
+
return self.text_value
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module FilterLexer
|
2
|
+
class Expression < Treetop::Runtime::SyntaxNode
|
3
|
+
end
|
4
|
+
|
5
|
+
class Group < Treetop::Runtime::SyntaxNode
|
6
|
+
end
|
7
|
+
|
8
|
+
class Filter < Treetop::Runtime::SyntaxNode
|
9
|
+
def identifier
|
10
|
+
return elements[0].sql
|
11
|
+
end
|
12
|
+
|
13
|
+
def operator
|
14
|
+
return elements[1].sql
|
15
|
+
end
|
16
|
+
|
17
|
+
def value
|
18
|
+
return elements[2].sql
|
19
|
+
end
|
20
|
+
|
21
|
+
def value_class
|
22
|
+
return elements[2].class
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Operators
|
27
|
+
|
28
|
+
class Operator < Treetop::Runtime::SyntaxNode
|
29
|
+
end
|
30
|
+
|
31
|
+
class OrOperator < Operator
|
32
|
+
end
|
33
|
+
|
34
|
+
class AndOperator < Operator
|
35
|
+
end
|
36
|
+
|
37
|
+
class EQOperator < Operator
|
38
|
+
end
|
39
|
+
|
40
|
+
class NEQOperator < Operator
|
41
|
+
end
|
42
|
+
|
43
|
+
class LTOperator < Operator
|
44
|
+
end
|
45
|
+
|
46
|
+
class LEOperator < Operator
|
47
|
+
end
|
48
|
+
|
49
|
+
class GTOperator < Operator
|
50
|
+
end
|
51
|
+
|
52
|
+
class GEOperator < Operator
|
53
|
+
end
|
54
|
+
|
55
|
+
class NotLikeOperator < Operator
|
56
|
+
end
|
57
|
+
|
58
|
+
class LikeOperator < Operator
|
59
|
+
end
|
60
|
+
|
61
|
+
# Values
|
62
|
+
|
63
|
+
class Value < Treetop::Runtime::SyntaxNode
|
64
|
+
end
|
65
|
+
|
66
|
+
class ValueSpecial < Value
|
67
|
+
end
|
68
|
+
|
69
|
+
class ValueScalar < Value
|
70
|
+
end
|
71
|
+
|
72
|
+
class BooleanLiteralFalse < ValueSpecial
|
73
|
+
end
|
74
|
+
|
75
|
+
class BooleanLiteralTrue < ValueSpecial
|
76
|
+
end
|
77
|
+
|
78
|
+
class NullLiteral < ValueSpecial
|
79
|
+
end
|
80
|
+
|
81
|
+
class StringLiteral < ValueScalar
|
82
|
+
end
|
83
|
+
|
84
|
+
class NumberLiteral < ValueScalar
|
85
|
+
end
|
86
|
+
|
87
|
+
# Identifier
|
88
|
+
|
89
|
+
class Identifier < Treetop::Runtime::SyntaxNode
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
@@ -0,0 +1,169 @@
|
|
1
|
+
grammar FilterLexer
|
2
|
+
# Complex combinations
|
3
|
+
|
4
|
+
rule expression
|
5
|
+
space? (expression_compound / body) space? <Expression>
|
6
|
+
end
|
7
|
+
|
8
|
+
rule expression_compound
|
9
|
+
body space? combine_operator space? expression <Expression>
|
10
|
+
end
|
11
|
+
|
12
|
+
rule body
|
13
|
+
group / filter
|
14
|
+
end
|
15
|
+
|
16
|
+
rule group
|
17
|
+
'(' expression ')' <Group>
|
18
|
+
end
|
19
|
+
|
20
|
+
# Filters
|
21
|
+
|
22
|
+
rule filter
|
23
|
+
filter_equality / filter_comparison / filter_like
|
24
|
+
end
|
25
|
+
|
26
|
+
rule filter_equality
|
27
|
+
identifier space? equality_operator space? value <Filter>
|
28
|
+
end
|
29
|
+
|
30
|
+
rule filter_comparison
|
31
|
+
identifier space? comparison_operator space? number <Filter>
|
32
|
+
end
|
33
|
+
|
34
|
+
rule filter_like
|
35
|
+
identifier space like_operator space string <Filter>
|
36
|
+
end
|
37
|
+
|
38
|
+
# Logic
|
39
|
+
|
40
|
+
rule combine_operator
|
41
|
+
or_operator / and_operator
|
42
|
+
end
|
43
|
+
|
44
|
+
rule or_operator
|
45
|
+
'' ('||' / 'OR' / 'or') <OrOperator>
|
46
|
+
end
|
47
|
+
|
48
|
+
rule and_operator
|
49
|
+
'' ('&&' / 'AND' / 'and') <AndOperator>
|
50
|
+
end
|
51
|
+
|
52
|
+
# Operators
|
53
|
+
|
54
|
+
rule equality_operator
|
55
|
+
eq_operator / neq_operator
|
56
|
+
end
|
57
|
+
|
58
|
+
rule comparison_operator
|
59
|
+
le_operator / lt_operator / ge_operator / gt_operator
|
60
|
+
end
|
61
|
+
|
62
|
+
rule like_operator
|
63
|
+
yes_like_operator / not_like_operator
|
64
|
+
end
|
65
|
+
|
66
|
+
rule eq_operator
|
67
|
+
'==' <EQOperator>
|
68
|
+
end
|
69
|
+
|
70
|
+
rule neq_operator
|
71
|
+
'' ('!=' / '<>') <NEQOperator>
|
72
|
+
end
|
73
|
+
|
74
|
+
rule lt_operator
|
75
|
+
'<' <LTOperator>
|
76
|
+
end
|
77
|
+
|
78
|
+
rule le_operator
|
79
|
+
'<=' <LEOperator>
|
80
|
+
end
|
81
|
+
|
82
|
+
rule gt_operator
|
83
|
+
'>' <GTOperator>
|
84
|
+
end
|
85
|
+
|
86
|
+
rule ge_operator
|
87
|
+
'>=' <GEOperator>
|
88
|
+
end
|
89
|
+
|
90
|
+
rule yes_like_operator
|
91
|
+
'' ('like' / 'LIKE' / '~=') <LikeOperator>
|
92
|
+
end
|
93
|
+
|
94
|
+
rule not_like_operator
|
95
|
+
'' ('!~=' / 'NOT LIKE' / 'not like') <NotLikeOperator>
|
96
|
+
end
|
97
|
+
|
98
|
+
# Literals
|
99
|
+
|
100
|
+
rule value
|
101
|
+
value_special / number / string
|
102
|
+
end
|
103
|
+
|
104
|
+
rule value_special
|
105
|
+
null / boolean
|
106
|
+
end
|
107
|
+
|
108
|
+
rule number
|
109
|
+
number_sign? (number_float / number_integer) number_exponent? <NumberLiteral>
|
110
|
+
end
|
111
|
+
|
112
|
+
rule number_integer
|
113
|
+
number_base
|
114
|
+
end
|
115
|
+
|
116
|
+
rule number_float
|
117
|
+
number_base '.' number_base
|
118
|
+
end
|
119
|
+
|
120
|
+
rule number_sign
|
121
|
+
'+' / '-'
|
122
|
+
end
|
123
|
+
|
124
|
+
rule number_exponent
|
125
|
+
'e' number_base
|
126
|
+
end
|
127
|
+
|
128
|
+
rule number_base
|
129
|
+
[0-9]+
|
130
|
+
end
|
131
|
+
|
132
|
+
rule boolean
|
133
|
+
boolean_true / boolean_false
|
134
|
+
end
|
135
|
+
|
136
|
+
rule boolean_true
|
137
|
+
'' ('true' / 'TRUE' / 'on' / 'ON' / 'yes' / 'YES') <BooleanLiteralTrue>
|
138
|
+
end
|
139
|
+
|
140
|
+
rule boolean_false
|
141
|
+
'' ('false' / 'FALSE' / 'off' / 'OFF' / 'no' / 'NO') <BooleanLiteralFalse>
|
142
|
+
end
|
143
|
+
|
144
|
+
rule null
|
145
|
+
'' ('null' / 'NULL' / 'nil' / 'NIL' / 'nul' / 'NUL') <NullLiteral>
|
146
|
+
end
|
147
|
+
|
148
|
+
rule string
|
149
|
+
string_single / string_double
|
150
|
+
end
|
151
|
+
|
152
|
+
rule string_single
|
153
|
+
"'" ([^'\\] / "\\" . )* "'" <StringLiteral>
|
154
|
+
end
|
155
|
+
|
156
|
+
rule string_double
|
157
|
+
'"' ([^"\\] / "\\" . )* '"' <StringLiteral>
|
158
|
+
end
|
159
|
+
|
160
|
+
rule identifier
|
161
|
+
[a-zA-Z] [a-zA-Z0-9_]* <Identifier>
|
162
|
+
end
|
163
|
+
|
164
|
+
# Whitespace
|
165
|
+
|
166
|
+
rule space
|
167
|
+
[\s]+
|
168
|
+
end
|
169
|
+
end
|
data/lib/filter_lexer.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'treetop'
|
2
|
+
|
3
|
+
require "filter_lexer/version"
|
4
|
+
require "filter_lexer/exceptions"
|
5
|
+
require "filter_lexer/nodes"
|
6
|
+
require 'filter_lexer/syntax.treetop'
|
7
|
+
|
8
|
+
module FilterLexer
|
9
|
+
class Parser
|
10
|
+
@@parser = FilterLexerParser.new
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def parse(data)
|
14
|
+
# Pass the data over to the parser instance
|
15
|
+
tree = @@parser.parse(data)
|
16
|
+
|
17
|
+
# If the AST is nil then there was an error during parsing
|
18
|
+
# we need to report a simple error message to help the user
|
19
|
+
if tree.nil?
|
20
|
+
raise ParseException, @@parser
|
21
|
+
end
|
22
|
+
|
23
|
+
# Cleanup the tree
|
24
|
+
clean_tree(tree)
|
25
|
+
|
26
|
+
return tree
|
27
|
+
end
|
28
|
+
|
29
|
+
def output_tree(element, indent = '')
|
30
|
+
puts "#{indent}#{element.class}: #{element.text_value}"
|
31
|
+
(element.elements || []).each do |e|
|
32
|
+
output_tree(e, "#{indent} ")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def is_syn(node)
|
39
|
+
return node.class.name == 'Treetop::Runtime::SyntaxNode'
|
40
|
+
end
|
41
|
+
|
42
|
+
def node_elem(node)
|
43
|
+
return node.elements || []
|
44
|
+
end
|
45
|
+
|
46
|
+
def clean_tree(root_node)
|
47
|
+
return if root_node.elements.nil?
|
48
|
+
# Clean child elements
|
49
|
+
root_node.elements.each { |node| clean_tree(node) }
|
50
|
+
# Remove empty syntax elements
|
51
|
+
root_node.elements.reject! { |node| node_elem(node).empty? && is_syn(node) }
|
52
|
+
# Replace syntax elements with just one child with that child
|
53
|
+
root_node.elements.map! { |node| (node_elem(node).size == 1 && is_syn(node)) ? node.elements.first : node }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filter_lexer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michon van Dooren
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: treetop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.6'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.6'
|
69
|
+
description: Simple treetop lexer that supports writing filters in an SQL-like manner.
|
70
|
+
email:
|
71
|
+
- michon1992@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- filter_lexer.gemspec
|
86
|
+
- lib/filter_lexer.rb
|
87
|
+
- lib/filter_lexer/exceptions.rb
|
88
|
+
- lib/filter_lexer/formatters/sql.rb
|
89
|
+
- lib/filter_lexer/nodes.rb
|
90
|
+
- lib/filter_lexer/syntax.treetop
|
91
|
+
- lib/filter_lexer/version.rb
|
92
|
+
homepage: http://github.com/MaienM/FilterLexer
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata:
|
96
|
+
allowed_push_host: https://rubygems.org
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.4.5.1
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: A basic lexer that supports basic sql-like filtering logic.
|
117
|
+
test_files: []
|