scanner 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in scanner.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Diego Alonso
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Scanner
2
+
3
+ Scanner is a gem to scan strings for regular expressions, and return
4
+ them as tokes, Each token will be identified by a symbol, and contain
5
+ some extra information, like the line and column number.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'scanner'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install scanner
20
+
21
+ ## Usage
22
+
23
+ Scanner is a module that you can include in your classes. It defines a
24
+ token function that accepts the regular expression that the token
25
+ matches.
26
+
27
+ Example code
28
+
29
+ class TestScanner
30
+ include Scanner
31
+ ignore /\s+/
32
+ token :number, /\d+/
33
+ token :id, /\w+/
34
+ end
35
+
36
+ @scanner = TestScanner.new
37
+ @scanner.parse("123")
38
+ @scanner.look_ahead.is?(:number) # Should be true
39
+
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,84 @@
1
+ module Scanner
2
+
3
+ def self.append_features(aModule)
4
+ super
5
+
6
+ aModule.instance_eval do
7
+ @language_tokens = {}
8
+ @ignore = nil
9
+
10
+ def token(token_symbol, regular_expression)
11
+ @language_tokens[token_symbol] = regular_expression
12
+ end
13
+
14
+ def ignore(regular_expression)
15
+ @ignore = regular_expression
16
+ end
17
+
18
+ token :eof, /\A\z/
19
+ end
20
+
21
+ end
22
+
23
+ private
24
+ def language_tokens
25
+ self.class.instance_eval { @language_tokens }
26
+ end
27
+
28
+ def ignore
29
+ self.class.instance_eval { @ignore }
30
+ end
31
+
32
+ public
33
+
34
+ def parse(program)
35
+ @program = program
36
+ @token_list = []
37
+ end
38
+
39
+ def consume
40
+ if @token_list.empty?
41
+ consume_next_token
42
+ else
43
+ @token_list.shift
44
+ end
45
+ end
46
+
47
+ def look_ahead(number_of_tokens = 1)
48
+ end_of_file_met = false
49
+ while @token_list.size < number_of_tokens
50
+ throw :scanner_exception if end_of_file_met
51
+ token = consume_next_token
52
+ @token_list << token
53
+ end_of_file_met = token.is? :eof
54
+ end
55
+ @token_list[-1]
56
+ end
57
+
58
+ private
59
+
60
+
61
+ def consume_next_token
62
+ clear_ignore_text
63
+
64
+ language_tokens.each do |symbol, reg_exp|
65
+ if @program =~ reg_exp
66
+ return Token.new(symbol, consume_regular_expression(reg_exp), 0, 0)
67
+ end
68
+ end
69
+
70
+ throw :scanner_exception
71
+ end
72
+
73
+ def consume_regular_expression(regexp)
74
+ content = @program[regexp]
75
+ @program.gsub!(regexp,"")
76
+ content
77
+ end
78
+
79
+ def clear_ignore_text
80
+ consume_regular_expression(ignore) if ignore
81
+ end
82
+
83
+
84
+ end
@@ -0,0 +1,21 @@
1
+ module Scanner
2
+ class Token
3
+ attr_reader :symbol, :content, :line, :column
4
+
5
+ def initialize(symbol, content, line, column)
6
+ @symbol = symbol
7
+ @content = content
8
+ @line = line
9
+ @column = column
10
+ end
11
+
12
+ def is?(symbol)
13
+ @symbol == symbol
14
+ end
15
+
16
+ def is_not?(symbol)
17
+ not is? symbol
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,3 @@
1
+ module Scanner
2
+ VERSION = "0.0.1"
3
+ end
data/lib/scanner.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "scanner/version"
2
+ require "scanner/scanner"
3
+ require "scanner/token"
4
+
5
+ module Scanner
6
+ end
data/scanner.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/scanner/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Diego Alonso"]
6
+ gem.email = ["diego@gapthemind.net"]
7
+ gem.description = %q{Simple scanner that returs tokens as symbols}
8
+ gem.summary = %q{Simple scanner. For the time being, it uses regular expressions to scan tokens}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "scanner"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Scanner::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "rake"
20
+ end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe Scanner do
4
+ before(:each) do
5
+ class TestScanner
6
+ include Scanner
7
+ ignore /\s+/
8
+ token :number, /\d+/
9
+ token :id, /\w+/
10
+ end
11
+
12
+ @scanner = TestScanner.new
13
+ end
14
+
15
+ it "detects the end of line" do
16
+ @scanner.parse("")
17
+ @scanner.consume.is?(:eof).should be true
18
+ end
19
+
20
+ describe "consume" do
21
+
22
+ it "consume returns the next token" do
23
+ @scanner.parse("123")
24
+ token = @scanner.consume
25
+ token.is?(:number).should be true
26
+ token.content.should eq("123")
27
+ end
28
+
29
+ it "consume clears ignore tokens before token" do
30
+ @scanner.parse(" 123")
31
+ token = @scanner.consume
32
+ token.is?(:number).should be true
33
+ end
34
+ end
35
+
36
+ describe "lookahead" do
37
+ it "returns the next token without arguments" do
38
+ @scanner.parse("123")
39
+ @scanner.look_ahead.is?(:number).should be true
40
+ end
41
+
42
+ it "does not consume the token" do
43
+ @scanner.parse("123")
44
+ @scanner.look_ahead
45
+ @scanner.consume.is?(:number).should be true
46
+ end
47
+
48
+ it "returns look ahead of n elements" do
49
+ @scanner.parse("123 abc")
50
+ @scanner.look_ahead(2).is?(:id).should be true
51
+ end
52
+ end
53
+
54
+
55
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scanner::Token do
4
+
5
+ it "is the right type of token" do
6
+ @token = Scanner::Token.new(:token_symbol, "content", 0, 0)
7
+ @token.is?(:token_symbol).should be true
8
+ end
9
+
10
+ it "is not the wrong type of token" do
11
+ @token = Scanner::Token.new(:token_symbol, "content", 0, 0)
12
+ @token.is_not?(:other_token_symbol).should be true
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ require 'scanner'
3
+
4
+ RSpec.configure do |config|
5
+ config.color_enabled = true
6
+ config.formatter = 'documentation'
7
+ end
8
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scanner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Diego Alonso
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple scanner that returs tokens as symbols
47
+ email:
48
+ - diego@gapthemind.net
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/scanner.rb
59
+ - lib/scanner/scanner.rb
60
+ - lib/scanner/token.rb
61
+ - lib/scanner/version.rb
62
+ - scanner.gemspec
63
+ - spec/scanner/scanner_spec.rb
64
+ - spec/scanner/token_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: -2890427904096243855
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ segments:
88
+ - 0
89
+ hash: -2890427904096243855
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.24
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Simple scanner. For the time being, it uses regular expressions to scan tokens
96
+ test_files:
97
+ - spec/scanner/scanner_spec.rb
98
+ - spec/scanner/token_spec.rb
99
+ - spec/spec_helper.rb