lexeme 0.0.4 → 0.0.5
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 +15 -0
- data/lib/lexeme/core_extensions.rb +4 -4
- data/lib/lexeme/lexeme.rb +15 -9
- data/lib/lexeme/ruleset.rb +3 -3
- data/lib/lexeme/token.rb +13 -4
- data/lib/lexeme/version.rb +1 -1
- metadata +7 -8
checksums.yaml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
!binary "U0hBMQ==":
|
|
3
|
+
metadata.gz: !binary |-
|
|
4
|
+
ODliZDY5NjQ5YTlmZmRjZTYyYjRhNjJlZmVlNWIxYTJiOTdmMWYyYw==
|
|
5
|
+
data.tar.gz: !binary |-
|
|
6
|
+
NjFhZWRlMjY0NmEwMDk2MjZiMGU1NWY1YWFmYWZjZDA5MzQ5NGJjMw==
|
|
7
|
+
SHA512:
|
|
8
|
+
metadata.gz: !binary |-
|
|
9
|
+
YzE0YWJjYzY4OGQ3NThlM2FlYTQ0ODc0MjE3MWE4ZjFmYWM3Zjk4MmQ3MDA0
|
|
10
|
+
NTYyNDc3NzJmYjQxYmY1ZGM0YjM0OTFiYzIxZGVmOGFiMjViODIwZGI5Y2Ix
|
|
11
|
+
M2ZiOTQ0YTYzMzRjMmVlZDg1MWNkNzllMTRmNjI4YWFiNDc3NmE=
|
|
12
|
+
data.tar.gz: !binary |-
|
|
13
|
+
MzE4Mjk4NTIxZDkwYzkwOTE4ZjFmN2ExMDMyNjJlYWYxZWEzMDg4YjA0Nzhm
|
|
14
|
+
YTNjNjI5MzJiNGY3ZTI0ZDY4OGQwYTMyYTkwOTg5ODdiYzNmMzBmMzRhMmNi
|
|
15
|
+
OTdhYzY1OWRlMTdlN2Q0YmMwNTcxNzAzMzE4MjIwZTgzODM0NDQ=
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
class ::String
|
|
2
|
-
|
|
3
|
-
def to_tokens
|
|
2
|
+
def tokenize
|
|
4
3
|
@lexer ||= Lexeme.define do
|
|
5
4
|
use_language :natural
|
|
6
5
|
end
|
|
@@ -14,7 +13,8 @@ class ::String
|
|
|
14
13
|
@lexer.tokens
|
|
15
14
|
end
|
|
16
15
|
|
|
17
|
-
def
|
|
18
|
-
to_tokens
|
|
16
|
+
def to_tokens
|
|
17
|
+
warn "String#to_tokens is obsolete, use String#tokenize instead"
|
|
18
|
+
tokenize
|
|
19
19
|
end
|
|
20
20
|
end
|
data/lib/lexeme/lexeme.rb
CHANGED
|
@@ -41,10 +41,12 @@ module Lexeme
|
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def use_language(name)
|
|
44
|
-
|
|
44
|
+
lang_file = "lexeme/languages/#{name}.rb"
|
|
45
|
+
require lang_file
|
|
46
|
+
|
|
45
47
|
instance_eval(&Language::send(name))
|
|
46
48
|
rescue LoadError
|
|
47
|
-
abort "Language file cannot be found"
|
|
49
|
+
abort "Language file #{lang_file} cannot be found"
|
|
48
50
|
end
|
|
49
51
|
|
|
50
52
|
private
|
|
@@ -55,12 +57,16 @@ module Lexeme
|
|
|
55
57
|
previous = ''
|
|
56
58
|
current = ''
|
|
57
59
|
tokens = []
|
|
58
|
-
|
|
60
|
+
new_line = 0
|
|
61
|
+
line_num = 1
|
|
59
62
|
|
|
60
63
|
input.each_char do |c|
|
|
61
64
|
if c == "\n"
|
|
62
|
-
|
|
65
|
+
new_line += 1
|
|
66
|
+
line_num += 1
|
|
63
67
|
c = ' '
|
|
68
|
+
else
|
|
69
|
+
new_line = 0
|
|
64
70
|
end
|
|
65
71
|
|
|
66
72
|
if !previous.empty? && ignorable?(previous)
|
|
@@ -74,7 +80,7 @@ module Lexeme
|
|
|
74
80
|
raise RuntimeError, "Unknown token `#{current}` on line #{line}" if
|
|
75
81
|
previous.empty?
|
|
76
82
|
|
|
77
|
-
token = identify(previous)
|
|
83
|
+
token = identify(previous, line_num - new_line)
|
|
78
84
|
|
|
79
85
|
raise RuntimeError, "Unknown token `#{previous}` on line #{line}" if
|
|
80
86
|
token.nil? || token.name.nil?
|
|
@@ -89,7 +95,7 @@ module Lexeme
|
|
|
89
95
|
end
|
|
90
96
|
|
|
91
97
|
if !previous.empty? && !ignorable?(previous)
|
|
92
|
-
token = identify(previous)
|
|
98
|
+
token = identify(previous, line_num - new_line)
|
|
93
99
|
raise RuntimeError, "Unknow token `#{previous}` on line #{line}" if
|
|
94
100
|
token.nil? || token.name.nil?
|
|
95
101
|
|
|
@@ -104,11 +110,11 @@ module Lexeme
|
|
|
104
110
|
end
|
|
105
111
|
|
|
106
112
|
def identifiable?(string)
|
|
107
|
-
@ruleset.identifiable?
|
|
113
|
+
@ruleset.identifiable?(string)
|
|
108
114
|
end
|
|
109
115
|
|
|
110
|
-
def identify(string)
|
|
111
|
-
@ruleset.identify
|
|
116
|
+
def identify(string, line)
|
|
117
|
+
@ruleset.identify(string, line)
|
|
112
118
|
end
|
|
113
119
|
end
|
|
114
120
|
end
|
data/lib/lexeme/ruleset.rb
CHANGED
|
@@ -45,12 +45,12 @@ module Lexeme
|
|
|
45
45
|
false
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
-
def identify(string)
|
|
48
|
+
def identify(string, line)
|
|
49
49
|
@rules.each do |r|
|
|
50
|
-
return Token.new(r.name, string) if string =~ r.regex
|
|
50
|
+
return Token.new(r.name, string, line) if string =~ r.regex
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
-
return Token.new(@unknown.name, string) if
|
|
53
|
+
return Token.new(@unknown.name, string, line) if
|
|
54
54
|
string =~ @unknown.regex
|
|
55
55
|
|
|
56
56
|
nil
|
data/lib/lexeme/token.rb
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
module Lexeme
|
|
2
2
|
class Token
|
|
3
|
-
attr_reader :name, :value
|
|
3
|
+
attr_reader :name, :value, :line
|
|
4
4
|
|
|
5
|
-
def initialize(name, value)
|
|
5
|
+
def initialize(name, value, line)
|
|
6
6
|
@name = name
|
|
7
7
|
@value = value
|
|
8
|
+
@line = line
|
|
8
9
|
end
|
|
9
10
|
|
|
10
11
|
def to_s
|
|
11
|
-
"#{
|
|
12
|
+
"#{line} => #{name} : #{value}"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def to_hash
|
|
16
|
+
{:line => line, :name => name, :value => value}
|
|
12
17
|
end
|
|
13
18
|
|
|
14
19
|
def to_ary
|
|
15
|
-
[
|
|
20
|
+
[name, value, line]
|
|
16
21
|
end
|
|
17
22
|
|
|
18
23
|
def to_array
|
|
19
24
|
to_ary
|
|
20
25
|
end
|
|
26
|
+
|
|
27
|
+
def to_a
|
|
28
|
+
to_ary
|
|
29
|
+
end
|
|
21
30
|
end
|
|
22
31
|
end
|
data/lib/lexeme/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lexeme
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
5
|
-
prerelease:
|
|
4
|
+
version: 0.0.5
|
|
6
5
|
platform: ruby
|
|
7
6
|
authors:
|
|
8
7
|
- Vladimir Ivic
|
|
9
8
|
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 2014-03-25 00:00:00.000000000 Z
|
|
13
12
|
dependencies: []
|
|
14
13
|
description: A simple lexical analyzer written in Ruby
|
|
15
14
|
email: vladimir.ivic@icloud.com
|
|
@@ -26,27 +25,27 @@ files:
|
|
|
26
25
|
- lib/lexeme/version.rb
|
|
27
26
|
- lib/lexeme/languages/natural.rb
|
|
28
27
|
homepage: https://github.com/mancmelou/lexeme
|
|
29
|
-
licenses:
|
|
28
|
+
licenses:
|
|
29
|
+
- MIT
|
|
30
|
+
metadata: {}
|
|
30
31
|
post_install_message:
|
|
31
32
|
rdoc_options: []
|
|
32
33
|
require_paths:
|
|
33
34
|
- lib
|
|
34
35
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
-
none: false
|
|
36
36
|
requirements:
|
|
37
37
|
- - ! '>='
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: '0'
|
|
40
40
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
|
-
none: false
|
|
42
41
|
requirements:
|
|
43
42
|
- - ! '>='
|
|
44
43
|
- !ruby/object:Gem::Version
|
|
45
44
|
version: '0'
|
|
46
45
|
requirements: []
|
|
47
46
|
rubyforge_project:
|
|
48
|
-
rubygems_version: 1.
|
|
47
|
+
rubygems_version: 2.1.4
|
|
49
48
|
signing_key:
|
|
50
|
-
specification_version:
|
|
49
|
+
specification_version: 4
|
|
51
50
|
summary: Lexeme
|
|
52
51
|
test_files: []
|