lexeme 0.0.3 → 0.0.4

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.
@@ -6,18 +6,6 @@ require 'lexeme/core_extensions'
6
6
  require 'lexeme/version'
7
7
 
8
8
  module Lexeme
9
- def self.analyze(source = nil)
10
- raise RuntimeError, 'Please use #define before calling #analyze.' unless @lexer
11
-
12
- return @lexer.instance_eval(&Proc.new) if
13
- block_given?
14
-
15
- return @lexer.analyze(source) unless
16
- source.nil?
17
-
18
- raise ArgumentError, 'Invalid parameters. Expected string or block.'
19
- end
20
-
21
9
  def self.define(&block)
22
10
  @lexer = Lexeme.new
23
11
  @lexer.instance_eval(&block)
@@ -1,19 +1,20 @@
1
- class ::String
1
+ class ::String
2
+ # Will move to tokenize and have #to_tokens obsolete
2
3
  def to_tokens
3
- content = to_s
4
-
5
- Lexeme.define do
6
- token :STOP => /^\.$/
7
- token :COMA => /^,$/
8
- token :QUES => /^\?$/
9
- token :EXCL => /^!$/
10
- token :QUOT => /^"$/
11
- token :APOS => /^'$/
12
- token :WORD => /^[\w\-]+$/
4
+ @lexer ||= Lexeme.define do
5
+ use_language :natural
13
6
  end
14
7
 
15
- Lexeme.analyze do
16
- return from_string content
8
+ string_content = self
9
+
10
+ @lexer.analyze do
11
+ from_string string_content
17
12
  end
13
+
14
+ @lexer.tokens
15
+ end
16
+
17
+ def tokenize
18
+ to_tokens
18
19
  end
19
20
  end
@@ -0,0 +1,15 @@
1
+ module Lexeme
2
+ module Language
3
+ def self.natural
4
+ Proc.new do
5
+ token :STOP => /^\.$/
6
+ token :COMA => /^,$/
7
+ token :QUES => /^\?$/
8
+ token :EXCL => /^!$/
9
+ token :QUOT => /^"$/
10
+ token :APOS => /^'$/
11
+ token :WORD => /^[\w\-]+$/
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,6 +1,11 @@
1
1
  module Lexeme
2
2
  class Lexeme
3
3
  attr_accessor :ruleset
4
+ attr_reader :tokens
5
+
6
+ def analyze(&block)
7
+ instance_eval(&block)
8
+ end
4
9
 
5
10
  def from_file(filepath = nil)
6
11
  raise ArgumentError, 'Argument 1 must be a String' unless
@@ -12,8 +17,7 @@ module Lexeme
12
17
  raise RuntimeError, 'Source file not readable' unless
13
18
  File.exists?(filepath)
14
19
 
15
-
16
- scan IO.read(filepath)
20
+ @tokens = scan(IO.read(filepath))
17
21
  end
18
22
 
19
23
  def from_string(source)
@@ -23,10 +27,9 @@ module Lexeme
23
27
  raise ArgumentError, 'Source not defined' if
24
28
  source.empty?
25
29
 
26
- scan source
30
+ @tokens = scan(source)
27
31
  end
28
32
 
29
- # Used by the Lexeme.define method
30
33
  def token(params)
31
34
  @ruleset ||= Ruleset.new
32
35
 
@@ -36,6 +39,13 @@ module Lexeme
36
39
 
37
40
  @ruleset
38
41
  end
42
+
43
+ def use_language(name)
44
+ require "lexeme/languages/#{name}.rb"
45
+ instance_eval(&Language::send(name))
46
+ rescue LoadError
47
+ abort "Language file cannot be found"
48
+ end
39
49
 
40
50
  private
41
51
 
@@ -1,3 +1,3 @@
1
1
  module Lexeme
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lexeme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-13 00:00:00.000000000 Z
12
+ date: 2013-07-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple lexical analyzer written in Ruby
15
15
  email: vladimir.ivic@icloud.com
@@ -24,7 +24,8 @@ files:
24
24
  - lib/lexeme/rule.rb
25
25
  - lib/lexeme/core_extensions.rb
26
26
  - lib/lexeme/version.rb
27
- homepage: http://rubygems.org/gems/lexeme
27
+ - lib/lexeme/languages/natural.rb
28
+ homepage: https://github.com/mancmelou/lexeme
28
29
  licenses: []
29
30
  post_install_message:
30
31
  rdoc_options: []